1*9356374aSAndroid Build Coastguard Worker // Copyright 2022 The Abseil Authors
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker // https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker
15*9356374aSAndroid Build Coastguard Worker #include "absl/base/attributes.h"
16*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
17*9356374aSAndroid Build Coastguard Worker #include "absl/base/optimization.h"
18*9356374aSAndroid Build Coastguard Worker #include "absl/debugging/stacktrace.h"
19*9356374aSAndroid Build Coastguard Worker #include "benchmark/benchmark.h"
20*9356374aSAndroid Build Coastguard Worker
21*9356374aSAndroid Build Coastguard Worker namespace absl {
22*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
23*9356374aSAndroid Build Coastguard Worker namespace {
24*9356374aSAndroid Build Coastguard Worker
25*9356374aSAndroid Build Coastguard Worker static constexpr int kMaxStackDepth = 100;
26*9356374aSAndroid Build Coastguard Worker static constexpr int kCacheSize = (1 << 16);
27*9356374aSAndroid Build Coastguard Worker void* pcs[kMaxStackDepth];
28*9356374aSAndroid Build Coastguard Worker
func(benchmark::State & state,int x,int depth)29*9356374aSAndroid Build Coastguard Worker ABSL_ATTRIBUTE_NOINLINE void func(benchmark::State& state, int x, int depth) {
30*9356374aSAndroid Build Coastguard Worker if (x <= 0) {
31*9356374aSAndroid Build Coastguard Worker // Touch a significant amount of memory so that the stack is likely to be
32*9356374aSAndroid Build Coastguard Worker // not cached in the L1 cache.
33*9356374aSAndroid Build Coastguard Worker state.PauseTiming();
34*9356374aSAndroid Build Coastguard Worker int* arr = new int[kCacheSize];
35*9356374aSAndroid Build Coastguard Worker for (int i = 0; i < kCacheSize; ++i) benchmark::DoNotOptimize(arr[i] = 100);
36*9356374aSAndroid Build Coastguard Worker delete[] arr;
37*9356374aSAndroid Build Coastguard Worker state.ResumeTiming();
38*9356374aSAndroid Build Coastguard Worker benchmark::DoNotOptimize(absl::GetStackTrace(pcs, depth, 0));
39*9356374aSAndroid Build Coastguard Worker return;
40*9356374aSAndroid Build Coastguard Worker }
41*9356374aSAndroid Build Coastguard Worker ABSL_BLOCK_TAIL_CALL_OPTIMIZATION();
42*9356374aSAndroid Build Coastguard Worker func(state, --x, depth);
43*9356374aSAndroid Build Coastguard Worker }
44*9356374aSAndroid Build Coastguard Worker
BM_GetStackTrace(benchmark::State & state)45*9356374aSAndroid Build Coastguard Worker void BM_GetStackTrace(benchmark::State& state) {
46*9356374aSAndroid Build Coastguard Worker int depth = state.range(0);
47*9356374aSAndroid Build Coastguard Worker for (auto s : state) {
48*9356374aSAndroid Build Coastguard Worker func(state, depth, depth);
49*9356374aSAndroid Build Coastguard Worker }
50*9356374aSAndroid Build Coastguard Worker }
51*9356374aSAndroid Build Coastguard Worker
52*9356374aSAndroid Build Coastguard Worker BENCHMARK(BM_GetStackTrace)->DenseRange(10, kMaxStackDepth, 10);
53*9356374aSAndroid Build Coastguard Worker } // namespace
54*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
55*9356374aSAndroid Build Coastguard Worker } // namespace absl
56