package trace
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"internal/trace/event"
"internal/trace/event/go122"
)
type timestamp uint64
type batch struct {
m ThreadID
time timestamp
data []byte
exp event .Experiment
}
func (b *batch ) isStringsBatch () bool {
return b .exp == event .NoExperiment && len (b .data ) > 0 && event .Type (b .data [0 ]) == go122 .EvStrings
}
func (b *batch ) isStacksBatch () bool {
return b .exp == event .NoExperiment && len (b .data ) > 0 && event .Type (b .data [0 ]) == go122 .EvStacks
}
func (b *batch ) isCPUSamplesBatch () bool {
return b .exp == event .NoExperiment && len (b .data ) > 0 && event .Type (b .data [0 ]) == go122 .EvCPUSamples
}
func (b *batch ) isFreqBatch () bool {
return b .exp == event .NoExperiment && len (b .data ) > 0 && event .Type (b .data [0 ]) == go122 .EvFrequency
}
func readBatch(r interface {
io .Reader
io .ByteReader
}) (batch , uint64 , error ) {
b , err := r .ReadByte ()
if err != nil {
return batch {}, 0 , err
}
if typ := event .Type (b ); typ != go122 .EvEventBatch && typ != go122 .EvExperimentalBatch {
return batch {}, 0 , fmt .Errorf ("expected batch event, got %s" , go122 .EventString (typ ))
}
exp := event .NoExperiment
if event .Type (b ) == go122 .EvExperimentalBatch {
e , err := r .ReadByte ()
if err != nil {
return batch {}, 0 , err
}
exp = event .Experiment (e )
}
gen , err := binary .ReadUvarint (r )
if err != nil {
return batch {}, gen , fmt .Errorf ("error reading batch gen: %w" , err )
}
m , err := binary .ReadUvarint (r )
if err != nil {
return batch {}, gen , fmt .Errorf ("error reading batch M ID: %w" , err )
}
ts , err := binary .ReadUvarint (r )
if err != nil {
return batch {}, gen , fmt .Errorf ("error reading batch timestamp: %w" , err )
}
size , err := binary .ReadUvarint (r )
if err != nil {
return batch {}, gen , fmt .Errorf ("error reading batch size: %w" , err )
}
if size > go122 .MaxBatchSize {
return batch {}, gen , fmt .Errorf ("invalid batch size %d, maximum is %d" , size , go122 .MaxBatchSize )
}
var data bytes .Buffer
data .Grow (int (size ))
n , err := io .CopyN (&data , r , int64 (size ))
if n != int64 (size ) {
return batch {}, gen , fmt .Errorf ("failed to read full batch: read %d but wanted %d" , n , size )
}
if err != nil {
return batch {}, gen , fmt .Errorf ("copying batch data: %w" , err )
}
return batch {
m : ThreadID (m ),
time : timestamp (ts ),
data : data .Bytes (),
exp : exp ,
}, gen , nil
}
The pages are generated with Golds v0.7.3 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .