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