github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/internal/trace/parser.go (about) 1 // Copyright 2014 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 trace 6 7 import ( 8 "github.com/shogo82148/std/fmt" 9 "github.com/shogo82148/std/io" 10 ) 11 12 // Event describes one event in the trace. 13 type Event struct { 14 Off int 15 Type byte 16 seq int64 17 Ts int64 18 P int 19 G uint64 20 StkID uint64 21 Stk []*Frame 22 Args [3]uint64 23 SArgs []string 24 // linked event (can be nil), depends on event type: 25 // for GCStart: the GCStop 26 // for GCSTWStart: the GCSTWDone 27 // for GCSweepStart: the GCSweepDone 28 // for GoCreate: first GoStart of the created goroutine 29 // for GoStart/GoStartLabel: the associated GoEnd, GoBlock or other blocking event 30 // for GoSched/GoPreempt: the next GoStart 31 // for GoBlock and other blocking events: the unblock event 32 // for GoUnblock: the associated GoStart 33 // for blocking GoSysCall: the associated GoSysExit 34 // for GoSysExit: the next GoStart 35 // for GCMarkAssistStart: the associated GCMarkAssistDone 36 // for UserTaskCreate: the UserTaskEnd 37 // for UserRegion: if the start region, the corresponding UserRegion end event 38 Link *Event 39 } 40 41 // Frame is a frame in stack traces. 42 type Frame struct { 43 PC uint64 44 Fn string 45 File string 46 Line int 47 } 48 49 const ( 50 // Special P identifiers: 51 FakeP = 1000000 + iota 52 TimerP 53 NetpollP 54 SyscallP 55 GCP 56 ProfileP 57 ) 58 59 // ParseResult is the result of Parse. 60 type ParseResult struct { 61 // Events is the sorted list of Events in the trace. 62 Events []*Event 63 // Stacks is the stack traces keyed by stack IDs from the trace. 64 Stacks map[uint64][]*Frame 65 } 66 67 // Parse parses, post-processes and verifies the trace. 68 func Parse(r io.Reader, bin string) (ParseResult, error) 69 70 func ReadVersion(r io.Reader) (ver int, off int, err error) 71 72 // ErrTimeOrder is returned by Parse when the trace contains 73 // time stamps that do not respect actual event ordering. 74 var ErrTimeOrder = fmt.Errorf("time stamps out of order") 75 76 // Print dumps events to stdout. For debugging. 77 func Print(events []*Event) 78 79 // PrintEvent dumps the event to stdout. For debugging. 80 func PrintEvent(ev *Event) 81 82 func (ev *Event) String() string 83 84 // BreakTimestampsForTesting causes the parser to randomly alter timestamps (for testing of broken cputicks). 85 var BreakTimestampsForTesting bool 86 87 // Event types in the trace. 88 // Verbatim copy from src/runtime/trace.go with the "trace" prefix removed. 89 const ( 90 EvNone = 0 91 EvBatch = 1 92 EvFrequency = 2 93 EvStack = 3 94 EvGomaxprocs = 4 95 EvProcStart = 5 96 EvProcStop = 6 97 EvGCStart = 7 98 EvGCDone = 8 99 EvSTWStart = 9 100 EvSTWDone = 10 101 EvGCSweepStart = 11 102 EvGCSweepDone = 12 103 EvGoCreate = 13 104 EvGoStart = 14 105 EvGoEnd = 15 106 EvGoStop = 16 107 EvGoSched = 17 108 EvGoPreempt = 18 109 EvGoSleep = 19 110 EvGoBlock = 20 111 EvGoUnblock = 21 112 EvGoBlockSend = 22 113 EvGoBlockRecv = 23 114 EvGoBlockSelect = 24 115 EvGoBlockSync = 25 116 EvGoBlockCond = 26 117 EvGoBlockNet = 27 118 EvGoSysCall = 28 119 EvGoSysExit = 29 120 EvGoSysBlock = 30 121 EvGoWaiting = 31 122 EvGoInSyscall = 32 123 EvHeapAlloc = 33 124 EvHeapGoal = 34 125 EvTimerGoroutine = 35 126 EvFutileWakeup = 36 127 EvString = 37 128 EvGoStartLocal = 38 129 EvGoUnblockLocal = 39 130 EvGoSysExitLocal = 40 131 EvGoStartLabel = 41 132 EvGoBlockGC = 42 133 EvGCMarkAssistStart = 43 134 EvGCMarkAssistDone = 44 135 EvUserTaskCreate = 45 136 EvUserTaskEnd = 46 137 EvUserRegion = 47 138 EvUserLog = 48 139 EvCPUSample = 49 140 EvCount = 50 141 ) 142 143 var EventDescriptions = [EvCount]struct { 144 Name string 145 minVersion int 146 Stack bool 147 Args []string 148 SArgs []string 149 }{ 150 EvNone: {"None", 1005, false, []string{}, nil}, 151 EvBatch: {"Batch", 1005, false, []string{"p", "ticks"}, nil}, 152 EvFrequency: {"Frequency", 1005, false, []string{"freq"}, nil}, 153 EvStack: {"Stack", 1005, false, []string{"id", "siz"}, nil}, 154 EvGomaxprocs: {"Gomaxprocs", 1005, true, []string{"procs"}, nil}, 155 EvProcStart: {"ProcStart", 1005, false, []string{"thread"}, nil}, 156 EvProcStop: {"ProcStop", 1005, false, []string{}, nil}, 157 EvGCStart: {"GCStart", 1005, true, []string{"seq"}, nil}, 158 EvGCDone: {"GCDone", 1005, false, []string{}, nil}, 159 EvSTWStart: {"STWStart", 1005, false, []string{"kindid"}, []string{"kind"}}, 160 EvSTWDone: {"STWDone", 1005, false, []string{}, nil}, 161 EvGCSweepStart: {"GCSweepStart", 1005, true, []string{}, nil}, 162 EvGCSweepDone: {"GCSweepDone", 1005, false, []string{"swept", "reclaimed"}, nil}, 163 EvGoCreate: {"GoCreate", 1005, true, []string{"g", "stack"}, nil}, 164 EvGoStart: {"GoStart", 1005, false, []string{"g", "seq"}, nil}, 165 EvGoEnd: {"GoEnd", 1005, false, []string{}, nil}, 166 EvGoStop: {"GoStop", 1005, true, []string{}, nil}, 167 EvGoSched: {"GoSched", 1005, true, []string{}, nil}, 168 EvGoPreempt: {"GoPreempt", 1005, true, []string{}, nil}, 169 EvGoSleep: {"GoSleep", 1005, true, []string{}, nil}, 170 EvGoBlock: {"GoBlock", 1005, true, []string{}, nil}, 171 EvGoUnblock: {"GoUnblock", 1005, true, []string{"g", "seq"}, nil}, 172 EvGoBlockSend: {"GoBlockSend", 1005, true, []string{}, nil}, 173 EvGoBlockRecv: {"GoBlockRecv", 1005, true, []string{}, nil}, 174 EvGoBlockSelect: {"GoBlockSelect", 1005, true, []string{}, nil}, 175 EvGoBlockSync: {"GoBlockSync", 1005, true, []string{}, nil}, 176 EvGoBlockCond: {"GoBlockCond", 1005, true, []string{}, nil}, 177 EvGoBlockNet: {"GoBlockNet", 1005, true, []string{}, nil}, 178 EvGoSysCall: {"GoSysCall", 1005, true, []string{}, nil}, 179 EvGoSysExit: {"GoSysExit", 1005, false, []string{"g", "seq", "ts"}, nil}, 180 EvGoSysBlock: {"GoSysBlock", 1005, false, []string{}, nil}, 181 EvGoWaiting: {"GoWaiting", 1005, false, []string{"g"}, nil}, 182 EvGoInSyscall: {"GoInSyscall", 1005, false, []string{"g"}, nil}, 183 EvHeapAlloc: {"HeapAlloc", 1005, false, []string{"mem"}, nil}, 184 EvHeapGoal: {"HeapGoal", 1005, false, []string{"mem"}, nil}, 185 EvTimerGoroutine: {"TimerGoroutine", 1005, false, []string{"g"}, nil}, 186 EvFutileWakeup: {"FutileWakeup", 1005, false, []string{}, nil}, 187 EvString: {"String", 1007, false, []string{}, nil}, 188 EvGoStartLocal: {"GoStartLocal", 1007, false, []string{"g"}, nil}, 189 EvGoUnblockLocal: {"GoUnblockLocal", 1007, true, []string{"g"}, nil}, 190 EvGoSysExitLocal: {"GoSysExitLocal", 1007, false, []string{"g", "ts"}, nil}, 191 EvGoStartLabel: {"GoStartLabel", 1008, false, []string{"g", "seq", "labelid"}, []string{"label"}}, 192 EvGoBlockGC: {"GoBlockGC", 1008, true, []string{}, nil}, 193 EvGCMarkAssistStart: {"GCMarkAssistStart", 1009, true, []string{}, nil}, 194 EvGCMarkAssistDone: {"GCMarkAssistDone", 1009, false, []string{}, nil}, 195 EvUserTaskCreate: {"UserTaskCreate", 1011, true, []string{"taskid", "pid", "typeid"}, []string{"name"}}, 196 EvUserTaskEnd: {"UserTaskEnd", 1011, true, []string{"taskid"}, nil}, 197 EvUserRegion: {"UserRegion", 1011, true, []string{"taskid", "mode", "typeid"}, []string{"name"}}, 198 EvUserLog: {"UserLog", 1011, true, []string{"id", "keyid"}, []string{"category", "message"}}, 199 EvCPUSample: {"CPUSample", 1019, true, []string{"ts", "p", "g"}, nil}, 200 }