1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
6 // Patterned after tcmalloc's algorithms; shorter code.
15 typedef struct __go_open_array Slice;
17 // NOTE(rsc): Everything here could use cas if contention became an issue.
20 // Per-call-stack allocation information.
21 // Lookup by hashing call stack into a linked-list hash table.
22 typedef struct Bucket Bucket;
25 Bucket *next; // next in hash list
26 Bucket *allnext; // next in list of all buckets
36 BuckHashSize = 179999,
38 static Bucket **buckhash;
39 static Bucket *buckets;
40 static uintptr bucketmem;
42 // Return the bucket for stk[0:nstk], allocating new bucket if needed.
44 stkbucket(uintptr *stk, int32 nstk)
51 buckhash = runtime_SysAlloc(BuckHashSize*sizeof buckhash[0]);
52 mstats.buckhash_sys += BuckHashSize*sizeof buckhash[0];
57 for(i=0; i<nstk; i++) {
66 for(b = buckhash[i]; b; b=b->next)
67 if(b->hash == h && b->nstk == (uintptr)nstk &&
68 runtime_mcmp((byte*)b->stk, (byte*)stk, nstk*sizeof stk[0]) == 0)
71 b = runtime_mallocgc(sizeof *b + nstk*sizeof stk[0], FlagNoProfiling, 0, 1);
72 bucketmem += sizeof *b + nstk*sizeof stk[0];
73 runtime_memmove(b->stk, stk, nstk*sizeof stk[0]);
76 b->next = buckhash[i];
83 // Map from pointer to Bucket* that allocated it.
85 // Linked-list hash table for top N-20 bits.
86 // Array index for next 13 bits.
87 // Linked list for next 7 bits.
88 // This is more efficient than using a general map,
89 // because of the typical clustering of the pointer keys.
91 typedef struct AddrHash AddrHash;
92 typedef struct AddrEntry AddrEntry;
96 AddrHash *next; // next in top-level hash table linked list
97 uintptr addr; // addr>>20
98 AddrEntry *dense[1<<13];
103 AddrEntry *next; // next in bottom-level linked list
109 AddrHashBits = 12 // 1MB per entry, so good for 4GB of used address space
111 static AddrHash *addrhash[1<<AddrHashBits];
112 static AddrEntry *addrfree;
113 static uintptr addrmem;
115 // Multiplicative hash function:
116 // hashMultiplier is the bottom 32 bits of int((sqrt(5)-1)/2 * (1<<32)).
117 // This is a good multiplier as suggested in CLR, Knuth. The hash
118 // value is taken to be the top AddrHashBits bits of the bottom 32 bits
119 // of the multiplied value.
121 HashMultiplier = 2654435769U
124 // Set the bucket associated with addr to b.
126 setaddrbucket(uintptr addr, Bucket *b)
133 h = (uint32)((addr>>20)*HashMultiplier) >> (32-AddrHashBits);
134 for(ah=addrhash[h]; ah; ah=ah->next)
135 if(ah->addr == (addr>>20))
138 ah = runtime_mallocgc(sizeof *ah, FlagNoProfiling, 0, 1);
139 addrmem += sizeof *ah;
140 ah->next = addrhash[h];
145 if((e = addrfree) == nil) {
146 e = runtime_mallocgc(64*sizeof *e, FlagNoProfiling, 0, 0);
147 addrmem += 64*sizeof *e;
148 for(i=0; i+1<64; i++)
153 e->addr = (uint32)~(addr & ((1<<20)-1));
155 h = (addr>>7)&(nelem(ah->dense)-1); // entry in dense is top 13 bits of low 20.
156 e->next = ah->dense[h];
160 // Get the bucket associated with addr and clear the association.
162 getaddrbucket(uintptr addr)
169 h = (uint32)((addr>>20)*HashMultiplier) >> (32-AddrHashBits);
170 for(ah=addrhash[h]; ah; ah=ah->next)
171 if(ah->addr == (addr>>20))
176 h = (addr>>7)&(nelem(ah->dense)-1); // entry in dense is top 13 bits of low 20.
177 for(l=&ah->dense[h]; (e=*l) != nil; l=&e->next) {
178 if(e->addr == (uint32)~(addr & ((1<<20)-1))) {
189 // Called by malloc to record a profiled block.
191 runtime_MProf_Malloc(void *p, uintptr size)
197 if(!__sync_bool_compare_and_swap(&m->nomemprof, 0, 1))
200 nstk = runtime_callers(1, stk, 32);
204 runtime_lock(&proflock);
205 b = stkbucket(stk, nstk);
207 b->alloc_bytes += size;
208 setaddrbucket((uintptr)p, b);
209 runtime_unlock(&proflock);
210 __sync_bool_compare_and_swap(&m->nomemprof, 1, 0);
212 if(__sync_bool_compare_and_swap(&m->gcing_for_prof, 1, 0))
213 __go_run_goroutine_gc(100);
216 // Called when freeing a profiled block.
218 runtime_MProf_Free(void *p, uintptr size)
222 if(!__sync_bool_compare_and_swap(&m->nomemprof, 0, 1))
225 runtime_lock(&proflock);
226 b = getaddrbucket((uintptr)p);
229 b->free_bytes += size;
231 runtime_unlock(&proflock);
232 __sync_bool_compare_and_swap(&m->nomemprof, 1, 0);
234 if(__sync_bool_compare_and_swap(&m->gcing_for_prof, 1, 0))
235 __go_run_goroutine_gc(101);
239 // Go interface to profile data. (Declared in extern.go)
240 // Assumes Go sizeof(int) == sizeof(int32)
242 // Must match MemProfileRecord in extern.go.
243 typedef struct Record Record;
245 int64 alloc_bytes, free_bytes;
246 int64 alloc_objects, free_objects;
250 // Write b's data to r.
252 record(Record *r, Bucket *b)
256 r->alloc_bytes = b->alloc_bytes;
257 r->free_bytes = b->free_bytes;
258 r->alloc_objects = b->allocs;
259 r->free_objects = b->frees;
260 for(i=0; i<b->nstk && i<nelem(r->stk); i++)
261 r->stk[i] = b->stk[i];
262 for(; i<nelem(r->stk); i++)
266 func MemProfile(p Slice, include_inuse_zero bool) (n int32, ok bool) {
270 __sync_bool_compare_and_swap(&m->nomemprof, 0, 1);
272 runtime_lock(&proflock);
274 for(b=buckets; b; b=b->allnext)
275 if(include_inuse_zero || b->alloc_bytes != b->free_bytes)
280 r = (Record*)p.__values;
281 for(b=buckets; b; b=b->allnext)
282 if(include_inuse_zero || b->alloc_bytes != b->free_bytes)
285 runtime_unlock(&proflock);
287 __sync_bool_compare_and_swap(&m->nomemprof, 1, 0);
289 if(__sync_bool_compare_and_swap(&m->gcing_for_prof, 1, 0))
290 __go_run_goroutine_gc(102);
294 runtime_MProf_Mark(void (*scan)(byte *, int64))
296 // buckhash is not allocated via mallocgc.
297 scan((byte*)&buckets, sizeof buckets);
298 scan((byte*)&addrhash, sizeof addrhash);
299 scan((byte*)&addrfree, sizeof addrfree);