Debian Codegraph @

trace/batchcursor.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 package trace
10
11 import (
12 "cmp"
13 "encoding/binary"
14 "fmt"
15
16 "golang.org/x/exp/trace/internal/tracev2"
17 )
18
19 type batchCursor struct {
20 m ThreadID
21 lastTs Time
22 idx int // next index into []batch
23 dataOff int // next index into batch.data
24 ev baseEvent // last read event
25 }
26
27 func (b *batchCursor) nextEvent(batches []batch, freq frequency) (ok bool, err error) {
28 // Batches should generally always have at least one event,
29 // but let's be defensive about that and accept empty batches.
30 for b.idx < len(batches) && len(batches[b.idx].data) == b.dataOff {
31 b.idx++
32 b.dataOff = 0
33 b.lastTs = 0
34 }
35 // Have we reached the end of the batches?
36 if b.idx == len(batches) {
37 return false, nil
38 }
39 // Initialize lastTs if it hasn't been yet.
40 if b.lastTs == 0 {
41 b.lastTs = freq.mul(batches[b.idx].time)
42 }
43 // Read an event out.
44 n, tsdiff, err := readTimedBaseEvent(batches[b.idx].data[b.dataOff:], &b.ev)
45 if err != nil {
46 return false, err
47 }
48 // Complete the timestamp from the cursor's last timestamp.
49 b.ev.time = freq.mul(tsdiff) + b.lastTs
50
51 // Move the cursor's timestamp forward.
52 b.lastTs = b.ev.time
53
54 // Move the cursor forward.
55 b.dataOff += n
56 return true, nil
57 }
58
59 func (b *batchCursor) compare(a *batchCursor) int {
60 return cmp.Compare(b.ev.time, a.ev.time)
61 }
62
63 // readTimedBaseEvent reads out the raw event data from b
64 // into e. It does not try to interpret the arguments
65 // but it does validate that the event is a regular
66 // event with a timestamp (vs. a structural event).
67 //
68 // It requires that the event its reading be timed, which must
69 // be the case for every event in a plain EventBatch.
70 func readTimedBaseEvent(b []byte, e *baseEvent) (int, timestamp, error) {
71 // Get the event type.
72 typ := tracev2.EventType(b[0])
73 specs := tracev2.Specs()
74 if int(typ) >= len(specs) {
75 return 0, 0, fmt.Errorf("found invalid event type: %v", typ)
76 }
77 e.typ = typ
78
79 // Get spec.
80 spec := &specs[typ]
81 if len(spec.Args) == 0 || !spec.IsTimedEvent {
82 return 0, 0, fmt.Errorf("found event without a timestamp: type=%v", typ)
83 }
84 n := 1
85
86 // Read timestamp diff.
87 ts, nb := binary.Uvarint(b[n:])
88 if nb <= 0 {
89 return 0, 0, fmt.Errorf("found invalid uvarint for timestamp")
90 }
91 n += nb
92
93 // Read the rest of the arguments.
94 for i := 0; i < len(spec.Args)-1; i++ {
95 arg, nb := binary.Uvarint(b[n:])
96 if nb <= 0 {
97 return 0, 0, fmt.Errorf("found invalid uvarint")
98 }
99 e.args[i] = arg
100 n += nb
101 }
102 return n, timestamp(ts), nil
103 }
104
105 func heapInsert(heap []*batchCursor, bc *batchCursor) []*batchCursor {
106 // Add the cursor to the end of the heap.
107 heap = append(heap, bc)
108
109 // Sift the new entry up to the right place.
110 heapSiftUp(heap, len(heap)-1)
111 return heap
112 }
113
114 func heapUpdate(heap []*batchCursor, i int) {
115 // Try to sift up.
116 if heapSiftUp(heap, i) != i {
117 return
118 }
119 // Try to sift down, if sifting up failed.
120 heapSiftDown(heap, i)
121 }
122
123 func heapRemove(heap []*batchCursor, i int) []*batchCursor {
124 // Sift index i up to the root, ignoring actual values.
125 for i > 0 {
126 heap[(i-1)/2], heap[i] = heap[i], heap[(i-1)/2]
127 i = (i - 1) / 2
128 }
129 // Swap the root with the last element, then remove it.
130 heap[0], heap[len(heap)-1] = heap[len(heap)-1], heap[0]
131 heap = heap[:len(heap)-1]
132 // Sift the root down.
133 heapSiftDown(heap, 0)
134 return heap
135 }
136
137 func heapSiftUp(heap []*batchCursor, i int) int {
138 for i > 0 && heap[(i-1)/2].ev.time > heap[i].ev.time {
139 heap[(i-1)/2], heap[i] = heap[i], heap[(i-1)/2]
140 i = (i - 1) / 2
141 }
142 return i
143 }
144
145 func heapSiftDown(heap []*batchCursor, i int) int {
146 for {
147 m := min3(heap, i, 2*i+1, 2*i+2)
148 if m == i {
149 // Heap invariant already applies.
150 break
151 }
152 heap[i], heap[m] = heap[m], heap[i]
153 i = m
154 }
155 return i
156 }
157
158 func min3(b []*batchCursor, i0, i1, i2 int) int {
159 minIdx := i0
160 minT := maxTime
161 if i0 < len(b) {
162 minT = b[i0].ev.time
163 }
164 if i1 < len(b) {
165 if t := b[i1].ev.time; t < minT {
166 minT = t
167 minIdx = i1
168 }
169 }
170 if i2 < len(b) {
171 if t := b[i2].ev.time; t < minT {
172 minT = t
173 minIdx = i2
174 }
175 }
176 return minIdx
177 }