github.com/GuanceCloud/cliutils@v1.1.21/pprofparser/domain/pprof/frame.go (about)

     1  package pprof
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/GuanceCloud/cliutils/pprofparser/domain/quantity"
     7  )
     8  
     9  const (
    10  	FieldQuantity     = "quantity"
    11  	FieldValue        = "value"
    12  	FieldUnit         = "unit"
    13  	FieldPercent      = "percent"
    14  	FieldFunctionName = "functionName"
    15  	FieldLine         = "line"
    16  	FieldFile         = "file"
    17  	FieldDirectory    = "directory"
    18  	FieldThreadID     = "threadID"
    19  	FieldThreadName   = "threadName"
    20  	FieldClass        = "class"
    21  	FieldNamespace    = "namespace"
    22  	FieldAssembly     = "assembly"
    23  	FieldPackage      = "package"
    24  	FieldPrintString  = "printString"
    25  	FieldSubFrames    = "subFrames"
    26  )
    27  
    28  func GetFrameJSONFields() []string {
    29  	return []string{
    30  		FieldQuantity,
    31  		FieldValue,
    32  		FieldUnit,
    33  		FieldPercent,
    34  		FieldFunctionName,
    35  		FieldLine,
    36  		FieldFile,
    37  		FieldDirectory,
    38  		FieldThreadID,
    39  		FieldThreadName,
    40  		FieldClass,
    41  		FieldNamespace,
    42  		FieldAssembly,
    43  		FieldPackage,
    44  		FieldPrintString,
    45  		FieldSubFrames,
    46  	}
    47  }
    48  
    49  type Frame struct {
    50  	Quantity    *quantity.Quantity `json:"quantity"`
    51  	Value       int64              `json:"value"`
    52  	Unit        *quantity.Unit     `json:"unit"`
    53  	Percent     string             `json:"percent"`
    54  	Function    string             `json:"functionName"`
    55  	Line        int64              `json:"line,omitempty"`
    56  	File        string             `json:"file,omitempty"`
    57  	Directory   string             `json:"directory,omitempty"`
    58  	ThreadID    string             `json:"threadID"`
    59  	ThreadName  string             `json:"threadName"`
    60  	Class       string             `json:"class,omitempty"`
    61  	Namespace   string             `json:"namespace,omitempty"`
    62  	Assembly    string             `json:"assembly,omitempty"`
    63  	Package     string             `json:"package,omitempty"`
    64  	PrintString string             `json:"printString"`
    65  	SubFrames   SubFrames          `json:"subFrames"`
    66  }
    67  
    68  func (f *Frame) MarshalJSON() ([]byte, error) {
    69  	toArr := []interface{}{
    70  		f.Quantity,
    71  		f.Value,
    72  		f.Unit,
    73  		f.Percent,
    74  		f.Function,
    75  		f.Line,
    76  		f.File,
    77  		f.Directory,
    78  		f.ThreadID,
    79  		f.ThreadName,
    80  		f.Class,
    81  		f.Namespace,
    82  		f.Assembly,
    83  		f.Package,
    84  		f.PrintString,
    85  		f.SubFrames,
    86  	}
    87  	return json.Marshal(toArr)
    88  }
    89  
    90  type SubFrames map[string]*Frame
    91  
    92  func (sf SubFrames) MarshalJSON() ([]byte, error) {
    93  	frames := make([]*Frame, 0, len(sf))
    94  	for _, frame := range sf {
    95  		frames = append(frames, frame)
    96  	}
    97  	return json.Marshal(frames)
    98  }