Debian Codegraph @

debian/rust-vendor/codspeed/instrument-hooks/example/main.c

raw
1 #include <stdio.h>
2 #ifdef _WIN32
3 #include <process.h>
4 #define getpid _getpid
5 #else
6 #include <unistd.h>
7 #endif
8
9 #include "core.h"
10
11 #if defined(_MSC_VER)
12 #define CODSPEED_NOINLINE __declspec(noinline)
13 #else
14 #define CODSPEED_NOINLINE __attribute__((noinline))
15 #endif
16
17 static int fib(int n) {
18 if (n <= 1) return n;
19 return fib(n - 1) + fib(n - 2);
20 }
21
22 static void expensive_setup(void) { fib(30); }
23
24 void example_function(void) {
25 // Simulate some work
26 for (volatile int i = 0; i < 100000; i++)
27 ;
28 printf("Benchmark executed\n");
29 }
30
31 CODSPEED_NOINLINE void __codspeed_root_frame__example(
32 void (*benchmark_fn)(void)) {
33 benchmark_fn();
34 }
35
36 int main() {
37 InstrumentHooks *hooks = instrument_hooks_init();
38 if (!hooks) {
39 printf("Failed to initialize instrument hooks\n");
40 return 1;
41 }
42
43 if (instrument_hooks_is_instrumented(hooks)) {
44 printf("Running under instrumentation\n");
45 } else {
46 printf("Not running under instrumentation\n");
47 }
48
49 instrument_hooks_set_integration(hooks, "custom-integration", "1.0.0");
50
51 printf("Starting benchmark...\n");
52 if (instrument_hooks_start_benchmark_inline(hooks) != 0) {
53 printf("Failed to start benchmark\n");
54 instrument_hooks_deinit(hooks);
55 return 1;
56 }
57
58 uint32_t pid = getpid();
59 for (int i = 0; i < 10; i++) {
60 // This won't be displayed in the flamegraph, because it's outside
61 // the benchmark marker regions.
62 expensive_setup();
63
64 uint64_t start_time = instrument_hooks_current_timestamp();
65 __codspeed_root_frame__example(example_function);
66 uint64_t end_time = instrument_hooks_current_timestamp();
67
68 // Add the markers which mark when the benchmarked function was running
69 instrument_hooks_add_marker(hooks, pid, MARKER_TYPE_BENCHMARK_START,
70 start_time);
71 instrument_hooks_add_marker(hooks, pid, MARKER_TYPE_BENCHMARK_END,
72 end_time);
73 }
74
75 if (instrument_hooks_stop_benchmark_inline(hooks) != 0) {
76 printf("Failed to stop benchmark\n");
77 instrument_hooks_deinit(hooks);
78 return 1;
79 }
80
81 if (instrument_hooks_set_executed_benchmark(hooks, pid,
82 "example_benchmark") != 0) {
83 printf("Failed to report benchmark execution\n");
84 instrument_hooks_deinit(hooks);
85 return 1;
86 }
87
88 printf("Benchmark completed successfully\n");
89
90 instrument_hooks_deinit(hooks);
91
92 return 0;
93 }