diff options
author | cinap_lenrek <cinap_lenrek@felloff.net> | 2020-04-26 19:54:46 +0200 |
---|---|---|
committer | cinap_lenrek <cinap_lenrek@felloff.net> | 2020-04-26 19:54:46 +0200 |
commit | 61a062ee9ff7bbd489baba6dd7c6adb1978e246b (patch) | |
tree | 2424b50e89b341e59088541ecf27eed747663dcc /sys/src/9/port/segment.c | |
parent | c6f7989176b9da3b977f397ac4f20bc2f86dec1b (diff) |
kernel: improve page reclaimation strategy and locking
when reclaiming pages from an image, always reclaim all
the hash chains equally. that way, we avoid being biased
towards the chains at the start of the Image.pghash[] array.
images can be in two states: active or inactive. inactive
images are the ones which are not used by program while
active ones aare.
when reclaiming pages, we should try to reclaim pages
from inactive images first and only if that set becomes
exhausted attempt to release text pages and attempt to
reclaim pages from active images.
when we run out of Image structures, it makes only sense
to reclaim pages from inactive images, as reclaiming pages
from active ones will never free any Image structures.
change putimage() to require a image already locked and
make it unlock the image. this avoids many pointless
unlock()/lock() sequences as all callers of putimage()
already had the image locked.
Diffstat (limited to 'sys/src/9/port/segment.c')
-rw-r--r-- | sys/src/9/port/segment.c | 32 |
1 files changed, 11 insertions, 21 deletions
diff --git a/sys/src/9/port/segment.c b/sys/src/9/port/segment.c index b9f3236b2..1898beaeb 100644 --- a/sys/src/9/port/segment.c +++ b/sys/src/9/port/segment.c @@ -102,7 +102,6 @@ putseg(Segment *s) } if(i->s == s) i->s = nil; - unlock(i); putimage(i); } else if(decref(s) != 0) return; @@ -260,7 +259,7 @@ attachimage(int type, Chan *c, uintptr base, ulong len) /* dump pages of inactive images to free image structures */ while((i = imagealloc.free) == nil) { unlock(&imagealloc); - if(imagereclaim(1000) == 0 && imagealloc.free == nil){ + if(imagereclaim(0) == 0 && imagealloc.free == nil){ freebroken(); /* can use the memory */ resrcwait("no image after reclaim"); } @@ -288,7 +287,6 @@ found: if(i->s == nil) { incref(i); if(waserror()) { - unlock(i); putimage(i); nexterror(); } @@ -316,14 +314,11 @@ imagecached(void) } ulong -imagereclaim(ulong pages) +imagereclaim(int active) { static Image *i, *ie; - ulong np; int j; - - if(pages == 0) - return 0; + ulong np; eqlock(&imagealloc.ireclaim); if(i == nil){ @@ -334,23 +329,19 @@ imagereclaim(ulong pages) for(j = 0; j < conf.nimage; j++, i++){ if(i >= ie) i = imagealloc.list; - if(i->ref == 0) + if(i->ref == 0 || (i->ref != i->pgref) == !active) continue; - /* - * if there are no free image structures, only - * reclaim pages from inactive images. - */ - if(imagealloc.free != nil || i->ref == i->pgref){ - np += pagereclaim(i, pages - np); - if(np >= pages) - break; - } + np += pagereclaim(i); + if(np >= 1000) + goto Done; } +Done: qunlock(&imagealloc.ireclaim); return np; } +/* putimage(): called with image locked and unlocks */ void putimage(Image *i) { @@ -358,14 +349,13 @@ putimage(Image *i) Chan *c; long r; + r = decref(i); if(i->notext){ - decref(i); + unlock(i); return; } c = nil; - lock(i); - r = decref(i); if(r == i->pgref){ /* * all remaining references to this image are from the |