File: unwinding.cpp

package info (click to toggle)
cpptrace 1.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,996 kB
  • sloc: cpp: 15,646; python: 962; ansic: 155; sh: 103; makefile: 86
file content (55 lines) | stat: -rw-r--r-- 1,262 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <cpptrace/cpptrace.hpp>

#include <benchmark/benchmark.h>

#include <iostream>

struct unwind_benchmark_info {
    benchmark::State& state;
    size_t& stack_depth;
};

void unwind_loop(unwind_benchmark_info info) {
    auto& [state, depth] = info;
    depth = cpptrace::generate_raw_trace().frames.size();
    for(auto _ : state) {
        benchmark::DoNotOptimize(cpptrace::generate_raw_trace());
    }
}

void foo(unwind_benchmark_info info, int n) {
    if(n == 0) {
        unwind_loop(info);
    } else {
        foo(info, n - 1);
    }
}

template<typename... Args>
void foo(unwind_benchmark_info info, int, Args... args) {
    foo(info, args...);
}

void function_two(unwind_benchmark_info info, int, float) {
    foo(info, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
}

void function_one(unwind_benchmark_info info, int) {
    function_two(info, 0, 0);
}

static void unwinding(benchmark::State& state) {
    size_t stack_depth = 0;
    function_one({state, stack_depth}, 0);
    static bool did_print = false;
    if(!did_print) {
        did_print = true;
        std::cerr<<"[info] Unwinding benchmark stack depth: "<<stack_depth<<std::endl;
    }
}

// Register the function as a benchmark
BENCHMARK(unwinding);

// Run the benchmark
BENCHMARK_MAIN();