| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 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 |
| 23 |
dataOff int |
| 24 |
ev baseEvent |
| 25 |
} |
| 26 |
|
| 27 |
func (b *batchCursor) nextEvent(batches []batch, freq frequency) (ok bool, err error) { |
| 28 |
|
| 29 |
|
| 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 |
|
| 36 |
if b.idx == len(batches) { |
| 37 |
return false, nil |
| 38 |
} |
| 39 |
|
| 40 |
if b.lastTs == 0 { |
| 41 |
b.lastTs = freq.mul(batches[b.idx].time) |
| 42 |
} |
| 43 |
|
| 44 |
n, tsdiff, err := readTimedBaseEvent(batches[b.idx].data[b.dataOff:], &b.ev) |
| 45 |
if err != nil { |
| 46 |
return false, err |
| 47 |
} |
| 48 |
|
| 49 |
b.ev.time = freq.mul(tsdiff) + b.lastTs |
| 50 |
|
| 51 |
|
| 52 |
b.lastTs = b.ev.time |
| 53 |
|
| 54 |
|
| 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 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
func readTimedBaseEvent(b []byte, e *baseEvent) (int, timestamp, error) { |
| 71 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 107 |
heap = append(heap, bc) |
| 108 |
|
| 109 |
|
| 110 |
heapSiftUp(heap, len(heap)-1) |
| 111 |
return heap |
| 112 |
} |
| 113 |
|
| 114 |
func heapUpdate(heap []*batchCursor, i int) { |
| 115 |
|
| 116 |
if heapSiftUp(heap, i) != i { |
| 117 |
return |
| 118 |
} |
| 119 |
|
| 120 |
heapSiftDown(heap, i) |
| 121 |
} |
| 122 |
|
| 123 |
func heapRemove(heap []*batchCursor, i int) []*batchCursor { |
| 124 |
|
| 125 |
for i > 0 { |
| 126 |
heap[(i-1)/2], heap[i] = heap[i], heap[(i-1)/2] |
| 127 |
i = (i - 1) / 2 |
| 128 |
} |
| 129 |
|
| 130 |
heap[0], heap[len(heap)-1] = heap[len(heap)-1], heap[0] |
| 131 |
heap = heap[:len(heap)-1] |
| 132 |
|
| 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 |
|
| 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 |
} |