proc.c 28 KB

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