Search code examples
c++cmakeclangprofiling

How to do profiling using Clang compiler and CMake


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)

Solution

  • 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