diff options
| author | Hana Kim <hakim@google.com> | 2018-04-19 14:58:42 -0400 |
|---|---|---|
| committer | Hyang-Ah Hana Kim <hyangah@gmail.com> | 2018-04-24 16:33:15 +0000 |
| commit | c2d10243688194346f660591fe4159e30a8d20ec (patch) | |
| tree | 9c3bf5c9b2f307ce2a655ef97fac72f2e1fe430f /src/internal/trace | |
| parent | fb017c60bc60f8df771ac2a9119ec55ea915929c (diff) | |
| download | go-c2d10243688194346f660591fe4159e30a8d20ec.tar.xz | |
runtime/trace: rename "Span" with "Region"
"Span" is a commonly used term in many distributed tracing systems
(Dapper, OpenCensus, OpenTracing, ...). They use it to refer to a
period of time, not necessarily tied into execution of underlying
processor, thread, or goroutine, unlike the "Span" of runtime/trace
package.
Since distributed tracing and go runtime execution tracing are
already similar enough to cause confusion, this CL attempts to avoid
using the same word if possible.
"Region" is being used in a certain tracing system to refer to a code
region which is pretty close to what runtime/trace.Span currently
refers to. So, replace that.
https://software.intel.com/en-us/itc-user-and-reference-guide-defining-and-recording-functions-or-regions
This CL also tweaks APIs a bit based on jbd and heschi's comments:
NewContext -> NewTask
and it now returns a Task object that exports End method.
StartSpan -> StartRegion
and it now returns a Region object that exports End method.
Also, changed WithSpan to WithRegion and it now takes func() with no
context. Another thought is to get rid of WithRegion. It is a nice
concept but in practice, it seems problematic (a lot of code churn,
and polluting stack trace). Already, the tracing concept is very low
level, and we hope this API to be used with great care.
Recommended usage will be
defer trace.StartRegion(ctx, "someRegion").End()
Left old APIs untouched in this CL. Once the usage of them are cleaned
up, they will be removed in a separate CL.
Change-Id: I73880635e437f3aad51314331a035dd1459b9f3a
Reviewed-on: https://go-review.googlesource.com/108296
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: JBD <jbd@google.com>
Diffstat (limited to 'src/internal/trace')
| -rw-r--r-- | src/internal/trace/goroutines.go | 72 | ||||
| -rw-r--r-- | src/internal/trace/parser.go | 48 |
2 files changed, 60 insertions, 60 deletions
diff --git a/src/internal/trace/goroutines.go b/src/internal/trace/goroutines.go index 8938936a16..2d7d3aa3ae 100644 --- a/src/internal/trace/goroutines.go +++ b/src/internal/trace/goroutines.go @@ -15,8 +15,8 @@ type GDesc struct { StartTime int64 EndTime int64 - // List of spans in the goroutine, sorted based on the start time. - Spans []*UserSpanDesc + // List of regions in the goroutine, sorted based on the start time. + Regions []*UserRegionDesc // Statistics of execution time during the goroutine execution. GExecutionStat @@ -24,20 +24,20 @@ type GDesc struct { *gdesc // private part. } -// UserSpanDesc represents a span and goroutine execution stats -// while the span was active. -type UserSpanDesc struct { +// UserRegionDesc represents a region and goroutine execution stats +// while the region was active. +type UserRegionDesc struct { TaskID uint64 Name string - // Span start event. Normally EvUserSpan start event or nil, - // but can be EvGoCreate event if the span is a synthetic - // span representing task inheritance from the parent goroutine. + // Region start event. Normally EvUserRegion start event or nil, + // but can be EvGoCreate event if the region is a synthetic + // region representing task inheritance from the parent goroutine. Start *Event - // Span end event. Normally EvUserSpan end event or nil, + // Region end event. Normally EvUserRegion end event or nil, // but can be EvGoStop or EvGoEnd event if the goroutine - // terminated without explicitely ending the span. + // terminated without explicitely ending the region. End *Event GExecutionStat @@ -118,7 +118,7 @@ func (g *GDesc) snapshotStat(lastTs, activeGCStartTime int64) (ret GExecutionSta // finalize is called when processing a goroutine end event or at // the end of trace processing. This finalizes the execution stat -// and any active spans in the goroutine, in which case trigger is nil. +// and any active regions in the goroutine, in which case trigger is nil. func (g *GDesc) finalize(lastTs, activeGCStartTime int64, trigger *Event) { if trigger != nil { g.EndTime = trigger.Ts @@ -126,10 +126,10 @@ func (g *GDesc) finalize(lastTs, activeGCStartTime int64, trigger *Event) { finalStat := g.snapshotStat(lastTs, activeGCStartTime) g.GExecutionStat = finalStat - for _, s := range g.activeSpans { + for _, s := range g.activeRegions { s.End = trigger s.GExecutionStat = finalStat.sub(s.GExecutionStat) - g.Spans = append(g.Spans, s) + g.Regions = append(g.Regions, s) } *(g.gdesc) = gdesc{} } @@ -144,7 +144,7 @@ type gdesc struct { blockGCTime int64 blockSchedTime int64 - activeSpans []*UserSpanDesc // stack of active spans + activeRegions []*UserRegionDesc // stack of active regions } // GoroutineStats generates statistics for all goroutines in the trace. @@ -159,14 +159,14 @@ func GoroutineStats(events []*Event) map[uint64]*GDesc { g := &GDesc{ID: ev.Args[0], CreationTime: ev.Ts, gdesc: new(gdesc)} g.blockSchedTime = ev.Ts // When a goroutine is newly created, inherit the - // task of the active span. For ease handling of - // this case, we create a fake span description with + // task of the active region. For ease handling of + // this case, we create a fake region description with // the task id. - if creatorG := gs[ev.G]; creatorG != nil && len(creatorG.gdesc.activeSpans) > 0 { - spans := creatorG.gdesc.activeSpans - s := spans[len(spans)-1] + if creatorG := gs[ev.G]; creatorG != nil && len(creatorG.gdesc.activeRegions) > 0 { + regions := creatorG.gdesc.activeRegions + s := regions[len(regions)-1] if s.TaskID != 0 { - g.gdesc.activeSpans = []*UserSpanDesc{ + g.gdesc.activeRegions = []*UserRegionDesc{ {TaskID: s.TaskID, Start: ev}, } } @@ -263,32 +263,32 @@ func GoroutineStats(events []*Event) map[uint64]*GDesc { } } gcStartTime = 0 // indicates gc is inactive. - case EvUserSpan: + case EvUserRegion: g := gs[ev.G] switch mode := ev.Args[1]; mode { - case 0: // span start - g.activeSpans = append(g.activeSpans, &UserSpanDesc{ + case 0: // region start + g.activeRegions = append(g.activeRegions, &UserRegionDesc{ Name: ev.SArgs[0], TaskID: ev.Args[0], Start: ev, GExecutionStat: g.snapshotStat(lastTs, gcStartTime), }) - case 1: // span end - var sd *UserSpanDesc - if spanStk := g.activeSpans; len(spanStk) > 0 { - n := len(spanStk) - sd = spanStk[n-1] - spanStk = spanStk[:n-1] // pop - g.activeSpans = spanStk + case 1: // region end + var sd *UserRegionDesc + if regionStk := g.activeRegions; len(regionStk) > 0 { + n := len(regionStk) + sd = regionStk[n-1] + regionStk = regionStk[:n-1] // pop + g.activeRegions = regionStk } else { - sd = &UserSpanDesc{ + sd = &UserRegionDesc{ Name: ev.SArgs[0], TaskID: ev.Args[0], } } sd.GExecutionStat = g.snapshotStat(lastTs, gcStartTime).sub(sd.GExecutionStat) sd.End = ev - g.Spans = append(g.Spans, sd) + g.Regions = append(g.Regions, sd) } } } @@ -296,10 +296,10 @@ func GoroutineStats(events []*Event) map[uint64]*GDesc { for _, g := range gs { g.finalize(lastTs, gcStartTime, nil) - // sort based on span start time - sort.Slice(g.Spans, func(i, j int) bool { - x := g.Spans[i].Start - y := g.Spans[j].Start + // sort based on region start time + sort.Slice(g.Regions, func(i, j int) bool { + x := g.Regions[i].Start + y := g.Regions[j].Start if x == nil { return true } diff --git a/src/internal/trace/parser.go b/src/internal/trace/parser.go index 6d142a593f..c371ff3092 100644 --- a/src/internal/trace/parser.go +++ b/src/internal/trace/parser.go @@ -56,7 +56,7 @@ type Event struct { // for GoSysExit: the next GoStart // for GCMarkAssistStart: the associated GCMarkAssistDone // for UserTaskCreate: the UserTaskEnd - // for UserSpan: if the start span, the corresponding UserSpan end event + // for UserRegion: if the start region, the corresponding UserRegion end event Link *Event } @@ -442,7 +442,7 @@ func parseEvents(ver int, rawEvents []rawEvent, strings map[uint64]string) (even case EvUserTaskCreate: // e.Args 0: taskID, 1:parentID, 2:nameID e.SArgs = []string{strings[e.Args[2]]} - case EvUserSpan: + case EvUserRegion: // e.Args 0: taskID, 1: mode, 2:nameID e.SArgs = []string{strings[e.Args[2]]} case EvUserLog: @@ -583,8 +583,8 @@ func postProcessTrace(ver int, events []*Event) error { gs := make(map[uint64]gdesc) ps := make(map[int]pdesc) - tasks := make(map[uint64]*Event) // task id to task creation events - activeSpans := make(map[uint64][]*Event) // goroutine id to stack of spans + tasks := make(map[uint64]*Event) // task id to task creation events + activeRegions := make(map[uint64][]*Event) // goroutine id to stack of regions gs[0] = gdesc{state: gRunning} var evGC, evSTW *Event @@ -730,12 +730,12 @@ func postProcessTrace(ver int, events []*Event) error { g.state = gDead p.g = 0 - if ev.Type == EvGoEnd { // flush all active spans - spans := activeSpans[ev.G] - for _, s := range spans { + if ev.Type == EvGoEnd { // flush all active regions + regions := activeRegions[ev.G] + for _, s := range regions { s.Link = ev } - delete(activeSpans, ev.G) + delete(activeRegions, ev.G) } case EvGoSched, EvGoPreempt: @@ -811,29 +811,29 @@ func postProcessTrace(ver int, events []*Event) error { taskCreateEv.Link = ev delete(tasks, taskid) } - case EvUserSpan: + case EvUserRegion: mode := ev.Args[1] - spans := activeSpans[ev.G] - if mode == 0 { // span start - activeSpans[ev.G] = append(spans, ev) // push - } else if mode == 1 { // span end - n := len(spans) - if n > 0 { // matching span start event is in the trace. - s := spans[n-1] - if s.Args[0] != ev.Args[0] || s.SArgs[0] != ev.SArgs[0] { // task id, span name mismatch - return fmt.Errorf("misuse of span in goroutine %d: span end %q when the inner-most active span start event is %q", ev.G, ev, s) + regions := activeRegions[ev.G] + if mode == 0 { // region start + activeRegions[ev.G] = append(regions, ev) // push + } else if mode == 1 { // region end + n := len(regions) + if n > 0 { // matching region start event is in the trace. + s := regions[n-1] + if s.Args[0] != ev.Args[0] || s.SArgs[0] != ev.SArgs[0] { // task id, region name mismatch + return fmt.Errorf("misuse of region in goroutine %d: span end %q when the inner-most active span start event is %q", ev.G, ev, s) } - // Link span start event with span end event + // Link region start event with span end event s.Link = ev if n > 1 { - activeSpans[ev.G] = spans[:n-1] + activeRegions[ev.G] = regions[:n-1] } else { - delete(activeSpans, ev.G) + delete(activeRegions, ev.G) } } } else { - return fmt.Errorf("invalid user span mode: %q", ev) + return fmt.Errorf("invalid user region mode: %q", ev) } } @@ -1056,7 +1056,7 @@ const ( EvGCMarkAssistDone = 44 // GC mark assist done [timestamp] EvUserTaskCreate = 45 // trace.NewContext [timestamp, internal task id, internal parent id, stack, name string] EvUserTaskEnd = 46 // end of task [timestamp, internal task id, stack] - EvUserSpan = 47 // trace.WithSpan [timestamp, internal task id, mode(0:start, 1:end), stack, name string] + EvUserRegion = 47 // trace.WithRegion [timestamp, internal task id, mode(0:start, 1:end), stack, name string] EvUserLog = 48 // trace.Log [timestamp, internal id, key string id, stack, value string] EvCount = 49 ) @@ -1115,6 +1115,6 @@ var EventDescriptions = [EvCount]struct { EvGCMarkAssistDone: {"GCMarkAssistDone", 1009, false, []string{}, nil}, EvUserTaskCreate: {"UserTaskCreate", 1011, true, []string{"taskid", "pid", "typeid"}, []string{"name"}}, EvUserTaskEnd: {"UserTaskEnd", 1011, true, []string{"taskid"}, nil}, - EvUserSpan: {"UserSpan", 1011, true, []string{"taskid", "mode", "typeid"}, []string{"name"}}, + EvUserRegion: {"UserRegion", 1011, true, []string{"taskid", "mode", "typeid"}, []string{"name"}}, EvUserLog: {"UserLog", 1011, true, []string{"id", "keyid"}, []string{"category", "message"}}, } |
