-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Share callstack b/w Caller and Stack #808
Changes from all commits
512b342
3e14bd1
3520ae7
954fa36
332434f
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 |
---|---|---|
|
@@ -31,45 +31,34 @@ import ( | |
const _zapPackage = "go.uber.org/zap" | ||
|
||
var ( | ||
_stacktracePool = sync.Pool{ | ||
New: func() interface{} { | ||
return newProgramCounters(64) | ||
}, | ||
} | ||
|
||
// We add "." and "/" suffixes to the package name to ensure we only match | ||
// the exact package and not any package with the same prefix. | ||
_zapStacktracePrefixes = addPrefix(_zapPackage, ".", "/") | ||
_zapStacktraceVendorContains = addPrefix("/vendor/", _zapStacktracePrefixes...) | ||
) | ||
|
||
func takeStacktrace() string { | ||
pcs := newProgramCounters() | ||
defer pcs.Release() | ||
|
||
return makeStacktrace( | ||
pcs.Callers(1 /* skip */, 0 /* limit */), | ||
true, // skip zap | ||
) | ||
} | ||
|
||
func makeStacktrace(pcs []uintptr, skipZapFrames bool) string { | ||
buffer := bufferpool.Get() | ||
defer buffer.Free() | ||
programCounters := _stacktracePool.Get().(*programCounters) | ||
defer _stacktracePool.Put(programCounters) | ||
|
||
var numFrames int | ||
for { | ||
// Skip the call to runtime.Counters and takeStacktrace so that the | ||
// program counters start at the caller of takeStacktrace. | ||
numFrames = runtime.Callers(2, programCounters.pcs) | ||
if numFrames < len(programCounters.pcs) { | ||
break | ||
} | ||
// Don't put the too-short counter slice back into the pool; this lets | ||
// the pool adjust if we consistently take deep stacktraces. | ||
programCounters = newProgramCounters(len(programCounters.pcs) * 2) | ||
} | ||
|
||
i := 0 | ||
skipZapFrames := true // skip all consecutive zap frames at the beginning. | ||
frames := runtime.CallersFrames(programCounters.pcs[:numFrames]) | ||
frames := runtime.CallersFrames(pcs) | ||
|
||
// Note: On the last iteration, frames.Next() returns false, with a valid | ||
// frame, but we ignore this frame. The last frame is a a runtime frame which | ||
// adds noise, since it's only either runtime.main or runtime.goexit. | ||
for frame, more := frames.Next(); more; frame, more = frames.Next() { | ||
// skip all consecutive zap frames at the beginning. | ||
if skipZapFrames && isZapFrame(frame.Function) { | ||
continue | ||
} else { | ||
|
@@ -113,8 +102,55 @@ type programCounters struct { | |
pcs []uintptr | ||
} | ||
|
||
func newProgramCounters(size int) *programCounters { | ||
return &programCounters{make([]uintptr, size)} | ||
var _stacktracePool = sync.Pool{ | ||
New: func() interface{} { | ||
return &programCounters{make([]uintptr, 64)} | ||
}, | ||
} | ||
|
||
func newProgramCounters() *programCounters { | ||
return _stacktracePool.Get().(*programCounters) | ||
} | ||
|
||
func (pcs *programCounters) Release() { | ||
_stacktracePool.Put(pcs) | ||
} | ||
|
||
// Callers captures program counters of callers of the current function. | ||
// | ||
// skip specifies the number of functions to skip. | ||
// limit specifies how deep into the stack we go, zero meaning unlimited. | ||
func (pcs *programCounters) Callers(skip int, limit int) []uintptr { | ||
pc := pcs.pcs | ||
if limit > 0 { | ||
// The shortened slice will never be placed back into the | ||
// pool, but the longer one will. | ||
if limit < len(pc) { | ||
pc = pc[:limit] | ||
} else { | ||
pc = make([]uintptr, limit) | ||
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. this case never gets hit in prod right? anyway we can refactor the change to avoid adding a case that's not necessary in prod? |
||
pcs.pcs = pc | ||
} | ||
} | ||
|
||
var numFrames int | ||
for { | ||
// Skip the call to runtime.Callers and programCounter.Callers | ||
// so that the program counters start at our caller. | ||
numFrames = runtime.Callers(skip+2, pc) | ||
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. can do |
||
|
||
// All required frames have been captured if the limit was | ||
// non-zero or if the stack depth is smaller than the size of | ||
// the slice. | ||
if limit > 0 || numFrames < len(pc) { | ||
return pc[:numFrames] | ||
} | ||
|
||
// Don't put the too-short counter slice back into the pool; this lets | ||
// the pool adjust if we consistently take deep stacktraces. | ||
pc = make([]uintptr, len(pcs.pcs)*2) | ||
pcs.pcs = pc | ||
} | ||
} | ||
|
||
func addPrefix(prefix string, ss ...string) []string { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,3 +73,56 @@ func BenchmarkTakeStacktrace(b *testing.B) { | |
takeStacktrace() | ||
} | ||
} | ||
|
||
func TestProgramCountersCallersDeepStack(t *testing.T) { | ||
tests := []struct { | ||
desc string | ||
stackDepth int | ||
limit int | ||
wantOccurrences int | ||
}{ | ||
{ | ||
desc: "large limit", | ||
stackDepth: 128, | ||
limit: 128, | ||
wantOccurrences: 127, | ||
// one of the slots is taken by the closure we pass to | ||
// withStackDepth. | ||
}, | ||
{ | ||
desc: "unlimited", | ||
stackDepth: 500, | ||
wantOccurrences: 500, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.desc, func(t *testing.T) { | ||
const withStackDepthName = "go.uber.org/zap.withStackDepth" | ||
|
||
var trace string | ||
withStackDepth(tt.stackDepth, func() { | ||
pcs := newProgramCounters().Callers(1, tt.limit) | ||
trace = makeStacktrace(pcs, false /* don't skip zap */) | ||
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. trying to avoid this boolean arg (as it's a test-only code path, and generally boolean args are not great) And keep calling that function recursively till we hit the limit we want, and that should add non-zap stack frames? |
||
}) | ||
|
||
for found := 0; found < tt.wantOccurrences; found++ { | ||
i := strings.Index(trace, withStackDepthName) | ||
if i < 0 { | ||
t.Fatalf(`expected %d occurrences of %q, found %d`, tt.wantOccurrences, withStackDepthName, found) | ||
} | ||
|
||
trace = trace[i+len(withStackDepthName):] | ||
} | ||
}) | ||
} | ||
|
||
} | ||
|
||
func withStackDepth(depth int, f func()) { | ||
if depth > 0 { | ||
withStackDepth(depth-1, f) | ||
} else { | ||
f() | ||
} | ||
} |
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.
if we are adding caller and stack, then the
stackSkip
passed in is 0 (e.g., no caller skip), so the caller value used here would be incorrect right, since we wouldn't be respecting thelog.callerSkip