Source file src/runtime/mprof.go
1
2
3
4
5
6
7
8 package runtime
9
10 import (
11 "runtime/internal/atomic"
12 "unsafe"
13 )
14
15
16 var proflock mutex
17
18
19
20
21 const (
22
23 memProfile bucketType = 1 + iota
24 blockProfile
25 mutexProfile
26
27
28 buckHashSize = 179999
29
30
31 maxStack = 32
32 )
33
34 type bucketType int
35
36
37
38
39
40
41
42
43
44
45
46
47
48 type bucket struct {
49 next *bucket
50 allnext *bucket
51 typ bucketType
52 hash uintptr
53 size uintptr
54 nstk uintptr
55 }
56
57
58
59 type memRecord struct {
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 active memRecordCycle
105
106
107
108
109
110
111
112
113
114
115
116 future [3]memRecordCycle
117 }
118
119
120 type memRecordCycle struct {
121 allocs, frees uintptr
122 alloc_bytes, free_bytes uintptr
123 }
124
125
126 func (a *memRecordCycle) add(b *memRecordCycle) {
127 a.allocs += b.allocs
128 a.frees += b.frees
129 a.alloc_bytes += b.alloc_bytes
130 a.free_bytes += b.free_bytes
131 }
132
133
134
135 type blockRecord struct {
136 count int64
137 cycles int64
138 }
139
140 var (
141 mbuckets *bucket
142 bbuckets *bucket
143 xbuckets *bucket
144 buckhash *[179999]*bucket
145 bucketmem uintptr
146
147 mProf struct {
148
149
150
151
152 cycle uint32
153
154
155 flushed bool
156 }
157 )
158
159 const mProfCycleWrap = uint32(len(memRecord{}.future)) * (2 << 24)
160
161
162 func newBucket(typ bucketType, nstk int) *bucket {
163 size := unsafe.Sizeof(bucket{}) + uintptr(nstk)*unsafe.Sizeof(uintptr(0))
164 switch typ {
165 default:
166 throw("invalid profile bucket type")
167 case memProfile:
168 size += unsafe.Sizeof(memRecord{})
169 case blockProfile, mutexProfile:
170 size += unsafe.Sizeof(blockRecord{})
171 }
172
173 b := (*bucket)(persistentalloc(size, 0, &memstats.buckhash_sys))
174 bucketmem += size
175 b.typ = typ
176 b.nstk = uintptr(nstk)
177 return b
178 }
179
180
181 func (b *bucket) stk() []uintptr {
182 stk := (*[maxStack]uintptr)(add(unsafe.Pointer(b), unsafe.Sizeof(*b)))
183 return stk[:b.nstk:b.nstk]
184 }
185
186
187 func (b *bucket) mp() *memRecord {
188 if b.typ != memProfile {
189 throw("bad use of bucket.mp")
190 }
191 data := add(unsafe.Pointer(b), unsafe.Sizeof(*b)+b.nstk*unsafe.Sizeof(uintptr(0)))
192 return (*memRecord)(data)
193 }
194
195
196 func (b *bucket) bp() *blockRecord {
197 if b.typ != blockProfile && b.typ != mutexProfile {
198 throw("bad use of bucket.bp")
199 }
200 data := add(unsafe.Pointer(b), unsafe.Sizeof(*b)+b.nstk*unsafe.Sizeof(uintptr(0)))
201 return (*blockRecord)(data)
202 }
203
204
205 func stkbucket(typ bucketType, size uintptr, stk []uintptr, alloc bool) *bucket {
206 if buckhash == nil {
207 buckhash = (*[buckHashSize]*bucket)(sysAlloc(unsafe.Sizeof(*buckhash), &memstats.buckhash_sys))
208 if buckhash == nil {
209 throw("runtime: cannot allocate memory")
210 }
211 }
212
213
214 var h uintptr
215 for _, pc := range stk {
216 h += pc
217 h += h << 10
218 h ^= h >> 6
219 }
220
221 h += size
222 h += h << 10
223 h ^= h >> 6
224
225 h += h << 3
226 h ^= h >> 11
227
228 i := int(h % buckHashSize)
229 for b := buckhash[i]; b != nil; b = b.next {
230 if b.typ == typ && b.hash == h && b.size == size && eqslice(b.stk(), stk) {
231 return b
232 }
233 }
234
235 if !alloc {
236 return nil
237 }
238
239
240 b := newBucket(typ, len(stk))
241 copy(b.stk(), stk)
242 b.hash = h
243 b.size = size
244 b.next = buckhash[i]
245 buckhash[i] = b
246 if typ == memProfile {
247 b.allnext = mbuckets
248 mbuckets = b
249 } else if typ == mutexProfile {
250 b.allnext = xbuckets
251 xbuckets = b
252 } else {
253 b.allnext = bbuckets
254 bbuckets = b
255 }
256 return b
257 }
258
259 func eqslice(x, y []uintptr) bool {
260 if len(x) != len(y) {
261 return false
262 }
263 for i, xi := range x {
264 if xi != y[i] {
265 return false
266 }
267 }
268 return true
269 }
270
271
272
273
274
275
276
277
278
279 func mProf_NextCycle() {
280 lock(&proflock)
281
282
283
284 mProf.cycle = (mProf.cycle + 1) % mProfCycleWrap
285 mProf.flushed = false
286 unlock(&proflock)
287 }
288
289
290
291
292
293
294
295
296 func mProf_Flush() {
297 lock(&proflock)
298 if !mProf.flushed {
299 mProf_FlushLocked()
300 mProf.flushed = true
301 }
302 unlock(&proflock)
303 }
304
305 func mProf_FlushLocked() {
306 c := mProf.cycle
307 for b := mbuckets; b != nil; b = b.allnext {
308 mp := b.mp()
309
310
311
312 mpc := &mp.future[c%uint32(len(mp.future))]
313 mp.active.add(mpc)
314 *mpc = memRecordCycle{}
315 }
316 }
317
318
319
320
321
322 func mProf_PostSweep() {
323 lock(&proflock)
324
325
326
327
328
329 c := mProf.cycle
330 for b := mbuckets; b != nil; b = b.allnext {
331 mp := b.mp()
332 mpc := &mp.future[(c+1)%uint32(len(mp.future))]
333 mp.active.add(mpc)
334 *mpc = memRecordCycle{}
335 }
336 unlock(&proflock)
337 }
338
339
340 func mProf_Malloc(p unsafe.Pointer, size uintptr) {
341 var stk [maxStack]uintptr
342 nstk := callers(4, stk[:])
343 lock(&proflock)
344 b := stkbucket(memProfile, size, stk[:nstk], true)
345 c := mProf.cycle
346 mp := b.mp()
347 mpc := &mp.future[(c+2)%uint32(len(mp.future))]
348 mpc.allocs++
349 mpc.alloc_bytes += size
350 unlock(&proflock)
351
352
353
354
355
356 systemstack(func() {
357 setprofilebucket(p, b)
358 })
359 }
360
361
362 func mProf_Free(b *bucket, size uintptr) {
363 lock(&proflock)
364 c := mProf.cycle
365 mp := b.mp()
366 mpc := &mp.future[(c+1)%uint32(len(mp.future))]
367 mpc.frees++
368 mpc.free_bytes += size
369 unlock(&proflock)
370 }
371
372 var blockprofilerate uint64
373
374
375
376
377
378
379
380 func SetBlockProfileRate(rate int) {
381 var r int64
382 if rate <= 0 {
383 r = 0
384 } else if rate == 1 {
385 r = 1
386 } else {
387
388 r = int64(float64(rate) * float64(tickspersecond()) / (1000 * 1000 * 1000))
389 if r == 0 {
390 r = 1
391 }
392 }
393
394 atomic.Store64(&blockprofilerate, uint64(r))
395 }
396
397 func blockevent(cycles int64, skip int) {
398 if cycles <= 0 {
399 cycles = 1
400 }
401 if blocksampled(cycles) {
402 saveblockevent(cycles, skip+1, blockProfile)
403 }
404 }
405
406 func blocksampled(cycles int64) bool {
407 rate := int64(atomic.Load64(&blockprofilerate))
408 if rate <= 0 || (rate > cycles && int64(fastrand())%rate > cycles) {
409 return false
410 }
411 return true
412 }
413
414 func saveblockevent(cycles int64, skip int, which bucketType) {
415 gp := getg()
416 var nstk int
417 var stk [maxStack]uintptr
418 if gp.m.curg == nil || gp.m.curg == gp {
419 nstk = callers(skip, stk[:])
420 } else {
421 nstk = gcallers(gp.m.curg, skip, stk[:])
422 }
423 lock(&proflock)
424 b := stkbucket(which, 0, stk[:nstk], true)
425 b.bp().count++
426 b.bp().cycles += cycles
427 unlock(&proflock)
428 }
429
430 var mutexprofilerate uint64
431
432
433
434
435
436
437
438
439 func SetMutexProfileFraction(rate int) int {
440 if rate < 0 {
441 return int(mutexprofilerate)
442 }
443 old := mutexprofilerate
444 atomic.Store64(&mutexprofilerate, uint64(rate))
445 return int(old)
446 }
447
448
449 func mutexevent(cycles int64, skip int) {
450 if cycles < 0 {
451 cycles = 0
452 }
453 rate := int64(atomic.Load64(&mutexprofilerate))
454
455
456 if rate > 0 && int64(fastrand())%rate == 0 {
457 saveblockevent(cycles, skip+1, mutexProfile)
458 }
459 }
460
461
462
463
464 type StackRecord struct {
465 Stack0 [32]uintptr
466 }
467
468
469
470 func (r *StackRecord) Stack() []uintptr {
471 for i, v := range r.Stack0 {
472 if v == 0 {
473 return r.Stack0[0:i]
474 }
475 }
476 return r.Stack0[0:]
477 }
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493 var MemProfileRate int = 512 * 1024
494
495
496
497 type MemProfileRecord struct {
498 AllocBytes, FreeBytes int64
499 AllocObjects, FreeObjects int64
500 Stack0 [32]uintptr
501 }
502
503
504 func (r *MemProfileRecord) InUseBytes() int64 { return r.AllocBytes - r.FreeBytes }
505
506
507 func (r *MemProfileRecord) InUseObjects() int64 {
508 return r.AllocObjects - r.FreeObjects
509 }
510
511
512
513 func (r *MemProfileRecord) Stack() []uintptr {
514 for i, v := range r.Stack0 {
515 if v == 0 {
516 return r.Stack0[0:i]
517 }
518 }
519 return r.Stack0[0:]
520 }
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543 func MemProfile(p []MemProfileRecord, inuseZero bool) (n int, ok bool) {
544 lock(&proflock)
545
546
547
548 mProf_FlushLocked()
549 clear := true
550 for b := mbuckets; b != nil; b = b.allnext {
551 mp := b.mp()
552 if inuseZero || mp.active.alloc_bytes != mp.active.free_bytes {
553 n++
554 }
555 if mp.active.allocs != 0 || mp.active.frees != 0 {
556 clear = false
557 }
558 }
559 if clear {
560
561
562
563
564 n = 0
565 for b := mbuckets; b != nil; b = b.allnext {
566 mp := b.mp()
567 for c := range mp.future {
568 mp.active.add(&mp.future[c])
569 mp.future[c] = memRecordCycle{}
570 }
571 if inuseZero || mp.active.alloc_bytes != mp.active.free_bytes {
572 n++
573 }
574 }
575 }
576 if n <= len(p) {
577 ok = true
578 idx := 0
579 for b := mbuckets; b != nil; b = b.allnext {
580 mp := b.mp()
581 if inuseZero || mp.active.alloc_bytes != mp.active.free_bytes {
582 record(&p[idx], b)
583 idx++
584 }
585 }
586 }
587 unlock(&proflock)
588 return
589 }
590
591
592 func record(r *MemProfileRecord, b *bucket) {
593 mp := b.mp()
594 r.AllocBytes = int64(mp.active.alloc_bytes)
595 r.FreeBytes = int64(mp.active.free_bytes)
596 r.AllocObjects = int64(mp.active.allocs)
597 r.FreeObjects = int64(mp.active.frees)
598 if raceenabled {
599 racewriterangepc(unsafe.Pointer(&r.Stack0[0]), unsafe.Sizeof(r.Stack0), getcallerpc(), funcPC(MemProfile))
600 }
601 if msanenabled {
602 msanwrite(unsafe.Pointer(&r.Stack0[0]), unsafe.Sizeof(r.Stack0))
603 }
604 copy(r.Stack0[:], b.stk())
605 for i := int(b.nstk); i < len(r.Stack0); i++ {
606 r.Stack0[i] = 0
607 }
608 }
609
610 func iterate_memprof(fn func(*bucket, uintptr, *uintptr, uintptr, uintptr, uintptr)) {
611 lock(&proflock)
612 for b := mbuckets; b != nil; b = b.allnext {
613 mp := b.mp()
614 fn(b, b.nstk, &b.stk()[0], b.size, mp.active.allocs, mp.active.frees)
615 }
616 unlock(&proflock)
617 }
618
619
620
621 type BlockProfileRecord struct {
622 Count int64
623 Cycles int64
624 StackRecord
625 }
626
627
628
629
630
631
632
633
634 func BlockProfile(p []BlockProfileRecord) (n int, ok bool) {
635 lock(&proflock)
636 for b := bbuckets; b != nil; b = b.allnext {
637 n++
638 }
639 if n <= len(p) {
640 ok = true
641 for b := bbuckets; b != nil; b = b.allnext {
642 bp := b.bp()
643 r := &p[0]
644 r.Count = bp.count
645 r.Cycles = bp.cycles
646 if raceenabled {
647 racewriterangepc(unsafe.Pointer(&r.Stack0[0]), unsafe.Sizeof(r.Stack0), getcallerpc(), funcPC(BlockProfile))
648 }
649 if msanenabled {
650 msanwrite(unsafe.Pointer(&r.Stack0[0]), unsafe.Sizeof(r.Stack0))
651 }
652 i := copy(r.Stack0[:], b.stk())
653 for ; i < len(r.Stack0); i++ {
654 r.Stack0[i] = 0
655 }
656 p = p[1:]
657 }
658 }
659 unlock(&proflock)
660 return
661 }
662
663
664
665
666
667
668
669 func MutexProfile(p []BlockProfileRecord) (n int, ok bool) {
670 lock(&proflock)
671 for b := xbuckets; b != nil; b = b.allnext {
672 n++
673 }
674 if n <= len(p) {
675 ok = true
676 for b := xbuckets; b != nil; b = b.allnext {
677 bp := b.bp()
678 r := &p[0]
679 r.Count = int64(bp.count)
680 r.Cycles = bp.cycles
681 i := copy(r.Stack0[:], b.stk())
682 for ; i < len(r.Stack0); i++ {
683 r.Stack0[i] = 0
684 }
685 p = p[1:]
686 }
687 }
688 unlock(&proflock)
689 return
690 }
691
692
693
694
695
696
697
698 func ThreadCreateProfile(p []StackRecord) (n int, ok bool) {
699 first := (*m)(atomic.Loadp(unsafe.Pointer(&allm)))
700 for mp := first; mp != nil; mp = mp.alllink {
701 n++
702 }
703 if n <= len(p) {
704 ok = true
705 i := 0
706 for mp := first; mp != nil; mp = mp.alllink {
707 p[i].Stack0 = mp.createstack
708 i++
709 }
710 }
711 return
712 }
713
714
715
716
717
718
719
720 func GoroutineProfile(p []StackRecord) (n int, ok bool) {
721 gp := getg()
722
723 isOK := func(gp1 *g) bool {
724
725
726 return gp1 != gp && readgstatus(gp1) != _Gdead && !isSystemGoroutine(gp1, false)
727 }
728
729 stopTheWorld("profile")
730
731 n = 1
732 for _, gp1 := range allgs {
733 if isOK(gp1) {
734 n++
735 }
736 }
737
738 if n <= len(p) {
739 ok = true
740 r := p
741
742
743 sp := getcallersp()
744 pc := getcallerpc()
745 systemstack(func() {
746 saveg(pc, sp, gp, &r[0])
747 })
748 r = r[1:]
749
750
751 for _, gp1 := range allgs {
752 if isOK(gp1) {
753 if len(r) == 0 {
754
755
756 break
757 }
758 saveg(^uintptr(0), ^uintptr(0), gp1, &r[0])
759 r = r[1:]
760 }
761 }
762 }
763
764 startTheWorld()
765
766 return n, ok
767 }
768
769 func saveg(pc, sp uintptr, gp *g, r *StackRecord) {
770 n := gentraceback(pc, sp, 0, gp, 0, &r.Stack0[0], len(r.Stack0), nil, nil, 0)
771 if n < len(r.Stack0) {
772 r.Stack0[n] = 0
773 }
774 }
775
776
777
778
779
780 func Stack(buf []byte, all bool) int {
781 if all {
782 stopTheWorld("stack trace")
783 }
784
785 n := 0
786 if len(buf) > 0 {
787 gp := getg()
788 sp := getcallersp()
789 pc := getcallerpc()
790 systemstack(func() {
791 g0 := getg()
792
793
794
795 g0.m.traceback = 1
796 g0.writebuf = buf[0:0:len(buf)]
797 goroutineheader(gp)
798 traceback(pc, sp, 0, gp)
799 if all {
800 tracebackothers(gp)
801 }
802 g0.m.traceback = 0
803 n = len(g0.writebuf)
804 g0.writebuf = nil
805 })
806 }
807
808 if all {
809 startTheWorld()
810 }
811 return n
812 }
813
814
815
816 var tracelock mutex
817
818 func tracealloc(p unsafe.Pointer, size uintptr, typ *_type) {
819 lock(&tracelock)
820 gp := getg()
821 gp.m.traceback = 2
822 if typ == nil {
823 print("tracealloc(", p, ", ", hex(size), ")\n")
824 } else {
825 print("tracealloc(", p, ", ", hex(size), ", ", typ.string(), ")\n")
826 }
827 if gp.m.curg == nil || gp == gp.m.curg {
828 goroutineheader(gp)
829 pc := getcallerpc()
830 sp := getcallersp()
831 systemstack(func() {
832 traceback(pc, sp, 0, gp)
833 })
834 } else {
835 goroutineheader(gp.m.curg)
836 traceback(^uintptr(0), ^uintptr(0), 0, gp.m.curg)
837 }
838 print("\n")
839 gp.m.traceback = 0
840 unlock(&tracelock)
841 }
842
843 func tracefree(p unsafe.Pointer, size uintptr) {
844 lock(&tracelock)
845 gp := getg()
846 gp.m.traceback = 2
847 print("tracefree(", p, ", ", hex(size), ")\n")
848 goroutineheader(gp)
849 pc := getcallerpc()
850 sp := getcallersp()
851 systemstack(func() {
852 traceback(pc, sp, 0, gp)
853 })
854 print("\n")
855 gp.m.traceback = 0
856 unlock(&tracelock)
857 }
858
859 func tracegc() {
860 lock(&tracelock)
861 gp := getg()
862 gp.m.traceback = 2
863 print("tracegc()\n")
864
865 tracebackothers(gp)
866 print("end tracegc\n")
867 print("\n")
868 gp.m.traceback = 0
869 unlock(&tracelock)
870 }
871
View as plain text