Debian Codegraph @

trace/batch.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 "bytes"
13 "encoding/binary"
14 "fmt"
15 "io"
16
17 "golang.org/x/exp/trace/internal/tracev2"
18 "golang.org/x/exp/trace/internal/version"
19 )
20
21 // timestamp is an unprocessed timestamp.
22 type timestamp uint64
23
24 // batch represents a batch of trace events.
25 // It is unparsed except for its header.
26 type batch struct {
27 m ThreadID
28 time timestamp
29 data []byte
30 exp tracev2.Experiment
31 }
32
33 func (b *batch) isStringsBatch() bool {
34 return b.exp == tracev2.NoExperiment && len(b.data) > 0 && tracev2.EventType(b.data[0]) == tracev2.EvStrings
35 }
36
37 func (b *batch) isStacksBatch() bool {
38 return b.exp == tracev2.NoExperiment && len(b.data) > 0 && tracev2.EventType(b.data[0]) == tracev2.EvStacks
39 }
40
41 func (b *batch) isCPUSamplesBatch() bool {
42 return b.exp == tracev2.NoExperiment && len(b.data) > 0 && tracev2.EventType(b.data[0]) == tracev2.EvCPUSamples
43 }
44
45 func (b *batch) isSyncBatch(ver version.Version) bool {
46 return (b.exp == tracev2.NoExperiment && len(b.data) > 0) &&
47 ((tracev2.EventType(b.data[0]) == tracev2.EvFrequency && ver < version.Go125) ||
48 (tracev2.EventType(b.data[0]) == tracev2.EvSync && ver >= version.Go125))
49 }
50
51 func (b *batch) isEndOfGeneration() bool {
52 return b.exp == tracev2.NoExperiment && len(b.data) > 0 && tracev2.EventType(b.data[0]) == tracev2.EvEndOfGeneration
53 }
54
55 // readBatch reads the next full batch from r.
56 func readBatch(r interface {
57 io.Reader
58 io.ByteReader
59 }) (batch, uint64, error) {
60 // Read batch header byte.
61 b, err := r.ReadByte()
62 if err != nil {
63 return batch{}, 0, err
64 }
65 if typ := tracev2.EventType(b); typ == tracev2.EvEndOfGeneration {
66 return batch{m: NoThread, exp: tracev2.NoExperiment, data: []byte{b}}, 0, nil
67 }
68 if typ := tracev2.EventType(b); typ != tracev2.EvEventBatch && typ != tracev2.EvExperimentalBatch {
69 return batch{}, 0, fmt.Errorf("expected batch event, got event %d", typ)
70 }
71
72 // Read the experiment of we have one.
73 exp := tracev2.NoExperiment
74 if tracev2.EventType(b) == tracev2.EvExperimentalBatch {
75 e, err := r.ReadByte()
76 if err != nil {
77 return batch{}, 0, err
78 }
79 exp = tracev2.Experiment(e)
80 }
81
82 // Read the batch header: gen (generation), thread (M) ID, base timestamp
83 // for the batch.
84 gen, err := binary.ReadUvarint(r)
85 if err != nil {
86 return batch{}, gen, fmt.Errorf("error reading batch gen: %w", err)
87 }
88 m, err := binary.ReadUvarint(r)
89 if err != nil {
90 return batch{}, gen, fmt.Errorf("error reading batch M ID: %w", err)
91 }
92 ts, err := binary.ReadUvarint(r)
93 if err != nil {
94 return batch{}, gen, fmt.Errorf("error reading batch timestamp: %w", err)
95 }
96
97 // Read in the size of the batch to follow.
98 size, err := binary.ReadUvarint(r)
99 if err != nil {
100 return batch{}, gen, fmt.Errorf("error reading batch size: %w", err)
101 }
102 if size > tracev2.MaxBatchSize {
103 return batch{}, gen, fmt.Errorf("invalid batch size %d, maximum is %d", size, tracev2.MaxBatchSize)
104 }
105
106 // Copy out the batch for later processing.
107 var data bytes.Buffer
108 data.Grow(int(size))
109 n, err := io.CopyN(&data, r, int64(size))
110 if n != int64(size) {
111 return batch{}, gen, fmt.Errorf("failed to read full batch: read %d but wanted %d", n, size)
112 }
113 if err != nil {
114 return batch{}, gen, fmt.Errorf("copying batch data: %w", err)
115 }
116
117 // Return the batch.
118 return batch{
119 m: ThreadID(m),
120 time: timestamp(ts),
121 data: data.Bytes(),
122 exp: exp,
123 }, gen, nil
124 }