-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: zhongzc <[email protected]>
- Loading branch information
Showing
5 changed files
with
126 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,117 @@ | ||
package minitrace | ||
|
||
import ( | ||
"context" | ||
"github.com/silentred/gid" | ||
"sync/atomic" | ||
"context" | ||
"github.com/silentred/gid" | ||
"sync/atomic" | ||
) | ||
|
||
type tracingKey struct{} | ||
|
||
var key = tracingKey{} | ||
|
||
func TraceEnable(ctx context.Context, event uint32) (context.Context, TraceHandle) { | ||
tCtx := &tracingContext{ | ||
maxId: 0, | ||
} | ||
spanCtx := &spanContext{ | ||
tracingContext: tCtx, | ||
tracedSpans: &localSpans{ | ||
spans: []Span{{ | ||
Id: 0, | ||
Link: Root{}, | ||
BeginCycles: monotimeNs(), | ||
EndCycles: 0, | ||
Event: event, | ||
}}, | ||
createTimeNs: realtimeNs(), | ||
refCount: 1, | ||
}, | ||
currentId: 0, | ||
currentGid: gid.Get(), | ||
} | ||
nCtx := context.WithValue(ctx, key, spanCtx) | ||
tCtx := &tracingContext{ | ||
maxId: 0, | ||
} | ||
spanCtx := &spanContext{ | ||
tracingContext: tCtx, | ||
tracedSpans: &localSpans{ | ||
spans: []Span{{ | ||
Id: 0, | ||
Link: Root{}, | ||
BeginCycles: monotimeNs(), | ||
EndCycles: 0, | ||
Event: event, | ||
}}, | ||
createTimeNs: realtimeNs(), | ||
refCount: 1, | ||
}, | ||
currentId: 0, | ||
currentGid: gid.Get(), | ||
} | ||
nCtx := context.WithValue(ctx, key, spanCtx) | ||
|
||
return nCtx, TraceHandle{SpanHandle{spanCtx, 0}} | ||
return nCtx, TraceHandle{SpanHandle{spanCtx, 0}} | ||
} | ||
|
||
func NewSpan(ctx context.Context, event uint32) (context.Context, SpanHandle) { | ||
switch v := ctx.Value(key).(type) { | ||
case *spanContext: | ||
id := atomic.AddUint64(&v.tracingContext.maxId, 1) | ||
goid := gid.Get() | ||
span := Span{ | ||
Id: id, | ||
Link: Parent{v.currentId}, | ||
BeginCycles: monotimeNs(), | ||
EndCycles: 0, | ||
Event: event, | ||
} | ||
switch v := ctx.Value(key).(type) { | ||
case *spanContext: | ||
id := atomic.AddUint64(&v.tracingContext.maxId, 1) | ||
goid := gid.Get() | ||
span := Span{ | ||
Id: id, | ||
Link: Parent{v.currentId}, | ||
BeginCycles: monotimeNs(), | ||
EndCycles: 0, | ||
Event: event, | ||
} | ||
|
||
if goid == v.currentGid { | ||
index := len(v.tracedSpans.spans) | ||
v.tracedSpans.refCount += 1 | ||
v.tracedSpans.spans = append(v.tracedSpans.spans, span) | ||
spanCtx := &spanContext{ | ||
tracingContext: v.tracingContext, | ||
tracedSpans: v.tracedSpans, | ||
currentId: id, | ||
currentGid: goid, | ||
} | ||
return context.WithValue(ctx, key, spanCtx), SpanHandle{spanCtx, index} | ||
} else { | ||
tracedSpans := &localSpans{ | ||
spans: []Span{span}, | ||
createTimeNs: realtimeNs(), | ||
refCount: 1, | ||
} | ||
spanCtx := &spanContext{ | ||
tracingContext: v.tracingContext, | ||
tracedSpans: tracedSpans, | ||
currentId: id, | ||
currentGid: goid, | ||
} | ||
return context.WithValue(ctx, key, spanCtx), SpanHandle{spanCtx, 0} | ||
} | ||
} | ||
if goid == v.currentGid { | ||
index := len(v.tracedSpans.spans) | ||
v.tracedSpans.refCount += 1 | ||
v.tracedSpans.spans = append(v.tracedSpans.spans, span) | ||
spanCtx := &spanContext{ | ||
tracingContext: v.tracingContext, | ||
tracedSpans: v.tracedSpans, | ||
currentId: id, | ||
currentGid: goid, | ||
} | ||
return context.WithValue(ctx, key, spanCtx), SpanHandle{spanCtx, index} | ||
} else { | ||
tracedSpans := &localSpans{ | ||
spans: []Span{span}, | ||
createTimeNs: realtimeNs(), | ||
refCount: 1, | ||
} | ||
spanCtx := &spanContext{ | ||
tracingContext: v.tracingContext, | ||
tracedSpans: tracedSpans, | ||
currentId: id, | ||
currentGid: goid, | ||
} | ||
return context.WithValue(ctx, key, spanCtx), SpanHandle{spanCtx, 0} | ||
} | ||
} | ||
|
||
return ctx, SpanHandle{} | ||
return ctx, SpanHandle{} | ||
} | ||
|
||
type SpanHandle struct { | ||
spanContext *spanContext | ||
index int | ||
spanContext *spanContext | ||
index int | ||
} | ||
|
||
func (hd SpanHandle) Finish() { | ||
if hd.spanContext != nil { | ||
spanCtx := hd.spanContext | ||
spanCtx.tracedSpans.spans[hd.index].EndCycles = monotimeNs() | ||
spanCtx.tracedSpans.refCount -= 1 | ||
if spanCtx.tracedSpans.refCount == 0 { | ||
spanCtx.tracingContext.mu.Lock() | ||
spanCtx.tracingContext.collectedSpans = append(spanCtx.tracingContext.collectedSpans, SpanSet{ | ||
CreateTimeNs: spanCtx.tracedSpans.createTimeNs, | ||
StartTimeNs: spanCtx.tracedSpans.createTimeNs, | ||
CyclesPerSec: 1_000_000_000, // nanoseconds per second | ||
Spans: spanCtx.tracedSpans.spans, | ||
}) | ||
spanCtx.tracingContext.mu.Unlock() | ||
} | ||
} | ||
if hd.spanContext != nil { | ||
spanCtx := hd.spanContext | ||
spanCtx.tracedSpans.spans[hd.index].EndCycles = monotimeNs() | ||
spanCtx.tracedSpans.refCount -= 1 | ||
if spanCtx.tracedSpans.refCount == 0 { | ||
spanCtx.tracingContext.mu.Lock() | ||
spanCtx.tracingContext.collectedSpans = append(spanCtx.tracingContext.collectedSpans, SpanSet{ | ||
CreateTimeNs: spanCtx.tracedSpans.createTimeNs, | ||
StartTimeNs: spanCtx.tracedSpans.createTimeNs, | ||
CyclesPerSec: 1_000_000_000, // nanoseconds per second | ||
Spans: spanCtx.tracedSpans.spans, | ||
}) | ||
spanCtx.tracingContext.mu.Unlock() | ||
} | ||
} | ||
} | ||
|
||
type TraceHandle struct { | ||
SpanHandle | ||
SpanHandle | ||
} | ||
|
||
func (hd TraceHandle) Finish() (res []SpanSet) { | ||
hd.SpanHandle.Finish() | ||
hd.SpanHandle.Finish() | ||
|
||
hd.spanContext.tracingContext.mu.Lock() | ||
res = hd.spanContext.tracingContext.collectedSpans | ||
hd.spanContext.tracingContext.collectedSpans = nil | ||
hd.spanContext.tracingContext.mu.Unlock() | ||
hd.spanContext.tracingContext.mu.Lock() | ||
res = hd.spanContext.tracingContext.collectedSpans | ||
hd.spanContext.tracingContext.collectedSpans = nil | ||
hd.spanContext.tracingContext.mu.Unlock() | ||
|
||
return | ||
return | ||
} |