Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TraceFrom(pcs) for "on-demand" StackTrace instantiation #25

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,24 @@ func (cs CallStack) Format(s fmt.State, verb rune) {
s.Write(closeBracketBytes)
}

// Convenience wrapper around runtime.Callers()
func Callers(skip int) []uintptr {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should choose a function name that aligns more closely to the Trace function since they both serve the same purpose just at different levels of abstraction. We should also improve the doc comment while we're at it.

Consider:

// TraceFast returns a low level representation of a CallStack. Use TraceFrom
// to convert the result into a CallStack. Use TraceFast as a lighter alternative
// to Trace when you want to record stack trace but will not always format it.
func TraceFast() []uintptr { ... }

var pcs [512]uintptr
n := runtime.Callers(skip+1, pcs[:])
return pcs[:n]
}

// Trace returns a CallStack for the current goroutine with element 0
// identifying the calling function.
func Trace() CallStack {
var pcs [512]uintptr
n := runtime.Callers(1, pcs[:])
return TraceFrom(Callers(1))
}

frames := runtime.CallersFrames(pcs[:n])
cs := make(CallStack, 0, n)
// TraceFrom creates a CallStack from the given program counters (as generated
// by runtime.Callers)
func TraceFrom(pcs []uintptr) CallStack {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs for TraceFrom should make it clear that it should only be given a return value from TraceFast and not just any slice of pcs. That gives us some wiggle room to adjust the implemenation without having to be compatible with arbitrary pcs []uintptr values.

frames := runtime.CallersFrames(pcs)
cs := make(CallStack, 0, len(pcs))

// Skip extra frame retrieved just to make sure the runtime.sigpanic
// special case is handled.
Expand Down
35 changes: 35 additions & 0 deletions stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,14 @@ func deepStack(depth int, b *testing.B) stack.CallStack {
return s
}

func deepCallers(depth int, b *testing.B) []uintptr {
if depth > 0 {
return deepCallers(depth-1, b)
}
b.StartTimer()
return stack.Callers(1)
}

func BenchmarkTrace10(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
Expand All @@ -518,6 +526,33 @@ func BenchmarkTrace100(b *testing.B) {
}
}

func BenchmarkCallers(b *testing.B) {
for i := 0; i < b.N; i++ {
stack.Callers(1)
}
}

func BenchmarkCallers10(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
deepCallers(10, b)
}
}

func BenchmarkCallers50(b *testing.B) {
b.StopTimer()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should call b.StopTimer inside the loop before calling deepCallers like it is in BenchmarkCallers10. I realize you followed the pattern I had in the BenchmarkTrace functions but I did it wrong there and never noticed until now. Please fix those too if you don't mind.

for i := 0; i < b.N; i++ {
deepCallers(50, b)
}
}

func BenchmarkCallers100(b *testing.B) {
b.StopTimer()
for i := 0; i < b.N; i++ {
deepCallers(100, b)
}
}

////////////////
// Benchmark functions followed by formatting
////////////////
Expand Down