Skip to content

Commit 2d93805

Browse files
committed
runtime: add a benchmark of Callers
We're about to make major changes to tracebacks. We have benchmarks of stack copying, but not of PC buffer filling, so add some that we can track through these changes. For #54466. Change-Id: I3ed61d75144ba03b61517cd9834eeb71c99d74df Reviewed-on: https://go-review.googlesource.com/c/go/+/472956 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Austin Clements <austin@google.com> Reviewed-by: Michael Pratt <mpratt@google.com>
1 parent ad3ea01 commit 2d93805

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

src/runtime/callers_test.go

+91
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,94 @@ func callerLine(t *testing.T, skip int) int {
339339
}
340340
return line
341341
}
342+
343+
func BenchmarkCallers(b *testing.B) {
344+
b.Run("cached", func(b *testing.B) {
345+
// Very pcvalueCache-friendly, no inlining.
346+
callersCached(b, 100)
347+
})
348+
b.Run("inlined", func(b *testing.B) {
349+
// Some inlining, still pretty cache-friendly.
350+
callersInlined(b, 100)
351+
})
352+
b.Run("no-cache", func(b *testing.B) {
353+
// Cache-hostile
354+
callersNoCache(b, 100)
355+
})
356+
}
357+
358+
func callersCached(b *testing.B, n int) int {
359+
if n <= 0 {
360+
pcs := make([]uintptr, 32)
361+
b.ResetTimer()
362+
for i := 0; i < b.N; i++ {
363+
runtime.Callers(0, pcs)
364+
}
365+
b.StopTimer()
366+
return 0
367+
}
368+
return 1 + callersCached(b, n-1)
369+
}
370+
371+
func callersInlined(b *testing.B, n int) int {
372+
if n <= 0 {
373+
pcs := make([]uintptr, 32)
374+
b.ResetTimer()
375+
for i := 0; i < b.N; i++ {
376+
runtime.Callers(0, pcs)
377+
}
378+
b.StopTimer()
379+
return 0
380+
}
381+
return 1 + callersInlined1(b, n-1)
382+
}
383+
func callersInlined1(b *testing.B, n int) int { return callersInlined2(b, n) }
384+
func callersInlined2(b *testing.B, n int) int { return callersInlined3(b, n) }
385+
func callersInlined3(b *testing.B, n int) int { return callersInlined4(b, n) }
386+
func callersInlined4(b *testing.B, n int) int { return callersInlined(b, n) }
387+
388+
func callersNoCache(b *testing.B, n int) int {
389+
if n <= 0 {
390+
pcs := make([]uintptr, 32)
391+
b.ResetTimer()
392+
for i := 0; i < b.N; i++ {
393+
runtime.Callers(0, pcs)
394+
}
395+
b.StopTimer()
396+
return 0
397+
}
398+
switch n % 16 {
399+
case 0:
400+
return 1 + callersNoCache(b, n-1)
401+
case 1:
402+
return 1 + callersNoCache(b, n-1)
403+
case 2:
404+
return 1 + callersNoCache(b, n-1)
405+
case 3:
406+
return 1 + callersNoCache(b, n-1)
407+
case 4:
408+
return 1 + callersNoCache(b, n-1)
409+
case 5:
410+
return 1 + callersNoCache(b, n-1)
411+
case 6:
412+
return 1 + callersNoCache(b, n-1)
413+
case 7:
414+
return 1 + callersNoCache(b, n-1)
415+
case 8:
416+
return 1 + callersNoCache(b, n-1)
417+
case 9:
418+
return 1 + callersNoCache(b, n-1)
419+
case 10:
420+
return 1 + callersNoCache(b, n-1)
421+
case 11:
422+
return 1 + callersNoCache(b, n-1)
423+
case 12:
424+
return 1 + callersNoCache(b, n-1)
425+
case 13:
426+
return 1 + callersNoCache(b, n-1)
427+
case 14:
428+
return 1 + callersNoCache(b, n-1)
429+
default:
430+
return 1 + callersNoCache(b, n-1)
431+
}
432+
}

0 commit comments

Comments
 (0)