proc.c 27 KB

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