2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Page heap.
|
|
|
|
//
|
|
|
|
// See malloc.h for overview.
|
|
|
|
//
|
|
|
|
// When a MSpan is in the heap free list, state == MSpanFree
|
|
|
|
// and heapmap(s->start) == span, heapmap(s->start+s->npages-1) == span.
|
|
|
|
//
|
|
|
|
// When a MSpan is allocated, state == MSpanInUse
|
|
|
|
// and heapmap(i) == span for all s->start <= i < s->start+s->npages.
|
|
|
|
|
|
|
|
#include "runtime.h"
|
2011-10-27 01:57:58 +02:00
|
|
|
#include "arch.h"
|
2010-12-03 05:34:57 +01:00
|
|
|
#include "malloc.h"
|
|
|
|
|
|
|
|
static MSpan *MHeap_AllocLocked(MHeap*, uintptr, int32);
|
|
|
|
static bool MHeap_Grow(MHeap*, uintptr);
|
|
|
|
static void MHeap_FreeLocked(MHeap*, MSpan*);
|
|
|
|
static MSpan *MHeap_AllocLarge(MHeap*, uintptr);
|
|
|
|
static MSpan *BestFit(MSpan*, uintptr, MSpan*);
|
|
|
|
|
|
|
|
static void
|
|
|
|
RecordSpan(void *vh, byte *p)
|
|
|
|
{
|
|
|
|
MHeap *h;
|
|
|
|
MSpan *s;
|
2012-10-23 06:31:11 +02:00
|
|
|
MSpan **all;
|
|
|
|
uint32 cap;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
h = vh;
|
|
|
|
s = (MSpan*)p;
|
2012-10-23 06:31:11 +02:00
|
|
|
if(h->nspan >= h->nspancap) {
|
|
|
|
cap = 64*1024/sizeof(all[0]);
|
|
|
|
if(cap < h->nspancap*3/2)
|
|
|
|
cap = h->nspancap*3/2;
|
|
|
|
all = (MSpan**)runtime_SysAlloc(cap*sizeof(all[0]));
|
|
|
|
if(h->allspans) {
|
|
|
|
runtime_memmove(all, h->allspans, h->nspancap*sizeof(all[0]));
|
|
|
|
runtime_SysFree(h->allspans, h->nspancap*sizeof(all[0]));
|
|
|
|
}
|
|
|
|
h->allspans = all;
|
|
|
|
h->nspancap = cap;
|
|
|
|
}
|
|
|
|
h->allspans[h->nspan++] = s;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the heap; fetch memory using alloc.
|
|
|
|
void
|
|
|
|
runtime_MHeap_Init(MHeap *h, void *(*alloc)(uintptr))
|
|
|
|
{
|
|
|
|
uint32 i;
|
|
|
|
|
|
|
|
runtime_FixAlloc_Init(&h->spanalloc, sizeof(MSpan), alloc, RecordSpan, h);
|
|
|
|
runtime_FixAlloc_Init(&h->cachealloc, sizeof(MCache), alloc, nil, nil);
|
|
|
|
// h->mapcache needs no init
|
|
|
|
for(i=0; i<nelem(h->free); i++)
|
|
|
|
runtime_MSpanList_Init(&h->free[i]);
|
|
|
|
runtime_MSpanList_Init(&h->large);
|
|
|
|
for(i=0; i<nelem(h->central); i++)
|
|
|
|
runtime_MCentral_Init(&h->central[i], i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate a new span of npage pages from the heap
|
|
|
|
// and record its size class in the HeapMap and HeapMapCache.
|
|
|
|
MSpan*
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_MHeap_Alloc(MHeap *h, uintptr npage, int32 sizeclass, int32 acct, int32 zeroed)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
|
|
|
MSpan *s;
|
|
|
|
|
|
|
|
runtime_lock(h);
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_purgecachedstats(runtime_m()->mcache);
|
2010-12-03 05:34:57 +01:00
|
|
|
s = MHeap_AllocLocked(h, npage, sizeclass);
|
|
|
|
if(s != nil) {
|
|
|
|
mstats.heap_inuse += npage<<PageShift;
|
|
|
|
if(acct) {
|
|
|
|
mstats.heap_objects++;
|
|
|
|
mstats.heap_alloc += npage<<PageShift;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
runtime_unlock(h);
|
2012-10-23 06:31:11 +02:00
|
|
|
if(s != nil && *(uintptr*)(s->start<<PageShift) != 0 && zeroed)
|
|
|
|
runtime_memclr((byte*)(s->start<<PageShift), s->npages<<PageShift);
|
2010-12-03 05:34:57 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static MSpan*
|
|
|
|
MHeap_AllocLocked(MHeap *h, uintptr npage, int32 sizeclass)
|
|
|
|
{
|
|
|
|
uintptr n;
|
|
|
|
MSpan *s, *t;
|
2011-03-17 00:05:44 +01:00
|
|
|
PageID p;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// Try in fixed-size lists up to max.
|
|
|
|
for(n=npage; n < nelem(h->free); n++) {
|
|
|
|
if(!runtime_MSpanList_IsEmpty(&h->free[n])) {
|
|
|
|
s = h->free[n].next;
|
|
|
|
goto HaveSpan;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Best fit in list of large spans.
|
|
|
|
if((s = MHeap_AllocLarge(h, npage)) == nil) {
|
|
|
|
if(!MHeap_Grow(h, npage))
|
|
|
|
return nil;
|
|
|
|
if((s = MHeap_AllocLarge(h, npage)) == nil)
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
HaveSpan:
|
|
|
|
// Mark span in use.
|
|
|
|
if(s->state != MSpanFree)
|
|
|
|
runtime_throw("MHeap_AllocLocked - MSpan not free");
|
|
|
|
if(s->npages < npage)
|
|
|
|
runtime_throw("MHeap_AllocLocked - bad npages");
|
|
|
|
runtime_MSpanList_Remove(s);
|
|
|
|
s->state = MSpanInUse;
|
2011-10-27 01:57:58 +02:00
|
|
|
mstats.heap_idle -= s->npages<<PageShift;
|
2012-03-02 21:01:37 +01:00
|
|
|
mstats.heap_released -= s->npreleased<<PageShift;
|
|
|
|
s->npreleased = 0;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
if(s->npages > npage) {
|
|
|
|
// Trim extra and put it back in the heap.
|
|
|
|
t = runtime_FixAlloc_Alloc(&h->spanalloc);
|
|
|
|
mstats.mspan_inuse = h->spanalloc.inuse;
|
|
|
|
mstats.mspan_sys = h->spanalloc.sys;
|
|
|
|
runtime_MSpan_Init(t, s->start + npage, s->npages - npage);
|
|
|
|
s->npages = npage;
|
2011-03-17 00:05:44 +01:00
|
|
|
p = t->start;
|
|
|
|
if(sizeof(void*) == 8)
|
|
|
|
p -= ((uintptr)h->arena_start>>PageShift);
|
|
|
|
if(p > 0)
|
|
|
|
h->map[p-1] = s;
|
|
|
|
h->map[p] = t;
|
|
|
|
h->map[p+t->npages-1] = t;
|
|
|
|
*(uintptr*)(t->start<<PageShift) = *(uintptr*)(s->start<<PageShift); // copy "needs zeroing" mark
|
2010-12-03 05:34:57 +01:00
|
|
|
t->state = MSpanInUse;
|
|
|
|
MHeap_FreeLocked(h, t);
|
2013-01-29 21:52:43 +01:00
|
|
|
t->unusedsince = s->unusedsince; // preserve age
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2013-01-29 21:52:43 +01:00
|
|
|
s->unusedsince = 0;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// Record span info, because gc needs to be
|
|
|
|
// able to map interior pointer to containing span.
|
|
|
|
s->sizeclass = sizeclass;
|
2012-10-23 06:31:11 +02:00
|
|
|
s->elemsize = (sizeclass==0 ? s->npages<<PageShift : (uintptr)runtime_class_to_size[sizeclass]);
|
|
|
|
s->types.compression = MTypes_Empty;
|
2011-03-17 00:05:44 +01:00
|
|
|
p = s->start;
|
|
|
|
if(sizeof(void*) == 8)
|
|
|
|
p -= ((uintptr)h->arena_start>>PageShift);
|
2010-12-03 05:34:57 +01:00
|
|
|
for(n=0; n<npage; n++)
|
2011-03-17 00:05:44 +01:00
|
|
|
h->map[p+n] = s;
|
2010-12-03 05:34:57 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate a span of exactly npage pages from the list of large spans.
|
|
|
|
static MSpan*
|
|
|
|
MHeap_AllocLarge(MHeap *h, uintptr npage)
|
|
|
|
{
|
|
|
|
return BestFit(&h->large, npage, nil);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search list for smallest span with >= npage pages.
|
|
|
|
// If there are multiple smallest spans, take the one
|
|
|
|
// with the earliest starting address.
|
|
|
|
static MSpan*
|
|
|
|
BestFit(MSpan *list, uintptr npage, MSpan *best)
|
|
|
|
{
|
|
|
|
MSpan *s;
|
|
|
|
|
|
|
|
for(s=list->next; s != list; s=s->next) {
|
|
|
|
if(s->npages < npage)
|
|
|
|
continue;
|
|
|
|
if(best == nil
|
|
|
|
|| s->npages < best->npages
|
|
|
|
|| (s->npages == best->npages && s->start < best->start))
|
|
|
|
best = s;
|
|
|
|
}
|
|
|
|
return best;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to add at least npage pages of memory to the heap,
|
|
|
|
// returning whether it worked.
|
|
|
|
static bool
|
|
|
|
MHeap_Grow(MHeap *h, uintptr npage)
|
|
|
|
{
|
|
|
|
uintptr ask;
|
|
|
|
void *v;
|
|
|
|
MSpan *s;
|
2011-03-17 00:05:44 +01:00
|
|
|
PageID p;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// Ask for a big chunk, to reduce the number of mappings
|
|
|
|
// the operating system needs to track; also amortizes
|
|
|
|
// the overhead of an operating system mapping.
|
2011-01-21 19:19:03 +01:00
|
|
|
// Allocate a multiple of 64kB (16 pages).
|
2010-12-03 05:34:57 +01:00
|
|
|
npage = (npage+15)&~15;
|
|
|
|
ask = npage<<PageShift;
|
2011-05-20 02:18:15 +02:00
|
|
|
if(ask < HeapAllocChunk)
|
2010-12-03 05:34:57 +01:00
|
|
|
ask = HeapAllocChunk;
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
v = runtime_MHeap_SysAlloc(h, ask);
|
2010-12-03 05:34:57 +01:00
|
|
|
if(v == nil) {
|
|
|
|
if(ask > (npage<<PageShift)) {
|
|
|
|
ask = npage<<PageShift;
|
2011-03-17 00:05:44 +01:00
|
|
|
v = runtime_MHeap_SysAlloc(h, ask);
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
if(v == nil) {
|
2012-05-24 22:44:34 +02:00
|
|
|
runtime_printf("runtime: out of memory: cannot allocate %D-byte block (%D in use)\n", (uint64)ask, mstats.heap_sys);
|
2010-12-03 05:34:57 +01:00
|
|
|
return false;
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
mstats.heap_sys += ask;
|
|
|
|
|
|
|
|
// Create a fake "in use" span and free it, so that the
|
|
|
|
// right coalescing happens.
|
|
|
|
s = runtime_FixAlloc_Alloc(&h->spanalloc);
|
|
|
|
mstats.mspan_inuse = h->spanalloc.inuse;
|
|
|
|
mstats.mspan_sys = h->spanalloc.sys;
|
|
|
|
runtime_MSpan_Init(s, (uintptr)v>>PageShift, ask>>PageShift);
|
2011-03-17 00:05:44 +01:00
|
|
|
p = s->start;
|
|
|
|
if(sizeof(void*) == 8)
|
|
|
|
p -= ((uintptr)h->arena_start>>PageShift);
|
|
|
|
h->map[p] = s;
|
|
|
|
h->map[p + s->npages - 1] = s;
|
2010-12-03 05:34:57 +01:00
|
|
|
s->state = MSpanInUse;
|
|
|
|
MHeap_FreeLocked(h, s);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
// Look up the span at the given address.
|
|
|
|
// Address is guaranteed to be in map
|
2010-12-03 05:34:57 +01:00
|
|
|
// and is guaranteed to be start or end of span.
|
|
|
|
MSpan*
|
2011-03-17 00:05:44 +01:00
|
|
|
runtime_MHeap_Lookup(MHeap *h, void *v)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
2011-03-17 00:05:44 +01:00
|
|
|
uintptr p;
|
|
|
|
|
|
|
|
p = (uintptr)v;
|
|
|
|
if(sizeof(void*) == 8)
|
|
|
|
p -= (uintptr)h->arena_start;
|
|
|
|
return h->map[p >> PageShift];
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
// Look up the span at the given address.
|
|
|
|
// Address is *not* guaranteed to be in map
|
2010-12-03 05:34:57 +01:00
|
|
|
// and may be anywhere in the span.
|
|
|
|
// Map entries for the middle of a span are only
|
|
|
|
// valid for allocated spans. Free spans may have
|
|
|
|
// other garbage in their middles, so we have to
|
|
|
|
// check for that.
|
|
|
|
MSpan*
|
2011-03-17 00:05:44 +01:00
|
|
|
runtime_MHeap_LookupMaybe(MHeap *h, void *v)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
|
|
|
MSpan *s;
|
2011-03-17 00:05:44 +01:00
|
|
|
PageID p, q;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
if((byte*)v < h->arena_start || (byte*)v >= h->arena_used)
|
|
|
|
return nil;
|
|
|
|
p = (uintptr)v>>PageShift;
|
|
|
|
q = p;
|
|
|
|
if(sizeof(void*) == 8)
|
|
|
|
q -= (uintptr)h->arena_start >> PageShift;
|
|
|
|
s = h->map[q];
|
2010-12-03 05:34:57 +01:00
|
|
|
if(s == nil || p < s->start || p - s->start >= s->npages)
|
|
|
|
return nil;
|
|
|
|
if(s->state != MSpanInUse)
|
|
|
|
return nil;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free the span back into the heap.
|
|
|
|
void
|
|
|
|
runtime_MHeap_Free(MHeap *h, MSpan *s, int32 acct)
|
|
|
|
{
|
|
|
|
runtime_lock(h);
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_purgecachedstats(runtime_m()->mcache);
|
2010-12-03 05:34:57 +01:00
|
|
|
mstats.heap_inuse -= s->npages<<PageShift;
|
|
|
|
if(acct) {
|
|
|
|
mstats.heap_alloc -= s->npages<<PageShift;
|
|
|
|
mstats.heap_objects--;
|
|
|
|
}
|
|
|
|
MHeap_FreeLocked(h, s);
|
|
|
|
runtime_unlock(h);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
MHeap_FreeLocked(MHeap *h, MSpan *s)
|
|
|
|
{
|
2011-03-17 00:05:44 +01:00
|
|
|
uintptr *sp, *tp;
|
2010-12-03 05:34:57 +01:00
|
|
|
MSpan *t;
|
2011-03-17 00:05:44 +01:00
|
|
|
PageID p;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
if(s->types.sysalloc)
|
|
|
|
runtime_settype_sysfree(s);
|
|
|
|
s->types.compression = MTypes_Empty;
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
if(s->state != MSpanInUse || s->ref != 0) {
|
2012-05-24 22:44:34 +02:00
|
|
|
runtime_printf("MHeap_FreeLocked - span %p ptr %p state %d ref %d\n", s, s->start<<PageShift, s->state, s->ref);
|
2010-12-03 05:34:57 +01:00
|
|
|
runtime_throw("MHeap_FreeLocked - invalid free");
|
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
mstats.heap_idle += s->npages<<PageShift;
|
2010-12-03 05:34:57 +01:00
|
|
|
s->state = MSpanFree;
|
|
|
|
runtime_MSpanList_Remove(s);
|
2011-03-17 00:05:44 +01:00
|
|
|
sp = (uintptr*)(s->start<<PageShift);
|
2013-01-29 21:52:43 +01:00
|
|
|
// Stamp newly unused spans. The scavenger will use that
|
|
|
|
// info to potentially give back some pages to the OS.
|
|
|
|
s->unusedsince = runtime_nanotime();
|
|
|
|
s->npreleased = 0;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// Coalesce with earlier, later spans.
|
2011-03-17 00:05:44 +01:00
|
|
|
p = s->start;
|
|
|
|
if(sizeof(void*) == 8)
|
|
|
|
p -= (uintptr)h->arena_start >> PageShift;
|
|
|
|
if(p > 0 && (t = h->map[p-1]) != nil && t->state != MSpanInUse) {
|
|
|
|
tp = (uintptr*)(t->start<<PageShift);
|
|
|
|
*tp |= *sp; // propagate "needs zeroing" mark
|
2010-12-03 05:34:57 +01:00
|
|
|
s->start = t->start;
|
|
|
|
s->npages += t->npages;
|
2012-03-02 21:01:37 +01:00
|
|
|
s->npreleased = t->npreleased; // absorb released pages
|
2011-03-17 00:05:44 +01:00
|
|
|
p -= t->npages;
|
|
|
|
h->map[p] = s;
|
2010-12-03 05:34:57 +01:00
|
|
|
runtime_MSpanList_Remove(t);
|
|
|
|
t->state = MSpanDead;
|
|
|
|
runtime_FixAlloc_Free(&h->spanalloc, t);
|
|
|
|
mstats.mspan_inuse = h->spanalloc.inuse;
|
|
|
|
mstats.mspan_sys = h->spanalloc.sys;
|
|
|
|
}
|
2011-03-17 00:05:44 +01:00
|
|
|
if(p+s->npages < nelem(h->map) && (t = h->map[p+s->npages]) != nil && t->state != MSpanInUse) {
|
|
|
|
tp = (uintptr*)(t->start<<PageShift);
|
|
|
|
*sp |= *tp; // propagate "needs zeroing" mark
|
2010-12-03 05:34:57 +01:00
|
|
|
s->npages += t->npages;
|
2012-03-02 21:01:37 +01:00
|
|
|
s->npreleased += t->npreleased;
|
2011-03-17 00:05:44 +01:00
|
|
|
h->map[p + s->npages - 1] = s;
|
2010-12-03 05:34:57 +01:00
|
|
|
runtime_MSpanList_Remove(t);
|
|
|
|
t->state = MSpanDead;
|
|
|
|
runtime_FixAlloc_Free(&h->spanalloc, t);
|
|
|
|
mstats.mspan_inuse = h->spanalloc.inuse;
|
|
|
|
mstats.mspan_sys = h->spanalloc.sys;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert s into appropriate list.
|
|
|
|
if(s->npages < nelem(h->free))
|
|
|
|
runtime_MSpanList_Insert(&h->free[s->npages], s);
|
|
|
|
else
|
|
|
|
runtime_MSpanList_Insert(&h->large, s);
|
2012-03-02 21:01:37 +01:00
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2012-11-21 08:03:38 +01:00
|
|
|
static void
|
|
|
|
forcegchelper(void *vnote)
|
|
|
|
{
|
|
|
|
Note *note = (Note*)vnote;
|
|
|
|
|
|
|
|
runtime_gc(1);
|
|
|
|
runtime_notewakeup(note);
|
|
|
|
}
|
|
|
|
|
2012-03-02 21:01:37 +01:00
|
|
|
// Release (part of) unused memory to OS.
|
2012-03-06 18:57:23 +01:00
|
|
|
// Goroutine created at startup.
|
2012-03-02 21:01:37 +01:00
|
|
|
// Loop forever.
|
|
|
|
void
|
|
|
|
runtime_MHeap_Scavenger(void* dummy)
|
|
|
|
{
|
|
|
|
MHeap *h;
|
|
|
|
MSpan *s, *list;
|
|
|
|
uint64 tick, now, forcegc, limit;
|
|
|
|
uint32 k, i;
|
|
|
|
uintptr released, sumreleased;
|
|
|
|
const byte *env;
|
|
|
|
bool trace;
|
2012-11-21 08:03:38 +01:00
|
|
|
Note note, *notep;
|
2012-03-02 21:01:37 +01:00
|
|
|
|
|
|
|
USED(dummy);
|
|
|
|
|
|
|
|
// If we go two minutes without a garbage collection, force one to run.
|
|
|
|
forcegc = 2*60*1e9;
|
|
|
|
// If a span goes unused for 5 minutes after a garbage collection,
|
|
|
|
// we hand it back to the operating system.
|
|
|
|
limit = 5*60*1e9;
|
|
|
|
// Make wake-up period small enough for the sampling to be correct.
|
|
|
|
if(forcegc < limit)
|
|
|
|
tick = forcegc/2;
|
|
|
|
else
|
|
|
|
tick = limit/2;
|
|
|
|
|
|
|
|
trace = false;
|
|
|
|
env = runtime_getenv("GOGCTRACE");
|
|
|
|
if(env != nil)
|
|
|
|
trace = runtime_atoi(env) > 0;
|
|
|
|
|
|
|
|
h = &runtime_mheap;
|
|
|
|
for(k=0;; k++) {
|
|
|
|
runtime_noteclear(¬e);
|
|
|
|
runtime_entersyscall();
|
|
|
|
runtime_notetsleep(¬e, tick);
|
|
|
|
runtime_exitsyscall();
|
|
|
|
|
|
|
|
runtime_lock(h);
|
|
|
|
now = runtime_nanotime();
|
|
|
|
if(now - mstats.last_gc > forcegc) {
|
|
|
|
runtime_unlock(h);
|
2012-11-21 08:03:38 +01:00
|
|
|
// The scavenger can not block other goroutines,
|
|
|
|
// otherwise deadlock detector can fire spuriously.
|
|
|
|
// GC blocks other goroutines via the runtime_worldsema.
|
|
|
|
runtime_noteclear(¬e);
|
|
|
|
notep = ¬e;
|
|
|
|
__go_go(forcegchelper, (void*)¬ep);
|
|
|
|
runtime_entersyscall();
|
|
|
|
runtime_notesleep(¬e);
|
|
|
|
runtime_exitsyscall();
|
2013-01-29 21:52:43 +01:00
|
|
|
if(trace)
|
|
|
|
runtime_printf("scvg%d: GC forced\n", k);
|
2012-03-02 21:01:37 +01:00
|
|
|
runtime_lock(h);
|
|
|
|
now = runtime_nanotime();
|
|
|
|
}
|
|
|
|
sumreleased = 0;
|
|
|
|
for(i=0; i < nelem(h->free)+1; i++) {
|
|
|
|
if(i < nelem(h->free))
|
|
|
|
list = &h->free[i];
|
|
|
|
else
|
|
|
|
list = &h->large;
|
|
|
|
if(runtime_MSpanList_IsEmpty(list))
|
|
|
|
continue;
|
|
|
|
for(s=list->next; s != list; s=s->next) {
|
2013-01-29 21:52:43 +01:00
|
|
|
if((now - s->unusedsince) > limit) {
|
2012-03-02 21:01:37 +01:00
|
|
|
released = (s->npages - s->npreleased) << PageShift;
|
|
|
|
mstats.heap_released += released;
|
|
|
|
sumreleased += released;
|
|
|
|
s->npreleased = s->npages;
|
|
|
|
runtime_SysUnused((void*)(s->start << PageShift), s->npages << PageShift);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
runtime_unlock(h);
|
|
|
|
|
|
|
|
if(trace) {
|
|
|
|
if(sumreleased > 0)
|
2012-05-24 22:44:34 +02:00
|
|
|
runtime_printf("scvg%d: %p MB released\n", k, sumreleased>>20);
|
|
|
|
runtime_printf("scvg%d: inuse: %D, idle: %D, sys: %D, released: %D, consumed: %D (MB)\n",
|
|
|
|
k, mstats.heap_inuse>>20, mstats.heap_idle>>20, mstats.heap_sys>>20,
|
|
|
|
mstats.heap_released>>20, (mstats.heap_sys - mstats.heap_released)>>20);
|
2012-03-02 21:01:37 +01:00
|
|
|
}
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize a new span with the given start and npages.
|
|
|
|
void
|
|
|
|
runtime_MSpan_Init(MSpan *span, PageID start, uintptr npages)
|
|
|
|
{
|
|
|
|
span->next = nil;
|
|
|
|
span->prev = nil;
|
|
|
|
span->start = start;
|
|
|
|
span->npages = npages;
|
|
|
|
span->freelist = nil;
|
|
|
|
span->ref = 0;
|
|
|
|
span->sizeclass = 0;
|
2012-10-23 06:31:11 +02:00
|
|
|
span->elemsize = 0;
|
2010-12-03 05:34:57 +01:00
|
|
|
span->state = 0;
|
2012-03-02 21:01:37 +01:00
|
|
|
span->unusedsince = 0;
|
|
|
|
span->npreleased = 0;
|
2012-10-23 06:31:11 +02:00
|
|
|
span->types.compression = MTypes_Empty;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize an empty doubly-linked list.
|
|
|
|
void
|
|
|
|
runtime_MSpanList_Init(MSpan *list)
|
|
|
|
{
|
|
|
|
list->state = MSpanListHead;
|
|
|
|
list->next = list;
|
|
|
|
list->prev = list;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime_MSpanList_Remove(MSpan *span)
|
|
|
|
{
|
|
|
|
if(span->prev == nil && span->next == nil)
|
|
|
|
return;
|
|
|
|
span->prev->next = span->next;
|
|
|
|
span->next->prev = span->prev;
|
|
|
|
span->prev = nil;
|
|
|
|
span->next = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
runtime_MSpanList_IsEmpty(MSpan *list)
|
|
|
|
{
|
|
|
|
return list->next == list;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime_MSpanList_Insert(MSpan *list, MSpan *span)
|
|
|
|
{
|
2011-03-17 00:05:44 +01:00
|
|
|
if(span->next != nil || span->prev != nil) {
|
2012-05-24 22:44:34 +02:00
|
|
|
runtime_printf("failed MSpanList_Insert %p %p %p\n", span, span->next, span->prev);
|
2010-12-03 05:34:57 +01:00
|
|
|
runtime_throw("MSpanList_Insert");
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
span->next = list->next;
|
|
|
|
span->prev = list;
|
|
|
|
span->next->prev = span;
|
|
|
|
span->prev->next = span;
|
|
|
|
}
|
2011-03-17 00:05:44 +01:00
|
|
|
|
|
|
|
|