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.
14 "internal/trace/v2/event"
15 "internal/trace/v2/event/go122"
18 // timestamp is an unprocessed timestamp.
21 // batch represents a batch of trace events.
22 // It is unparsed except for its header.
29 func (b *batch) isStringsBatch() bool {
30 return len(b.data) > 0 && event.Type(b.data[0]) == go122.EvStrings
33 func (b *batch) isStacksBatch() bool {
34 return len(b.data) > 0 && event.Type(b.data[0]) == go122.EvStacks
37 func (b *batch) isCPUSamplesBatch() bool {
38 return len(b.data) > 0 && event.Type(b.data[0]) == go122.EvCPUSamples
41 func (b *batch) isFreqBatch() bool {
42 return len(b.data) > 0 && event.Type(b.data[0]) == go122.EvFrequency
45 // readBatch reads the next full batch from r.
46 func readBatch(r *bufio.Reader) (batch, uint64, error) {
47 // Read batch header byte.
48 b, err := r.ReadByte()
50 return batch{}, 0, err
52 if typ := event.Type(b); typ != go122.EvEventBatch {
53 return batch{}, 0, fmt.Errorf("expected batch event (%s), got %s", go122.EventString(go122.EvEventBatch), go122.EventString(typ))
56 // Read the batch header: gen (generation), thread (M) ID, base timestamp
58 gen, err := binary.ReadUvarint(r)
60 return batch{}, gen, fmt.Errorf("error reading batch gen: %w", err)
62 m, err := binary.ReadUvarint(r)
64 return batch{}, gen, fmt.Errorf("error reading batch M ID: %w", err)
66 ts, err := binary.ReadUvarint(r)
68 return batch{}, gen, fmt.Errorf("error reading batch timestamp: %w", err)
71 // Read in the size of the batch to follow.
72 size, err := binary.ReadUvarint(r)
74 return batch{}, gen, fmt.Errorf("error reading batch size: %w", err)
76 if size > go122.MaxBatchSize {
77 return batch{}, gen, fmt.Errorf("invalid batch size %d, maximum is %d", size, go122.MaxBatchSize)
80 // Copy out the batch for later processing.
83 n, err := io.CopyN(&data, r, int64(size))
85 return batch{}, gen, fmt.Errorf("failed to read full batch: read %d but wanted %d", n, size)
88 return batch{}, gen, fmt.Errorf("copying batch data: %w", err)