Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cfbd1c7

Browse files
committedAug 12, 2020
cmd/go/internal/trace: add function to distinguish goroutines
trace.StartGoroutine will associate the trace information on the context with a new chrome profiler thread id. The chrome profiler doesn't expect multiple trace events to have the same thread id, so this will allow us to display concurrent events on the trace. Updates #38714 Change-Id: I81690861df4f444f14f02a99e0fe551395b660a7 Reviewed-on: https://go-review.googlesource.com/c/go/+/238542 Run-TryBot: Michael Matloob <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 2bfa45c commit cfbd1c7

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed
 

‎go

13.5 MB
Binary file not shown.

‎src/cmd/go/internal/trace/trace.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,33 @@ func StartSpan(ctx context.Context, name string) (context.Context, *Span) {
3333
if !ok {
3434
return ctx, nil
3535
}
36-
childSpan := &Span{t: tc.t, name: name, start: time.Now()}
36+
childSpan := &Span{t: tc.t, name: name, tid: tc.tid, start: time.Now()}
3737
tc.t.writeEvent(&traceviewer.Event{
3838
Name: childSpan.name,
3939
Time: float64(childSpan.start.UnixNano()) / float64(time.Microsecond),
40+
TID: childSpan.tid,
4041
Phase: "B",
4142
})
42-
ctx = context.WithValue(ctx, traceKey{}, traceContext{tc.t})
43+
ctx = context.WithValue(ctx, traceKey{}, traceContext{tc.t, tc.tid})
4344
return ctx, childSpan
4445
}
4546

47+
// Goroutine associates the context with a new Thread ID. The Chrome trace viewer associates each
48+
// trace event with a thread, and doesn't expect events with the same thread id to happen at the
49+
// same time.
50+
func Goroutine(ctx context.Context) context.Context {
51+
tc, ok := getTraceContext(ctx)
52+
if !ok {
53+
return ctx
54+
}
55+
return context.WithValue(ctx, traceKey{}, traceContext{tc.t, tc.t.getNextTID()})
56+
}
57+
4658
type Span struct {
4759
t *tracer
4860

4961
name string
62+
tid uint64
5063
start time.Time
5164
end time.Time
5265
}
@@ -59,12 +72,15 @@ func (s *Span) Done() {
5972
s.t.writeEvent(&traceviewer.Event{
6073
Name: s.name,
6174
Time: float64(s.end.UnixNano()) / float64(time.Microsecond),
75+
TID: s.tid,
6276
Phase: "E",
6377
})
6478
}
6579

6680
type tracer struct {
6781
file chan traceFile // 1-buffered
82+
83+
nextTID uint64
6884
}
6985

7086
func (t *tracer) writeEvent(ev *traceviewer.Event) error {
@@ -102,12 +118,17 @@ func (t *tracer) Close() error {
102118
return firstErr
103119
}
104120

121+
func (t *tracer) getNextTID() uint64 {
122+
return atomic.AddUint64(&t.nextTID, 1)
123+
}
124+
105125
// traceKey is the context key for tracing information. It is unexported to prevent collisions with context keys defined in
106126
// other packages.
107127
type traceKey struct{}
108128

109129
type traceContext struct {
110-
t *tracer
130+
t *tracer
131+
tid uint64
111132
}
112133

113134
// Start starts a trace which writes to the given file.

0 commit comments

Comments
 (0)
Please sign in to comment.