run.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include "mk.h"
  2. typedef struct Event
  3. {
  4. int pid;
  5. Job *job;
  6. } Event;
  7. static Event *events;
  8. static int nevents, nrunning, nproclimit;
  9. typedef struct Process
  10. {
  11. int pid;
  12. int status;
  13. struct Process *b, *f;
  14. } Process;
  15. static Process *phead, *pfree;
  16. static void sched(void);
  17. static void pnew(int, int), pdelete(Process *);
  18. int pidslot(int);
  19. void
  20. run(Job *j)
  21. {
  22. Job *jj;
  23. if(jobs){
  24. for(jj = jobs; jj->next; jj = jj->next)
  25. ;
  26. jj->next = j;
  27. } else
  28. jobs = j;
  29. j->next = 0;
  30. /* this code also in waitup after parse redirect */
  31. if(nrunning < nproclimit)
  32. sched();
  33. }
  34. static void
  35. sched(void)
  36. {
  37. char *flags;
  38. Job *j;
  39. Bufblock *buf;
  40. int slot;
  41. Node *n;
  42. Envy *e;
  43. if(jobs == 0){
  44. usage();
  45. return;
  46. }
  47. j = jobs;
  48. jobs = j->next;
  49. if(DEBUG(D_EXEC))
  50. fprint(1, "firing up job for target %s\n", wtos(j->t, ' '));
  51. slot = nextslot();
  52. events[slot].job = j;
  53. buf = newbuf();
  54. e = buildenv(j, slot);
  55. shprint(j->r->recipe, e, buf);
  56. if(!tflag && (nflag || !(j->r->attr&QUIET)))
  57. Bwrite(&bout, buf->start, (long)strlen(buf->start));
  58. freebuf(buf);
  59. if(nflag||tflag){
  60. for(n = j->n; n; n = n->next){
  61. if(tflag){
  62. if(!(n->flags&VIRTUAL))
  63. touch(n->name);
  64. else if(explain)
  65. Bprint(&bout, "no touch of virtual '%s'\n", n->name);
  66. }
  67. n->time = time((long *)0);
  68. MADESET(n, MADE);
  69. }
  70. } else {
  71. if(DEBUG(D_EXEC))
  72. fprint(1, "recipe='%s'\n", j->r->recipe); /**/
  73. Bflush(&bout);
  74. if(j->r->attr&NOMINUSE)
  75. flags = 0;
  76. else
  77. flags = "-e";
  78. events[slot].pid = execsh(flags, j->r->recipe, 0, e);
  79. usage();
  80. nrunning++;
  81. if(DEBUG(D_EXEC))
  82. fprint(1, "pid for target %s = %d\n", wtos(j->t, ' '), events[slot].pid);
  83. }
  84. }
  85. int
  86. waitup(int echildok, int *retstatus)
  87. {
  88. Envy *e;
  89. int pid;
  90. int slot;
  91. Symtab *s;
  92. Word *w;
  93. Job *j;
  94. char buf[ERRMAX];
  95. Bufblock *bp;
  96. int uarg = 0;
  97. int done;
  98. Node *n;
  99. Process *p;
  100. extern int runerrs;
  101. /* first check against the proces slist */
  102. if(retstatus)
  103. for(p = phead; p; p = p->f)
  104. if(p->pid == *retstatus){
  105. *retstatus = p->status;
  106. pdelete(p);
  107. return(-1);
  108. }
  109. again: /* rogue processes */
  110. pid = waitfor(buf);
  111. if(pid == -1){
  112. if(echildok > 0)
  113. return(1);
  114. else {
  115. fprint(2, "mk: (waitup %d) ", echildok);
  116. perror("mk wait");
  117. Exit();
  118. }
  119. }
  120. if(DEBUG(D_EXEC))
  121. fprint(1, "waitup got pid=%d, status='%s'\n", pid, buf);
  122. if(retstatus && pid == *retstatus){
  123. *retstatus = buf[0]? 1:0;
  124. return(-1);
  125. }
  126. slot = pidslot(pid);
  127. if(slot < 0){
  128. if(DEBUG(D_EXEC))
  129. fprint(2, "mk: wait returned unexpected process %d\n", pid);
  130. pnew(pid, buf[0]? 1:0);
  131. goto again;
  132. }
  133. j = events[slot].job;
  134. usage();
  135. nrunning--;
  136. events[slot].pid = -1;
  137. if(buf[0]){
  138. e = buildenv(j, slot);
  139. bp = newbuf();
  140. shprint(j->r->recipe, e, bp);
  141. front(bp->start);
  142. fprint(2, "mk: %s: exit status=%s", bp->start, buf);
  143. freebuf(bp);
  144. for(n = j->n, done = 0; n; n = n->next)
  145. if(n->flags&DELETE){
  146. if(done++ == 0)
  147. fprint(2, ", deleting");
  148. fprint(2, " '%s'", n->name);
  149. delete(n->name);
  150. }
  151. fprint(2, "\n");
  152. if(kflag){
  153. runerrs++;
  154. uarg = 1;
  155. } else {
  156. jobs = 0;
  157. Exit();
  158. }
  159. }
  160. for(w = j->t; w; w = w->next){
  161. if((s = symlook(w->s, S_NODE, 0)) == 0)
  162. continue; /* not interested in this node */
  163. update(uarg, s->u.ptr);
  164. }
  165. if(nrunning < nproclimit)
  166. sched();
  167. return(0);
  168. }
  169. void
  170. nproc(void)
  171. {
  172. Symtab *sym;
  173. Word *w;
  174. if(sym = symlook("NPROC", S_VAR, 0)) {
  175. w = sym->u.ptr;
  176. if (w && w->s && w->s[0])
  177. nproclimit = atoi(w->s);
  178. }
  179. if(nproclimit < 1)
  180. nproclimit = 1;
  181. if(DEBUG(D_EXEC))
  182. fprint(1, "nprocs = %d\n", nproclimit);
  183. if(nproclimit > nevents){
  184. if(nevents)
  185. events = (Event *)Realloc((char *)events, nproclimit*sizeof(Event));
  186. else
  187. events = (Event *)Malloc(nproclimit*sizeof(Event));
  188. while(nevents < nproclimit)
  189. events[nevents++].pid = 0;
  190. }
  191. }
  192. int
  193. nextslot(void)
  194. {
  195. int i;
  196. for(i = 0; i < nproclimit; i++)
  197. if(events[i].pid <= 0) return i;
  198. assert(/*out of slots!!*/ 0);
  199. return 0; /* cyntax */
  200. }
  201. int
  202. pidslot(int pid)
  203. {
  204. int i;
  205. for(i = 0; i < nevents; i++)
  206. if(events[i].pid == pid) return(i);
  207. if(DEBUG(D_EXEC))
  208. fprint(2, "mk: wait returned unexpected process %d\n", pid);
  209. return(-1);
  210. }
  211. static void
  212. pnew(int pid, int status)
  213. {
  214. Process *p;
  215. if(pfree){
  216. p = pfree;
  217. pfree = p->f;
  218. } else
  219. p = (Process *)Malloc(sizeof(Process));
  220. p->pid = pid;
  221. p->status = status;
  222. p->f = phead;
  223. phead = p;
  224. if(p->f)
  225. p->f->b = p;
  226. p->b = 0;
  227. }
  228. static void
  229. pdelete(Process *p)
  230. {
  231. if(p->f)
  232. p->f->b = p->b;
  233. if(p->b)
  234. p->b->f = p->f;
  235. else
  236. phead = p->f;
  237. p->f = pfree;
  238. pfree = p;
  239. }
  240. void
  241. killchildren(char *msg)
  242. {
  243. Process *p;
  244. kflag = 1; /* to make sure waitup doesn't exit */
  245. jobs = 0; /* make sure no more get scheduled */
  246. for(p = phead; p; p = p->f)
  247. expunge(p->pid, msg);
  248. while(waitup(1, (int *)0) == 0)
  249. ;
  250. Bprint(&bout, "mk: %s\n", msg);
  251. Exit();
  252. }
  253. static long tslot[1000];
  254. static long tick;
  255. void
  256. usage(void)
  257. {
  258. long t;
  259. time(&t);
  260. if(tick)
  261. tslot[nrunning] += (t-tick);
  262. tick = t;
  263. }
  264. void
  265. prusage(void)
  266. {
  267. int i;
  268. usage();
  269. for(i = 0; i <= nevents; i++)
  270. fprint(1, "%d: %ld\n", i, tslot[i]);
  271. }