-
Notifications
You must be signed in to change notification settings - Fork 34
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should call |
||
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 | ||
//////////////// | ||
|
There was a problem hiding this comment.
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: