Debian Codegraph @

trace/base.go

raw symbols SCIP
1 // Copyright 2023 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.
4
5 // Code generated by "gen.bash" from internal/trace; DO NOT EDIT.
6
7 //go:build go1.23
8
9 // This file contains data types that all implementations of the trace format
10 // parser need to provide to the rest of the package.
11
12 package trace
13
14 import (
15 "fmt"
16 "math"
17 "strings"
18
19 "golang.org/x/exp/trace/internal/tracev2"
20 "golang.org/x/exp/trace/internal/version"
21 )
22
23 // timedEventArgs is an array that is able to hold the arguments for any
24 // timed event.
25 type timedEventArgs [tracev2.MaxTimedEventArgs - 1]uint64
26
27 // baseEvent is the basic unprocessed event. This serves as a common
28 // fundamental data structure across.
29 type baseEvent struct {
30 typ tracev2.EventType
31 time Time
32 args timedEventArgs
33 }
34
35 // extra returns a slice representing extra available space in args
36 // that the parser can use to pass data up into Event.
37 func (e *baseEvent) extra(v version.Version) []uint64 {
38 switch v {
39 case version.Go122:
40 return e.args[len(tracev2.Specs()[e.typ].Args)-1:]
41 }
42 panic(fmt.Sprintf("unsupported version: go 1.%d", v))
43 }
44
45 // evTable contains the per-generation data necessary to
46 // interpret an individual event.
47 type evTable struct {
48 sync
49 strings dataTable[stringID, string]
50 stacks dataTable[stackID, stack]
51 pcs map[uint64]frame
52
53 // extraStrings are strings that get generated during
54 // parsing but haven't come directly from the trace, so
55 // they don't appear in strings.
56 extraStrings []string
57 extraStringIDs map[string]extraStringID
58 nextExtra extraStringID
59
60 // expBatches contains extra unparsed data relevant to a specific experiment.
61 expBatches map[tracev2.Experiment][]ExperimentalBatch
62 }
63
64 // addExtraString adds an extra string to the evTable and returns
65 // a unique ID for the string in the table.
66 func (t *evTable) addExtraString(s string) extraStringID {
67 if s == "" {
68 return 0
69 }
70 if t.extraStringIDs == nil {
71 t.extraStringIDs = make(map[string]extraStringID)
72 }
73 if id, ok := t.extraStringIDs[s]; ok {
74 return id
75 }
76 t.nextExtra++
77 id := t.nextExtra
78 t.extraStrings = append(t.extraStrings, s)
79 t.extraStringIDs[s] = id
80 return id
81 }
82
83 // getExtraString returns the extra string for the provided ID.
84 // The ID must have been produced by addExtraString for this evTable.
85 func (t *evTable) getExtraString(id extraStringID) string {
86 if id == 0 {
87 return ""
88 }
89 return t.extraStrings[id-1]
90 }
91
92 // dataTable is a mapping from EIs to Es.
93 type dataTable[EI ~uint64, E any] struct {
94 present []uint8
95 dense []E
96 sparse map[EI]E
97 }
98
99 // insert tries to add a mapping from id to s.
100 //
101 // Returns an error if a mapping for id already exists, regardless
102 // of whether or not s is the same in content. This should be used
103 // for validation during parsing.
104 func (d *dataTable[EI, E]) insert(id EI, data E) error {
105 if d.sparse == nil {
106 d.sparse = make(map[EI]E)
107 }
108 if existing, ok := d.get(id); ok {
109 return fmt.Errorf("multiple %Ts with the same ID: id=%d, new=%v, existing=%v", data, id, data, existing)
110 }
111 d.sparse[id] = data
112 return nil
113 }
114
115 // append adds a new element to the data table and returns its ID.
116 func (d *dataTable[EI, E]) append(data E) EI {
117 if d.sparse == nil {
118 d.sparse = make(map[EI]E)
119 }
120 id := EI(len(d.sparse)) + 1
121 d.sparse[id] = data
122 return id
123 }
124
125 // compactify attempts to compact sparse into dense.
126 //
127 // This is intended to be called only once after insertions are done.
128 func (d *dataTable[EI, E]) compactify() {
129 if d.sparse == nil || len(d.dense) != 0 {
130 // Already compactified.
131 return
132 }
133 // Find the range of IDs.
134 maxID := EI(0)
135 minID := ^EI(0)
136 for id := range d.sparse {
137 if id > maxID {
138 maxID = id
139 }
140 if id < minID {
141 minID = id
142 }
143 }
144 if maxID >= math.MaxInt {
145 // We can't create a slice big enough to hold maxID elements
146 return
147 }
148 // We're willing to waste at most 2x memory.
149 if int(maxID-minID) > max(len(d.sparse), 2*len(d.sparse)) {
150 return
151 }
152 if int(minID) > len(d.sparse) {
153 return
154 }
155 size := int(maxID) + 1
156 d.present = make([]uint8, (size+7)/8)
157 d.dense = make([]E, size)
158 for id, data := range d.sparse {
159 d.dense[id] = data
160 d.present[id/8] |= uint8(1) << (id % 8)
161 }
162 d.sparse = nil
163 }
164
165 // get returns the E for id or false if it doesn't
166 // exist. This should be used for validation during parsing.
167 func (d *dataTable[EI, E]) get(id EI) (E, bool) {
168 if id == 0 {
169 return *new(E), true
170 }
171 if uint64(id) < uint64(len(d.dense)) {
172 if d.present[id/8]&(uint8(1)<<(id%8)) != 0 {
173 return d.dense[id], true
174 }
175 } else if d.sparse != nil {
176 if data, ok := d.sparse[id]; ok {
177 return data, true
178 }
179 }
180 return *new(E), false
181 }
182
183 // forEach iterates over all ID/value pairs in the data table.
184 func (d *dataTable[EI, E]) forEach(yield func(EI, E) bool) bool {
185 for id, value := range d.dense {
186 if d.present[id/8]&(uint8(1)<<(id%8)) == 0 {
187 continue
188 }
189 if !yield(EI(id), value) {
190 return false
191 }
192 }
193 if d.sparse == nil {
194 return true
195 }
196 for id, value := range d.sparse {
197 if !yield(id, value) {
198 return false
199 }
200 }
201 return true
202 }
203
204 // mustGet returns the E for id or panics if it fails.
205 //
206 // This should only be used if id has already been validated.
207 func (d *dataTable[EI, E]) mustGet(id EI) E {
208 data, ok := d.get(id)
209 if !ok {
210 panic(fmt.Sprintf("expected id %d in %T table", id, data))
211 }
212 return data
213 }
214
215 // frequency is nanoseconds per timestamp unit.
216 type frequency float64
217
218 // mul multiplies an unprocessed to produce a time in nanoseconds.
219 func (f frequency) mul(t timestamp) Time {
220 return Time(float64(t) * float64(f))
221 }
222
223 // stringID is an index into the string table for a generation.
224 type stringID uint64
225
226 // extraStringID is an index into the extra string table for a generation.
227 type extraStringID uint64
228
229 // stackID is an index into the stack table for a generation.
230 type stackID uint64
231
232 // cpuSample represents a CPU profiling sample captured by the trace.
233 type cpuSample struct {
234 schedCtx
235 time Time
236 stack stackID
237 }
238
239 // asEvent produces a complete Event from a cpuSample. It needs
240 // the evTable from the generation that created it.
241 //
242 // We don't just store it as an Event in generation to minimize
243 // the amount of pointer data floating around.
244 func (s cpuSample) asEvent(table *evTable) Event {
245 // TODO(mknyszek): This is go122-specific, but shouldn't be.
246 // Generalize this in the future.
247 e := Event{
248 table: table,
249 ctx: s.schedCtx,
250 base: baseEvent{
251 typ: tracev2.EvCPUSample,
252 time: s.time,
253 },
254 }
255 e.base.args[0] = uint64(s.stack)
256 return e
257 }
258
259 // stack represents a goroutine stack sample.
260 type stack struct {
261 pcs []uint64
262 }
263
264 func (s stack) String() string {
265 var sb strings.Builder
266 for _, frame := range s.pcs {
267 fmt.Fprintf(&sb, "\t%#v\n", frame)
268 }
269 return sb.String()
270 }
271
272 // frame represents a single stack frame.
273 type frame struct {
274 pc uint64
275 funcID stringID
276 fileID stringID
277 line uint64
278 }