github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/internal/trace/v2/event.go (about) 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 trace 6 7 import ( 8 "github.com/shogo82148/std/time" 9 ) 10 11 // EventKind indicates the kind of event this is. 12 // 13 // Use this information to obtain a more specific event that 14 // allows access to more detailed information. 15 type EventKind uint16 16 17 const ( 18 EventBad EventKind = iota 19 20 // EventKindSync is an event that indicates a global synchronization 21 // point in the trace. At the point of a sync event, the 22 // trace reader can be certain that all resources (e.g. threads, 23 // goroutines) that have existed until that point have been enumerated. 24 EventSync 25 26 // EventMetric is an event that represents the value of a metric at 27 // a particular point in time. 28 EventMetric 29 30 // EventLabel attaches a label to a resource. 31 EventLabel 32 33 // EventStackSample represents an execution sample, indicating what a 34 // thread/proc/goroutine was doing at a particular point in time via 35 // its backtrace. 36 // 37 // Note: Samples should be considered a close approximation of 38 // what a thread/proc/goroutine was executing at a given point in time. 39 // These events may slightly contradict the situation StateTransitions 40 // describe, so they should only be treated as a best-effort annotation. 41 EventStackSample 42 43 // EventRangeBegin and EventRangeEnd are a pair of generic events representing 44 // a special range of time. Ranges are named and scoped to some resource 45 // (identified via ResourceKind). A range that has begun but has not ended 46 // is considered active. 47 // 48 // EvRangeBegin and EvRangeEnd will share the same name, and an End will always 49 // follow a Begin on the same instance of the resource. The associated 50 // resource ID can be obtained from the Event. ResourceNone indicates the 51 // range is globally scoped. That is, any goroutine/proc/thread can start or 52 // stop, but only one such range may be active at any given time. 53 // 54 // EventRangeActive is like EventRangeBegin, but indicates that the range was 55 // already active. In this case, the resource referenced may not be in the current 56 // context. 57 EventRangeBegin 58 EventRangeActive 59 EventRangeEnd 60 61 // EvTaskBegin and EvTaskEnd are a pair of events representing a runtime/trace.Task. 62 EventTaskBegin 63 EventTaskEnd 64 65 // EventRegionBegin and EventRegionEnd are a pair of events represent a runtime/trace.Region. 66 EventRegionBegin 67 EventRegionEnd 68 69 // EventLog represents a runtime/trace.Log call. 70 EventLog 71 72 // Transitions in state for some resource. 73 EventStateTransition 74 ) 75 76 // String returns a string form of the EventKind. 77 func (e EventKind) String() string 78 79 // Time is a timestamp in nanoseconds. 80 // 81 // It corresponds to the monotonic clock on the platform that the 82 // trace was taken, and so is possible to correlate with timestamps 83 // for other traces taken on the same machine using the same clock 84 // (i.e. no reboots in between). 85 // 86 // The actual absolute value of the timestamp is only meaningful in 87 // relation to other timestamps from the same clock. 88 // 89 // BUG: Timestamps coming from traces on Windows platforms are 90 // only comparable with timestamps from the same trace. Timestamps 91 // across traces cannot be compared, because the system clock is 92 // not used as of Go 1.22. 93 // 94 // BUG: Traces produced by Go versions 1.21 and earlier cannot be 95 // compared with timestamps from other traces taken on the same 96 // machine. This is because the system clock was not used at all 97 // to collect those timestamps. 98 type Time int64 99 100 // Sub subtracts t0 from t, returning the duration in nanoseconds. 101 func (t Time) Sub(t0 Time) time.Duration 102 103 // Metric provides details about a Metric event. 104 type Metric struct { 105 // Name is the name of the sampled metric. 106 // 107 // Names follow the same convention as metric names in the 108 // runtime/metrics package, meaning they include the unit. 109 // Names that match with the runtime/metrics package represent 110 // the same quantity. Note that this corresponds to the 111 // runtime/metrics package for the Go version this trace was 112 // collected for. 113 Name string 114 115 // Value is the sampled value of the metric. 116 // 117 // The Value's Kind is tied to the name of the metric, and so is 118 // guaranteed to be the same for metric samples for the same metric. 119 Value Value 120 } 121 122 // Label provides details about a Label event. 123 type Label struct { 124 // Label is the label applied to some resource. 125 Label string 126 127 // Resource is the resource to which this label should be applied. 128 Resource ResourceID 129 } 130 131 // Range provides details about a Range event. 132 type Range struct { 133 // Name is a human-readable name for the range. 134 // 135 // This name can be used to identify the end of the range for the resource 136 // its scoped to, because only one of each type of range may be active on 137 // a particular resource. The relevant resource should be obtained from the 138 // Event that produced these details. The corresponding RangeEnd will have 139 // an identical name. 140 Name string 141 142 // Scope is the resource that the range is scoped to. 143 // 144 // For example, a ResourceGoroutine scope means that the same goroutine 145 // must have a start and end for the range, and that goroutine can only 146 // have one range of a particular name active at any given time. The 147 // ID that this range is scoped to may be obtained via Event.Goroutine. 148 // 149 // The ResourceNone scope means that the range is globally scoped. As a 150 // result, any goroutine/proc/thread may start or end the range, and only 151 // one such named range may be active globally at any given time. 152 // 153 // For RangeBegin and RangeEnd events, this will always reference some 154 // resource ID in the current execution context. For RangeActive events, 155 // this may reference a resource not in the current context. Prefer Scope 156 // over the current execution context. 157 Scope ResourceID 158 } 159 160 // RangeAttributes provides attributes about a completed Range. 161 type RangeAttribute struct { 162 // Name is the human-readable name for the range. 163 Name string 164 165 // Value is the value of the attribute. 166 Value Value 167 } 168 169 // TaskID is the internal ID of a task used to disambiguate tasks (even if they 170 // are of the same type). 171 type TaskID uint64 172 173 const ( 174 // NoTask indicates the lack of a task. 175 NoTask = TaskID(^uint64(0)) 176 177 // BackgroundTask is the global task that events are attached to if there was 178 // no other task in the context at the point the event was emitted. 179 BackgroundTask = TaskID(0) 180 ) 181 182 // Task provides details about a Task event. 183 type Task struct { 184 // ID is a unique identifier for the task. 185 // 186 // This can be used to associate the beginning of a task with its end. 187 ID TaskID 188 189 // ParentID is the ID of the parent task. 190 Parent TaskID 191 192 // Type is the taskType that was passed to runtime/trace.NewTask. 193 // 194 // May be "" if a task's TaskBegin event isn't present in the trace. 195 Type string 196 } 197 198 // Region provides details about a Region event. 199 type Region struct { 200 // Task is the ID of the task this region is associated with. 201 Task TaskID 202 203 // Type is the regionType that was passed to runtime/trace.StartRegion or runtime/trace.WithRegion. 204 Type string 205 } 206 207 // Log provides details about a Log event. 208 type Log struct { 209 // Task is the ID of the task this region is associated with. 210 Task TaskID 211 212 // Category is the category that was passed to runtime/trace.Log or runtime/trace.Logf. 213 Category string 214 215 // Message is the message that was passed to runtime/trace.Log or runtime/trace.Logf. 216 Message string 217 } 218 219 // Stack represents a stack. It's really a handle to a stack and it's trivially comparable. 220 // 221 // If two Stacks are equal then their Frames are guaranteed to be identical. If they are not 222 // equal, however, their Frames may still be equal. 223 type Stack struct { 224 table *evTable 225 id stackID 226 } 227 228 // Frames is an iterator over the frames in a Stack. 229 func (s Stack) Frames(yield func(f StackFrame) bool) bool 230 231 // NoStack is a sentinel value that can be compared against any Stack value, indicating 232 // a lack of a stack trace. 233 var NoStack = Stack{} 234 235 // StackFrame represents a single frame of a stack. 236 type StackFrame struct { 237 // PC is the program counter of the function call if this 238 // is not a leaf frame. If it's a leaf frame, it's the point 239 // at which the stack trace was taken. 240 PC uint64 241 242 // Func is the name of the function this frame maps to. 243 Func string 244 245 // File is the file which contains the source code of Func. 246 File string 247 248 // Line is the line number within File which maps to PC. 249 Line uint64 250 } 251 252 // Event represents a single event in the trace. 253 type Event struct { 254 table *evTable 255 ctx schedCtx 256 base baseEvent 257 } 258 259 // Kind returns the kind of event that this is. 260 func (e Event) Kind() EventKind 261 262 // Time returns the timestamp of the event. 263 func (e Event) Time() Time 264 265 // Goroutine returns the ID of the goroutine that was executing when 266 // this event happened. It describes part of the execution context 267 // for this event. 268 // 269 // Note that for goroutine state transitions this always refers to the 270 // state before the transition. For example, if a goroutine is just 271 // starting to run on this thread and/or proc, then this will return 272 // NoGoroutine. In this case, the goroutine starting to run will be 273 // can be found at Event.StateTransition().Resource. 274 func (e Event) Goroutine() GoID 275 276 // Proc returns the ID of the proc this event event pertains to. 277 // 278 // Note that for proc state transitions this always refers to the 279 // state before the transition. For example, if a proc is just 280 // starting to run on this thread, then this will return NoProc. 281 func (e Event) Proc() ProcID 282 283 // Thread returns the ID of the thread this event pertains to. 284 // 285 // Note that for thread state transitions this always refers to the 286 // state before the transition. For example, if a thread is just 287 // starting to run, then this will return NoThread. 288 // 289 // Note: tracking thread state is not currently supported, so this 290 // will always return a valid thread ID. However thread state transitions 291 // may be tracked in the future, and callers must be robust to this 292 // possibility. 293 func (e Event) Thread() ThreadID 294 295 // Stack returns a handle to a stack associated with the event. 296 // 297 // This represents a stack trace at the current moment in time for 298 // the current execution context. 299 func (e Event) Stack() Stack 300 301 // Metric returns details about a Metric event. 302 // 303 // Panics if Kind != EventMetric. 304 func (e Event) Metric() Metric 305 306 // Label returns details about a Label event. 307 // 308 // Panics if Kind != EventLabel. 309 func (e Event) Label() Label 310 311 // Range returns details about an EventRangeBegin, EventRangeActive, or EventRangeEnd event. 312 // 313 // Panics if Kind != EventRangeBegin, Kind != EventRangeActive, and Kind != EventRangeEnd. 314 func (e Event) Range() Range 315 316 // RangeAttributes returns attributes for a completed range. 317 // 318 // Panics if Kind != EventRangeEnd. 319 func (e Event) RangeAttributes() []RangeAttribute 320 321 // Task returns details about a TaskBegin or TaskEnd event. 322 // 323 // Panics if Kind != EventTaskBegin and Kind != EventTaskEnd. 324 func (e Event) Task() Task 325 326 // Region returns details about a RegionBegin or RegionEnd event. 327 // 328 // Panics if Kind != EventRegionBegin and Kind != EventRegionEnd. 329 func (e Event) Region() Region 330 331 // Log returns details about a Log event. 332 // 333 // Panics if Kind != EventLog. 334 func (e Event) Log() Log 335 336 // StateTransition returns details about a StateTransition event. 337 // 338 // Panics if Kind != EventStateTransition. 339 func (e Event) StateTransition() StateTransition 340 341 // String returns the event as a human-readable string. 342 // 343 // The format of the string is intended for debugging and is subject to change. 344 func (e Event) String() string