github.com/grailbio/bigslice@v0.0.0-20230519005545-30c4c12152ad/internal/trace/trace.go (about)

     1  // Copyright 2020 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache 2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  package trace
     6  
     7  import (
     8  	"encoding/json"
     9  	"io"
    10  )
    11  
    12  // T represents the JSON object format in the Chrome tracing format. For more
    13  // details, see:
    14  // https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview
    15  type T struct {
    16  	Events []Event `json:"traceEvents"`
    17  }
    18  
    19  // Event is an event in the Chrome tracing format. The fields are mirrored
    20  // exactly.
    21  type Event struct {
    22  	Pid  int                    `json:"pid"`
    23  	Tid  int                    `json:"tid"`
    24  	Ts   int64                  `json:"ts"`
    25  	Ph   string                 `json:"ph"`
    26  	Dur  int64                  `json:"dur,omitempty"`
    27  	Name string                 `json:"name"`
    28  	Cat  string                 `json:"cat,omitempty"`
    29  	Args map[string]interface{} `json:"args"`
    30  }
    31  
    32  // Encode JSON encodes t into w.
    33  func (t *T) Encode(w io.Writer) error {
    34  	return json.NewEncoder(w).Encode(t)
    35  }
    36  
    37  // Decode decodes the JSON object format read from r into t. Call this with a t
    38  // zero value.
    39  func (t *T) Decode(r io.Reader) error {
    40  	return json.NewDecoder(r).Decode(t)
    41  }