aboutsummaryrefslogtreecommitdiff
path: root/content/posts/2024-02-28-converting-valgrind-callgrinds-to-svg-format.md
diff options
context:
space:
mode:
Diffstat (limited to 'content/posts/2024-02-28-converting-valgrind-callgrinds-to-svg-format.md')
-rw-r--r--content/posts/2024-02-28-converting-valgrind-callgrinds-to-svg-format.md98
1 files changed, 98 insertions, 0 deletions
diff --git a/content/posts/2024-02-28-converting-valgrind-callgrinds-to-svg-format.md b/content/posts/2024-02-28-converting-valgrind-callgrinds-to-svg-format.md
new file mode 100644
index 0000000..42c20c0
--- /dev/null
+++ b/content/posts/2024-02-28-converting-valgrind-callgrinds-to-svg-format.md
@@ -0,0 +1,98 @@
1---
2title: Converting Valgrind callgrinds to SVG format
3url: /converting-valgrind-callgrinds-to-svg-format.html
4date: 2024-02-28T03:23:00+01:00
5type: post
6draft: false
7---
8
9Lately I have been doing a lot of systems programming and profiling is
10the name of the game when it comes to developing good software.
11
12I found [Valgrind](https://valgrind.org) indispensable for profiling
13and getting callgraphs.
14
15Most of the time there are better alternatives that SVG to drill intro the
16callgraphs but if you need to put put a callgraph on a webpage or maybe
17send it to somebody that does not have all of the necessary software to
18view these things then SVG is the perfect format for this.
19
20Theses are couple of amazing applications to view callgraphs that get
21exported by [Valgrind](https://valgrind.org):
22
23- [Kcachegrind](https://kcachegrind.github.io/html/Home.html) - Linux
24- [WinCacheGrind](https://ceefour.github.io/wincachegrind/) - Windows
25- [Profiling Viewer](https://profilingviewer.com/) - macOS
26
27This is how Kcachekrind looks with a callgraph loaded in. Not only that,
28with Kcachegrind you can also explore assembly produced by the compiler
29and get much more insight into the profile of your application.
30
31![Kcachekrind screenshot](/assets/posts/valgrind-callgrind-svg/kcachegrind.png)
32
33After this point you will need couple of things installed on your
34system. I will show how you do this on Fedora 39.
35
36```sh
37# Install valgrind which will do the actual profiling.
38sudo dnf install valgrind
39
40# Install Kcachegrind for local visualizing.
41sudo dnf install kcachegrind
42
43# Install gprof2dot for conversion from .dot to .svg.
44pip install gprof2dot
45```
46
47Let's make a simple C program and test out profiling and the whole shebang.
48
49```c
50#include <stdio.h>
51
52int main() {
53 printf("Oh, hi Mark\n");
54 return 0;
55}
56```
57
58Then let's compile, convert to dot and then SVG file.
59
60```shell
61clang hi.c -o c-hi
62valgrind --tool=callgrind --dump-instr=yes --collect-jumps=yes ./c-hi
63gprof2dot --format=callgrind --output=out.c.dot --colormap=print callgrind.out.546168
64cat out.c.dot | dot -Tsvg > out.c.svg
65```
66
67This gives us an SVG file like this.
68
69![SVG callgrind for C program](/assets/posts/valgrind-callgrind-svg/out.c.svg)
70
71And this also works on other binaries.
72
73Lets give Zig a go.
74
75```zig
76const std = @import("std");
77
78pub fn main() !void {
79 std.debug.print("Oh, hi Mark!\n", .{});
80}
81```
82
83Now repeat the whole compile, convert cycle.
84
85```shell
86zig build-exe hi.zig --name zig-hi
87valgrind --tool=callgrind --dump-instr=yes --collect-jumps=yes ./zig-hi
88gprof2dot --format=callgrind --output=out.zig.dot --colormap=print callgrind.out.546168
89cat out.zig.dot | dot -Tsvg > out.zig.svg
90```
91
92![SVG callgrind for Zig program](/assets/posts/valgrind-callgrind-svg/out.zig.svg)
93
94Now, to be fair
95[Kcachegrind](https://kcachegrind.github.io/html/Home.html) is much nicer
96for local exploration and digging deep into the callgraphs, but the SVG
97format can still provide valid information for documentation and things
98of that nature.