github.com/ice-blockchain/go/src@v0.0.0-20240403114104-1564d284e521/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 "fmt" 9 "math" 10 "strings" 11 "time" 12 13 "internal/trace/v2/event" 14 "internal/trace/v2/event/go122" 15 "internal/trace/v2/version" 16 ) 17 18 // EventKind indicates the kind of event this is. 19 // 20 // Use this information to obtain a more specific event that 21 // allows access to more detailed information. 22 type EventKind uint16 23 24 const ( 25 EventBad EventKind = iota 26 27 // EventKindSync is an event that indicates a global synchronization 28 // point in the trace. At the point of a sync event, the 29 // trace reader can be certain that all resources (e.g. threads, 30 // goroutines) that have existed until that point have been enumerated. 31 EventSync 32 33 // EventMetric is an event that represents the value of a metric at 34 // a particular point in time. 35 EventMetric 36 37 // EventLabel attaches a label to a resource. 38 EventLabel 39 40 // EventStackSample represents an execution sample, indicating what a 41 // thread/proc/goroutine was doing at a particular point in time via 42 // its backtrace. 43 // 44 // Note: Samples should be considered a close approximation of 45 // what a thread/proc/goroutine was executing at a given point in time. 46 // These events may slightly contradict the situation StateTransitions 47 // describe, so they should only be treated as a best-effort annotation. 48 EventStackSample 49 50 // EventRangeBegin and EventRangeEnd are a pair of generic events representing 51 // a special range of time. Ranges are named and scoped to some resource 52 // (identified via ResourceKind). A range that has begun but has not ended 53 // is considered active. 54 // 55 // EvRangeBegin and EvRangeEnd will share the same name, and an End will always 56 // follow a Begin on the same instance of the resource. The associated 57 // resource ID can be obtained from the Event. ResourceNone indicates the 58 // range is globally scoped. That is, any goroutine/proc/thread can start or 59 // stop, but only one such range may be active at any given time. 60 // 61 // EventRangeActive is like EventRangeBegin, but indicates that the range was 62 // already active. In this case, the resource referenced may not be in the current 63 // context. 64 EventRangeBegin 65 EventRangeActive 66 EventRangeEnd 67 68 // EvTaskBegin and EvTaskEnd are a pair of events representing a runtime/trace.Task. 69 EventTaskBegin 70 EventTaskEnd 71 72 // EventRegionBegin and EventRegionEnd are a pair of events represent a runtime/trace.Region. 73 EventRegionBegin 74 EventRegionEnd 75 76 // EventLog represents a runtime/trace.Log call. 77 EventLog 78 79 // Transitions in state for some resource. 80 EventStateTransition 81 ) 82 83 // String returns a string form of the EventKind. 84 func (e EventKind) String() string { 85 if int(e) >= len(eventKindStrings) { 86 return eventKindStrings[0] 87 } 88 return eventKindStrings[e] 89 } 90 91 var eventKindStrings = [...]string{ 92 EventBad: "Bad", 93 EventSync: "Sync", 94 EventMetric: "Metric", 95 EventLabel: "Label", 96 EventStackSample: "StackSample", 97 EventRangeBegin: "RangeBegin", 98 EventRangeActive: "RangeActive", 99 EventRangeEnd: "RangeEnd", 100 EventTaskBegin: "TaskBegin", 101 EventTaskEnd: "TaskEnd", 102 EventRegionBegin: "RegionBegin", 103 EventRegionEnd: "RegionEnd", 104 EventLog: "Log", 105 EventStateTransition: "StateTransition", 106 } 107 108 const maxTime = Time(math.MaxInt64) 109 110 // Time is a timestamp in nanoseconds. 111 // 112 // It corresponds to the monotonic clock on the platform that the 113 // trace was taken, and so is possible to correlate with timestamps 114 // for other traces taken on the same machine using the same clock 115 // (i.e. no reboots in between). 116 // 117 // The actual absolute value of the timestamp is only meaningful in 118 // relation to other timestamps from the same clock. 119 // 120 // BUG: Timestamps coming from traces on Windows platforms are 121 // only comparable with timestamps from the same trace. Timestamps 122 // across traces cannot be compared, because the system clock is 123 // not used as of Go 1.22. 124 // 125 // BUG: Traces produced by Go versions 1.21 and earlier cannot be 126 // compared with timestamps from other traces taken on the same 127 // machine. This is because the system clock was not used at all 128 // to collect those timestamps. 129 type Time int64 130 131 // Sub subtracts t0 from t, returning the duration in nanoseconds. 132 func (t Time) Sub(t0 Time) time.Duration { 133 return time.Duration(int64(t) - int64(t0)) 134 } 135 136 // Metric provides details about a Metric event. 137 type Metric struct { 138 // Name is the name of the sampled metric. 139 // 140 // Names follow the same convention as metric names in the 141 // runtime/metrics package, meaning they include the unit. 142 // Names that match with the runtime/metrics package represent 143 // the same quantity. Note that this corresponds to the 144 // runtime/metrics package for the Go version this trace was 145 // collected for. 146 Name string 147 148 // Value is the sampled value of the metric. 149 // 150 // The Value's Kind is tied to the name of the metric, and so is 151 // guaranteed to be the same for metric samples for the same metric. 152 Value Value 153 } 154 155 // Label provides details about a Label event. 156 type Label struct { 157 // Label is the label applied to some resource. 158 Label string 159 160 // Resource is the resource to which this label should be applied. 161 Resource ResourceID 162 } 163 164 // Range provides details about a Range event. 165 type Range struct { 166 // Name is a human-readable name for the range. 167 // 168 // This name can be used to identify the end of the range for the resource 169 // its scoped to, because only one of each type of range may be active on 170 // a particular resource. The relevant resource should be obtained from the 171 // Event that produced these details. The corresponding RangeEnd will have 172 // an identical name. 173 Name string 174 175 // Scope is the resource that the range is scoped to. 176 // 177 // For example, a ResourceGoroutine scope means that the same goroutine 178 // must have a start and end for the range, and that goroutine can only 179 // have one range of a particular name active at any given time. The 180 // ID that this range is scoped to may be obtained via Event.Goroutine. 181 // 182 // The ResourceNone scope means that the range is globally scoped. As a 183 // result, any goroutine/proc/thread may start or end the range, and only 184 // one such named range may be active globally at any given time. 185 // 186 // For RangeBegin and RangeEnd events, this will always reference some 187 // resource ID in the current execution context. For RangeActive events, 188 // this may reference a resource not in the current context. Prefer Scope 189 // over the current execution context. 190 Scope ResourceID 191 } 192 193 // RangeAttributes provides attributes about a completed Range. 194 type RangeAttribute struct { 195 // Name is the human-readable name for the range. 196 Name string 197 198 // Value is the value of the attribute. 199 Value Value 200 } 201 202 // TaskID is the internal ID of a task used to disambiguate tasks (even if they 203 // are of the same type). 204 type TaskID uint64 205 206 const ( 207 // NoTask indicates the lack of a task. 208 NoTask = TaskID(^uint64(0)) 209 210 // BackgroundTask is the global task that events are attached to if there was 211 // no other task in the context at the point the event was emitted. 212 BackgroundTask = TaskID(0) 213 ) 214 215 // Task provides details about a Task event. 216 type Task struct { 217 // ID is a unique identifier for the task. 218 // 219 // This can be used to associate the beginning of a task with its end. 220 ID TaskID 221 222 // ParentID is the ID of the parent task. 223 Parent TaskID 224 225 // Type is the taskType that was passed to runtime/trace.NewTask. 226 // 227 // May be "" if a task's TaskBegin event isn't present in the trace. 228 Type string 229 } 230 231 // Region provides details about a Region event. 232 type Region struct { 233 // Task is the ID of the task this region is associated with. 234 Task TaskID 235 236 // Type is the regionType that was passed to runtime/trace.StartRegion or runtime/trace.WithRegion. 237 Type string 238 } 239 240 // Log provides details about a Log event. 241 type Log struct { 242 // Task is the ID of the task this region is associated with. 243 Task TaskID 244 245 // Category is the category that was passed to runtime/trace.Log or runtime/trace.Logf. 246 Category string 247 248 // Message is the message that was passed to runtime/trace.Log or runtime/trace.Logf. 249 Message string 250 } 251 252 // Stack represents a stack. It's really a handle to a stack and it's trivially comparable. 253 // 254 // If two Stacks are equal then their Frames are guaranteed to be identical. If they are not 255 // equal, however, their Frames may still be equal. 256 type Stack struct { 257 table *evTable 258 id stackID 259 } 260 261 // Frames is an iterator over the frames in a Stack. 262 func (s Stack) Frames(yield func(f StackFrame) bool) bool { 263 if s.id == 0 { 264 return true 265 } 266 stk := s.table.stacks.mustGet(s.id) 267 for _, pc := range stk.pcs { 268 f := s.table.pcs[pc] 269 sf := StackFrame{ 270 PC: f.pc, 271 Func: s.table.strings.mustGet(f.funcID), 272 File: s.table.strings.mustGet(f.fileID), 273 Line: f.line, 274 } 275 if !yield(sf) { 276 return false 277 } 278 } 279 return true 280 } 281 282 // NoStack is a sentinel value that can be compared against any Stack value, indicating 283 // a lack of a stack trace. 284 var NoStack = Stack{} 285 286 // StackFrame represents a single frame of a stack. 287 type StackFrame struct { 288 // PC is the program counter of the function call if this 289 // is not a leaf frame. If it's a leaf frame, it's the point 290 // at which the stack trace was taken. 291 PC uint64 292 293 // Func is the name of the function this frame maps to. 294 Func string 295 296 // File is the file which contains the source code of Func. 297 File string 298 299 // Line is the line number within File which maps to PC. 300 Line uint64 301 } 302 303 // Event represents a single event in the trace. 304 type Event struct { 305 table *evTable 306 ctx schedCtx 307 base baseEvent 308 } 309 310 // Kind returns the kind of event that this is. 311 func (e Event) Kind() EventKind { 312 return go122Type2Kind[e.base.typ] 313 } 314 315 // Time returns the timestamp of the event. 316 func (e Event) Time() Time { 317 return e.base.time 318 } 319 320 // Goroutine returns the ID of the goroutine that was executing when 321 // this event happened. It describes part of the execution context 322 // for this event. 323 // 324 // Note that for goroutine state transitions this always refers to the 325 // state before the transition. For example, if a goroutine is just 326 // starting to run on this thread and/or proc, then this will return 327 // NoGoroutine. In this case, the goroutine starting to run will be 328 // can be found at Event.StateTransition().Resource. 329 func (e Event) Goroutine() GoID { 330 return e.ctx.G 331 } 332 333 // Proc returns the ID of the proc this event event pertains to. 334 // 335 // Note that for proc state transitions this always refers to the 336 // state before the transition. For example, if a proc is just 337 // starting to run on this thread, then this will return NoProc. 338 func (e Event) Proc() ProcID { 339 return e.ctx.P 340 } 341 342 // Thread returns the ID of the thread this event pertains to. 343 // 344 // Note that for thread state transitions this always refers to the 345 // state before the transition. For example, if a thread is just 346 // starting to run, then this will return NoThread. 347 // 348 // Note: tracking thread state is not currently supported, so this 349 // will always return a valid thread ID. However thread state transitions 350 // may be tracked in the future, and callers must be robust to this 351 // possibility. 352 func (e Event) Thread() ThreadID { 353 return e.ctx.M 354 } 355 356 // Stack returns a handle to a stack associated with the event. 357 // 358 // This represents a stack trace at the current moment in time for 359 // the current execution context. 360 func (e Event) Stack() Stack { 361 if e.base.typ == evSync { 362 return NoStack 363 } 364 if e.base.typ == go122.EvCPUSample { 365 return Stack{table: e.table, id: stackID(e.base.args[0])} 366 } 367 spec := go122.Specs()[e.base.typ] 368 if len(spec.StackIDs) == 0 { 369 return NoStack 370 } 371 // The stack for the main execution context is always the 372 // first stack listed in StackIDs. Subtract one from this 373 // because we've peeled away the timestamp argument. 374 id := stackID(e.base.args[spec.StackIDs[0]-1]) 375 if id == 0 { 376 return NoStack 377 } 378 return Stack{table: e.table, id: id} 379 } 380 381 // Metric returns details about a Metric event. 382 // 383 // Panics if Kind != EventMetric. 384 func (e Event) Metric() Metric { 385 if e.Kind() != EventMetric { 386 panic("Metric called on non-Metric event") 387 } 388 var m Metric 389 switch e.base.typ { 390 case go122.EvProcsChange: 391 m.Name = "/sched/gomaxprocs:threads" 392 m.Value = Value{kind: ValueUint64, scalar: e.base.args[0]} 393 case go122.EvHeapAlloc: 394 m.Name = "/memory/classes/heap/objects:bytes" 395 m.Value = Value{kind: ValueUint64, scalar: e.base.args[0]} 396 case go122.EvHeapGoal: 397 m.Name = "/gc/heap/goal:bytes" 398 m.Value = Value{kind: ValueUint64, scalar: e.base.args[0]} 399 default: 400 panic(fmt.Sprintf("internal error: unexpected event type for Metric kind: %s", go122.EventString(e.base.typ))) 401 } 402 return m 403 } 404 405 // Label returns details about a Label event. 406 // 407 // Panics if Kind != EventLabel. 408 func (e Event) Label() Label { 409 if e.Kind() != EventLabel { 410 panic("Label called on non-Label event") 411 } 412 if e.base.typ != go122.EvGoLabel { 413 panic(fmt.Sprintf("internal error: unexpected event type for Label kind: %s", go122.EventString(e.base.typ))) 414 } 415 return Label{ 416 Label: e.table.strings.mustGet(stringID(e.base.args[0])), 417 Resource: ResourceID{Kind: ResourceGoroutine, id: int64(e.ctx.G)}, 418 } 419 } 420 421 // Range returns details about an EventRangeBegin, EventRangeActive, or EventRangeEnd event. 422 // 423 // Panics if Kind != EventRangeBegin, Kind != EventRangeActive, and Kind != EventRangeEnd. 424 func (e Event) Range() Range { 425 if kind := e.Kind(); kind != EventRangeBegin && kind != EventRangeActive && kind != EventRangeEnd { 426 panic("Range called on non-Range event") 427 } 428 var r Range 429 switch e.base.typ { 430 case go122.EvSTWBegin, go122.EvSTWEnd: 431 // N.B. ordering.advance smuggles in the STW reason as e.base.args[0] 432 // for go122.EvSTWEnd (it's already there for Begin). 433 r.Name = "stop-the-world (" + e.table.strings.mustGet(stringID(e.base.args[0])) + ")" 434 r.Scope = ResourceID{Kind: ResourceGoroutine, id: int64(e.Goroutine())} 435 case go122.EvGCBegin, go122.EvGCActive, go122.EvGCEnd: 436 r.Name = "GC concurrent mark phase" 437 r.Scope = ResourceID{Kind: ResourceNone} 438 case go122.EvGCSweepBegin, go122.EvGCSweepActive, go122.EvGCSweepEnd: 439 r.Name = "GC incremental sweep" 440 r.Scope = ResourceID{Kind: ResourceProc} 441 if e.base.typ == go122.EvGCSweepActive { 442 r.Scope.id = int64(e.base.args[0]) 443 } else { 444 r.Scope.id = int64(e.Proc()) 445 } 446 r.Scope.id = int64(e.Proc()) 447 case go122.EvGCMarkAssistBegin, go122.EvGCMarkAssistActive, go122.EvGCMarkAssistEnd: 448 r.Name = "GC mark assist" 449 r.Scope = ResourceID{Kind: ResourceGoroutine} 450 if e.base.typ == go122.EvGCMarkAssistActive { 451 r.Scope.id = int64(e.base.args[0]) 452 } else { 453 r.Scope.id = int64(e.Goroutine()) 454 } 455 default: 456 panic(fmt.Sprintf("internal error: unexpected event type for Range kind: %s", go122.EventString(e.base.typ))) 457 } 458 return r 459 } 460 461 // RangeAttributes returns attributes for a completed range. 462 // 463 // Panics if Kind != EventRangeEnd. 464 func (e Event) RangeAttributes() []RangeAttribute { 465 if e.Kind() != EventRangeEnd { 466 panic("Range called on non-Range event") 467 } 468 if e.base.typ != go122.EvGCSweepEnd { 469 return nil 470 } 471 return []RangeAttribute{ 472 { 473 Name: "bytes swept", 474 Value: Value{kind: ValueUint64, scalar: e.base.args[0]}, 475 }, 476 { 477 Name: "bytes reclaimed", 478 Value: Value{kind: ValueUint64, scalar: e.base.args[1]}, 479 }, 480 } 481 } 482 483 // Task returns details about a TaskBegin or TaskEnd event. 484 // 485 // Panics if Kind != EventTaskBegin and Kind != EventTaskEnd. 486 func (e Event) Task() Task { 487 if kind := e.Kind(); kind != EventTaskBegin && kind != EventTaskEnd { 488 panic("Task called on non-Task event") 489 } 490 parentID := NoTask 491 var typ string 492 switch e.base.typ { 493 case go122.EvUserTaskBegin: 494 parentID = TaskID(e.base.args[1]) 495 typ = e.table.strings.mustGet(stringID(e.base.args[2])) 496 case go122.EvUserTaskEnd: 497 parentID = TaskID(e.base.extra(version.Go122)[0]) 498 typ = e.table.getExtraString(extraStringID(e.base.extra(version.Go122)[1])) 499 default: 500 panic(fmt.Sprintf("internal error: unexpected event type for Task kind: %s", go122.EventString(e.base.typ))) 501 } 502 return Task{ 503 ID: TaskID(e.base.args[0]), 504 Parent: parentID, 505 Type: typ, 506 } 507 } 508 509 // Region returns details about a RegionBegin or RegionEnd event. 510 // 511 // Panics if Kind != EventRegionBegin and Kind != EventRegionEnd. 512 func (e Event) Region() Region { 513 if kind := e.Kind(); kind != EventRegionBegin && kind != EventRegionEnd { 514 panic("Region called on non-Region event") 515 } 516 if e.base.typ != go122.EvUserRegionBegin && e.base.typ != go122.EvUserRegionEnd { 517 panic(fmt.Sprintf("internal error: unexpected event type for Region kind: %s", go122.EventString(e.base.typ))) 518 } 519 return Region{ 520 Task: TaskID(e.base.args[0]), 521 Type: e.table.strings.mustGet(stringID(e.base.args[1])), 522 } 523 } 524 525 // Log returns details about a Log event. 526 // 527 // Panics if Kind != EventLog. 528 func (e Event) Log() Log { 529 if e.Kind() != EventLog { 530 panic("Log called on non-Log event") 531 } 532 if e.base.typ != go122.EvUserLog { 533 panic(fmt.Sprintf("internal error: unexpected event type for Log kind: %s", go122.EventString(e.base.typ))) 534 } 535 return Log{ 536 Task: TaskID(e.base.args[0]), 537 Category: e.table.strings.mustGet(stringID(e.base.args[1])), 538 Message: e.table.strings.mustGet(stringID(e.base.args[2])), 539 } 540 } 541 542 // StateTransition returns details about a StateTransition event. 543 // 544 // Panics if Kind != EventStateTransition. 545 func (e Event) StateTransition() StateTransition { 546 if e.Kind() != EventStateTransition { 547 panic("StateTransition called on non-StateTransition event") 548 } 549 var s StateTransition 550 switch e.base.typ { 551 case go122.EvProcStart: 552 s = procStateTransition(ProcID(e.base.args[0]), ProcIdle, ProcRunning) 553 case go122.EvProcStop: 554 s = procStateTransition(e.ctx.P, ProcRunning, ProcIdle) 555 case go122.EvProcSteal: 556 // N.B. ordering.advance populates e.base.extra. 557 beforeState := ProcRunning 558 if go122.ProcStatus(e.base.extra(version.Go122)[0]) == go122.ProcSyscallAbandoned { 559 // We've lost information because this ProcSteal advanced on a 560 // SyscallAbandoned state. Treat the P as idle because ProcStatus 561 // treats SyscallAbandoned as Idle. Otherwise we'll have an invalid 562 // transition. 563 beforeState = ProcIdle 564 } 565 s = procStateTransition(ProcID(e.base.args[0]), beforeState, ProcIdle) 566 case go122.EvProcStatus: 567 // N.B. ordering.advance populates e.base.extra. 568 s = procStateTransition(ProcID(e.base.args[0]), ProcState(e.base.extra(version.Go122)[0]), go122ProcStatus2ProcState[e.base.args[1]]) 569 case go122.EvGoCreate, go122.EvGoCreateBlocked: 570 status := GoRunnable 571 if e.base.typ == go122.EvGoCreateBlocked { 572 status = GoWaiting 573 } 574 s = goStateTransition(GoID(e.base.args[0]), GoNotExist, status) 575 s.Stack = Stack{table: e.table, id: stackID(e.base.args[1])} 576 case go122.EvGoCreateSyscall: 577 s = goStateTransition(GoID(e.base.args[0]), GoNotExist, GoSyscall) 578 case go122.EvGoStart: 579 s = goStateTransition(GoID(e.base.args[0]), GoRunnable, GoRunning) 580 case go122.EvGoDestroy: 581 s = goStateTransition(e.ctx.G, GoRunning, GoNotExist) 582 s.Stack = e.Stack() // This event references the resource the event happened on. 583 case go122.EvGoDestroySyscall: 584 s = goStateTransition(e.ctx.G, GoSyscall, GoNotExist) 585 case go122.EvGoStop: 586 s = goStateTransition(e.ctx.G, GoRunning, GoRunnable) 587 s.Reason = e.table.strings.mustGet(stringID(e.base.args[0])) 588 s.Stack = e.Stack() // This event references the resource the event happened on. 589 case go122.EvGoBlock: 590 s = goStateTransition(e.ctx.G, GoRunning, GoWaiting) 591 s.Reason = e.table.strings.mustGet(stringID(e.base.args[0])) 592 s.Stack = e.Stack() // This event references the resource the event happened on. 593 case go122.EvGoUnblock, go122.EvGoSwitch, go122.EvGoSwitchDestroy: 594 // N.B. GoSwitch and GoSwitchDestroy both emit additional events, but 595 // the first thing they both do is unblock the goroutine they name, 596 // identically to an unblock event (even their arguments match). 597 s = goStateTransition(GoID(e.base.args[0]), GoWaiting, GoRunnable) 598 case go122.EvGoSyscallBegin: 599 s = goStateTransition(e.ctx.G, GoRunning, GoSyscall) 600 s.Stack = e.Stack() // This event references the resource the event happened on. 601 case go122.EvGoSyscallEnd: 602 s = goStateTransition(e.ctx.G, GoSyscall, GoRunning) 603 s.Stack = e.Stack() // This event references the resource the event happened on. 604 case go122.EvGoSyscallEndBlocked: 605 s = goStateTransition(e.ctx.G, GoSyscall, GoRunnable) 606 s.Stack = e.Stack() // This event references the resource the event happened on. 607 case go122.EvGoStatus: 608 // N.B. ordering.advance populates e.base.extra. 609 s = goStateTransition(GoID(e.base.args[0]), GoState(e.base.extra(version.Go122)[0]), go122GoStatus2GoState[e.base.args[2]]) 610 default: 611 panic(fmt.Sprintf("internal error: unexpected event type for StateTransition kind: %s", go122.EventString(e.base.typ))) 612 } 613 return s 614 } 615 616 const evSync = ^event.Type(0) 617 618 var go122Type2Kind = [...]EventKind{ 619 go122.EvCPUSample: EventStackSample, 620 go122.EvProcsChange: EventMetric, 621 go122.EvProcStart: EventStateTransition, 622 go122.EvProcStop: EventStateTransition, 623 go122.EvProcSteal: EventStateTransition, 624 go122.EvProcStatus: EventStateTransition, 625 go122.EvGoCreate: EventStateTransition, 626 go122.EvGoCreateSyscall: EventStateTransition, 627 go122.EvGoStart: EventStateTransition, 628 go122.EvGoDestroy: EventStateTransition, 629 go122.EvGoDestroySyscall: EventStateTransition, 630 go122.EvGoStop: EventStateTransition, 631 go122.EvGoBlock: EventStateTransition, 632 go122.EvGoUnblock: EventStateTransition, 633 go122.EvGoSyscallBegin: EventStateTransition, 634 go122.EvGoSyscallEnd: EventStateTransition, 635 go122.EvGoSyscallEndBlocked: EventStateTransition, 636 go122.EvGoStatus: EventStateTransition, 637 go122.EvSTWBegin: EventRangeBegin, 638 go122.EvSTWEnd: EventRangeEnd, 639 go122.EvGCActive: EventRangeActive, 640 go122.EvGCBegin: EventRangeBegin, 641 go122.EvGCEnd: EventRangeEnd, 642 go122.EvGCSweepActive: EventRangeActive, 643 go122.EvGCSweepBegin: EventRangeBegin, 644 go122.EvGCSweepEnd: EventRangeEnd, 645 go122.EvGCMarkAssistActive: EventRangeActive, 646 go122.EvGCMarkAssistBegin: EventRangeBegin, 647 go122.EvGCMarkAssistEnd: EventRangeEnd, 648 go122.EvHeapAlloc: EventMetric, 649 go122.EvHeapGoal: EventMetric, 650 go122.EvGoLabel: EventLabel, 651 go122.EvUserTaskBegin: EventTaskBegin, 652 go122.EvUserTaskEnd: EventTaskEnd, 653 go122.EvUserRegionBegin: EventRegionBegin, 654 go122.EvUserRegionEnd: EventRegionEnd, 655 go122.EvUserLog: EventLog, 656 go122.EvGoSwitch: EventStateTransition, 657 go122.EvGoSwitchDestroy: EventStateTransition, 658 go122.EvGoCreateBlocked: EventStateTransition, 659 evSync: EventSync, 660 } 661 662 var go122GoStatus2GoState = [...]GoState{ 663 go122.GoRunnable: GoRunnable, 664 go122.GoRunning: GoRunning, 665 go122.GoWaiting: GoWaiting, 666 go122.GoSyscall: GoSyscall, 667 } 668 669 var go122ProcStatus2ProcState = [...]ProcState{ 670 go122.ProcRunning: ProcRunning, 671 go122.ProcIdle: ProcIdle, 672 go122.ProcSyscall: ProcRunning, 673 go122.ProcSyscallAbandoned: ProcIdle, 674 } 675 676 // String returns the event as a human-readable string. 677 // 678 // The format of the string is intended for debugging and is subject to change. 679 func (e Event) String() string { 680 var sb strings.Builder 681 fmt.Fprintf(&sb, "M=%d P=%d G=%d", e.Thread(), e.Proc(), e.Goroutine()) 682 fmt.Fprintf(&sb, " %s Time=%d", e.Kind(), e.Time()) 683 // Kind-specific fields. 684 switch kind := e.Kind(); kind { 685 case EventMetric: 686 m := e.Metric() 687 fmt.Fprintf(&sb, " Name=%q Value=%s", m.Name, valueAsString(m.Value)) 688 case EventLabel: 689 l := e.Label() 690 fmt.Fprintf(&sb, " Label=%q Resource=%s", l.Label, l.Resource) 691 case EventRangeBegin, EventRangeActive, EventRangeEnd: 692 r := e.Range() 693 fmt.Fprintf(&sb, " Name=%q Scope=%s", r.Name, r.Scope) 694 if kind == EventRangeEnd { 695 fmt.Fprintf(&sb, " Attributes=[") 696 for i, attr := range e.RangeAttributes() { 697 if i != 0 { 698 fmt.Fprintf(&sb, " ") 699 } 700 fmt.Fprintf(&sb, "%q=%s", attr.Name, valueAsString(attr.Value)) 701 } 702 fmt.Fprintf(&sb, "]") 703 } 704 case EventTaskBegin, EventTaskEnd: 705 t := e.Task() 706 fmt.Fprintf(&sb, " ID=%d Parent=%d Type=%q", t.ID, t.Parent, t.Type) 707 case EventRegionBegin, EventRegionEnd: 708 r := e.Region() 709 fmt.Fprintf(&sb, " Task=%d Type=%q", r.Task, r.Type) 710 case EventLog: 711 l := e.Log() 712 fmt.Fprintf(&sb, " Task=%d Category=%q Message=%q", l.Task, l.Category, l.Message) 713 case EventStateTransition: 714 s := e.StateTransition() 715 fmt.Fprintf(&sb, " Resource=%s Reason=%q", s.Resource, s.Reason) 716 switch s.Resource.Kind { 717 case ResourceGoroutine: 718 id := s.Resource.Goroutine() 719 old, new := s.Goroutine() 720 fmt.Fprintf(&sb, " GoID=%d %s->%s", id, old, new) 721 case ResourceProc: 722 id := s.Resource.Proc() 723 old, new := s.Proc() 724 fmt.Fprintf(&sb, " ProcID=%d %s->%s", id, old, new) 725 } 726 if s.Stack != NoStack { 727 fmt.Fprintln(&sb) 728 fmt.Fprintln(&sb, "TransitionStack=") 729 s.Stack.Frames(func(f StackFrame) bool { 730 fmt.Fprintf(&sb, "\t%s @ 0x%x\n", f.Func, f.PC) 731 fmt.Fprintf(&sb, "\t\t%s:%d\n", f.File, f.Line) 732 return true 733 }) 734 } 735 } 736 if stk := e.Stack(); stk != NoStack { 737 fmt.Fprintln(&sb) 738 fmt.Fprintln(&sb, "Stack=") 739 stk.Frames(func(f StackFrame) bool { 740 fmt.Fprintf(&sb, "\t%s @ 0x%x\n", f.Func, f.PC) 741 fmt.Fprintf(&sb, "\t\t%s:%d\n", f.File, f.Line) 742 return true 743 }) 744 } 745 return sb.String() 746 } 747 748 // validateTableIDs checks to make sure lookups in e.table 749 // will work. 750 func (e Event) validateTableIDs() error { 751 if e.base.typ == evSync { 752 return nil 753 } 754 spec := go122.Specs()[e.base.typ] 755 756 // Check stacks. 757 for _, i := range spec.StackIDs { 758 id := stackID(e.base.args[i-1]) 759 _, ok := e.table.stacks.get(id) 760 if !ok { 761 return fmt.Errorf("found invalid stack ID %d for event %s", id, spec.Name) 762 } 763 } 764 // N.B. Strings referenced by stack frames are validated 765 // early on, when reading the stacks in to begin with. 766 767 // Check strings. 768 for _, i := range spec.StringIDs { 769 id := stringID(e.base.args[i-1]) 770 _, ok := e.table.strings.get(id) 771 if !ok { 772 return fmt.Errorf("found invalid string ID %d for event %s", id, spec.Name) 773 } 774 } 775 return nil 776 } 777 778 func syncEvent(table *evTable, ts Time) Event { 779 return Event{ 780 table: table, 781 ctx: schedCtx{ 782 G: NoGoroutine, 783 P: NoProc, 784 M: NoThread, 785 }, 786 base: baseEvent{ 787 typ: evSync, 788 time: ts, 789 }, 790 } 791 }