proc.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  1. #include <u.h>
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "../port/error.h"
  7. #include "edf.h"
  8. #include <trace.h>
  9. int coopsched;
  10. int schedgain = 30; /* units in seconds */
  11. int nrdy;
  12. Ref noteidalloc;
  13. void updatecpu(Proc*);
  14. int reprioritize(Proc*);
  15. ulong delayedscheds; /* statistics */
  16. long skipscheds;
  17. long preempts;
  18. ulong load;
  19. static Ref pidalloc;
  20. static struct Procalloc
  21. {
  22. Lock;
  23. Proc* ht[128];
  24. Proc* arena;
  25. Proc* free;
  26. } procalloc;
  27. enum
  28. {
  29. Q=10,
  30. DQ=4,
  31. Scaling=2,
  32. };
  33. Schedq runq[Nrq];
  34. ulong runvec;
  35. char *statename[] =
  36. { /* BUG: generate automatically */
  37. "Dead",
  38. "Moribund",
  39. "Ready",
  40. "Scheding",
  41. "Running",
  42. "Queueing",
  43. "QueueingR",
  44. "QueueingW",
  45. "Wakeme",
  46. "Broken",
  47. "Stopped",
  48. "Rendez",
  49. "Waitrelease",
  50. };
  51. static void pidhash(Proc*);
  52. static void pidunhash(Proc*);
  53. static void rebalance(void);
  54. /*
  55. * Always splhi()'ed.
  56. */
  57. void
  58. schedinit(void) /* never returns */
  59. {
  60. Edf *e;
  61. setlabel(&m->sched);
  62. if(up) {
  63. if((e = up->edf) && (e->flags & Admitted))
  64. edfrecord(up);
  65. m->proc = 0;
  66. switch(up->state) {
  67. case Running:
  68. ready(up);
  69. break;
  70. case Moribund:
  71. up->state = Dead;
  72. edfstop(up);
  73. if (up->edf)
  74. free(up->edf);
  75. up->edf = nil;
  76. /*
  77. * Holding locks from pexit:
  78. * procalloc
  79. * palloc
  80. */
  81. mmurelease(up);
  82. up->qnext = procalloc.free;
  83. procalloc.free = up;
  84. unlock(&palloc);
  85. unlock(&procalloc);
  86. break;
  87. }
  88. up->mach = nil;
  89. updatecpu(up);
  90. up = nil;
  91. }
  92. sched();
  93. }
  94. /*
  95. * If changing this routine, look also at sleep(). It
  96. * contains a copy of the guts of sched().
  97. */
  98. void
  99. sched(void)
  100. {
  101. Proc *p;
  102. if(m->ilockdepth)
  103. panic("ilockdepth %d, last lock 0x%p at 0x%lux, sched called from 0x%lux",
  104. m->ilockdepth, up?up->lastilock:nil,
  105. (up && up->lastilock)?up->lastilock->pc:0,
  106. getcallerpc(&p+2));
  107. if(up){
  108. /*
  109. * Delay the sched until the process gives up the locks
  110. * it is holding. This avoids dumb lock loops.
  111. * Don't delay if the process is Moribund.
  112. * It called sched to die.
  113. * But do sched eventually. This avoids a missing unlock
  114. * from hanging the entire kernel.
  115. * But don't reschedule procs holding palloc or procalloc.
  116. * Those are far too important to be holding while asleep.
  117. *
  118. * This test is not exact. There can still be a few instructions
  119. * in the middle of taslock when a process holds a lock
  120. * but Lock.p has not yet been initialized.
  121. */
  122. if(up->nlocks.ref)
  123. if(up->state != Moribund)
  124. if(up->delaysched < 20
  125. || palloc.Lock.p == up
  126. || procalloc.Lock.p == up){
  127. up->delaysched++;
  128. delayedscheds++;
  129. return;
  130. }
  131. up->delaysched = 0;
  132. splhi();
  133. /* statistics */
  134. m->cs++;
  135. procsave(up);
  136. if(setlabel(&up->sched)){
  137. procrestore(up);
  138. spllo();
  139. return;
  140. }
  141. gotolabel(&m->sched);
  142. }
  143. p = runproc();
  144. if(!p->edf){
  145. updatecpu(p);
  146. p->priority = reprioritize(p);
  147. }
  148. if(p != m->readied)
  149. m->schedticks = m->ticks + HZ/10;
  150. m->readied = 0;
  151. up = p;
  152. up->state = Running;
  153. up->mach = MACHP(m->machno);
  154. m->proc = up;
  155. mmuswitch(up);
  156. gotolabel(&up->sched);
  157. }
  158. int
  159. anyready(void)
  160. {
  161. return runvec;
  162. }
  163. int
  164. anyhigher(void)
  165. {
  166. return runvec & ~((1<<(up->priority+1))-1);
  167. }
  168. /*
  169. * here once per clock tick to see if we should resched
  170. */
  171. void
  172. hzsched(void)
  173. {
  174. /* once a second, rebalance will reprioritize ready procs */
  175. if(m->machno == 0)
  176. rebalance();
  177. /* unless preempted, get to run for at least 100ms */
  178. if(anyhigher()
  179. || (!up->fixedpri && m->ticks > m->schedticks && anyready())){
  180. m->readied = nil; /* avoid cooperative scheduling */
  181. up->delaysched++;
  182. }
  183. }
  184. /*
  185. * here at the end of non-clock interrupts to see if we should preempt the
  186. * current process. Returns 1 if preempted, 0 otherwise.
  187. */
  188. int
  189. preempted(void)
  190. {
  191. if(up && up->state == Running)
  192. if(up->preempted == 0)
  193. if(anyhigher())
  194. if(!active.exiting){
  195. m->readied = nil; /* avoid cooperative scheduling */
  196. up->preempted = 1;
  197. sched();
  198. splhi();
  199. up->preempted = 0;
  200. return 1;
  201. }
  202. return 0;
  203. }
  204. /*
  205. * Update the cpu time average for this particular process,
  206. * which is about to change from up -> not up or vice versa.
  207. * p->lastupdate is the last time an updatecpu happened.
  208. *
  209. * The cpu time average is a decaying average that lasts
  210. * about D clock ticks. D is chosen to be approximately
  211. * the cpu time of a cpu-intensive "quick job". A job has to run
  212. * for approximately D clock ticks before we home in on its
  213. * actual cpu usage. Thus if you manage to get in and get out
  214. * quickly, you won't be penalized during your burst. Once you
  215. * start using your share of the cpu for more than about D
  216. * clock ticks though, your p->cpu hits 1000 (1.0) and you end up
  217. * below all the other quick jobs. Interactive tasks, because
  218. * they basically always use less than their fair share of cpu,
  219. * will be rewarded.
  220. *
  221. * If the process has not been running, then we want to
  222. * apply the filter
  223. *
  224. * cpu = cpu * (D-1)/D
  225. *
  226. * n times, yielding
  227. *
  228. * cpu = cpu * ((D-1)/D)^n
  229. *
  230. * but D is big enough that this is approximately
  231. *
  232. * cpu = cpu * (D-n)/D
  233. *
  234. * so we use that instead.
  235. *
  236. * If the process has been running, we apply the filter to
  237. * 1 - cpu, yielding a similar equation. Note that cpu is
  238. * stored in fixed point (* 1000).
  239. *
  240. * Updatecpu must be called before changing up, in order
  241. * to maintain accurate cpu usage statistics. It can be called
  242. * at any time to bring the stats for a given proc up-to-date.
  243. */
  244. void
  245. updatecpu(Proc *p)
  246. {
  247. int n, t, ocpu;
  248. int D = schedgain*HZ*Scaling;
  249. if(p->edf)
  250. return;
  251. t = MACHP(0)->ticks*Scaling + Scaling/2;
  252. n = t - p->lastupdate;
  253. p->lastupdate = t;
  254. if(n == 0)
  255. return;
  256. if(n > D)
  257. n = D;
  258. ocpu = p->cpu;
  259. if(p != up)
  260. p->cpu = (ocpu*(D-n))/D;
  261. else{
  262. t = 1000 - ocpu;
  263. t = (t*(D-n))/D;
  264. p->cpu = 1000 - t;
  265. }
  266. //iprint("pid %d %s for %d cpu %d -> %d\n", p->pid,p==up?"active":"inactive",n, ocpu,p->cpu);
  267. }
  268. /*
  269. * On average, p has used p->cpu of a cpu recently.
  270. * Its fair share is conf.nmach/m->load of a cpu. If it has been getting
  271. * too much, penalize it. If it has been getting not enough, reward it.
  272. * I don't think you can get much more than your fair share that
  273. * often, so most of the queues are for using less. Having a priority
  274. * of 3 means you're just right. Having a higher priority (up to p->basepri)
  275. * means you're not using as much as you could.
  276. */
  277. int
  278. reprioritize(Proc *p)
  279. {
  280. int fairshare, n, load, ratio;
  281. load = MACHP(0)->load;
  282. if(load == 0)
  283. return p->basepri;
  284. /*
  285. * fairshare = 1.000 * conf.nproc * 1.000/load,
  286. * except the decimal point is moved three places
  287. * on both load and fairshare.
  288. */
  289. fairshare = (conf.nmach*1000*1000)/load;
  290. n = p->cpu;
  291. if(n == 0)
  292. n = 1;
  293. ratio = (fairshare+n/2) / n;
  294. if(ratio > p->basepri)
  295. ratio = p->basepri;
  296. if(ratio < 0)
  297. panic("reprioritize");
  298. //iprint("pid %d cpu %d load %d fair %d pri %d\n", p->pid, p->cpu, load, fairshare, ratio);
  299. return ratio;
  300. }
  301. /*
  302. * add a process to a scheduling queue
  303. */
  304. void
  305. queueproc(Schedq *rq, Proc *p)
  306. {
  307. int pri;
  308. pri = rq - runq;
  309. lock(runq);
  310. p->priority = pri;
  311. p->rnext = 0;
  312. if(rq->tail)
  313. rq->tail->rnext = p;
  314. else
  315. rq->head = p;
  316. rq->tail = p;
  317. rq->n++;
  318. nrdy++;
  319. runvec |= 1<<pri;
  320. unlock(runq);
  321. }
  322. /*
  323. * try to remove a process from a scheduling queue (called splhi)
  324. */
  325. Proc*
  326. dequeueproc(Schedq *rq, Proc *tp)
  327. {
  328. Proc *l, *p;
  329. if(!canlock(runq))
  330. return nil;
  331. /*
  332. * the queue may have changed before we locked runq,
  333. * refind the target process.
  334. */
  335. l = 0;
  336. for(p = rq->head; p; p = p->rnext){
  337. if(p == tp)
  338. break;
  339. l = p;
  340. }
  341. /*
  342. * p->mach==0 only when process state is saved
  343. */
  344. if(p == 0 || p->mach){
  345. unlock(runq);
  346. return nil;
  347. }
  348. if(p->rnext == 0)
  349. rq->tail = l;
  350. if(l)
  351. l->rnext = p->rnext;
  352. else
  353. rq->head = p->rnext;
  354. if(rq->head == nil)
  355. runvec &= ~(1<<(rq-runq));
  356. rq->n--;
  357. nrdy--;
  358. if(p->state != Ready)
  359. print("dequeueproc %s %lud %s\n", p->text, p->pid, statename[p->state]);
  360. unlock(runq);
  361. return p;
  362. }
  363. /*
  364. * ready(p) picks a new priority for a process and sticks it in the
  365. * runq for that priority.
  366. */
  367. void
  368. ready(Proc *p)
  369. {
  370. int s, pri;
  371. Schedq *rq;
  372. void (*pt)(Proc*, int, vlong);
  373. s = splhi();
  374. if(edfready(p)){
  375. splx(s);
  376. return;
  377. }
  378. if(up != p)
  379. m->readied = p; /* group scheduling */
  380. updatecpu(p);
  381. pri = reprioritize(p);
  382. p->priority = pri;
  383. rq = &runq[pri];
  384. p->state = Ready;
  385. queueproc(rq, p);
  386. pt = proctrace;
  387. if(pt)
  388. pt(p, SReady, 0);
  389. splx(s);
  390. }
  391. /*
  392. * yield the processor and drop our priority
  393. */
  394. void
  395. yield(void)
  396. {
  397. if(anyready()){
  398. /* pretend we just used 1/2 tick */
  399. up->lastupdate -= Scaling/2;
  400. sched();
  401. }
  402. }
  403. /*
  404. * recalculate priorities once a second. We need to do this
  405. * since priorities will otherwise only be recalculated when
  406. * the running process blocks.
  407. */
  408. ulong balancetime;
  409. static void
  410. rebalance(void)
  411. {
  412. int pri, npri, t, x;
  413. Schedq *rq;
  414. Proc *p;
  415. t = m->ticks;
  416. if(t - balancetime < HZ)
  417. return;
  418. balancetime = t;
  419. for(pri=0, rq=runq; pri<Npriq; pri++, rq++){
  420. another:
  421. p = rq->head;
  422. if(p == nil)
  423. continue;
  424. if(p->mp != MACHP(m->machno))
  425. continue;
  426. if(pri == p->basepri)
  427. continue;
  428. updatecpu(p);
  429. npri = reprioritize(p);
  430. if(npri != pri){
  431. x = splhi();
  432. p = dequeueproc(rq, p);
  433. if(p)
  434. queueproc(&runq[npri], p);
  435. splx(x);
  436. goto another;
  437. }
  438. }
  439. }
  440. /*
  441. * pick a process to run
  442. */
  443. Proc*
  444. runproc(void)
  445. {
  446. Schedq *rq;
  447. Proc *p;
  448. ulong start, now;
  449. int i;
  450. void (*pt)(Proc*, int, vlong);
  451. start = perfticks();
  452. /* cooperative scheduling until the clock ticks */
  453. if(coopsched && (p=m->readied) && p->mach==0 && p->state==Ready
  454. && runq[Nrq-1].head == nil && runq[Nrq-2].head == nil){
  455. skipscheds++;
  456. rq = &runq[p->priority];
  457. goto found;
  458. }
  459. preempts++;
  460. loop:
  461. /*
  462. * find a process that last ran on this processor (affinity),
  463. * or one that hasn't moved in a while (load balancing). Every
  464. * time around the loop affinity goes down.
  465. */
  466. spllo();
  467. for(i = 0;; i++){
  468. /*
  469. * find the highest priority target process that this
  470. * processor can run given affinity constraints.
  471. *
  472. */
  473. for(rq = &runq[Nrq-1]; rq >= runq; rq--){
  474. for(p = rq->head; p; p = p->rnext){
  475. if(p->mp == nil || p->mp == MACHP(m->machno)
  476. || (!p->wired && i > 0))
  477. goto found;
  478. }
  479. }
  480. /* waste time or halt the CPU */
  481. idlehands();
  482. /* remember how much time we're here */
  483. now = perfticks();
  484. m->perf.inidle += now-start;
  485. start = now;
  486. }
  487. found:
  488. splhi();
  489. p = dequeueproc(rq, p);
  490. if(p == nil)
  491. goto loop;
  492. p->state = Scheding;
  493. p->mp = MACHP(m->machno);
  494. if(edflock(p)){
  495. edfrun(p, rq == &runq[PriEdf]); /* start deadline timer and do admin */
  496. edfunlock();
  497. }
  498. pt = proctrace;
  499. if(pt)
  500. pt(p, SRun, 0);
  501. return p;
  502. }
  503. int
  504. canpage(Proc *p)
  505. {
  506. int ok = 0;
  507. splhi();
  508. lock(runq);
  509. /* Only reliable way to see if we are Running */
  510. if(p->mach == 0) {
  511. p->newtlb = 1;
  512. ok = 1;
  513. }
  514. unlock(runq);
  515. spllo();
  516. return ok;
  517. }
  518. Proc*
  519. newproc(void)
  520. {
  521. Proc *p;
  522. lock(&procalloc);
  523. for(;;) {
  524. if(p = procalloc.free)
  525. break;
  526. unlock(&procalloc);
  527. resrcwait("no procs");
  528. lock(&procalloc);
  529. }
  530. procalloc.free = p->qnext;
  531. unlock(&procalloc);
  532. p->state = Scheding;
  533. p->psstate = "New";
  534. p->mach = 0;
  535. p->qnext = 0;
  536. p->nchild = 0;
  537. p->nwait = 0;
  538. p->waitq = 0;
  539. p->parent = 0;
  540. p->pgrp = 0;
  541. p->egrp = 0;
  542. p->fgrp = 0;
  543. p->rgrp = 0;
  544. p->pdbg = 0;
  545. p->fpstate = FPinit;
  546. p->kp = 0;
  547. p->procctl = 0;
  548. p->notepending = 0;
  549. p->ureg = 0;
  550. p->privatemem = 0;
  551. p->noswap = 0;
  552. p->errstr = p->errbuf0;
  553. p->syserrstr = p->errbuf1;
  554. p->errbuf0[0] = '\0';
  555. p->errbuf1[0] = '\0';
  556. p->nlocks.ref = 0;
  557. p->delaysched = 0;
  558. p->trace = 0;
  559. kstrdup(&p->user, "*nouser");
  560. kstrdup(&p->text, "*notext");
  561. kstrdup(&p->args, "");
  562. p->nargs = 0;
  563. p->setargs = 0;
  564. memset(p->seg, 0, sizeof p->seg);
  565. p->pid = incref(&pidalloc);
  566. pidhash(p);
  567. p->noteid = incref(&noteidalloc);
  568. if(p->pid==0 || p->noteid==0)
  569. panic("pidalloc");
  570. if(p->kstack == 0)
  571. p->kstack = smalloc(KSTACK);
  572. /* sched params */
  573. p->mp = 0;
  574. p->wired = 0;
  575. procpriority(p, PriNormal, 0);
  576. p->cpu = 0;
  577. p->lastupdate = MACHP(0)->ticks*Scaling;
  578. p->edf = nil;
  579. return p;
  580. }
  581. /*
  582. * wire this proc to a machine
  583. */
  584. void
  585. procwired(Proc *p, int bm)
  586. {
  587. Proc *pp;
  588. int i;
  589. char nwired[MAXMACH];
  590. Mach *wm;
  591. if(bm < 0){
  592. /* pick a machine to wire to */
  593. memset(nwired, 0, sizeof(nwired));
  594. p->wired = 0;
  595. pp = proctab(0);
  596. for(i=0; i<conf.nproc; i++, pp++){
  597. wm = pp->wired;
  598. if(wm && pp->pid)
  599. nwired[wm->machno]++;
  600. }
  601. bm = 0;
  602. for(i=0; i<conf.nmach; i++)
  603. if(nwired[i] < nwired[bm])
  604. bm = i;
  605. } else {
  606. /* use the virtual machine requested */
  607. bm = bm % conf.nmach;
  608. }
  609. p->wired = MACHP(bm);
  610. p->mp = p->wired;
  611. }
  612. void
  613. procpriority(Proc *p, int pri, int fixed)
  614. {
  615. if(pri >= Npriq)
  616. pri = Npriq - 1;
  617. else if(pri < 0)
  618. pri = 0;
  619. p->basepri = pri;
  620. p->priority = pri;
  621. if(fixed){
  622. p->fixedpri = 1;
  623. } else {
  624. p->fixedpri = 0;
  625. }
  626. }
  627. void
  628. procinit0(void) /* bad planning - clashes with devproc.c */
  629. {
  630. Proc *p;
  631. int i;
  632. procalloc.free = xalloc(conf.nproc*sizeof(Proc));
  633. if(procalloc.free == nil){
  634. xsummary();
  635. panic("cannot allocate %lud procs (%ludMB)\n", conf.nproc, conf.nproc*sizeof(Proc)/(1024*1024));
  636. }
  637. procalloc.arena = procalloc.free;
  638. p = procalloc.free;
  639. for(i=0; i<conf.nproc-1; i++,p++)
  640. p->qnext = p+1;
  641. p->qnext = 0;
  642. }
  643. /*
  644. * sleep if a condition is not true. Another process will
  645. * awaken us after it sets the condition. When we awaken
  646. * the condition may no longer be true.
  647. *
  648. * we lock both the process and the rendezvous to keep r->p
  649. * and p->r synchronized.
  650. */
  651. void
  652. sleep(Rendez *r, int (*f)(void*), void *arg)
  653. {
  654. int s;
  655. void (*pt)(Proc*, int, vlong);
  656. s = splhi();
  657. if(up->nlocks.ref)
  658. print("process %lud sleeps with %lud locks held, last lock 0x%p locked at pc 0x%lux, sleep called from 0x%lux\n",
  659. up->pid, up->nlocks.ref, up->lastlock, up->lastlock->pc, getcallerpc(&r));
  660. lock(r);
  661. lock(&up->rlock);
  662. if(r->p){
  663. print("double sleep called from 0x%lux, %lud %lud\n", getcallerpc(&r), r->p->pid, up->pid);
  664. dumpstack();
  665. }
  666. /*
  667. * Wakeup only knows there may be something to do by testing
  668. * r->p in order to get something to lock on.
  669. * Flush that information out to memory in case the sleep is
  670. * committed.
  671. */
  672. r->p = up;
  673. if((*f)(arg) || up->notepending){
  674. /*
  675. * if condition happened or a note is pending
  676. * never mind
  677. */
  678. r->p = nil;
  679. unlock(&up->rlock);
  680. unlock(r);
  681. } else {
  682. /*
  683. * now we are committed to
  684. * change state and call scheduler
  685. */
  686. pt = proctrace;
  687. if(pt)
  688. pt(up, SSleep, 0);
  689. up->state = Wakeme;
  690. up->r = r;
  691. /* statistics */
  692. m->cs++;
  693. procsave(up);
  694. if(setlabel(&up->sched)) {
  695. /*
  696. * here when the process is awakened
  697. */
  698. procrestore(up);
  699. spllo();
  700. } else {
  701. /*
  702. * here to go to sleep (i.e. stop Running)
  703. */
  704. unlock(&up->rlock);
  705. unlock(r);
  706. gotolabel(&m->sched);
  707. }
  708. }
  709. if(up->notepending) {
  710. up->notepending = 0;
  711. splx(s);
  712. if(up->procctl == Proc_exitme && up->closingfgrp)
  713. forceclosefgrp();
  714. error(Eintr);
  715. }
  716. splx(s);
  717. }
  718. static int
  719. tfn(void *arg)
  720. {
  721. return up->trend == nil || up->tfn(arg);
  722. }
  723. void
  724. twakeup(Ureg*, Timer *t)
  725. {
  726. Proc *p;
  727. Rendez *trend;
  728. p = t->ta;
  729. trend = p->trend;
  730. p->trend = 0;
  731. if(trend)
  732. wakeup(trend);
  733. }
  734. void
  735. tsleep(Rendez *r, int (*fn)(void*), void *arg, ulong ms)
  736. {
  737. if (up->tt){
  738. print("tsleep: timer active: mode %d, tf 0x%lux\n", up->tmode, up->tf);
  739. timerdel(up);
  740. }
  741. up->tns = MS2NS(ms);
  742. up->tf = twakeup;
  743. up->tmode = Trelative;
  744. up->ta = up;
  745. up->trend = r;
  746. up->tfn = fn;
  747. timeradd(up);
  748. if(waserror()){
  749. timerdel(up);
  750. nexterror();
  751. }
  752. sleep(r, tfn, arg);
  753. if (up->tt)
  754. timerdel(up);
  755. up->twhen = 0;
  756. poperror();
  757. }
  758. /*
  759. * Expects that only one process can call wakeup for any given Rendez.
  760. * We hold both locks to ensure that r->p and p->r remain consistent.
  761. * Richard Miller has a better solution that doesn't require both to
  762. * be held simultaneously, but I'm a paranoid - presotto.
  763. */
  764. Proc*
  765. wakeup(Rendez *r)
  766. {
  767. Proc *p;
  768. int s;
  769. s = splhi();
  770. lock(r);
  771. p = r->p;
  772. if(p != nil){
  773. lock(&p->rlock);
  774. if(p->state != Wakeme || p->r != r)
  775. panic("wakeup: state");
  776. r->p = nil;
  777. p->r = nil;
  778. ready(p);
  779. unlock(&p->rlock);
  780. }
  781. unlock(r);
  782. splx(s);
  783. return p;
  784. }
  785. /*
  786. * if waking a sleeping process, this routine must hold both
  787. * p->rlock and r->lock. However, it can't know them in
  788. * the same order as wakeup causing a possible lock ordering
  789. * deadlock. We break the deadlock by giving up the p->rlock
  790. * lock if we can't get the r->lock and retrying.
  791. */
  792. int
  793. postnote(Proc *p, int dolock, char *n, int flag)
  794. {
  795. int s, ret;
  796. Rendez *r;
  797. Proc *d, **l;
  798. if(dolock)
  799. qlock(&p->debug);
  800. if(flag != NUser && (p->notify == 0 || p->notified))
  801. p->nnote = 0;
  802. ret = 0;
  803. if(p->nnote < NNOTE) {
  804. strcpy(p->note[p->nnote].msg, n);
  805. p->note[p->nnote++].flag = flag;
  806. ret = 1;
  807. }
  808. p->notepending = 1;
  809. if(dolock)
  810. qunlock(&p->debug);
  811. /* this loop is to avoid lock ordering problems. */
  812. for(;;){
  813. s = splhi();
  814. lock(&p->rlock);
  815. r = p->r;
  816. /* waiting for a wakeup? */
  817. if(r == nil)
  818. break; /* no */
  819. /* try for the second lock */
  820. if(canlock(r)){
  821. if(p->state != Wakeme || r->p != p)
  822. panic("postnote: state %d %d %d", r->p != p, p->r != r, p->state);
  823. p->r = nil;
  824. r->p = nil;
  825. ready(p);
  826. unlock(r);
  827. break;
  828. }
  829. /* give other process time to get out of critical section and try again */
  830. unlock(&p->rlock);
  831. splx(s);
  832. sched();
  833. }
  834. unlock(&p->rlock);
  835. splx(s);
  836. if(p->state != Rendezvous)
  837. return ret;
  838. /* Try and pull out of a rendezvous */
  839. lock(p->rgrp);
  840. if(p->state == Rendezvous) {
  841. p->rendval = ~0;
  842. l = &REND(p->rgrp, p->rendtag);
  843. for(d = *l; d; d = d->rendhash) {
  844. if(d == p) {
  845. *l = p->rendhash;
  846. break;
  847. }
  848. l = &d->rendhash;
  849. }
  850. ready(p);
  851. }
  852. unlock(p->rgrp);
  853. return ret;
  854. }
  855. /*
  856. * weird thing: keep at most NBROKEN around
  857. */
  858. #define NBROKEN 4
  859. struct
  860. {
  861. QLock;
  862. int n;
  863. Proc *p[NBROKEN];
  864. }broken;
  865. void
  866. addbroken(Proc *p)
  867. {
  868. qlock(&broken);
  869. if(broken.n == NBROKEN) {
  870. ready(broken.p[0]);
  871. memmove(&broken.p[0], &broken.p[1], sizeof(Proc*)*(NBROKEN-1));
  872. --broken.n;
  873. }
  874. broken.p[broken.n++] = p;
  875. qunlock(&broken);
  876. edfstop(up);
  877. p->state = Broken;
  878. p->psstate = 0;
  879. sched();
  880. }
  881. void
  882. unbreak(Proc *p)
  883. {
  884. int b;
  885. qlock(&broken);
  886. for(b=0; b < broken.n; b++)
  887. if(broken.p[b] == p) {
  888. broken.n--;
  889. memmove(&broken.p[b], &broken.p[b+1],
  890. sizeof(Proc*)*(NBROKEN-(b+1)));
  891. ready(p);
  892. break;
  893. }
  894. qunlock(&broken);
  895. }
  896. int
  897. freebroken(void)
  898. {
  899. int i, n;
  900. qlock(&broken);
  901. n = broken.n;
  902. for(i=0; i<n; i++) {
  903. ready(broken.p[i]);
  904. broken.p[i] = 0;
  905. }
  906. broken.n = 0;
  907. qunlock(&broken);
  908. return n;
  909. }
  910. void
  911. pexit(char *exitstr, int freemem)
  912. {
  913. Proc *p;
  914. Segment **s, **es;
  915. long utime, stime;
  916. Waitq *wq, *f, *next;
  917. Fgrp *fgrp;
  918. Egrp *egrp;
  919. Rgrp *rgrp;
  920. Pgrp *pgrp;
  921. Chan *dot;
  922. void (*pt)(Proc*, int, vlong);
  923. up->alarm = 0;
  924. if (up->tt)
  925. timerdel(up);
  926. pt = proctrace;
  927. if(pt)
  928. pt(up, SDead, 0);
  929. /* nil out all the resources under lock (free later) */
  930. qlock(&up->debug);
  931. fgrp = up->fgrp;
  932. up->fgrp = nil;
  933. egrp = up->egrp;
  934. up->egrp = nil;
  935. rgrp = up->rgrp;
  936. up->rgrp = nil;
  937. pgrp = up->pgrp;
  938. up->pgrp = nil;
  939. dot = up->dot;
  940. up->dot = nil;
  941. qunlock(&up->debug);
  942. if(fgrp)
  943. closefgrp(fgrp);
  944. if(egrp)
  945. closeegrp(egrp);
  946. if(rgrp)
  947. closergrp(rgrp);
  948. if(dot)
  949. cclose(dot);
  950. if(pgrp)
  951. closepgrp(pgrp);
  952. /*
  953. * if not a kernel process and have a parent,
  954. * do some housekeeping.
  955. */
  956. if(up->kp == 0) {
  957. p = up->parent;
  958. if(p == 0) {
  959. if(exitstr == 0)
  960. exitstr = "unknown";
  961. panic("boot process died: %s", exitstr);
  962. }
  963. while(waserror())
  964. ;
  965. wq = smalloc(sizeof(Waitq));
  966. poperror();
  967. wq->w.pid = up->pid;
  968. utime = up->time[TUser] + up->time[TCUser];
  969. stime = up->time[TSys] + up->time[TCSys];
  970. wq->w.time[TUser] = tk2ms(utime);
  971. wq->w.time[TSys] = tk2ms(stime);
  972. wq->w.time[TReal] = tk2ms(MACHP(0)->ticks - up->time[TReal]);
  973. if(exitstr && exitstr[0])
  974. snprint(wq->w.msg, sizeof(wq->w.msg), "%s %lud: %s", up->text, up->pid, exitstr);
  975. else
  976. wq->w.msg[0] = '\0';
  977. lock(&p->exl);
  978. /*
  979. * If my parent is no longer alive, or if there would be more
  980. * than 128 zombie child processes for my parent, then don't
  981. * leave a wait record behind. This helps prevent badly
  982. * written daemon processes from accumulating lots of wait
  983. * records.
  984. */
  985. if(p->pid == up->parentpid && p->state != Broken && p->nwait < 128) {
  986. p->nchild--;
  987. p->time[TCUser] += utime;
  988. p->time[TCSys] += stime;
  989. wq->next = p->waitq;
  990. p->waitq = wq;
  991. p->nwait++;
  992. wakeup(&p->waitr);
  993. unlock(&p->exl);
  994. }
  995. else {
  996. unlock(&p->exl);
  997. free(wq);
  998. }
  999. }
  1000. if(!freemem)
  1001. addbroken(up);
  1002. qlock(&up->seglock);
  1003. es = &up->seg[NSEG];
  1004. for(s = up->seg; s < es; s++) {
  1005. if(*s) {
  1006. putseg(*s);
  1007. *s = 0;
  1008. }
  1009. }
  1010. qunlock(&up->seglock);
  1011. lock(&up->exl); /* Prevent my children from leaving waits */
  1012. pidunhash(up);
  1013. up->pid = 0;
  1014. wakeup(&up->waitr);
  1015. unlock(&up->exl);
  1016. for(f = up->waitq; f; f = next) {
  1017. next = f->next;
  1018. free(f);
  1019. }
  1020. /* release debuggers */
  1021. qlock(&up->debug);
  1022. if(up->pdbg) {
  1023. wakeup(&up->pdbg->sleep);
  1024. up->pdbg = 0;
  1025. }
  1026. qunlock(&up->debug);
  1027. /* Sched must not loop for these locks */
  1028. lock(&procalloc);
  1029. lock(&palloc);
  1030. edfstop(up);
  1031. up->state = Moribund;
  1032. sched();
  1033. panic("pexit");
  1034. }
  1035. int
  1036. haswaitq(void *x)
  1037. {
  1038. Proc *p;
  1039. p = (Proc *)x;
  1040. return p->waitq != 0;
  1041. }
  1042. ulong
  1043. pwait(Waitmsg *w)
  1044. {
  1045. ulong cpid;
  1046. Waitq *wq;
  1047. if(!canqlock(&up->qwaitr))
  1048. error(Einuse);
  1049. if(waserror()) {
  1050. qunlock(&up->qwaitr);
  1051. nexterror();
  1052. }
  1053. lock(&up->exl);
  1054. if(up->nchild == 0 && up->waitq == 0) {
  1055. unlock(&up->exl);
  1056. error(Enochild);
  1057. }
  1058. unlock(&up->exl);
  1059. sleep(&up->waitr, haswaitq, up);
  1060. lock(&up->exl);
  1061. wq = up->waitq;
  1062. up->waitq = wq->next;
  1063. up->nwait--;
  1064. unlock(&up->exl);
  1065. qunlock(&up->qwaitr);
  1066. poperror();
  1067. if(w)
  1068. memmove(w, &wq->w, sizeof(Waitmsg));
  1069. cpid = wq->w.pid;
  1070. free(wq);
  1071. return cpid;
  1072. }
  1073. Proc*
  1074. proctab(int i)
  1075. {
  1076. return &procalloc.arena[i];
  1077. }
  1078. void
  1079. dumpaproc(Proc *p)
  1080. {
  1081. ulong bss;
  1082. char *s;
  1083. if(p == 0)
  1084. return;
  1085. bss = 0;
  1086. if(p->seg[BSEG])
  1087. bss = p->seg[BSEG]->top;
  1088. s = p->psstate;
  1089. if(s == 0)
  1090. s = statename[p->state];
  1091. print("%3lud:%10s pc %8lux dbgpc %8lux %8s (%s) ut %ld st %ld bss %lux qpc %lux nl %lud nd %lud lpc %lux pri %lud\n",
  1092. p->pid, p->text, p->pc, dbgpc(p), s, statename[p->state],
  1093. p->time[0], p->time[1], bss, p->qpc, p->nlocks.ref, p->delaysched, p->lastlock ? p->lastlock->pc : 0, p->priority);
  1094. }
  1095. void
  1096. procdump(void)
  1097. {
  1098. int i;
  1099. Proc *p;
  1100. if(up)
  1101. print("up %lud\n", up->pid);
  1102. else
  1103. print("no current process\n");
  1104. for(i=0; i<conf.nproc; i++) {
  1105. p = &procalloc.arena[i];
  1106. if(p->state == Dead)
  1107. continue;
  1108. dumpaproc(p);
  1109. }
  1110. }
  1111. /*
  1112. * wait till all processes have flushed their mmu
  1113. * state about segement s
  1114. */
  1115. void
  1116. procflushseg(Segment *s)
  1117. {
  1118. int i, ns, nm, nwait;
  1119. Proc *p;
  1120. /*
  1121. * tell all processes with this
  1122. * segment to flush their mmu's
  1123. */
  1124. nwait = 0;
  1125. for(i=0; i<conf.nproc; i++) {
  1126. p = &procalloc.arena[i];
  1127. if(p->state == Dead)
  1128. continue;
  1129. for(ns = 0; ns < NSEG; ns++)
  1130. if(p->seg[ns] == s){
  1131. p->newtlb = 1;
  1132. for(nm = 0; nm < conf.nmach; nm++){
  1133. if(MACHP(nm)->proc == p){
  1134. MACHP(nm)->flushmmu = 1;
  1135. nwait++;
  1136. }
  1137. }
  1138. break;
  1139. }
  1140. }
  1141. if(nwait == 0)
  1142. return;
  1143. /*
  1144. * wait for all processors to take a clock interrupt
  1145. * and flush their mmu's
  1146. */
  1147. for(nm = 0; nm < conf.nmach; nm++)
  1148. if(MACHP(nm) != m)
  1149. while(MACHP(nm)->flushmmu)
  1150. sched();
  1151. }
  1152. void
  1153. scheddump(void)
  1154. {
  1155. Proc *p;
  1156. Schedq *rq;
  1157. for(rq = &runq[Nrq-1]; rq >= runq; rq--){
  1158. if(rq->head == 0)
  1159. continue;
  1160. print("rq%ld:", rq-runq);
  1161. for(p = rq->head; p; p = p->rnext)
  1162. print(" %lud(%lud)", p->pid, m->ticks - p->readytime);
  1163. print("\n");
  1164. delay(150);
  1165. }
  1166. print("nrdy %d\n", nrdy);
  1167. }
  1168. void
  1169. kproc(char *name, void (*func)(void *), void *arg)
  1170. {
  1171. Proc *p;
  1172. static Pgrp *kpgrp;
  1173. p = newproc();
  1174. p->psstate = 0;
  1175. p->procmode = 0640;
  1176. p->kp = 1;
  1177. p->noswap = 1;
  1178. p->fpsave = up->fpsave;
  1179. p->scallnr = up->scallnr;
  1180. p->s = up->s;
  1181. p->nerrlab = 0;
  1182. p->slash = up->slash;
  1183. p->dot = up->dot;
  1184. if(p->dot)
  1185. incref(p->dot);
  1186. memmove(p->note, up->note, sizeof(p->note));
  1187. p->nnote = up->nnote;
  1188. p->notified = 0;
  1189. p->lastnote = up->lastnote;
  1190. p->notify = up->notify;
  1191. p->ureg = 0;
  1192. p->dbgreg = 0;
  1193. procpriority(p, PriKproc, 0);
  1194. kprocchild(p, func, arg);
  1195. kstrdup(&p->user, eve);
  1196. kstrdup(&p->text, name);
  1197. if(kpgrp == 0)
  1198. kpgrp = newpgrp();
  1199. p->pgrp = kpgrp;
  1200. incref(kpgrp);
  1201. memset(p->time, 0, sizeof(p->time));
  1202. p->time[TReal] = MACHP(0)->ticks;
  1203. ready(p);
  1204. /*
  1205. * since the bss/data segments are now shareable,
  1206. * any mmu info about this process is now stale
  1207. * and has to be discarded.
  1208. */
  1209. p->newtlb = 1;
  1210. flushmmu();
  1211. }
  1212. /*
  1213. * called splhi() by notify(). See comment in notify for the
  1214. * reasoning.
  1215. */
  1216. void
  1217. procctl(Proc *p)
  1218. {
  1219. char *state;
  1220. ulong s;
  1221. switch(p->procctl) {
  1222. case Proc_exitbig:
  1223. spllo();
  1224. pexit("Killed: Insufficient physical memory", 1);
  1225. case Proc_exitme:
  1226. spllo(); /* pexit has locks in it */
  1227. pexit("Killed", 1);
  1228. case Proc_traceme:
  1229. if(p->nnote == 0)
  1230. return;
  1231. /* No break */
  1232. case Proc_stopme:
  1233. p->procctl = 0;
  1234. state = p->psstate;
  1235. p->psstate = "Stopped";
  1236. /* free a waiting debugger */
  1237. s = spllo();
  1238. qlock(&p->debug);
  1239. if(p->pdbg) {
  1240. wakeup(&p->pdbg->sleep);
  1241. p->pdbg = 0;
  1242. }
  1243. qunlock(&p->debug);
  1244. splhi();
  1245. p->state = Stopped;
  1246. sched();
  1247. p->psstate = state;
  1248. splx(s);
  1249. return;
  1250. }
  1251. }
  1252. #include "errstr.h"
  1253. void
  1254. error(char *err)
  1255. {
  1256. spllo();
  1257. assert(up->nerrlab < NERR);
  1258. kstrcpy(up->errstr, err, ERRMAX);
  1259. setlabel(&up->errlab[NERR-1]);
  1260. nexterror();
  1261. }
  1262. void
  1263. nexterror(void)
  1264. {
  1265. gotolabel(&up->errlab[--up->nerrlab]);
  1266. }
  1267. void
  1268. exhausted(char *resource)
  1269. {
  1270. char buf[ERRMAX];
  1271. sprint(buf, "no free %s", resource);
  1272. iprint("%s\n", buf);
  1273. error(buf);
  1274. }
  1275. void
  1276. killbig(char *why)
  1277. {
  1278. int i;
  1279. Segment *s;
  1280. ulong l, max;
  1281. Proc *p, *ep, *kp;
  1282. max = 0;
  1283. kp = 0;
  1284. ep = procalloc.arena+conf.nproc;
  1285. for(p = procalloc.arena; p < ep; p++) {
  1286. if(p->state == Dead || p->kp)
  1287. continue;
  1288. l = 0;
  1289. for(i=1; i<NSEG; i++) {
  1290. s = p->seg[i];
  1291. if(s != 0)
  1292. l += s->top - s->base;
  1293. }
  1294. if(l > max && ((p->procmode&0222) || strcmp(eve, p->user)!=0)) {
  1295. kp = p;
  1296. max = l;
  1297. }
  1298. }
  1299. print("%lud: %s killed: %s\n", kp->pid, kp->text, why);
  1300. for(p = procalloc.arena; p < ep; p++) {
  1301. if(p->state == Dead || p->kp)
  1302. continue;
  1303. if(p != kp && p->seg[BSEG] && p->seg[BSEG] == kp->seg[BSEG])
  1304. p->procctl = Proc_exitbig;
  1305. }
  1306. kp->procctl = Proc_exitbig;
  1307. for(i = 0; i < NSEG; i++) {
  1308. s = kp->seg[i];
  1309. if(s != 0 && canqlock(&s->lk)) {
  1310. mfreeseg(s, s->base, (s->top - s->base)/BY2PG);
  1311. qunlock(&s->lk);
  1312. }
  1313. }
  1314. }
  1315. /*
  1316. * change ownership to 'new' of all processes owned by 'old'. Used when
  1317. * eve changes.
  1318. */
  1319. void
  1320. renameuser(char *old, char *new)
  1321. {
  1322. Proc *p, *ep;
  1323. ep = procalloc.arena+conf.nproc;
  1324. for(p = procalloc.arena; p < ep; p++)
  1325. if(p->user!=nil && strcmp(old, p->user)==0)
  1326. kstrdup(&p->user, new);
  1327. }
  1328. /*
  1329. * time accounting called by clock() splhi'd
  1330. */
  1331. void
  1332. accounttime(void)
  1333. {
  1334. Proc *p;
  1335. ulong n, per;
  1336. static ulong nrun;
  1337. p = m->proc;
  1338. if(p) {
  1339. nrun++;
  1340. p->time[p->insyscall]++;
  1341. }
  1342. /* calculate decaying duty cycles */
  1343. n = perfticks();
  1344. per = n - m->perf.last;
  1345. m->perf.last = n;
  1346. per = (m->perf.period*(HZ-1) + per)/HZ;
  1347. if(per != 0)
  1348. m->perf.period = per;
  1349. m->perf.avg_inidle = (m->perf.avg_inidle*(HZ-1)+m->perf.inidle)/HZ;
  1350. m->perf.inidle = 0;
  1351. m->perf.avg_inintr = (m->perf.avg_inintr*(HZ-1)+m->perf.inintr)/HZ;
  1352. m->perf.inintr = 0;
  1353. /* only one processor gets to compute system load averages */
  1354. if(m->machno != 0)
  1355. return;
  1356. /*
  1357. * calculate decaying load average.
  1358. * if we decay by (n-1)/n then it takes
  1359. * n clock ticks to go from load L to .36 L once
  1360. * things quiet down. it takes about 5 n clock
  1361. * ticks to go to zero. so using HZ means this is
  1362. * approximately the load over the last second,
  1363. * with a tail lasting about 5 seconds.
  1364. */
  1365. n = nrun;
  1366. nrun = 0;
  1367. n = (nrdy+n)*1000;
  1368. m->load = (m->load*(HZ-1)+n)/HZ;
  1369. }
  1370. static void
  1371. pidhash(Proc *p)
  1372. {
  1373. int h;
  1374. h = p->pid % nelem(procalloc.ht);
  1375. lock(&procalloc);
  1376. p->pidhash = procalloc.ht[h];
  1377. procalloc.ht[h] = p;
  1378. unlock(&procalloc);
  1379. }
  1380. static void
  1381. pidunhash(Proc *p)
  1382. {
  1383. int h;
  1384. Proc **l;
  1385. h = p->pid % nelem(procalloc.ht);
  1386. lock(&procalloc);
  1387. for(l = &procalloc.ht[h]; *l != nil; l = &(*l)->pidhash)
  1388. if(*l == p){
  1389. *l = p->pidhash;
  1390. break;
  1391. }
  1392. unlock(&procalloc);
  1393. }
  1394. int
  1395. procindex(ulong pid)
  1396. {
  1397. Proc *p;
  1398. int h;
  1399. int s;
  1400. s = -1;
  1401. h = pid % nelem(procalloc.ht);
  1402. lock(&procalloc);
  1403. for(p = procalloc.ht[h]; p != nil; p = p->pidhash)
  1404. if(p->pid == pid){
  1405. s = p - procalloc.arena;
  1406. break;
  1407. }
  1408. unlock(&procalloc);
  1409. return s;
  1410. }