Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
Signed-off-by: zhongzc <[email protected]>
  • Loading branch information
zhongzc committed Jun 7, 2020
1 parent 195dc43 commit 8a7889c
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 127 deletions.
20 changes: 10 additions & 10 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ package minitrace
import "sync"

type spanContext struct {
tracingContext *tracingContext
tracedSpans *localSpans
currentId uint64
currentGid int64
tracingContext *tracingContext
tracedSpans *localSpans
currentId uint64
currentGid int64
}

type localSpans struct {
spans []Span
createTimeNs uint64
refCount int
spans []Span
createTimeNs uint64
refCount int
}

type tracingContext struct {
maxId uint64
maxId uint64

mu sync.Mutex
collectedSpans []SpanSet
mu sync.Mutex
collectedSpans []SpanSet
}
25 changes: 12 additions & 13 deletions minitrace.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package minitrace

type SpanSet struct {
CreateTimeNs uint64
StartTimeNs uint64
CyclesPerSec uint64
Spans []Span
CreateTimeNs uint64
StartTimeNs uint64
CyclesPerSec uint64
Spans []Span
}

type Span struct {
Id uint64
Link Link
BeginCycles uint64
EndCycles uint64
Event uint32
Id uint64
Link Link
BeginCycles uint64
EndCycles uint64
Event uint32
}

type Link interface {
isLink()
isLink()
}

func (_ Root) isLink() {}
Expand All @@ -25,9 +25,8 @@ func (_ Continue) isLink() {}

type Root struct{}
type Parent struct {
Id uint64
Id uint64
}
type Continue struct {
Id uint64
Id uint64
}

30 changes: 15 additions & 15 deletions minitrace_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package minitrace

import (
"context"
"testing"
"context"
"testing"
)

func benchmarkMiniTrace(l int, b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
tracedFunc(l, b)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
tracedFunc(l, b)
}
}

func BenchmarkMiniTrace10(b *testing.B) { benchmarkMiniTrace(10, b) }
Expand All @@ -18,15 +18,15 @@ func BenchmarkMiniTrace1000(b *testing.B) { benchmarkMiniTrace(1000, b) }
func BenchmarkMiniTrace10000(b *testing.B) { benchmarkMiniTrace(10000, b) }

func tracedFunc(l int, b *testing.B) {
ctx, handle := TraceEnable(context.Background(), 0)
ctx, handle := TraceEnable(context.Background(), 0)

for i := 1; i < l; i++ {
_, handle := NewSpan(ctx, uint32(i))
handle.Finish()
}
for i := 1; i < l; i++ {
_, handle := NewSpan(ctx, uint32(i))
handle.Finish()
}

spanSets := handle.Finish()
if l != len(spanSets[0].Spans) {
b.Fatalf("expected length %d, got %d", l, len(spanSets[0].Spans))
}
spanSets := handle.Finish()
if l != len(spanSets[0].Spans) {
b.Fatalf("expected length %d, got %d", l, len(spanSets[0].Spans))
}
}
6 changes: 3 additions & 3 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ func nanotime() int64
func walltime() (sec int64, nsec int32)

func monotimeNs() uint64 {
return uint64(nanotime())
return uint64(nanotime())
}

func realtimeNs() uint64 {
sec, nsec := walltime()
return uint64(sec*1_000_000_000 + int64(nsec))
sec, nsec := walltime()
return uint64(sec*1_000_000_000 + int64(nsec))
}
172 changes: 86 additions & 86 deletions trace.go
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
}

0 comments on commit 8a7889c

Please sign in to comment.