CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/benchmarks/AP_gbenchmark.h
Views: 1798
1
/*
2
* Utility header for benchmarks with Google Benchmark.
3
*/
4
#include <benchmark/benchmark.h>
5
6
/* The two functions below are an approach proposed by Chandler Carruth in
7
* CPPCON 2015: CppCon 2015: "Tuning C++: Benchmarks, and CPUs, and Compilers!
8
* Oh My!" in order keep the compiler from optimizing the use of a variable
9
* (gbenchmark_escape) or whole memory (gbenchmark_clobber).
10
*
11
* The compiler optimizer may sometimes remove code when it sees it isn't
12
* necessary. For example, when a variable isn't used, the optimizer removes
13
* the code that computes the value for that variable - that's not good for
14
* benchmarks. The function gbenchmark_escape(void *p) makes the compiler think
15
* that that p is being used in a code that might have "unknowable side
16
* effects", which keeps it from removing the variable. The "side effects" in
17
* the case here would be the benchmark numbers.
18
*
19
* Here is an example that would give wrong benchmark values:
20
*
21
* static void BM_Test(benchmark::State& state)
22
* {
23
* while (state.KeepRunning()) {
24
* float a = expensive_operation();
25
* }
26
* }
27
*
28
* Since variable a isn't used, the call to expensive_operation() is removed
29
* from the compiled program. The benchmark would show that
30
* expensive_operation() is extremely fast. The following code would fix that:
31
*
32
* static void BM_Test(benchmark::State& state)
33
* {
34
* while (state.KeepRunning()) {
35
* float a = expensive_operation();
36
* gbenchmark_escape(&a);
37
* }
38
* }
39
*/
40
41
inline void gbenchmark_escape(void* p)
42
{
43
asm volatile("" : : "g"(p) : "memory");
44
}
45
46
inline void gbenchmark_clobber()
47
{
48
asm volatile("" : : : "memory");
49
}
50
51