aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/trace_profile/callgrind
diff options
context:
space:
mode:
Diffstat (limited to 'src/trace_profile/callgrind')
-rw-r--r--src/trace_profile/callgrind/Makefile14
-rw-r--r--src/trace_profile/callgrind/cg_example.cc36
2 files changed, 50 insertions, 0 deletions
diff --git a/src/trace_profile/callgrind/Makefile b/src/trace_profile/callgrind/Makefile
new file mode 100644
index 0000000..0da099c
--- /dev/null
+++ b/src/trace_profile/callgrind/Makefile
@@ -0,0 +1,14 @@
+prof: cg_example
+ valgrind --tool=callgrind --dump-instr=yes --instr-atstart=no -- ./cg_example
+
+prof-cache: cg_example
+ valgrind --tool=callgrind --dump-instr=yes --instr-atstart=no --cache-sim=yes -- ./cg_example
+
+show:
+ callgrind_annotate
+
+cg_example: cg_example.cc
+ clang++ -o cg_example -Wall -Wextra -O2 -g -fno-inline cg_example.cc -lpthread
+
+clean:
+ $(RM) callgrind.* cg_example
diff --git a/src/trace_profile/callgrind/cg_example.cc b/src/trace_profile/callgrind/cg_example.cc
new file mode 100644
index 0000000..d143790
--- /dev/null
+++ b/src/trace_profile/callgrind/cg_example.cc
@@ -0,0 +1,36 @@
+#include <cstdio>
+#include <thread>
+
+#include <valgrind/callgrind.h>
+
+struct cnt {
+ volatile int a;
+ volatile int b;
+};
+
+void inc_a(cnt &c) { c.a++; }
+void inc_b(cnt &c) { c.b++; }
+
+int main() {
+ cnt C{0, 0};
+
+ CALLGRIND_START_INSTRUMENTATION;
+
+ std::thread T1([&C]() {
+ for (int i = 0; i < 20000000; ++i) {
+ inc_a(C);
+ }
+ });
+ std::thread T2([&C]() {
+ for (int i = 0; i < 10000000; ++i) {
+ inc_b(C);
+ }
+ });
+ T1.join();
+ T2.join();
+
+ CALLGRIND_STOP_INSTRUMENTATION;
+
+ printf("%d %d\n", C.a, C.b);
+ return 0;
+}