How do I produce a graphical code profile report for C++ code compiled with Clang LLVM?
What command-line options to I pass to clang++ to instruct it to gather profiling data when the code is executed?
Into which file(s) is the gathered profiling data stored?
What are the post-processing steps to convert the collected profile data into a graphical report that shows how often each function is called, what percentage of time is spent in each function, and from which functions each function is called (similar to https://s3-us-west-2.amazonaws.com/brunorijsman-public/example-rift-python-code-profile.png)?
I have full control over the C++ source code and Makefile.
It has to be LLVM clang++ (GNU g++ is not an option for me). Xcode is also not an option for me.
Clang supports a couple of different code coverage implementations (which also output how often a line has been executed) such as Source-Based Code Coverage and a gcov-compatible one. Open source tools seems to have better support for gcov output in general, so I would recommend that route.
What command-line options to I pass to clang++ to instruct it to gather profiling data when the code is executed?
-fprofile-instr-generate -fcoverage-mapping when compiling and -fprofile-instr-generate when linking.-fprofile-arcs -ftest-coverageInto which file(s) is the gathered profiling data stored?
default.profraw in your current working directory. The profiling data file name can be changed by recompiling with -fprofile-instr-generate=filename or by setting the environment variable LLVM_PROFILE_FILE before running the executable.*.gcda and *.gcno files.What are the post-processing steps to convert the collected profile data into a graphical report that shows how often each function is called, what percentage of time is spent in each function
For Source-Based Code Coverage:
.profraw file into a .profdata file: llvm-profdata merge -o default.profdata -sparse=true default.profrawllvm-cov show --instr-profile default.profdata ./your_program to view the coverage in the terminal or use llvm-cov export ./your_program --instr-profile default.profdata > out.json to convert your profiling data to JSON and find/create a program to generate a report for you.For gcov-compatible output:
lcov or gcovr to generate HTML output. This lets you easily view line and branch coverage for each file. I tend to use gcovr since it is a simple pip install gcovr away if you don't have it installed. Then the usage would be gcovr --gcov-executable "llvm-cov gcov" -r . --html --html-details -o out.html.and from which functions each function is called (similar to https://s3-us-west-2.amazonaws.com/brunorijsman-public/example-rift-python-code-profile.png)?
For this type of information I would try to have a look at Callgrind and KCacheGrind. I have not found any tool which can generate this type of information given *.profdata or *.gcda files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With