3

Question

1/ What output I should expect while I would like to do profiling using clang compiler?
2/ How can I do profiling for a C++ project which uses clang as a compilerandCMake` as a build tool?

Regrading profiling what I have used

1/ Firstly, I have used valgrind tool to check the performance of a cpp executable.
2/ Later, I have used g++ compiler and gone through this where I have seen steps to perform profiling with gprof. Stuffs using gprof I have done by command line. From, this source I came to know that gprof can provide a output of text file (mentioned as analysis.txt) where function call number, execution time etc is written.

My goal and approaches I have taken so far

  • Now, in my project, I can only use clang compiler and CMake build tool. I have read clang documentation, mainly this and this.
  • In first trial, I have put two cpp file in the same directory (which is obviously not the desired project structure) and followed the following command to see how Instrumentation is done and what outcome is coming
clang++-10 -fprofile-instr-generate -fcoverage-mapping test_gprof.cpp test_gprof_new.cpp -o code_coverage
LLVM_PROFILE_FILE="code_coverage.profraw" ./code_coverage
llvm-profdata merge -sparse code_coverage.profraw -o code_coverage.profdata
llvm-cov show -show-line-counts-or-regions --show-regions=1 --show-expansions ./code_coverage -instr-profile=code_coverage.profdata
llvm-cov report ./code_coverage -instr-profile=code_coverage.profdata

I am really not sure whether I have followed correct steps or not, but I have expected to see some analysis statistics.

  • Finally, I can see a report on which I have understood nothing. Here, my first question came to mind that what exactly I can expect while I do profiling?
  • And, I have no idea how to activate this profiling process in CMake for clang compiler. A dummy folder structure is given below which resembles the real one
clang_profile_cmake/
├── CMakeLists.txt
├── example
│   └── main.cpp
├── include
│   ├── test_gprof.h
│   └── test_gprof_new.h
├── README.md
└── src
    ├── test_gprof.cpp
    └── test_gprof_new.cpp

Recent search (Still unable to generate any profiling data by CMake, Clang compiler)

  • I have to add LLVM to my project. I have followed this
  • The CMakeLists.txt file I am using is available here
  • Don't understand how/where/in which step I can enable the profiling flags (-fprofile-instr-generate -fcoverage-mapping, LLVM_PROFILE_FILE, llvm-profdata merge ... etc)
user10634362
  • 549
  • 5
  • 21
  • Does this answer your question? [How to use gprof with cmake](https://stackoverflow.com/questions/26491948/how-to-use-gprof-with-cmake) – bremen_matt Oct 04 '22 at 07:54
  • @bremen_matt I have tried this and also [this answer 1](https://stackoverflow.com/a/40922025/10634362) & [this answer 2](https://stackoverflow.com/a/26657026/10634362). But got the following error `clang: warning: argument unused during compilation: '-pg' [-Wunused-command-line-argument] clang: warning: argument unused during compilation: '-pg' [-Wunused-command-line-argument]`. I am wondering whether `Clang` or `LLVM` supports `-pg` option or not but [It shows that clang support -pg](https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-pg). – user10634362 Oct 04 '22 at 08:35
  • @bremen_matt I am not sure will I use `gprof` with `Clang` or should I follow [Profiling with Instrumentation](https://clang.llvm.org/docs/UsersManual.html#profiling-with-instrumentation). No idea is there difference or priority. – user10634362 Oct 04 '22 at 08:37
  • @bremen_matt I have figured out the problem. It is needed to execute the binary and the have to use `gprof banary_name gmon.out > analysis.txt`, with respect to [this](https://www.thegeekstuff.com/2012/08/gprof-tutorial/). But funny thing is, after doing this content of `analysis.txt` is kinda weird. No information of the used function is written there. – user10634362 Oct 04 '22 at 13:07
  • My poor understanding is that the pg flag is inserting some stuff into the binary that the profiler then picks up on. It might be helpful to build the binary as RelWithDebInfo or plain old Debug. Perhaps then you will get useful info. That is a complete guess on my part... – bremen_matt Oct 05 '22 at 08:08
  • I have found the answer, although if I can get a response from anyone from the community who has used it before would be nice. I am posting the answer/approach which has solved it – user10634362 Oct 05 '22 at 12:51
  • @bremen_matt `pg` flag is for `gprof` profiling tool what I have understood. – user10634362 Oct 05 '22 at 13:10

1 Answers1

5

What I have found that, I have to learn more how to deal with CMake as the total answer was just in front of me, and now I have just added those command in CMake.

clang++-10 -fprofile-instr-generate -fcoverage-mapping test_gprof.cpp test_gprof_new.cpp -o code_coverage

Following is the present CMake file

# Set Clang Compiler
set(CMAKE_CXX_COMPILER "/usr/bin/clang++-10")
set(CMAKE_CXX_COMPILER clang++-10)

# Set llvm clang instrumentation compile flags
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping")

# Project name
project(clang_profiling_cmake)

# Next will be filled with common CMake Pattern

To build or to see the steps of profiling, one can use the following script. In my case, I have set output executable name as clang_prof_exec.

rm -rf bin/ build/ lib/
mkdir build
cd build
cmake ..
make
cd ../bin

# creation of profraw file by executing the executable binary
LLVM_PROFILE_FILE="clang_prof_exec.profraw" ./clang_prof_exec

# Creation of profile data
llvm-profdata merge -sparse clang_prof_exec.profraw -o clang_prof_exec.profdata

# following commands are needed to investigate profiling output. Use any of these
llvm-cov show ./clang_prof_exec -instr-profile=clang_prof_exec.profdata
llvm-cov report ./clang_prof_exec -instr-profile=clang_prof_exec.profdata
user10634362
  • 549
  • 5
  • 21
  • The `CMAKE_CXX_COMPILER` variable should never be manually set in a CMake file unless it's a [CMake toolchain File](https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html). Otherwise this should only be set either by a `-D` from command line, from a specified toolchain file, or from detecting the `CXX` env variable – Human-Compiler Oct 06 '22 at 20:49