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 // NOTE(rsc): Everything here could use cas if contention became an issue.
18 // Per-call-stack allocation information.
19 // Lookup by hashing call stack into a linked-list hash table.
20 typedef struct Bucket Bucket;
23 Bucket *next; // next in hash list
24 Bucket *allnext; // next in list of all buckets
34 BuckHashSize = 179999,
36 static Bucket **buckhash;
37 static Bucket *buckets;
38 static uintptr bucketmem;
40 // Return the bucket for stk[0:nstk], allocating new bucket if needed.
42 stkbucket(uintptr *stk, int32 nstk)
49 buckhash = runtime_SysAlloc(BuckHashSize*sizeof buckhash[0]);
50 mstats.buckhash_sys += BuckHashSize*sizeof buckhash[0];
55 for(i=0; i<nstk; i++) {
64 for(b = buckhash[i]; b; b=b->next)
65 if(b->hash == h && b->nstk == (uintptr)nstk &&
66 runtime_mcmp((byte*)b->stk, (byte*)stk, nstk*sizeof stk[0]) == 0)
69 b = runtime_mallocgc(sizeof *b + nstk*sizeof stk[0], FlagNoProfiling, 0, 1);
70 bucketmem += sizeof *b + nstk*sizeof stk[0];
71 runtime_memmove(b->stk, stk, nstk*sizeof stk[0]);
74 b->next = buckhash[i];
81 // Map from pointer to Bucket* that allocated it.
83 // Linked-list hash table for top N-20 bits.
84 // Array index for next 13 bits.
85 // Linked list for next 7 bits.
86 // This is more efficient than using a general map,
87 // because of the typical clustering of the pointer keys.
89 typedef struct AddrHash AddrHash;
90 typedef struct AddrEntry AddrEntry;
94 AddrHash *next; // next in top-level hash table linked list
95 uintptr addr; // addr>>20
96 AddrEntry *dense[1<<13];
101 AddrEntry *next; // next in bottom-level linked list
107 AddrHashBits = 12 // 1MB per entry, so good for 4GB of used address space
109 static AddrHash *addrhash[1<<AddrHashBits];
110 static AddrEntry *addrfree;
111 static uintptr addrmem;
113 // Multiplicative hash function:
114 // hashMultiplier is the bottom 32 bits of int((sqrt(5)-1)/2 * (1<<32)).
115 // This is a good multiplier as suggested in CLR, Knuth. The hash
116 // value is taken to be the top AddrHashBits bits of the bottom 32 bits
117 // of the multiplied value.
119 HashMultiplier = 2654435769U
122 // Set the bucket associated with addr to b.
124 setaddrbucket(uintptr addr, Bucket *b)
131 h = (uint32)((addr>>20)*HashMultiplier) >> (32-AddrHashBits);
132 for(ah=addrhash[h]; ah; ah=ah->next)
133 if(ah->addr == (addr>>20))
136 ah = runtime_mallocgc(sizeof *ah, FlagNoProfiling, 0, 1);
137 addrmem += sizeof *ah;
138 ah->next = addrhash[h];
143 if((e = addrfree) == nil) {
144 e = runtime_mallocgc(64*sizeof *e, FlagNoProfiling, 0, 0);
145 addrmem += 64*sizeof *e;
146 for(i=0; i+1<64; i++)
151 e->addr = (uint32)~(addr & ((1<<20)-1));
153 h = (addr>>7)&(nelem(ah->dense)-1); // entry in dense is top 13 bits of low 20.
154 e->next = ah->dense[h];
158 // Get the bucket associated with addr and clear the association.
160 getaddrbucket(uintptr addr)
167 h = (uint32)((addr>>20)*HashMultiplier) >> (32-AddrHashBits);
168 for(ah=addrhash[h]; ah; ah=ah->next)
169 if(ah->addr == (addr>>20))
174 h = (addr>>7)&(nelem(ah->dense)-1); // entry in dense is top 13 bits of low 20.
175 for(l=&ah->dense[h]; (e=*l) != nil; l=&e->next) {
176 if(e->addr == (uint32)~(addr & ((1<<20)-1))) {
187 // Called by malloc to record a profiled block.
189 runtime_MProf_Malloc(void *p, uintptr size)
202 nstk = runtime_callers(1, stk, 32);
206 runtime_lock(&proflock);
207 b = stkbucket(stk, nstk);
209 b->alloc_bytes += size;
210 setaddrbucket((uintptr)p, b);
211 runtime_unlock(&proflock);
216 // Called when freeing a profiled block.
218 runtime_MProf_Free(void *p, uintptr size)
228 runtime_lock(&proflock);
229 b = getaddrbucket((uintptr)p);
232 b->free_bytes += size;
234 runtime_unlock(&proflock);
240 // Go interface to profile data. (Declared in extern.go)
241 // Assumes Go sizeof(int) == sizeof(int32)
243 // Must match MemProfileRecord in extern.go.
244 typedef struct Record Record;
246 int64 alloc_bytes, free_bytes;
247 int64 alloc_objects, free_objects;
251 // Write b's data to r.
253 record(Record *r, Bucket *b)
257 r->alloc_bytes = b->alloc_bytes;
258 r->free_bytes = b->free_bytes;
259 r->alloc_objects = b->allocs;
260 r->free_objects = b->frees;
261 for(i=0; i<b->nstk && i<nelem(r->stk); i++)
262 r->stk[i] = b->stk[i];
263 for(; i<nelem(r->stk); i++)
267 func MemProfile(p Slice, include_inuse_zero bool) (n int32, ok bool) {
271 runtime_lock(&proflock);
273 for(b=buckets; b; b=b->allnext)
274 if(include_inuse_zero || b->alloc_bytes != b->free_bytes)
279 r = (Record*)p.__values;
280 for(b=buckets; b; b=b->allnext)
281 if(include_inuse_zero || b->alloc_bytes != b->free_bytes)
284 runtime_unlock(&proflock);
288 runtime_MProf_Mark(void (*scan)(byte *, int64))
290 // buckhash is not allocated via mallocgc.
291 scan((byte*)&buckets, sizeof buckets);
292 scan((byte*)&addrhash, sizeof addrhash);
293 scan((byte*)&addrfree, sizeof addrfree);