summaryrefslogtreecommitdiff
path: root/sys/src/9/port
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2022-08-10 12:27:15 +0000
committerOri Bernstein <ori@eigenstate.org>2022-08-10 12:27:15 +0000
commit07e14e43baafbfe7855a2003faa15e70ec5640a1 (patch)
treed6e6fc30035f3f793fa5be05a5542fc9e91c484b /sys/src/9/port
parent4521ae32bb95562b4210c23bee02a9fe1b1e3137 (diff)
9/port: allow kiloprocs -- allocate procs lazily
Treallocate the small data structures around procs eagerly, but use malloc to allocate the large proc data structures when we need them, which allows us to scale to many more procs. There are still many scalability bottlenecks, so we only crank up the nproc limit by a little bit this time around, and crank it up more as we optimize more.
Diffstat (limited to 'sys/src/9/port')
-rw-r--r--sys/src/9/port/devproc.c20
-rw-r--r--sys/src/9/port/devswap.c10
-rw-r--r--sys/src/9/port/edf.c17
-rw-r--r--sys/src/9/port/portdat.h3
-rw-r--r--sys/src/9/port/proc.c65
5 files changed, 60 insertions, 55 deletions
diff --git a/sys/src/9/port/devproc.c b/sys/src/9/port/devproc.c
index d19d4a9a0..503b00922 100644
--- a/sys/src/9/port/devproc.c
+++ b/sys/src/9/port/devproc.c
@@ -149,7 +149,8 @@ static char *sname[]={ "Text", "Data", "Bss", "Stack", "Shared", "Phys", "Fixed"
#define QSHIFT 5 /* location in qid of proc slot # */
#define QID(q) ((((ulong)(q).path)&0x0000001F)>>0)
-#define SLOT(q) (((((ulong)(q).path)&0x07FFFFFE0)>>QSHIFT)-1)
+#define SLOTMAX 0x4000000
+#define SLOT(q) (((((ulong)(q).path)>>QSHIFT)&(SLOTMAX-1))-1)
#define PID(q) ((q).vers)
#define NOTEID(q) ((q).vers)
@@ -202,6 +203,8 @@ procgen(Chan *c, char *name, Dirtab *tab, int, int s, Dir *dp)
return -1;
p = proctab(s);
+ if(p == nil)
+ return 0;
pid = p->pid;
if(pid == 0)
return 0;
@@ -227,7 +230,7 @@ procgen(Chan *c, char *name, Dirtab *tab, int, int s, Dir *dp)
panic("procgen");
tab = &procdir[s];
- path = c->qid.path&~(((1<<QSHIFT)-1)); /* slot component */
+ path = c->qid.path&~((1<<QSHIFT)-1); /* slot component */
/* p->procmode determines default mode for files in /proc */
p = proctab(SLOT(c->qid));
@@ -281,8 +284,9 @@ _proctrace(Proc* p, Tevent etype, vlong ts)
static void
procinit(void)
{
- if(conf.nproc >= (1<<(16-QSHIFT))-1)
- print("warning: too many procs for devproc\n");
+ /* slot masks: lets see how big we can go */
+ if(conf.nproc > SLOTMAX)
+ panic("warning: too many procs for devproc\n");
}
static Chan*
@@ -335,8 +339,7 @@ changenoteid(Proc *p, ulong noteid)
setnoteid(p, noteid);
return;
}
- for(i = 0; i < conf.nproc; i++){
- pp = proctab(i);
+ for(i = 0; (pp = proctab(i)) != nil; i++){
if(pp->noteid != noteid || pp->kp)
continue;
if(strcmp(pp->user, p->user) == 0){
@@ -354,8 +357,7 @@ postnotepg(ulong noteid, char *n, int flag)
Proc *p;
int i;
- for(i = 0; i < conf.nproc; i++){
- p = proctab(i);
+ for(i = 0; (p = proctab(i)) != nil; i++){
if(p == up)
continue;
if(p->noteid != noteid || p->kp)
@@ -414,7 +416,7 @@ procopen(Chan *c, int omode0)
nexterror();
}
pid = PID(c->qid);
- if(p->pid != pid)
+ if(p == nil || p->pid != pid)
error(Eprocdied);
omode = openmode(omode0);
diff --git a/sys/src/9/port/devswap.c b/sys/src/9/port/devswap.c
index 261ecf7c1..a30560c90 100644
--- a/sys/src/9/port/devswap.c
+++ b/sys/src/9/port/devswap.c
@@ -162,7 +162,7 @@ pager(void*)
while(waserror())
;
- x = -1;
+ x = 0;
for(;;){
up->psstate = "Reclaim";
if(reclaim()){
@@ -183,12 +183,13 @@ pager(void*)
i = ageclock;
do {
- if(++x >= conf.nproc){
+ p = proctab(x++);
+ if(p == nil){
if(++ageclock == i)
goto Killbig;
x = 0;
+ continue;
}
- p = proctab(x);
} while(p->state == Dead || p->noswap || !canqlock(&p->seglock));
up->psstate = "Pageout";
for(i = 0; i < NSEG; i++) {
@@ -274,8 +275,7 @@ canflush(Proc *p, Segment *s)
* Now we must do hardwork to ensure all processes which have tlb
* entries for this segment will be flushed if we succeed in paging it out
*/
- for(x = 0; x < conf.nproc; x++){
- p = proctab(x);
+ for(x = 0; (p = proctab(x)) != nil; x++){
if(p->state == Dead)
continue;
for(i = 0; i < NSEG; i++){
diff --git a/sys/src/9/port/edf.c b/sys/src/9/port/edf.c
index 99bbf1f61..213567023 100644
--- a/sys/src/9/port/edf.c
+++ b/sys/src/9/port/edf.c
@@ -373,17 +373,15 @@ edfadmit(Proc *p)
pt(p, SAdmit, 0);
/* Look for another proc with the same period to synchronize to */
- SET(r);
- for(i=0; i<conf.nproc; i++) {
- r = proctab(i);
+ for(i=0; (r = proctab(i)) != nil; i++) {
if(r->state == Dead || r == p)
continue;
- if (r->edf == nil || (r->edf->flags & Admitted) == 0)
+ if(r->edf == nil || (r->edf->flags & Admitted) == 0)
continue;
- if (r->edf->T == e->T)
- break;
+ if(r->edf->T == e->T)
+ break;
}
- if (i == conf.nproc){
+ if(r == nil){
/* Can't synchronize to another proc, release now */
e->t = now;
e->d = 0;
@@ -627,11 +625,10 @@ testschedulability(Proc *theproc)
/* initialize */
DPRINT("schedulability test %lud\n", theproc->pid);
qschedulability = nil;
- for(i=0; i<conf.nproc; i++) {
- p = proctab(i);
+ for(i=0; (p = proctab(i)) != nil; i++) {
if(p->state == Dead)
continue;
- if ((p->edf == nil || (p->edf->flags & Admitted) == 0) && p != theproc)
+ if((p->edf == nil || (p->edf->flags & Admitted) == 0) && p != theproc)
continue;
p->edf->testtype = Rl;
p->edf->testtime = 0;
diff --git a/sys/src/9/port/portdat.h b/sys/src/9/port/portdat.h
index e2aa2aa07..8f3904441 100644
--- a/sys/src/9/port/portdat.h
+++ b/sys/src/9/port/portdat.h
@@ -671,6 +671,7 @@ struct Proc
ulong pid;
ulong noteid; /* Equivalent of note group */
ulong parentpid;
+ ulong index;
Proc *parent; /* Process to send wait record on exit */
Lock exl; /* Lock count and waitq */
@@ -760,7 +761,7 @@ struct Proc
ulong delaysched;
ulong priority; /* priority level */
ulong basepri; /* base priority level */
- uchar fixedpri; /* priority level deson't change */
+ uchar fixedpri; /* priority level doesn't change */
ulong cpu; /* cpu average */
ulong lastupdate;
uchar yield; /* non-zero if the process just did a sleep(0) */
diff --git a/sys/src/9/port/proc.c b/sys/src/9/port/proc.c
index c8deaa401..0af37022c 100644
--- a/sys/src/9/port/proc.c
+++ b/sys/src/9/port/proc.c
@@ -23,8 +23,9 @@ ulong load;
static struct Procalloc
{
Lock;
- Proc* arena;
- Proc* free;
+ Proc **tab;
+ Proc *free;
+ int nextindex;
} procalloc;
enum
@@ -630,13 +631,25 @@ canpage(Proc *p)
Proc*
newproc(void)
{
+ char *b;
Proc *p;
lock(&procalloc);
p = procalloc.free;
- if(p == nil || (p->kstack == nil && (p->kstack = malloc(KSTACK)) == nil)){
- unlock(&procalloc);
- return nil;
+ if(p == nil){
+ if(procalloc.nextindex >= conf.nproc){
+ unlock(&procalloc);
+ return nil;
+ }
+ b = malloc(KSTACK+sizeof(Proc));
+ if(b == nil){
+ unlock(&procalloc);
+ return nil;
+ }
+ p = (Proc*)(b + KSTACK);
+ p->index = procalloc.nextindex++;
+ p->kstack = b;
+ procalloc.tab[p->index] = p;
}
procalloc.free = p->qnext;
p->qnext = nil;
@@ -682,8 +695,7 @@ procwired(Proc *p, int bm)
/* pick a machine to wire to */
memset(nwired, 0, sizeof(nwired));
p->wired = nil;
- for(i=0; i<conf.nproc; i++){
- pp = proctab(i);
+ for(i=0; (pp = proctab(i)) != nil; i++){
wm = pp->wired;
if(wm != nil && pp->pid)
nwired[wm->machno]++;
@@ -720,20 +732,14 @@ procpriority(Proc *p, int pri, int fixed)
void
procinit0(void) /* bad planning - clashes with devproc.c */
{
- Proc *p;
- int i;
-
- p = xalloc(conf.nproc*sizeof(Proc));
- if(p == nil){
+ procalloc.free = nil;
+ /* allocate 1 extra for a nil terminator */
+ procalloc.tab = xalloc((conf.nproc+1)*sizeof(Proc*));
+ if(procalloc.tab == nil){
xsummary();
panic("cannot allocate %lud procs (%ludMB)", conf.nproc, conf.nproc*sizeof(Proc)/(1024*1024));
}
- procalloc.arena = p;
- procalloc.free = p;
- for(i=0; i<conf.nproc-1; i++, p++)
- p->qnext = p+1;
- p->qnext = nil;
-
+ memset(procalloc.tab, 0, (conf.nproc+1)*sizeof(Proc*));
pidinit();
}
@@ -1263,7 +1269,7 @@ pwait(Waitmsg *w)
Proc*
proctab(int i)
{
-#define proctab(x) (&procalloc.arena[(x)])
+#define proctab(x) (procalloc.tab[(x)])
return proctab(i);
}
@@ -1304,8 +1310,7 @@ procflushmmu(int (*match)(Proc*, void*), void *a)
*/
memset(await, 0, conf.nmach*sizeof(await[0]));
nwait = 0;
- for(i = 0; i < conf.nproc; i++){
- p = proctab(i);
+ for(i = 0; (p = proctab(i)) != nil; i++){
if(p->state != Dead && (*match)(p, a)){
p->newtlb = 1;
for(nm = 0; nm < conf.nmach; nm++){
@@ -1572,8 +1577,7 @@ killbig(char *why)
max = 0;
kp = nil;
- for(i = 0; i < conf.nproc; i++) {
- p = proctab(i);
+ for(i = 0; (p = proctab(i)) != nil; i++) {
if(p->state == Dead || p->kp || p->parentpid == 0)
continue;
if((p->noswap || (p->procmode & 0222) == 0) && strcmp(eve, p->user) == 0)
@@ -1588,8 +1592,7 @@ killbig(char *why)
return;
print("%lud: %s killed: %s\n", kp->pid, kp->text, why);
qlock(&kp->seglock);
- for(i = 0; i < conf.nproc; i++) {
- p = proctab(i);
+ for(i = 0; (p = proctab(i)) != nil; i++) {
if(p->state == Dead || p->kp)
continue;
if(p != kp && p->seg[BSEG] != nil && p->seg[BSEG] == kp->seg[BSEG])
@@ -1624,8 +1627,7 @@ renameuser(char *old, char *new)
Proc *p;
int i;
- for(i = 0; i < conf.nproc; i++){
- p = proctab(i);
+ for(i = 0; (p = proctab(i)) != nil; i++){
qlock(&p->debug);
if(p->user != nil && strcmp(old, p->user) == 0)
kstrdup(&p->user, new);
@@ -1809,12 +1811,15 @@ piddel(Pid *i)
int
procindex(ulong pid)
{
+ Proc *p;
Pid *i;
+ int x;
i = pidlookup(pid);
if(i != nil){
- int x = i->procindex;
- if(proctab(x)->pid == pid)
+ x = i->procindex;
+ p = proctab(x);
+ if(p != nil && p->pid == pid)
return x;
}
return -1;
@@ -1869,7 +1874,7 @@ pidalloc(Proc *p)
p->parentpid = 0;
i = pidadd(0);
- i->procindex = (int)(p - procalloc.arena);
+ i->procindex = p->index;
if(p->noteid == 0){
incref(i);