2015-05-29 François Dumont fdumont@gcc.gnu.org>
[official-gcc.git] / libgo / go / runtime / mprof.go
blobf4da45f5c307d51894645e6c4623c412b5f04167
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.
5 // Malloc profiling.
6 // Patterned after tcmalloc's algorithms; shorter code.
8 package runtime
10 import (
11 "unsafe"
14 // NOTE(rsc): Everything here could use cas if contention became an issue.
15 var proflock mutex
17 // All memory allocations are local and do not escape outside of the profiler.
18 // The profiler is forbidden from referring to garbage-collected memory.
20 const (
21 // profile types
22 memProfile bucketType = 1 + iota
23 blockProfile
25 // size of bucket hash table
26 buckHashSize = 179999
28 // max depth of stack to record in bucket
29 maxStack = 32
32 type bucketType int
34 // A bucket holds per-call-stack profiling information.
35 // The representation is a bit sleazy, inherited from C.
36 // This struct defines the bucket header. It is followed in
37 // memory by the stack words and then the actual record
38 // data, either a memRecord or a blockRecord.
40 // Per-call-stack profiling information.
41 // Lookup by hashing call stack into a linked-list hash table.
42 type bucket struct {
43 next *bucket
44 allnext *bucket
45 typ bucketType // memBucket or blockBucket
46 hash uintptr
47 size uintptr
48 nstk uintptr
51 // A memRecord is the bucket data for a bucket of type memProfile,
52 // part of the memory profile.
53 type memRecord struct {
54 // The following complex 3-stage scheme of stats accumulation
55 // is required to obtain a consistent picture of mallocs and frees
56 // for some point in time.
57 // The problem is that mallocs come in real time, while frees
58 // come only after a GC during concurrent sweeping. So if we would
59 // naively count them, we would get a skew toward mallocs.
61 // Mallocs are accounted in recent stats.
62 // Explicit frees are accounted in recent stats.
63 // GC frees are accounted in prev stats.
64 // After GC prev stats are added to final stats and
65 // recent stats are moved into prev stats.
66 allocs uintptr
67 frees uintptr
68 alloc_bytes uintptr
69 free_bytes uintptr
71 // changes between next-to-last GC and last GC
72 prev_allocs uintptr
73 prev_frees uintptr
74 prev_alloc_bytes uintptr
75 prev_free_bytes uintptr
77 // changes since last GC
78 recent_allocs uintptr
79 recent_frees uintptr
80 recent_alloc_bytes uintptr
81 recent_free_bytes uintptr
84 // A blockRecord is the bucket data for a bucket of type blockProfile,
85 // part of the blocking profile.
86 type blockRecord struct {
87 count int64
88 cycles int64
91 var (
92 mbuckets *bucket // memory profile buckets
93 bbuckets *bucket // blocking profile buckets
94 buckhash *[179999]*bucket
95 bucketmem uintptr
98 // newBucket allocates a bucket with the given type and number of stack entries.
99 func newBucket(typ bucketType, nstk int) *bucket {
100 size := unsafe.Sizeof(bucket{}) + uintptr(nstk)*unsafe.Sizeof(uintptr(0))
101 switch typ {
102 default:
103 gothrow("invalid profile bucket type")
104 case memProfile:
105 size += unsafe.Sizeof(memRecord{})
106 case blockProfile:
107 size += unsafe.Sizeof(blockRecord{})
110 b := (*bucket)(persistentalloc(size, 0, &memstats.buckhash_sys))
111 bucketmem += size
112 b.typ = typ
113 b.nstk = uintptr(nstk)
114 return b
117 // stk returns the slice in b holding the stack.
118 func (b *bucket) stk() []uintptr {
119 stk := (*[maxStack]uintptr)(add(unsafe.Pointer(b), unsafe.Sizeof(*b)))
120 return stk[:b.nstk:b.nstk]
123 // mp returns the memRecord associated with the memProfile bucket b.
124 func (b *bucket) mp() *memRecord {
125 if b.typ != memProfile {
126 gothrow("bad use of bucket.mp")
128 data := add(unsafe.Pointer(b), unsafe.Sizeof(*b)+b.nstk*unsafe.Sizeof(uintptr(0)))
129 return (*memRecord)(data)
132 // bp returns the blockRecord associated with the blockProfile bucket b.
133 func (b *bucket) bp() *blockRecord {
134 if b.typ != blockProfile {
135 gothrow("bad use of bucket.bp")
137 data := add(unsafe.Pointer(b), unsafe.Sizeof(*b)+b.nstk*unsafe.Sizeof(uintptr(0)))
138 return (*blockRecord)(data)
141 // Return the bucket for stk[0:nstk], allocating new bucket if needed.
142 func stkbucket(typ bucketType, size uintptr, stk []uintptr, alloc bool) *bucket {
143 if buckhash == nil {
144 buckhash = (*[buckHashSize]*bucket)(sysAlloc(unsafe.Sizeof(*buckhash), &memstats.buckhash_sys))
145 if buckhash == nil {
146 gothrow("runtime: cannot allocate memory")
150 // Hash stack.
151 var h uintptr
152 for _, pc := range stk {
153 h += pc
154 h += h << 10
155 h ^= h >> 6
157 // hash in size
158 h += size
159 h += h << 10
160 h ^= h >> 6
161 // finalize
162 h += h << 3
163 h ^= h >> 11
165 i := int(h % buckHashSize)
166 for b := buckhash[i]; b != nil; b = b.next {
167 if b.typ == typ && b.hash == h && b.size == size && eqslice(b.stk(), stk) {
168 return b
172 if !alloc {
173 return nil
176 // Create new bucket.
177 b := newBucket(typ, len(stk))
178 copy(b.stk(), stk)
179 b.hash = h
180 b.size = size
181 b.next = buckhash[i]
182 buckhash[i] = b
183 if typ == memProfile {
184 b.allnext = mbuckets
185 mbuckets = b
186 } else {
187 b.allnext = bbuckets
188 bbuckets = b
190 return b
193 func sysAlloc(n uintptr, stat *uint64) unsafe.Pointer
195 func eqslice(x, y []uintptr) bool {
196 if len(x) != len(y) {
197 return false
199 for i, xi := range x {
200 if xi != y[i] {
201 return false
204 return true
207 func mprof_GC() {
208 for b := mbuckets; b != nil; b = b.allnext {
209 mp := b.mp()
210 mp.allocs += mp.prev_allocs
211 mp.frees += mp.prev_frees
212 mp.alloc_bytes += mp.prev_alloc_bytes
213 mp.free_bytes += mp.prev_free_bytes
215 mp.prev_allocs = mp.recent_allocs
216 mp.prev_frees = mp.recent_frees
217 mp.prev_alloc_bytes = mp.recent_alloc_bytes
218 mp.prev_free_bytes = mp.recent_free_bytes
220 mp.recent_allocs = 0
221 mp.recent_frees = 0
222 mp.recent_alloc_bytes = 0
223 mp.recent_free_bytes = 0
227 // Record that a gc just happened: all the 'recent' statistics are now real.
228 func mProf_GC() {
229 lock(&proflock)
230 mprof_GC()
231 unlock(&proflock)
234 // Called by malloc to record a profiled block.
235 func mProf_Malloc(p unsafe.Pointer, size uintptr) {
236 var stk [maxStack]uintptr
237 nstk := callers(4, &stk[0], len(stk))
238 lock(&proflock)
239 b := stkbucket(memProfile, size, stk[:nstk], true)
240 mp := b.mp()
241 mp.recent_allocs++
242 mp.recent_alloc_bytes += size
243 unlock(&proflock)
245 // Setprofilebucket locks a bunch of other mutexes, so we call it outside of proflock.
246 // This reduces potential contention and chances of deadlocks.
247 // Since the object must be alive during call to mProf_Malloc,
248 // it's fine to do this non-atomically.
249 setprofilebucket(p, b)
252 func setprofilebucket_m() // mheap.c
254 func setprofilebucket(p unsafe.Pointer, b *bucket) {
255 g := getg()
256 g.m.ptrarg[0] = p
257 g.m.ptrarg[1] = unsafe.Pointer(b)
258 onM(setprofilebucket_m)
261 // Called when freeing a profiled block.
262 func mProf_Free(b *bucket, size uintptr, freed bool) {
263 lock(&proflock)
264 mp := b.mp()
265 if freed {
266 mp.recent_frees++
267 mp.recent_free_bytes += size
268 } else {
269 mp.prev_frees++
270 mp.prev_free_bytes += size
272 unlock(&proflock)
275 var blockprofilerate uint64 // in CPU ticks
277 // SetBlockProfileRate controls the fraction of goroutine blocking events
278 // that are reported in the blocking profile. The profiler aims to sample
279 // an average of one blocking event per rate nanoseconds spent blocked.
281 // To include every blocking event in the profile, pass rate = 1.
282 // To turn off profiling entirely, pass rate <= 0.
283 func SetBlockProfileRate(rate int) {
284 var r int64
285 if rate <= 0 {
286 r = 0 // disable profiling
287 } else if rate == 1 {
288 r = 1 // profile everything
289 } else {
290 // convert ns to cycles, use float64 to prevent overflow during multiplication
291 r = int64(float64(rate) * float64(tickspersecond()) / (1000 * 1000 * 1000))
292 if r == 0 {
293 r = 1
297 atomicstore64(&blockprofilerate, uint64(r))
300 func blockevent(cycles int64, skip int) {
301 if cycles <= 0 {
302 cycles = 1
304 rate := int64(atomicload64(&blockprofilerate))
305 if rate <= 0 || (rate > cycles && int64(fastrand1())%rate > cycles) {
306 return
308 gp := getg()
309 var nstk int
310 var stk [maxStack]uintptr
311 if gp.m.curg == nil || gp.m.curg == gp {
312 nstk = callers(skip, &stk[0], len(stk))
313 } else {
314 nstk = gcallers(gp.m.curg, skip, &stk[0], len(stk))
316 lock(&proflock)
317 b := stkbucket(blockProfile, 0, stk[:nstk], true)
318 b.bp().count++
319 b.bp().cycles += cycles
320 unlock(&proflock)
323 // Go interface to profile data.
325 // A StackRecord describes a single execution stack.
326 type StackRecord struct {
327 Stack0 [32]uintptr // stack trace for this record; ends at first 0 entry
330 // Stack returns the stack trace associated with the record,
331 // a prefix of r.Stack0.
332 func (r *StackRecord) Stack() []uintptr {
333 for i, v := range r.Stack0 {
334 if v == 0 {
335 return r.Stack0[0:i]
338 return r.Stack0[0:]
341 // MemProfileRate controls the fraction of memory allocations
342 // that are recorded and reported in the memory profile.
343 // The profiler aims to sample an average of
344 // one allocation per MemProfileRate bytes allocated.
346 // To include every allocated block in the profile, set MemProfileRate to 1.
347 // To turn off profiling entirely, set MemProfileRate to 0.
349 // The tools that process the memory profiles assume that the
350 // profile rate is constant across the lifetime of the program
351 // and equal to the current value. Programs that change the
352 // memory profiling rate should do so just once, as early as
353 // possible in the execution of the program (for example,
354 // at the beginning of main).
355 var MemProfileRate int = 512 * 1024
357 // A MemProfileRecord describes the live objects allocated
358 // by a particular call sequence (stack trace).
359 type MemProfileRecord struct {
360 AllocBytes, FreeBytes int64 // number of bytes allocated, freed
361 AllocObjects, FreeObjects int64 // number of objects allocated, freed
362 Stack0 [32]uintptr // stack trace for this record; ends at first 0 entry
365 // InUseBytes returns the number of bytes in use (AllocBytes - FreeBytes).
366 func (r *MemProfileRecord) InUseBytes() int64 { return r.AllocBytes - r.FreeBytes }
368 // InUseObjects returns the number of objects in use (AllocObjects - FreeObjects).
369 func (r *MemProfileRecord) InUseObjects() int64 {
370 return r.AllocObjects - r.FreeObjects
373 // Stack returns the stack trace associated with the record,
374 // a prefix of r.Stack0.
375 func (r *MemProfileRecord) Stack() []uintptr {
376 for i, v := range r.Stack0 {
377 if v == 0 {
378 return r.Stack0[0:i]
381 return r.Stack0[0:]
384 // MemProfile returns n, the number of records in the current memory profile.
385 // If len(p) >= n, MemProfile copies the profile into p and returns n, true.
386 // If len(p) < n, MemProfile does not change p and returns n, false.
388 // If inuseZero is true, the profile includes allocation records
389 // where r.AllocBytes > 0 but r.AllocBytes == r.FreeBytes.
390 // These are sites where memory was allocated, but it has all
391 // been released back to the runtime.
393 // Most clients should use the runtime/pprof package or
394 // the testing package's -test.memprofile flag instead
395 // of calling MemProfile directly.
396 func MemProfile(p []MemProfileRecord, inuseZero bool) (n int, ok bool) {
397 lock(&proflock)
398 clear := true
399 for b := mbuckets; b != nil; b = b.allnext {
400 mp := b.mp()
401 if inuseZero || mp.alloc_bytes != mp.free_bytes {
404 if mp.allocs != 0 || mp.frees != 0 {
405 clear = false
408 if clear {
409 // Absolutely no data, suggesting that a garbage collection
410 // has not yet happened. In order to allow profiling when
411 // garbage collection is disabled from the beginning of execution,
412 // accumulate stats as if a GC just happened, and recount buckets.
413 mprof_GC()
414 mprof_GC()
415 n = 0
416 for b := mbuckets; b != nil; b = b.allnext {
417 mp := b.mp()
418 if inuseZero || mp.alloc_bytes != mp.free_bytes {
423 if n <= len(p) {
424 ok = true
425 idx := 0
426 for b := mbuckets; b != nil; b = b.allnext {
427 mp := b.mp()
428 if inuseZero || mp.alloc_bytes != mp.free_bytes {
429 record(&p[idx], b)
430 idx++
434 unlock(&proflock)
435 return
438 // Write b's data to r.
439 func record(r *MemProfileRecord, b *bucket) {
440 mp := b.mp()
441 r.AllocBytes = int64(mp.alloc_bytes)
442 r.FreeBytes = int64(mp.free_bytes)
443 r.AllocObjects = int64(mp.allocs)
444 r.FreeObjects = int64(mp.frees)
445 copy(r.Stack0[:], b.stk())
446 for i := int(b.nstk); i < len(r.Stack0); i++ {
447 r.Stack0[i] = 0
451 func iterate_memprof(fn func(*bucket, uintptr, *uintptr, uintptr, uintptr, uintptr)) {
452 lock(&proflock)
453 for b := mbuckets; b != nil; b = b.allnext {
454 mp := b.mp()
455 fn(b, uintptr(b.nstk), &b.stk()[0], b.size, mp.allocs, mp.frees)
457 unlock(&proflock)
460 // BlockProfileRecord describes blocking events originated
461 // at a particular call sequence (stack trace).
462 type BlockProfileRecord struct {
463 Count int64
464 Cycles int64
465 StackRecord
468 // BlockProfile returns n, the number of records in the current blocking profile.
469 // If len(p) >= n, BlockProfile copies the profile into p and returns n, true.
470 // If len(p) < n, BlockProfile does not change p and returns n, false.
472 // Most clients should use the runtime/pprof package or
473 // the testing package's -test.blockprofile flag instead
474 // of calling BlockProfile directly.
475 func BlockProfile(p []BlockProfileRecord) (n int, ok bool) {
476 lock(&proflock)
477 for b := bbuckets; b != nil; b = b.allnext {
480 if n <= len(p) {
481 ok = true
482 for b := bbuckets; b != nil; b = b.allnext {
483 bp := b.bp()
484 r := &p[0]
485 r.Count = int64(bp.count)
486 r.Cycles = int64(bp.cycles)
487 i := copy(r.Stack0[:], b.stk())
488 for ; i < len(r.Stack0); i++ {
489 r.Stack0[i] = 0
491 p = p[1:]
494 unlock(&proflock)
495 return
498 // ThreadCreateProfile returns n, the number of records in the thread creation profile.
499 // If len(p) >= n, ThreadCreateProfile copies the profile into p and returns n, true.
500 // If len(p) < n, ThreadCreateProfile does not change p and returns n, false.
502 // Most clients should use the runtime/pprof package instead
503 // of calling ThreadCreateProfile directly.
504 func ThreadCreateProfile(p []StackRecord) (n int, ok bool) {
505 first := (*m)(atomicloadp(unsafe.Pointer(&allm)))
506 for mp := first; mp != nil; mp = mp.alllink {
509 if n <= len(p) {
510 ok = true
511 i := 0
512 for mp := first; mp != nil; mp = mp.alllink {
513 for s := range mp.createstack {
514 p[i].Stack0[s] = uintptr(mp.createstack[s])
519 return
522 var allgs []*g // proc.c
524 // GoroutineProfile returns n, the number of records in the active goroutine stack profile.
525 // If len(p) >= n, GoroutineProfile copies the profile into p and returns n, true.
526 // If len(p) < n, GoroutineProfile does not change p and returns n, false.
528 // Most clients should use the runtime/pprof package instead
529 // of calling GoroutineProfile directly.
530 func GoroutineProfile(p []StackRecord) (n int, ok bool) {
532 n = NumGoroutine()
533 if n <= len(p) {
534 gp := getg()
535 semacquire(&worldsema, false)
536 gp.m.gcing = 1
537 onM(stoptheworld)
539 n = NumGoroutine()
540 if n <= len(p) {
541 ok = true
542 r := p
543 sp := getcallersp(unsafe.Pointer(&p))
544 pc := getcallerpc(unsafe.Pointer(&p))
545 onM(func() {
546 saveg(pc, sp, gp, &r[0])
548 r = r[1:]
549 for _, gp1 := range allgs {
550 if gp1 == gp || readgstatus(gp1) == _Gdead {
551 continue
553 saveg(^uintptr(0), ^uintptr(0), gp1, &r[0])
554 r = r[1:]
558 gp.m.gcing = 0
559 semrelease(&worldsema)
560 onM(starttheworld)
563 return n, ok
566 func saveg(pc, sp uintptr, gp *g, r *StackRecord) {
567 n := gentraceback(pc, sp, 0, gp, 0, &r.Stack0[0], len(r.Stack0), nil, nil, 0)
568 if n < len(r.Stack0) {
569 r.Stack0[n] = 0
573 // Stack formats a stack trace of the calling goroutine into buf
574 // and returns the number of bytes written to buf.
575 // If all is true, Stack formats stack traces of all other goroutines
576 // into buf after the trace for the current goroutine.
577 func Stack(buf []byte, all bool) int {
578 if all {
579 semacquire(&worldsema, false)
580 gp := getg()
581 gp.m.gcing = 1
582 onM(stoptheworld)
585 n := 0
586 if len(buf) > 0 {
587 gp := getg()
588 sp := getcallersp(unsafe.Pointer(&buf))
589 pc := getcallerpc(unsafe.Pointer(&buf))
590 onM(func() {
591 g0 := getg()
592 g0.writebuf = buf[0:0:len(buf)]
593 goroutineheader(gp)
594 traceback(pc, sp, 0, gp)
595 if all {
596 tracebackothers(gp)
598 n = len(g0.writebuf)
599 g0.writebuf = nil
603 if all {
604 gp := getg()
605 gp.m.gcing = 0
606 semrelease(&worldsema)
607 onM(starttheworld)
609 return n
612 // Tracing of alloc/free/gc.
614 var tracelock mutex
616 func tracealloc(p unsafe.Pointer, size uintptr, typ *_type) {
617 lock(&tracelock)
618 gp := getg()
619 gp.m.traceback = 2
620 if typ == nil {
621 print("tracealloc(", p, ", ", hex(size), ")\n")
622 } else {
623 print("tracealloc(", p, ", ", hex(size), ", ", *typ._string, ")\n")
625 if gp.m.curg == nil || gp == gp.m.curg {
626 goroutineheader(gp)
627 pc := getcallerpc(unsafe.Pointer(&p))
628 sp := getcallersp(unsafe.Pointer(&p))
629 onM(func() {
630 traceback(pc, sp, 0, gp)
632 } else {
633 goroutineheader(gp.m.curg)
634 traceback(^uintptr(0), ^uintptr(0), 0, gp.m.curg)
636 print("\n")
637 gp.m.traceback = 0
638 unlock(&tracelock)
641 func tracefree(p unsafe.Pointer, size uintptr) {
642 lock(&tracelock)
643 gp := getg()
644 gp.m.traceback = 2
645 print("tracefree(", p, ", ", hex(size), ")\n")
646 goroutineheader(gp)
647 pc := getcallerpc(unsafe.Pointer(&p))
648 sp := getcallersp(unsafe.Pointer(&p))
649 onM(func() {
650 traceback(pc, sp, 0, gp)
652 print("\n")
653 gp.m.traceback = 0
654 unlock(&tracelock)
657 func tracegc() {
658 lock(&tracelock)
659 gp := getg()
660 gp.m.traceback = 2
661 print("tracegc()\n")
662 // running on m->g0 stack; show all non-g0 goroutines
663 tracebackothers(gp)
664 print("end tracegc\n")
665 print("\n")
666 gp.m.traceback = 0
667 unlock(&tracelock)