Source file
src/runtime/traceallocfree.go
1
2
3
4
5
6
7 package runtime
8
9 import (
10 "internal/abi"
11 "internal/runtime/sys"
12 "internal/trace/tracev2"
13 )
14
15
16 const (
17 traceAllocFreeTypesBatch = iota
18 traceAllocFreeInfoBatch
19 )
20
21
22
23
24
25 func traceSnapshotMemory(gen uintptr) {
26 assertWorldStopped()
27
28
29
30 var flushed bool
31 w := unsafeTraceExpWriter(gen, nil, tracev2.AllocFree)
32 w, flushed = w.ensure(1 + 4*traceBytesPerNumber)
33 if flushed {
34
35 w.byte(byte(traceAllocFreeInfoBatch))
36 }
37
38
39 w.varint(uint64(trace.minPageHeapAddr))
40 w.varint(uint64(pageSize))
41 w.varint(uint64(minHeapAlign))
42 w.varint(uint64(fixedStack))
43
44
45 w.flush().end()
46
47
48 trace := traceAcquire()
49 if !trace.ok() {
50 throw("traceSnapshotMemory: tracing is not enabled")
51 }
52
53
54 for _, s := range mheap_.allspans {
55 if s.state.get() == mSpanDead {
56 continue
57 }
58
59 trace.SpanExists(s)
60
61
62 if s.state.get() != mSpanInUse {
63 continue
64 }
65
66
67 abits := s.allocBitsForIndex(0)
68 for i := uintptr(0); i < uintptr(s.nelems); i++ {
69 if abits.index < uintptr(s.freeindex) || abits.isMarked() {
70 x := s.base() + i*s.elemsize
71 trace.HeapObjectExists(x, s.typePointersOfUnchecked(x).typ)
72 }
73 abits.advance()
74 }
75 }
76
77
78 forEachGRace(func(gp *g) {
79 trace.GoroutineStackExists(gp.stack.lo, gp.stack.hi-gp.stack.lo)
80 })
81 traceRelease(trace)
82 }
83
84 func traceSpanTypeAndClass(s *mspan) traceArg {
85 if s.state.get() == mSpanInUse {
86 return traceArg(s.spanclass) << 1
87 }
88 return traceArg(1)
89 }
90
91
92 func (tl traceLocker) SpanExists(s *mspan) {
93 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvSpan, traceSpanID(s), traceArg(s.npages), traceSpanTypeAndClass(s))
94 }
95
96
97 func (tl traceLocker) SpanAlloc(s *mspan) {
98 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvSpanAlloc, traceSpanID(s), traceArg(s.npages), traceSpanTypeAndClass(s))
99 }
100
101
102 func (tl traceLocker) SpanFree(s *mspan) {
103 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvSpanFree, traceSpanID(s))
104 }
105
106
107 func traceSpanID(s *mspan) traceArg {
108 return traceArg(uint64(s.base())-trace.minPageHeapAddr) / pageSize
109 }
110
111
112
113
114 func (tl traceLocker) HeapObjectExists(addr uintptr, typ *abi.Type) {
115 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvHeapObject, traceHeapObjectID(addr), tl.rtype(typ))
116 }
117
118
119
120
121 func (tl traceLocker) HeapObjectAlloc(addr uintptr, typ *abi.Type) {
122 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvHeapObjectAlloc, traceHeapObjectID(addr), tl.rtype(typ))
123 }
124
125
126 func (tl traceLocker) HeapObjectFree(addr uintptr) {
127 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvHeapObjectFree, traceHeapObjectID(addr))
128 }
129
130
131 func traceHeapObjectID(addr uintptr) traceArg {
132 return traceArg(uint64(addr)-trace.minPageHeapAddr) / minHeapAlign
133 }
134
135
136 func (tl traceLocker) GoroutineStackExists(base, size uintptr) {
137 order := traceCompressStackSize(size)
138 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvGoroutineStack, traceGoroutineStackID(base), order)
139 }
140
141
142 func (tl traceLocker) GoroutineStackAlloc(base, size uintptr) {
143 order := traceCompressStackSize(size)
144 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvGoroutineStackAlloc, traceGoroutineStackID(base), order)
145 }
146
147
148 func (tl traceLocker) GoroutineStackFree(base uintptr) {
149 tl.eventWriter(tracev2.GoRunning, tracev2.ProcRunning).event(tracev2.EvGoroutineStackFree, traceGoroutineStackID(base))
150 }
151
152
153 func traceGoroutineStackID(base uintptr) traceArg {
154 return traceArg(uint64(base)-trace.minPageHeapAddr) / fixedStack
155 }
156
157
158 func traceCompressStackSize(size uintptr) traceArg {
159 if size&(size-1) != 0 {
160 throw("goroutine stack size is not a power of 2")
161 }
162 return traceArg(sys.Len64(uint64(size)))
163 }
164
View as plain text