2010-03-23 21:48:23 -06: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.
|
|
|
|
|
|
|
|
// Malloc profiling.
|
|
|
|
// Patterned after tcmalloc's algorithms; shorter code.
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
#include "runtime.h"
|
2011-12-16 13:33:58 -07:00
|
|
|
#include "arch_GOARCH.h"
|
2010-03-23 21:48:23 -06:00
|
|
|
#include "malloc.h"
|
2011-12-16 13:33:58 -07:00
|
|
|
#include "defs_GOOS_GOARCH.h"
|
2010-03-23 21:48:23 -06:00
|
|
|
#include "type.h"
|
|
|
|
|
|
|
|
// NOTE(rsc): Everything here could use cas if contention became an issue.
|
|
|
|
static Lock proflock;
|
|
|
|
|
pprof: add goroutine blocking profiling
The profiler collects goroutine blocking information similar to Google Perf Tools.
You may see an example of the profile (converted to svg) attached to
http://code.google.com/p/go/issues/detail?id=3946
The public API changes are:
+pkg runtime, func BlockProfile([]BlockProfileRecord) (int, bool)
+pkg runtime, func SetBlockProfileRate(int)
+pkg runtime, method (*BlockProfileRecord) Stack() []uintptr
+pkg runtime, type BlockProfileRecord struct
+pkg runtime, type BlockProfileRecord struct, Count int64
+pkg runtime, type BlockProfileRecord struct, Cycles int64
+pkg runtime, type BlockProfileRecord struct, embedded StackRecord
R=rsc, dave, minux.ma, r
CC=gobot, golang-dev, r, remyoudompheng
https://golang.org/cl/6443115
2012-10-06 02:56:04 -06:00
|
|
|
enum { MProf, BProf }; // profile types
|
|
|
|
|
|
|
|
// Per-call-stack profiling information.
|
2010-03-23 21:48:23 -06:00
|
|
|
// Lookup by hashing call stack into a linked-list hash table.
|
|
|
|
typedef struct Bucket Bucket;
|
|
|
|
struct Bucket
|
|
|
|
{
|
|
|
|
Bucket *next; // next in hash list
|
pprof: add goroutine blocking profiling
The profiler collects goroutine blocking information similar to Google Perf Tools.
You may see an example of the profile (converted to svg) attached to
http://code.google.com/p/go/issues/detail?id=3946
The public API changes are:
+pkg runtime, func BlockProfile([]BlockProfileRecord) (int, bool)
+pkg runtime, func SetBlockProfileRate(int)
+pkg runtime, method (*BlockProfileRecord) Stack() []uintptr
+pkg runtime, type BlockProfileRecord struct
+pkg runtime, type BlockProfileRecord struct, Count int64
+pkg runtime, type BlockProfileRecord struct, Cycles int64
+pkg runtime, type BlockProfileRecord struct, embedded StackRecord
R=rsc, dave, minux.ma, r
CC=gobot, golang-dev, r, remyoudompheng
https://golang.org/cl/6443115
2012-10-06 02:56:04 -06:00
|
|
|
Bucket *allnext; // next in list of all mbuckets/bbuckets
|
|
|
|
int32 typ;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
struct // typ == MProf
|
|
|
|
{
|
|
|
|
uintptr allocs;
|
|
|
|
uintptr frees;
|
|
|
|
uintptr alloc_bytes;
|
|
|
|
uintptr free_bytes;
|
|
|
|
uintptr recent_allocs; // since last gc
|
|
|
|
uintptr recent_frees;
|
|
|
|
uintptr recent_alloc_bytes;
|
|
|
|
uintptr recent_free_bytes;
|
|
|
|
};
|
|
|
|
struct // typ == BProf
|
|
|
|
{
|
|
|
|
int64 count;
|
|
|
|
int64 cycles;
|
|
|
|
};
|
|
|
|
};
|
2010-03-23 21:48:23 -06:00
|
|
|
uintptr hash;
|
|
|
|
uintptr nstk;
|
|
|
|
uintptr stk[1];
|
|
|
|
};
|
|
|
|
enum {
|
|
|
|
BuckHashSize = 179999,
|
|
|
|
};
|
|
|
|
static Bucket **buckhash;
|
pprof: add goroutine blocking profiling
The profiler collects goroutine blocking information similar to Google Perf Tools.
You may see an example of the profile (converted to svg) attached to
http://code.google.com/p/go/issues/detail?id=3946
The public API changes are:
+pkg runtime, func BlockProfile([]BlockProfileRecord) (int, bool)
+pkg runtime, func SetBlockProfileRate(int)
+pkg runtime, method (*BlockProfileRecord) Stack() []uintptr
+pkg runtime, type BlockProfileRecord struct
+pkg runtime, type BlockProfileRecord struct, Count int64
+pkg runtime, type BlockProfileRecord struct, Cycles int64
+pkg runtime, type BlockProfileRecord struct, embedded StackRecord
R=rsc, dave, minux.ma, r
CC=gobot, golang-dev, r, remyoudompheng
https://golang.org/cl/6443115
2012-10-06 02:56:04 -06:00
|
|
|
static Bucket *mbuckets; // memory profile buckets
|
|
|
|
static Bucket *bbuckets; // blocking profile buckets
|
2010-03-23 21:48:23 -06:00
|
|
|
static uintptr bucketmem;
|
|
|
|
|
|
|
|
// Return the bucket for stk[0:nstk], allocating new bucket if needed.
|
|
|
|
static Bucket*
|
pprof: add goroutine blocking profiling
The profiler collects goroutine blocking information similar to Google Perf Tools.
You may see an example of the profile (converted to svg) attached to
http://code.google.com/p/go/issues/detail?id=3946
The public API changes are:
+pkg runtime, func BlockProfile([]BlockProfileRecord) (int, bool)
+pkg runtime, func SetBlockProfileRate(int)
+pkg runtime, method (*BlockProfileRecord) Stack() []uintptr
+pkg runtime, type BlockProfileRecord struct
+pkg runtime, type BlockProfileRecord struct, Count int64
+pkg runtime, type BlockProfileRecord struct, Cycles int64
+pkg runtime, type BlockProfileRecord struct, embedded StackRecord
R=rsc, dave, minux.ma, r
CC=gobot, golang-dev, r, remyoudompheng
https://golang.org/cl/6443115
2012-10-06 02:56:04 -06:00
|
|
|
stkbucket(int32 typ, uintptr *stk, int32 nstk, bool alloc)
|
2010-03-23 21:48:23 -06:00
|
|
|
{
|
|
|
|
int32 i;
|
|
|
|
uintptr h;
|
|
|
|
Bucket *b;
|
|
|
|
|
2010-03-29 18:30:07 -06:00
|
|
|
if(buckhash == nil) {
|
runtime: ,s/[a-zA-Z0-9_]+/runtime·&/g, almost
Prefix all external symbols in runtime by runtime·,
to avoid conflicts with possible symbols of the same
name in linked-in C libraries. The obvious conflicts
are printf, malloc, and free, but hide everything to
avoid future pain.
The symbols left alone are:
** known to cgo **
_cgo_free
_cgo_malloc
libcgo_thread_start
initcgo
ncgocall
** known to linker **
_rt0_$GOARCH
_rt0_$GOARCH_$GOOS
text
etext
data
end
pclntab
epclntab
symtab
esymtab
** known to C compiler **
_divv
_modv
_div64by32
etc (arch specific)
Tested on darwin/386, darwin/amd64, linux/386, linux/amd64.
Built (but not tested) for freebsd/386, freebsd/amd64, linux/arm, windows/386.
R=r, PeterGo
CC=golang-dev
https://golang.org/cl/2899041
2010-11-04 12:00:19 -06:00
|
|
|
buckhash = runtime·SysAlloc(BuckHashSize*sizeof buckhash[0]);
|
2010-03-29 18:30:07 -06:00
|
|
|
mstats.buckhash_sys += BuckHashSize*sizeof buckhash[0];
|
|
|
|
}
|
2010-03-23 21:48:23 -06:00
|
|
|
|
|
|
|
// Hash stack.
|
|
|
|
h = 0;
|
|
|
|
for(i=0; i<nstk; i++) {
|
|
|
|
h += stk[i];
|
|
|
|
h += h<<10;
|
|
|
|
h ^= h>>6;
|
|
|
|
}
|
|
|
|
h += h<<3;
|
|
|
|
h ^= h>>11;
|
2010-03-24 10:40:09 -06:00
|
|
|
|
2010-03-23 21:48:23 -06:00
|
|
|
i = h%BuckHashSize;
|
|
|
|
for(b = buckhash[i]; b; b=b->next)
|
pprof: add goroutine blocking profiling
The profiler collects goroutine blocking information similar to Google Perf Tools.
You may see an example of the profile (converted to svg) attached to
http://code.google.com/p/go/issues/detail?id=3946
The public API changes are:
+pkg runtime, func BlockProfile([]BlockProfileRecord) (int, bool)
+pkg runtime, func SetBlockProfileRate(int)
+pkg runtime, method (*BlockProfileRecord) Stack() []uintptr
+pkg runtime, type BlockProfileRecord struct
+pkg runtime, type BlockProfileRecord struct, Count int64
+pkg runtime, type BlockProfileRecord struct, Cycles int64
+pkg runtime, type BlockProfileRecord struct, embedded StackRecord
R=rsc, dave, minux.ma, r
CC=gobot, golang-dev, r, remyoudompheng
https://golang.org/cl/6443115
2012-10-06 02:56:04 -06:00
|
|
|
if(b->typ == typ && b->hash == h && b->nstk == nstk &&
|
runtime: ,s/[a-zA-Z0-9_]+/runtime·&/g, almost
Prefix all external symbols in runtime by runtime·,
to avoid conflicts with possible symbols of the same
name in linked-in C libraries. The obvious conflicts
are printf, malloc, and free, but hide everything to
avoid future pain.
The symbols left alone are:
** known to cgo **
_cgo_free
_cgo_malloc
libcgo_thread_start
initcgo
ncgocall
** known to linker **
_rt0_$GOARCH
_rt0_$GOARCH_$GOOS
text
etext
data
end
pclntab
epclntab
symtab
esymtab
** known to C compiler **
_divv
_modv
_div64by32
etc (arch specific)
Tested on darwin/386, darwin/amd64, linux/386, linux/amd64.
Built (but not tested) for freebsd/386, freebsd/amd64, linux/arm, windows/386.
R=r, PeterGo
CC=golang-dev
https://golang.org/cl/2899041
2010-11-04 12:00:19 -06:00
|
|
|
runtime·mcmp((byte*)b->stk, (byte*)stk, nstk*sizeof stk[0]) == 0)
|
2010-03-23 21:48:23 -06:00
|
|
|
return b;
|
|
|
|
|
2012-02-22 19:45:01 -07:00
|
|
|
if(!alloc)
|
|
|
|
return nil;
|
|
|
|
|
2011-02-02 21:03:47 -07:00
|
|
|
b = runtime·mallocgc(sizeof *b + nstk*sizeof stk[0], FlagNoProfiling, 0, 1);
|
2010-03-23 21:48:23 -06:00
|
|
|
bucketmem += sizeof *b + nstk*sizeof stk[0];
|
runtime: ,s/[a-zA-Z0-9_]+/runtime·&/g, almost
Prefix all external symbols in runtime by runtime·,
to avoid conflicts with possible symbols of the same
name in linked-in C libraries. The obvious conflicts
are printf, malloc, and free, but hide everything to
avoid future pain.
The symbols left alone are:
** known to cgo **
_cgo_free
_cgo_malloc
libcgo_thread_start
initcgo
ncgocall
** known to linker **
_rt0_$GOARCH
_rt0_$GOARCH_$GOOS
text
etext
data
end
pclntab
epclntab
symtab
esymtab
** known to C compiler **
_divv
_modv
_div64by32
etc (arch specific)
Tested on darwin/386, darwin/amd64, linux/386, linux/amd64.
Built (but not tested) for freebsd/386, freebsd/amd64, linux/arm, windows/386.
R=r, PeterGo
CC=golang-dev
https://golang.org/cl/2899041
2010-11-04 12:00:19 -06:00
|
|
|
runtime·memmove(b->stk, stk, nstk*sizeof stk[0]);
|
pprof: add goroutine blocking profiling
The profiler collects goroutine blocking information similar to Google Perf Tools.
You may see an example of the profile (converted to svg) attached to
http://code.google.com/p/go/issues/detail?id=3946
The public API changes are:
+pkg runtime, func BlockProfile([]BlockProfileRecord) (int, bool)
+pkg runtime, func SetBlockProfileRate(int)
+pkg runtime, method (*BlockProfileRecord) Stack() []uintptr
+pkg runtime, type BlockProfileRecord struct
+pkg runtime, type BlockProfileRecord struct, Count int64
+pkg runtime, type BlockProfileRecord struct, Cycles int64
+pkg runtime, type BlockProfileRecord struct, embedded StackRecord
R=rsc, dave, minux.ma, r
CC=gobot, golang-dev, r, remyoudompheng
https://golang.org/cl/6443115
2012-10-06 02:56:04 -06:00
|
|
|
b->typ = typ;
|
2010-03-23 21:48:23 -06:00
|
|
|
b->hash = h;
|
|
|
|
b->nstk = nstk;
|
|
|
|
b->next = buckhash[i];
|
|
|
|
buckhash[i] = b;
|
pprof: add goroutine blocking profiling
The profiler collects goroutine blocking information similar to Google Perf Tools.
You may see an example of the profile (converted to svg) attached to
http://code.google.com/p/go/issues/detail?id=3946
The public API changes are:
+pkg runtime, func BlockProfile([]BlockProfileRecord) (int, bool)
+pkg runtime, func SetBlockProfileRate(int)
+pkg runtime, method (*BlockProfileRecord) Stack() []uintptr
+pkg runtime, type BlockProfileRecord struct
+pkg runtime, type BlockProfileRecord struct, Count int64
+pkg runtime, type BlockProfileRecord struct, Cycles int64
+pkg runtime, type BlockProfileRecord struct, embedded StackRecord
R=rsc, dave, minux.ma, r
CC=gobot, golang-dev, r, remyoudompheng
https://golang.org/cl/6443115
2012-10-06 02:56:04 -06:00
|
|
|
if(typ == MProf) {
|
|
|
|
b->allnext = mbuckets;
|
|
|
|
mbuckets = b;
|
|
|
|
} else {
|
|
|
|
b->allnext = bbuckets;
|
|
|
|
bbuckets = b;
|
|
|
|
}
|
2010-03-23 21:48:23 -06:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2012-02-22 19:45:01 -07:00
|
|
|
// Record that a gc just happened: all the 'recent' statistics are now real.
|
|
|
|
void
|
|
|
|
runtime·MProf_GC(void)
|
|
|
|
{
|
|
|
|
Bucket *b;
|
|
|
|
|
|
|
|
runtime·lock(&proflock);
|
pprof: add goroutine blocking profiling
The profiler collects goroutine blocking information similar to Google Perf Tools.
You may see an example of the profile (converted to svg) attached to
http://code.google.com/p/go/issues/detail?id=3946
The public API changes are:
+pkg runtime, func BlockProfile([]BlockProfileRecord) (int, bool)
+pkg runtime, func SetBlockProfileRate(int)
+pkg runtime, method (*BlockProfileRecord) Stack() []uintptr
+pkg runtime, type BlockProfileRecord struct
+pkg runtime, type BlockProfileRecord struct, Count int64
+pkg runtime, type BlockProfileRecord struct, Cycles int64
+pkg runtime, type BlockProfileRecord struct, embedded StackRecord
R=rsc, dave, minux.ma, r
CC=gobot, golang-dev, r, remyoudompheng
https://golang.org/cl/6443115
2012-10-06 02:56:04 -06:00
|
|
|
for(b=mbuckets; b; b=b->allnext) {
|
2012-02-22 19:45:01 -07:00
|
|
|
b->allocs += b->recent_allocs;
|
|
|
|
b->frees += b->recent_frees;
|
|
|
|
b->alloc_bytes += b->recent_alloc_bytes;
|
|
|
|
b->free_bytes += b->recent_free_bytes;
|
|
|
|
b->recent_allocs = 0;
|
|
|
|
b->recent_frees = 0;
|
|
|
|
b->recent_alloc_bytes = 0;
|
|
|
|
b->recent_free_bytes = 0;
|
|
|
|
}
|
|
|
|
runtime·unlock(&proflock);
|
|
|
|
}
|
|
|
|
|
2010-03-23 21:48:23 -06:00
|
|
|
// Map from pointer to Bucket* that allocated it.
|
|
|
|
// Three levels:
|
2012-05-31 15:30:55 -06:00
|
|
|
// Linked-list hash table for top N-AddrHashShift bits.
|
|
|
|
// Array index for next AddrDenseBits bits.
|
|
|
|
// Linked list for next AddrHashShift-AddrDenseBits bits.
|
2010-03-23 21:48:23 -06:00
|
|
|
// This is more efficient than using a general map,
|
|
|
|
// because of the typical clustering of the pointer keys.
|
|
|
|
|
|
|
|
typedef struct AddrHash AddrHash;
|
|
|
|
typedef struct AddrEntry AddrEntry;
|
|
|
|
|
2012-05-31 15:30:55 -06:00
|
|
|
enum {
|
|
|
|
AddrHashBits = 12, // good for 4GB of used address space
|
|
|
|
AddrHashShift = 20, // each AddrHash knows about 1MB of address space
|
|
|
|
AddrDenseBits = 8, // good for a profiling rate of 4096 bytes
|
|
|
|
};
|
|
|
|
|
2010-03-23 21:48:23 -06:00
|
|
|
struct AddrHash
|
|
|
|
{
|
|
|
|
AddrHash *next; // next in top-level hash table linked list
|
|
|
|
uintptr addr; // addr>>20
|
2012-05-31 15:30:55 -06:00
|
|
|
AddrEntry *dense[1<<AddrDenseBits];
|
2010-03-23 21:48:23 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
struct AddrEntry
|
|
|
|
{
|
|
|
|
AddrEntry *next; // next in bottom-level linked list
|
|
|
|
uint32 addr;
|
|
|
|
Bucket *b;
|
|
|
|
};
|
|
|
|
|
|
|
|
static AddrHash *addrhash[1<<AddrHashBits];
|
|
|
|
static AddrEntry *addrfree;
|
|
|
|
static uintptr addrmem;
|
|
|
|
|
|
|
|
// Multiplicative hash function:
|
|
|
|
// hashMultiplier is the bottom 32 bits of int((sqrt(5)-1)/2 * (1<<32)).
|
|
|
|
// This is a good multiplier as suggested in CLR, Knuth. The hash
|
|
|
|
// value is taken to be the top AddrHashBits bits of the bottom 32 bits
|
2011-05-30 02:02:59 -06:00
|
|
|
// of the multiplied value.
|
2010-03-23 21:48:23 -06:00
|
|
|
enum {
|
|
|
|
HashMultiplier = 2654435769U
|
|
|
|
};
|
|
|
|
|
|
|
|
// Set the bucket associated with addr to b.
|
|
|
|
static void
|
|
|
|
setaddrbucket(uintptr addr, Bucket *b)
|
|
|
|
{
|
|
|
|
int32 i;
|
|
|
|
uint32 h;
|
|
|
|
AddrHash *ah;
|
|
|
|
AddrEntry *e;
|
|
|
|
|
2012-05-31 15:30:55 -06:00
|
|
|
h = (uint32)((addr>>AddrHashShift)*HashMultiplier) >> (32-AddrHashBits);
|
2010-03-23 21:48:23 -06:00
|
|
|
for(ah=addrhash[h]; ah; ah=ah->next)
|
2012-05-31 15:30:55 -06:00
|
|
|
if(ah->addr == (addr>>AddrHashShift))
|
2010-03-23 21:48:23 -06:00
|
|
|
goto found;
|
|
|
|
|
2011-02-02 21:03:47 -07:00
|
|
|
ah = runtime·mallocgc(sizeof *ah, FlagNoProfiling, 0, 1);
|
2010-03-23 21:48:23 -06:00
|
|
|
addrmem += sizeof *ah;
|
|
|
|
ah->next = addrhash[h];
|
2012-05-31 15:30:55 -06:00
|
|
|
ah->addr = addr>>AddrHashShift;
|
2010-03-23 21:48:23 -06:00
|
|
|
addrhash[h] = ah;
|
|
|
|
|
|
|
|
found:
|
|
|
|
if((e = addrfree) == nil) {
|
2011-02-02 21:03:47 -07:00
|
|
|
e = runtime·mallocgc(64*sizeof *e, FlagNoProfiling, 0, 0);
|
2010-03-23 21:48:23 -06:00
|
|
|
addrmem += 64*sizeof *e;
|
|
|
|
for(i=0; i+1<64; i++)
|
|
|
|
e[i].next = &e[i+1];
|
|
|
|
e[63].next = nil;
|
|
|
|
}
|
|
|
|
addrfree = e->next;
|
2012-05-31 15:30:55 -06:00
|
|
|
e->addr = (uint32)~(addr & ((1<<AddrHashShift)-1));
|
2010-03-23 21:48:23 -06:00
|
|
|
e->b = b;
|
2012-05-31 15:30:55 -06:00
|
|
|
h = (addr>>(AddrHashShift-AddrDenseBits))&(nelem(ah->dense)-1); // entry in dense is top 8 bits of low 20.
|
2010-03-23 21:48:23 -06:00
|
|
|
e->next = ah->dense[h];
|
|
|
|
ah->dense[h] = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the bucket associated with addr and clear the association.
|
|
|
|
static Bucket*
|
|
|
|
getaddrbucket(uintptr addr)
|
|
|
|
{
|
|
|
|
uint32 h;
|
|
|
|
AddrHash *ah;
|
|
|
|
AddrEntry *e, **l;
|
|
|
|
Bucket *b;
|
2010-03-24 10:40:09 -06:00
|
|
|
|
2012-05-31 15:30:55 -06:00
|
|
|
h = (uint32)((addr>>AddrHashShift)*HashMultiplier) >> (32-AddrHashBits);
|
2010-03-23 21:48:23 -06:00
|
|
|
for(ah=addrhash[h]; ah; ah=ah->next)
|
2012-05-31 15:30:55 -06:00
|
|
|
if(ah->addr == (addr>>AddrHashShift))
|
2010-03-23 21:48:23 -06:00
|
|
|
goto found;
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
found:
|
2012-05-31 15:30:55 -06:00
|
|
|
h = (addr>>(AddrHashShift-AddrDenseBits))&(nelem(ah->dense)-1); // entry in dense is top 8 bits of low 20.
|
2010-03-23 21:48:23 -06:00
|
|
|
for(l=&ah->dense[h]; (e=*l) != nil; l=&e->next) {
|
2012-05-31 15:30:55 -06:00
|
|
|
if(e->addr == (uint32)~(addr & ((1<<AddrHashShift)-1))) {
|
2010-03-23 21:48:23 -06:00
|
|
|
*l = e->next;
|
|
|
|
b = e->b;
|
|
|
|
e->next = addrfree;
|
|
|
|
addrfree = e;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called by malloc to record a profiled block.
|
|
|
|
void
|
runtime: ,s/[a-zA-Z0-9_]+/runtime·&/g, almost
Prefix all external symbols in runtime by runtime·,
to avoid conflicts with possible symbols of the same
name in linked-in C libraries. The obvious conflicts
are printf, malloc, and free, but hide everything to
avoid future pain.
The symbols left alone are:
** known to cgo **
_cgo_free
_cgo_malloc
libcgo_thread_start
initcgo
ncgocall
** known to linker **
_rt0_$GOARCH
_rt0_$GOARCH_$GOOS
text
etext
data
end
pclntab
epclntab
symtab
esymtab
** known to C compiler **
_divv
_modv
_div64by32
etc (arch specific)
Tested on darwin/386, darwin/amd64, linux/386, linux/amd64.
Built (but not tested) for freebsd/386, freebsd/amd64, linux/arm, windows/386.
R=r, PeterGo
CC=golang-dev
https://golang.org/cl/2899041
2010-11-04 12:00:19 -06:00
|
|
|
runtime·MProf_Malloc(void *p, uintptr size)
|
2010-03-23 21:48:23 -06:00
|
|
|
{
|
|
|
|
int32 nstk;
|
|
|
|
uintptr stk[32];
|
|
|
|
Bucket *b;
|
|
|
|
|
2010-03-24 10:40:09 -06:00
|
|
|
if(m->nomemprof > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m->nomemprof++;
|
runtime: ,s/[a-zA-Z0-9_]+/runtime·&/g, almost
Prefix all external symbols in runtime by runtime·,
to avoid conflicts with possible symbols of the same
name in linked-in C libraries. The obvious conflicts
are printf, malloc, and free, but hide everything to
avoid future pain.
The symbols left alone are:
** known to cgo **
_cgo_free
_cgo_malloc
libcgo_thread_start
initcgo
ncgocall
** known to linker **
_rt0_$GOARCH
_rt0_$GOARCH_$GOOS
text
etext
data
end
pclntab
epclntab
symtab
esymtab
** known to C compiler **
_divv
_modv
_div64by32
etc (arch specific)
Tested on darwin/386, darwin/amd64, linux/386, linux/amd64.
Built (but not tested) for freebsd/386, freebsd/amd64, linux/arm, windows/386.
R=r, PeterGo
CC=golang-dev
https://golang.org/cl/2899041
2010-11-04 12:00:19 -06:00
|
|
|
nstk = runtime·callers(1, stk, 32);
|
|
|
|
runtime·lock(&proflock);
|
pprof: add goroutine blocking profiling
The profiler collects goroutine blocking information similar to Google Perf Tools.
You may see an example of the profile (converted to svg) attached to
http://code.google.com/p/go/issues/detail?id=3946
The public API changes are:
+pkg runtime, func BlockProfile([]BlockProfileRecord) (int, bool)
+pkg runtime, func SetBlockProfileRate(int)
+pkg runtime, method (*BlockProfileRecord) Stack() []uintptr
+pkg runtime, type BlockProfileRecord struct
+pkg runtime, type BlockProfileRecord struct, Count int64
+pkg runtime, type BlockProfileRecord struct, Cycles int64
+pkg runtime, type BlockProfileRecord struct, embedded StackRecord
R=rsc, dave, minux.ma, r
CC=gobot, golang-dev, r, remyoudompheng
https://golang.org/cl/6443115
2012-10-06 02:56:04 -06:00
|
|
|
b = stkbucket(MProf, stk, nstk, true);
|
2012-02-22 19:45:01 -07:00
|
|
|
b->recent_allocs++;
|
|
|
|
b->recent_alloc_bytes += size;
|
2010-03-23 21:48:23 -06:00
|
|
|
setaddrbucket((uintptr)p, b);
|
runtime: ,s/[a-zA-Z0-9_]+/runtime·&/g, almost
Prefix all external symbols in runtime by runtime·,
to avoid conflicts with possible symbols of the same
name in linked-in C libraries. The obvious conflicts
are printf, malloc, and free, but hide everything to
avoid future pain.
The symbols left alone are:
** known to cgo **
_cgo_free
_cgo_malloc
libcgo_thread_start
initcgo
ncgocall
** known to linker **
_rt0_$GOARCH
_rt0_$GOARCH_$GOOS
text
etext
data
end
pclntab
epclntab
symtab
esymtab
** known to C compiler **
_divv
_modv
_div64by32
etc (arch specific)
Tested on darwin/386, darwin/amd64, linux/386, linux/amd64.
Built (but not tested) for freebsd/386, freebsd/amd64, linux/arm, windows/386.
R=r, PeterGo
CC=golang-dev
https://golang.org/cl/2899041
2010-11-04 12:00:19 -06:00
|
|
|
runtime·unlock(&proflock);
|
2010-03-24 10:40:09 -06:00
|
|
|
m->nomemprof--;
|
2010-03-23 21:48:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called when freeing a profiled block.
|
|
|
|
void
|
runtime: ,s/[a-zA-Z0-9_]+/runtime·&/g, almost
Prefix all external symbols in runtime by runtime·,
to avoid conflicts with possible symbols of the same
name in linked-in C libraries. The obvious conflicts
are printf, malloc, and free, but hide everything to
avoid future pain.
The symbols left alone are:
** known to cgo **
_cgo_free
_cgo_malloc
libcgo_thread_start
initcgo
ncgocall
** known to linker **
_rt0_$GOARCH
_rt0_$GOARCH_$GOOS
text
etext
data
end
pclntab
epclntab
symtab
esymtab
** known to C compiler **
_divv
_modv
_div64by32
etc (arch specific)
Tested on darwin/386, darwin/amd64, linux/386, linux/amd64.
Built (but not tested) for freebsd/386, freebsd/amd64, linux/arm, windows/386.
R=r, PeterGo
CC=golang-dev
https://golang.org/cl/2899041
2010-11-04 12:00:19 -06:00
|
|
|
runtime·MProf_Free(void *p, uintptr size)
|
2010-03-23 21:48:23 -06:00
|
|
|
{
|
|
|
|
Bucket *b;
|
|
|
|
|
2010-03-24 10:40:09 -06:00
|
|
|
if(m->nomemprof > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m->nomemprof++;
|
runtime: ,s/[a-zA-Z0-9_]+/runtime·&/g, almost
Prefix all external symbols in runtime by runtime·,
to avoid conflicts with possible symbols of the same
name in linked-in C libraries. The obvious conflicts
are printf, malloc, and free, but hide everything to
avoid future pain.
The symbols left alone are:
** known to cgo **
_cgo_free
_cgo_malloc
libcgo_thread_start
initcgo
ncgocall
** known to linker **
_rt0_$GOARCH
_rt0_$GOARCH_$GOOS
text
etext
data
end
pclntab
epclntab
symtab
esymtab
** known to C compiler **
_divv
_modv
_div64by32
etc (arch specific)
Tested on darwin/386, darwin/amd64, linux/386, linux/amd64.
Built (but not tested) for freebsd/386, freebsd/amd64, linux/arm, windows/386.
R=r, PeterGo
CC=golang-dev
https://golang.org/cl/2899041
2010-11-04 12:00:19 -06:00
|
|
|
runtime·lock(&proflock);
|
2010-03-23 21:48:23 -06:00
|
|
|
b = getaddrbucket((uintptr)p);
|
|
|
|
if(b != nil) {
|
2012-02-22 19:45:01 -07:00
|
|
|
b->recent_frees++;
|
|
|
|
b->recent_free_bytes += size;
|
2010-03-23 21:48:23 -06:00
|
|
|
}
|
runtime: ,s/[a-zA-Z0-9_]+/runtime·&/g, almost
Prefix all external symbols in runtime by runtime·,
to avoid conflicts with possible symbols of the same
name in linked-in C libraries. The obvious conflicts
are printf, malloc, and free, but hide everything to
avoid future pain.
The symbols left alone are:
** known to cgo **
_cgo_free
_cgo_malloc
libcgo_thread_start
initcgo
ncgocall
** known to linker **
_rt0_$GOARCH
_rt0_$GOARCH_$GOOS
text
etext
data
end
pclntab
epclntab
symtab
esymtab
** known to C compiler **
_divv
_modv
_div64by32
etc (arch specific)
Tested on darwin/386, darwin/amd64, linux/386, linux/amd64.
Built (but not tested) for freebsd/386, freebsd/amd64, linux/arm, windows/386.
R=r, PeterGo
CC=golang-dev
https://golang.org/cl/2899041
2010-11-04 12:00:19 -06:00
|
|
|
runtime·unlock(&proflock);
|
2010-03-24 10:40:09 -06:00
|
|
|
m->nomemprof--;
|
2010-03-23 21:48:23 -06:00
|
|
|
}
|
|
|
|
|
pprof: add goroutine blocking profiling
The profiler collects goroutine blocking information similar to Google Perf Tools.
You may see an example of the profile (converted to svg) attached to
http://code.google.com/p/go/issues/detail?id=3946
The public API changes are:
+pkg runtime, func BlockProfile([]BlockProfileRecord) (int, bool)
+pkg runtime, func SetBlockProfileRate(int)
+pkg runtime, method (*BlockProfileRecord) Stack() []uintptr
+pkg runtime, type BlockProfileRecord struct
+pkg runtime, type BlockProfileRecord struct, Count int64
+pkg runtime, type BlockProfileRecord struct, Cycles int64
+pkg runtime, type BlockProfileRecord struct, embedded StackRecord
R=rsc, dave, minux.ma, r
CC=gobot, golang-dev, r, remyoudompheng
https://golang.org/cl/6443115
2012-10-06 02:56:04 -06:00
|
|
|
int64 runtime·blockprofilerate; // in CPU ticks
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime·SetBlockProfileRate(intgo rate)
|
|
|
|
{
|
|
|
|
runtime·atomicstore64((uint64*)&runtime·blockprofilerate, rate * runtime·tickspersecond() / (1000*1000*1000));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime·blockevent(int64 cycles, int32 skip)
|
|
|
|
{
|
|
|
|
int32 nstk;
|
|
|
|
int64 rate;
|
|
|
|
uintptr stk[32];
|
|
|
|
Bucket *b;
|
|
|
|
|
|
|
|
if(cycles <= 0)
|
|
|
|
return;
|
|
|
|
rate = runtime·atomicload64((uint64*)&runtime·blockprofilerate);
|
|
|
|
if(rate <= 0 || (rate > cycles && runtime·fastrand1()%rate > cycles))
|
|
|
|
return;
|
|
|
|
|
|
|
|
nstk = runtime·callers(skip, stk, 32);
|
|
|
|
runtime·lock(&proflock);
|
|
|
|
b = stkbucket(BProf, stk, nstk, true);
|
|
|
|
b->count++;
|
|
|
|
b->cycles += cycles;
|
|
|
|
runtime·unlock(&proflock);
|
|
|
|
}
|
2010-03-23 21:48:23 -06:00
|
|
|
|
2012-10-21 11:08:13 -06:00
|
|
|
// Go interface to profile data. (Declared in debug.go)
|
2010-03-23 21:48:23 -06:00
|
|
|
|
2012-02-08 08:33:54 -07:00
|
|
|
// Must match MemProfileRecord in debug.go.
|
2010-03-24 10:40:09 -06:00
|
|
|
typedef struct Record Record;
|
|
|
|
struct Record {
|
|
|
|
int64 alloc_bytes, free_bytes;
|
|
|
|
int64 alloc_objects, free_objects;
|
|
|
|
uintptr stk[32];
|
|
|
|
};
|
|
|
|
|
|
|
|
// Write b's data to r.
|
|
|
|
static void
|
|
|
|
record(Record *r, Bucket *b)
|
|
|
|
{
|
|
|
|
int32 i;
|
|
|
|
|
|
|
|
r->alloc_bytes = b->alloc_bytes;
|
|
|
|
r->free_bytes = b->free_bytes;
|
|
|
|
r->alloc_objects = b->allocs;
|
|
|
|
r->free_objects = b->frees;
|
|
|
|
for(i=0; i<b->nstk && i<nelem(r->stk); i++)
|
|
|
|
r->stk[i] = b->stk[i];
|
|
|
|
for(; i<nelem(r->stk); i++)
|
|
|
|
r->stk[i] = 0;
|
2010-03-23 21:48:23 -06:00
|
|
|
}
|
|
|
|
|
2012-09-24 12:58:34 -06:00
|
|
|
func MemProfile(p Slice, include_inuse_zero bool) (n int, ok bool) {
|
2010-03-24 10:40:09 -06:00
|
|
|
Bucket *b;
|
|
|
|
Record *r;
|
|
|
|
|
runtime: ,s/[a-zA-Z0-9_]+/runtime·&/g, almost
Prefix all external symbols in runtime by runtime·,
to avoid conflicts with possible symbols of the same
name in linked-in C libraries. The obvious conflicts
are printf, malloc, and free, but hide everything to
avoid future pain.
The symbols left alone are:
** known to cgo **
_cgo_free
_cgo_malloc
libcgo_thread_start
initcgo
ncgocall
** known to linker **
_rt0_$GOARCH
_rt0_$GOARCH_$GOOS
text
etext
data
end
pclntab
epclntab
symtab
esymtab
** known to C compiler **
_divv
_modv
_div64by32
etc (arch specific)
Tested on darwin/386, darwin/amd64, linux/386, linux/amd64.
Built (but not tested) for freebsd/386, freebsd/amd64, linux/arm, windows/386.
R=r, PeterGo
CC=golang-dev
https://golang.org/cl/2899041
2010-11-04 12:00:19 -06:00
|
|
|
runtime·lock(&proflock);
|
2010-03-24 10:40:09 -06:00
|
|
|
n = 0;
|
pprof: add goroutine blocking profiling
The profiler collects goroutine blocking information similar to Google Perf Tools.
You may see an example of the profile (converted to svg) attached to
http://code.google.com/p/go/issues/detail?id=3946
The public API changes are:
+pkg runtime, func BlockProfile([]BlockProfileRecord) (int, bool)
+pkg runtime, func SetBlockProfileRate(int)
+pkg runtime, method (*BlockProfileRecord) Stack() []uintptr
+pkg runtime, type BlockProfileRecord struct
+pkg runtime, type BlockProfileRecord struct, Count int64
+pkg runtime, type BlockProfileRecord struct, Cycles int64
+pkg runtime, type BlockProfileRecord struct, embedded StackRecord
R=rsc, dave, minux.ma, r
CC=gobot, golang-dev, r, remyoudompheng
https://golang.org/cl/6443115
2012-10-06 02:56:04 -06:00
|
|
|
for(b=mbuckets; b; b=b->allnext)
|
2010-03-24 10:40:09 -06:00
|
|
|
if(include_inuse_zero || b->alloc_bytes != b->free_bytes)
|
|
|
|
n++;
|
|
|
|
ok = false;
|
|
|
|
if(n <= p.len) {
|
|
|
|
ok = true;
|
|
|
|
r = (Record*)p.array;
|
pprof: add goroutine blocking profiling
The profiler collects goroutine blocking information similar to Google Perf Tools.
You may see an example of the profile (converted to svg) attached to
http://code.google.com/p/go/issues/detail?id=3946
The public API changes are:
+pkg runtime, func BlockProfile([]BlockProfileRecord) (int, bool)
+pkg runtime, func SetBlockProfileRate(int)
+pkg runtime, method (*BlockProfileRecord) Stack() []uintptr
+pkg runtime, type BlockProfileRecord struct
+pkg runtime, type BlockProfileRecord struct, Count int64
+pkg runtime, type BlockProfileRecord struct, Cycles int64
+pkg runtime, type BlockProfileRecord struct, embedded StackRecord
R=rsc, dave, minux.ma, r
CC=gobot, golang-dev, r, remyoudompheng
https://golang.org/cl/6443115
2012-10-06 02:56:04 -06:00
|
|
|
for(b=mbuckets; b; b=b->allnext)
|
2010-03-24 10:40:09 -06:00
|
|
|
if(include_inuse_zero || b->alloc_bytes != b->free_bytes)
|
|
|
|
record(r++, b);
|
|
|
|
}
|
runtime: ,s/[a-zA-Z0-9_]+/runtime·&/g, almost
Prefix all external symbols in runtime by runtime·,
to avoid conflicts with possible symbols of the same
name in linked-in C libraries. The obvious conflicts
are printf, malloc, and free, but hide everything to
avoid future pain.
The symbols left alone are:
** known to cgo **
_cgo_free
_cgo_malloc
libcgo_thread_start
initcgo
ncgocall
** known to linker **
_rt0_$GOARCH
_rt0_$GOARCH_$GOOS
text
etext
data
end
pclntab
epclntab
symtab
esymtab
** known to C compiler **
_divv
_modv
_div64by32
etc (arch specific)
Tested on darwin/386, darwin/amd64, linux/386, linux/amd64.
Built (but not tested) for freebsd/386, freebsd/amd64, linux/arm, windows/386.
R=r, PeterGo
CC=golang-dev
https://golang.org/cl/2899041
2010-11-04 12:00:19 -06:00
|
|
|
runtime·unlock(&proflock);
|
2010-03-24 10:40:09 -06:00
|
|
|
}
|
2012-02-08 08:33:54 -07:00
|
|
|
|
pprof: add goroutine blocking profiling
The profiler collects goroutine blocking information similar to Google Perf Tools.
You may see an example of the profile (converted to svg) attached to
http://code.google.com/p/go/issues/detail?id=3946
The public API changes are:
+pkg runtime, func BlockProfile([]BlockProfileRecord) (int, bool)
+pkg runtime, func SetBlockProfileRate(int)
+pkg runtime, method (*BlockProfileRecord) Stack() []uintptr
+pkg runtime, type BlockProfileRecord struct
+pkg runtime, type BlockProfileRecord struct, Count int64
+pkg runtime, type BlockProfileRecord struct, Cycles int64
+pkg runtime, type BlockProfileRecord struct, embedded StackRecord
R=rsc, dave, minux.ma, r
CC=gobot, golang-dev, r, remyoudompheng
https://golang.org/cl/6443115
2012-10-06 02:56:04 -06:00
|
|
|
// Must match BlockProfileRecord in debug.go.
|
|
|
|
typedef struct BRecord BRecord;
|
|
|
|
struct BRecord {
|
|
|
|
int64 count;
|
|
|
|
int64 cycles;
|
|
|
|
uintptr stk[32];
|
|
|
|
};
|
|
|
|
|
|
|
|
func BlockProfile(p Slice) (n int, ok bool) {
|
|
|
|
Bucket *b;
|
|
|
|
BRecord *r;
|
|
|
|
int32 i;
|
|
|
|
|
|
|
|
runtime·lock(&proflock);
|
|
|
|
n = 0;
|
|
|
|
for(b=bbuckets; b; b=b->allnext)
|
|
|
|
n++;
|
|
|
|
ok = false;
|
|
|
|
if(n <= p.len) {
|
|
|
|
ok = true;
|
|
|
|
r = (BRecord*)p.array;
|
|
|
|
for(b=bbuckets; b; b=b->allnext, r++) {
|
|
|
|
r->count = b->count;
|
|
|
|
r->cycles = b->cycles;
|
|
|
|
for(i=0; i<b->nstk && i<nelem(r->stk); i++)
|
|
|
|
r->stk[i] = b->stk[i];
|
|
|
|
for(; i<nelem(r->stk); i++)
|
|
|
|
r->stk[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
runtime·unlock(&proflock);
|
|
|
|
}
|
|
|
|
|
2012-02-22 19:45:01 -07:00
|
|
|
// Must match StackRecord in debug.go.
|
2012-02-08 08:33:54 -07:00
|
|
|
typedef struct TRecord TRecord;
|
|
|
|
struct TRecord {
|
|
|
|
uintptr stk[32];
|
|
|
|
};
|
|
|
|
|
2012-09-24 12:58:34 -06:00
|
|
|
func ThreadCreateProfile(p Slice) (n int, ok bool) {
|
2012-02-08 08:33:54 -07:00
|
|
|
TRecord *r;
|
|
|
|
M *first, *m;
|
|
|
|
|
|
|
|
first = runtime·atomicloadp(&runtime·allm);
|
|
|
|
n = 0;
|
|
|
|
for(m=first; m; m=m->alllink)
|
|
|
|
n++;
|
|
|
|
ok = false;
|
|
|
|
if(n <= p.len) {
|
|
|
|
ok = true;
|
|
|
|
r = (TRecord*)p.array;
|
|
|
|
for(m=first; m; m=m->alllink) {
|
|
|
|
runtime·memmove(r->stk, m->createstack, sizeof r->stk);
|
|
|
|
r++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-22 19:45:01 -07:00
|
|
|
|
2012-09-24 12:58:34 -06:00
|
|
|
func Stack(b Slice, all bool) (n int) {
|
2012-02-22 19:45:01 -07:00
|
|
|
byte *pc, *sp;
|
|
|
|
|
|
|
|
sp = runtime·getcallersp(&b);
|
|
|
|
pc = runtime·getcallerpc(&b);
|
|
|
|
|
|
|
|
if(all) {
|
|
|
|
runtime·semacquire(&runtime·worldsema);
|
|
|
|
m->gcing = 1;
|
|
|
|
runtime·stoptheworld();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(b.len == 0)
|
|
|
|
n = 0;
|
|
|
|
else{
|
|
|
|
g->writebuf = (byte*)b.array;
|
|
|
|
g->writenbuf = b.len;
|
|
|
|
runtime·goroutineheader(g);
|
|
|
|
runtime·traceback(pc, sp, 0, g);
|
|
|
|
if(all)
|
|
|
|
runtime·tracebackothers(g);
|
|
|
|
n = b.len - g->writenbuf;
|
|
|
|
g->writebuf = nil;
|
|
|
|
g->writenbuf = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(all) {
|
|
|
|
m->gcing = 0;
|
|
|
|
runtime·semrelease(&runtime·worldsema);
|
2012-05-15 09:10:16 -06:00
|
|
|
runtime·starttheworld();
|
2012-02-22 19:45:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
saveg(byte *pc, byte *sp, G *g, TRecord *r)
|
|
|
|
{
|
|
|
|
int32 n;
|
|
|
|
|
|
|
|
n = runtime·gentraceback(pc, sp, 0, g, 0, r->stk, nelem(r->stk));
|
|
|
|
if(n < nelem(r->stk))
|
|
|
|
r->stk[n] = 0;
|
|
|
|
}
|
|
|
|
|
2012-09-24 12:58:34 -06:00
|
|
|
func GoroutineProfile(b Slice) (n int, ok bool) {
|
2012-02-22 19:45:01 -07:00
|
|
|
byte *pc, *sp;
|
|
|
|
TRecord *r;
|
|
|
|
G *gp;
|
|
|
|
|
|
|
|
sp = runtime·getcallersp(&b);
|
|
|
|
pc = runtime·getcallerpc(&b);
|
|
|
|
|
|
|
|
ok = false;
|
|
|
|
n = runtime·gcount();
|
|
|
|
if(n <= b.len) {
|
|
|
|
runtime·semacquire(&runtime·worldsema);
|
|
|
|
m->gcing = 1;
|
|
|
|
runtime·stoptheworld();
|
|
|
|
|
|
|
|
n = runtime·gcount();
|
|
|
|
if(n <= b.len) {
|
|
|
|
ok = true;
|
|
|
|
r = (TRecord*)b.array;
|
|
|
|
saveg(pc, sp, g, r++);
|
|
|
|
for(gp = runtime·allg; gp != nil; gp = gp->alllink) {
|
|
|
|
if(gp == g || gp->status == Gdead)
|
|
|
|
continue;
|
2012-05-30 11:07:52 -06:00
|
|
|
saveg(gp->sched.pc, (byte*)gp->sched.sp, gp, r++);
|
2012-02-22 19:45:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m->gcing = 0;
|
|
|
|
runtime·semrelease(&runtime·worldsema);
|
2012-05-15 09:10:16 -06:00
|
|
|
runtime·starttheworld();
|
2012-02-22 19:45:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|