Source file src/internal/trace/raw/event.go

     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  package raw
     6  
     7  import (
     8  	"encoding/binary"
     9  	"strconv"
    10  	"strings"
    11  
    12  	"internal/trace/event"
    13  	"internal/trace/version"
    14  )
    15  
    16  // Event is a simple representation of a trace event.
    17  //
    18  // Note that this typically includes much more than just
    19  // timestamped events, and it also represents parts of the
    20  // trace format's framing. (But not interpreted.)
    21  type Event struct {
    22  	Version version.Version
    23  	Ev      event.Type
    24  	Args    []uint64
    25  	Data    []byte
    26  }
    27  
    28  // String returns the canonical string representation of the event.
    29  //
    30  // This format is the same format that is parsed by the TextReader
    31  // and emitted by the TextWriter.
    32  func (e *Event) String() string {
    33  	spec := e.Version.Specs()[e.Ev]
    34  
    35  	var s strings.Builder
    36  	s.WriteString(spec.Name)
    37  	for i := range spec.Args {
    38  		s.WriteString(" ")
    39  		s.WriteString(spec.Args[i])
    40  		s.WriteString("=")
    41  		s.WriteString(strconv.FormatUint(e.Args[i], 10))
    42  	}
    43  	if spec.IsStack {
    44  		frames := e.Args[len(spec.Args):]
    45  		for i := 0; i < len(frames); i++ {
    46  			if i%4 == 0 {
    47  				s.WriteString("\n\t")
    48  			} else {
    49  				s.WriteString(" ")
    50  			}
    51  			s.WriteString(frameFields[i%4])
    52  			s.WriteString("=")
    53  			s.WriteString(strconv.FormatUint(frames[i], 10))
    54  		}
    55  	}
    56  	if e.Data != nil {
    57  		s.WriteString("\n\tdata=")
    58  		s.WriteString(strconv.Quote(string(e.Data)))
    59  	}
    60  	return s.String()
    61  }
    62  
    63  // EncodedSize returns the canonical encoded size of an event.
    64  func (e *Event) EncodedSize() int {
    65  	size := 1
    66  	var buf [binary.MaxVarintLen64]byte
    67  	for _, arg := range e.Args {
    68  		size += binary.PutUvarint(buf[:], arg)
    69  	}
    70  	spec := e.Version.Specs()[e.Ev]
    71  	if spec.HasData {
    72  		size += binary.PutUvarint(buf[:], uint64(len(e.Data)))
    73  		size += len(e.Data)
    74  	}
    75  	return size
    76  }
    77  

View as plain text