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