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