| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 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 |
|
| 24 |
|
| 25 |
type timedEventArgs [tracev2.MaxTimedEventArgs - 1]uint64 |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
type baseEvent struct { |
| 30 |
typ tracev2.EventType |
| 31 |
time Time |
| 32 |
args timedEventArgs |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 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 |
|
| 46 |
|
| 47 |
type evTable struct { |
| 48 |
sync |
| 49 |
strings dataTable[stringID, string] |
| 50 |
stacks dataTable[stackID, stack] |
| 51 |
pcs map[uint64]frame |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
extraStrings []string |
| 57 |
extraStringIDs map[string]extraStringID |
| 58 |
nextExtra extraStringID |
| 59 |
|
| 60 |
|
| 61 |
expBatches map[tracev2.Experiment][]ExperimentalBatch |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 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 |
|
| 84 |
|
| 85 |
func (t *evTable) getExtraString(id extraStringID) string { |
| 86 |
if id == 0 { |
| 87 |
return "" |
| 88 |
} |
| 89 |
return t.extraStrings[id-1] |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
type dataTable[EI ~uint64, E any] struct { |
| 94 |
present []uint8 |
| 95 |
dense []E |
| 96 |
sparse map[EI]E |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
|
| 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 |
|
| 126 |
|
| 127 |
|
| 128 |
func (d *dataTable[EI, E]) compactify() { |
| 129 |
if d.sparse == nil || len(d.dense) != 0 { |
| 130 |
|
| 131 |
return |
| 132 |
} |
| 133 |
|
| 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 |
|
| 146 |
return |
| 147 |
} |
| 148 |
|
| 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 |
|
| 166 |
|
| 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 |
|
| 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 |
|
| 205 |
|
| 206 |
|
| 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 |
|
| 216 |
type frequency float64 |
| 217 |
|
| 218 |
|
| 219 |
func (f frequency) mul(t timestamp) Time { |
| 220 |
return Time(float64(t) * float64(f)) |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
type stringID uint64 |
| 225 |
|
| 226 |
|
| 227 |
type extraStringID uint64 |
| 228 |
|
| 229 |
|
| 230 |
type stackID uint64 |
| 231 |
|
| 232 |
|
| 233 |
type cpuSample struct { |
| 234 |
schedCtx |
| 235 |
time Time |
| 236 |
stack stackID |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
func (s cpuSample) asEvent(table *evTable) Event { |
| 245 |
|
| 246 |
|
| 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 |
|
| 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 |
|
| 273 |
type frame struct { |
| 274 |
pc uint64 |
| 275 |
funcID stringID |
| 276 |
fileID stringID |
| 277 |
line uint64 |
| 278 |
} |