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