Converting Valgrind callgrinds to SVG format
Lately I have been doing a lot of systems programming and profiling is the name of the game when it comes to developing good software.
I found Valgrind indispensable for profiling and getting callgraphs.
Most of the time there are better alternatives that SVG to drill intro the callgraphs but if you need to put put a callgraph on a webpage or maybe send it to somebody that does not have all of the necessary software to view these things then SVG is the perfect format for this.
Theses are couple of amazing applications to view callgraphs that get exported by Valgrind:
- Kcachegrind - Linux
- WinCacheGrind - Windows
- Profiling Viewer - macOS
This is how Kcachekrind looks with a callgraph loaded in. Not only that,
with Kcachegrind you can also explore assembly produced by the compiler
and get much more insight into the profile of your application.
After this point you will need couple of things installed on your system. I will show how you do this on Fedora 39.
# Install valgrind which will do the actual profiling.
sudo dnf install valgrind
# Install Kcachegrind for local visualizing.
sudo dnf install kcachegrind
# Install gprof2dot for conversion from .dot to .svg.
pip install gprof2dot
Let's make a simple C program and test out profiling and the whole shebang.
#include <stdio.h>
int main() {
printf("Oh, hi Mark\n");
return 0;
}
Then let's compile, convert to dot and then SVG file.
clang hi.c -o c-hi
valgrind --tool=callgrind --dump-instr=yes --collect-jumps=yes ./c-hi
gprof2dot --format=callgrind --output=out.c.dot --colormap=print callgrind.out.546168
cat out.c.dot | dot -Tsvg > out.c.svg
This gives us an SVG file like this.
And this also works on other binaries.
Lets give Zig a go.
const std = @import("std");
pub fn main() !void {
std.debug.print("Oh, hi Mark!\n", .{});
}
Now repeat the whole compile, convert cycle.
zig build-exe hi.zig --name zig-hi
valgrind --tool=callgrind --dump-instr=yes --collect-jumps=yes ./zig-hi
gprof2dot --format=callgrind --output=out.zig.dot --colormap=print callgrind.out.546168
cat out.zig.dot | dot -Tsvg > out.zig.svg
Now, to be fair Kcachegrind is much nicer for local exploration and digging deep into the callgraphs, but the SVG format can still provide valid information for documentation and things of that nature.
Other posts
- LED technology might not be as eco-friendly as you think
- Elitist attitudes are sapping all the fun from programming
- Debian based riced up distribution for Developers and DevOps folks
- My journey from being an internet über consumer to being a full hominum again
- Simplifying and reducing clutter in my life and work
- Create placeholder images with sharp Node.js image processing library
- Simple world clock with eInk display and Raspberry Pi Zero
- Replacing Dropbox in favor of DigitalOcean spaces
- Using GoAccess with Nginx to replace Google Analytics
- State of Web Technologies and Web development in year 2022
- List of essential Linux commands for server management
- Using DigitalOcean Spaces Object Storage with FUSE
- Using Digitalocean Spaces to sync between computers
- Wireless sensor networks