Profiling/Analyzing Performance
-
Apparently/sadly (per questions in the past) I am the only user here who regularly profiles/analyzes performance of my applications! I was good with qmake, but now I have moved to cmake I don't know how to achieve the equivalent exactly.
-
If I use cmake's Profile configuration it builds/runs but I don't see any analysis anywhere. I don't see where it has put any generated run-analysis file (such as
perf.data
). May be related to next point.... -
If I use Analyze > Performance Analyzer it fails with "no samples/no perf" etc. warning. Yes, was also so in the past. I spent ages installing stuff for this, but the problem is that it requires a kernel patch (which disappears on each OS update) and editing Linux system files to enable. I don't want or need this. More to the point, when I did do it in the past I did not understand its graphs at all!
-
So I don't want to go down that/ the
perf
route, just as I abandoned it in the past. I just want to compile and link with good old gprof, quite good enough and intelligible for me. I managed this under qmake as below, and would just like to make that work for cmake.
In my old
.pro
files I put in the following comment to help me:# The following could be done here, # but instead is placed in ./mkspecs/features/gprof.prf (which is a symlink to ./gprof.prf) # to show how a new configuration can be written for use elsewhere #CONFIG(gprof) { # QMAKE_CFLAGS += -pg # QMAKE_CXXFLAGS += -pg # QMAKE_LFLAGS += -pg #}
Those three additional flags are all that I need for profiling with gprof. I "got clever" and put them in
./mkspecs/features/gprof.prf
so that qmake could use this for a template for a Configuration:...$ cat mkspecs/features/gprof.prf # New configuration for `gprof` # ./mkspecs/features/prof.prf should be a symlink to this message("Setting options for `gprof`") QMAKE_CXXFLAGS += -pg QMAKE_CFLAGS += -pg QMAKE_LFLAGS += -pg DEFINES += GPROF=1
However I am happy to use the first approach, no "template", from cmake project provided I can manually (a) create my own new Configuration (like Release or the existing Profile) for GProf in a project and (b) I know where exactly, and with what syntax, I can add those options changes to whatever cmake variables/
CMakeLists.txt
files.Thank you!
-
-
-
You can do this easily with CMake. Just add this code below at the top of your project:
option(ENABLE_PROFILING "This option will enable GNU profiling" OFF) if (ENABLE_PROFILING) add_compile_options(-pg) add_compile_definitions(GPROF=1) endif()
See it in action at this screencast:
-
@cristian-adam
Thanks, you're a star, will do this next week. And from what I see I can add this is a separate Configuration I create, name for myself and use from Creator, right? Will mark your post as solution then.You have earned your basic points for this answer! But if you want "brownie points": I can hardly remember much about it, but as I show above (second box-out) if I put the desired lines in
./mkspecs/features/gprof.prf
, relative I think to my source directory, that (because ofmkspecs/features
path and/or.prf
suffix?) made it so as soon as I entered Creator using qmake I got that as a new Configuration I could use in my project. Without even editing.pro
file. As something like a "Configuration Template" by doing that? So (although of course I can do it manually each time to eachCMakeLists.txt
and I won't die in a ditch over it, I like it available in many of my projects), does cmake have any equivalent like that? Or was that something only qmake could offer?As I said above, if you have had enough of me and would like your weekend to start now, please feel free! But if you want those brownie points... ;-) Thanks as always.
-
You can (ab)use Qt Creator's Package Manager Auto-Setup feature that injects a piece of CMake code in every CMake project loaded.
This was initially done for conan / vcpkg, but also for any CMake package managers.
In this case we copy the code above:
option(ENABLE_PROFILING "This option will enable GNU profiling" OFF) if (ENABLE_PROFILING) add_compile_options(-pg) add_compile_definitions(GPROF=1) endif()
and save it as
QtCreatorPackageManager.cmake
in the project's source directory.Qt Creator (
auto-setup.cmake
) will look to see if this file exists, and if it does, it loads it. -
@cristian-adam
Thank you Cristian. I tried to follow just what you show but somehow got in a mess. In case anyone else is interested, here is what I ended up doing to create the gprof-profiling configuration. I number the steps. If anyone wants to tell me what I did wrong/unnecessarily please feel free. I just know I ended up with what I intended. It is possible I have missed some step below.-
I put the lines shown in a
QtCreatorPackageManager.cmake
file. However, it is important to note that this lacked the equivalent of theQMAKE_LFLAGS += -pg
per my original qmake example. That is a linker option, and without it the executable will not produce the profile output file when run. I addedadd_link_options(-pg)
into that file. -
I did Build > Clear CMake Configuration. Now, I thought the screen cast above showed that being done, but now I don't see that and I don't know where I got this from. At any rate, since I presently had the Debug configuration selected I got into a mess where it said I had lost that or it was not configured. Exiting Creator and re-entering did not solve. Deleting the whole
build
sub-directory hierarchy did not revert. I sawCMakeLists.txt.user
had been updated so deleted that as well. Then I seemed to get back to where I had been, and Debug configuration was recovered. I don't know what was going on here. I only know that reset of instructions start from this clean state. -
Re-entering Creator now put me on Configure Project page. I accepted Configure Project button. Phew, everything is back!
-
Now I go to Projects page. Debug configuration is selected and populated.
-
I want to add a new GProf configuration. I expect to use Add button, but that is a non-editable which just shows the 5 pre-supplied configurations, so I don't know what that is about. So instead I press Clone... and clone to new GProf. Whether I was right starting from Debug to clone I do not know.
-
For Build Directory it has populated with
.../build/Desktop-Debug2
. I change theDebug2
toGProf
. It comes up with a Changing Build Directory dialog asking me to confirm "and start with a basic CMake configuration", which I don't understand bit I confirm. Then I get another dialog about something, I'm beyond lost here, just confirm/OK whatever you get asked. -
On Current Configuration tab I find
ENABLE_PROFILING
option and check its box. While I am here I expand Build Steps Details and add--verbose
to CMake arguments. I click button Run CMake back above under Current Configuration (else I think it's this which gives a dialog asking "Exit without saving?" even though there is no Save button). I may have done this at same time as #6 above. I'm lost :( -
Anyway, by now I select Build > Rebuild. From the
--verbose
option I can see the compilation includes-DGPROF=1 ... -pg
and the link includes-pg
, as desired. -
Finally, I can press Run or Start debugging... button and it runs my GProf configuration.
-
When I exit the profiled executable run I can (in a terminal)
cd build/Desktop-GProf/
and, lo and behold, I find the desiredgmon.out
file has been created there! So I can execute e.g.gprof myexecutable | more
and begin to look at gathered statistics.
I am exhausted! All I know is it's way harder than what I had to do with qmake for a new configuration, I didn't understand what steps were/were not necessary and I didn't understand what the various dialog challenges I got at various steps were about.
If someone wants to write a minimal, concise steps I needed to create a new configuration for GProf, including putting it in a
Desktop-GProf
directory instead ofDesktop-Debug2
without dialogs I don't understand, please be my guest... :) -
-
Working with CMake is a bit different. The
QtCreatorPackageManager.cmake
with the content:option(ENABLE_PROFILING "This option will enable GNU profiling" OFF) if (ENABLE_PROFILING) add_compile_options(-pg) add_link_options(-pg) add_compile_definitions(GPROF=1) endif()
Would be a feature that one would add to an existing project.
But if you want to have a build type and build directory specified, then you need to use a
CMakePresets.json
file.Here is one that works on macOS:
{ "version": 4, "cmakeMinimumRequired": { "major": 3, "minor": 23, "patch": 0 }, "configurePresets": [ { "name": "macOS-GProf", "displayName": "macOS GProf", "generator": "Unix Makefiles", "binaryDir": "${sourceDir}/build/${presetName}", "toolchainFile" : "$env{HOME}/Qt/6.8.0/macos/lib/cmake/Qt6/qt.toolchain.cmake", "cacheVariables": { "CMAKE_BUILD_TYPE": "GProf", "ENABLE_PROFILING": true } } ], "buildPresets": [ { "configurePreset": "macOS-GProf", "name": "makefile-verbose", "displayName": "Unix Makefiles verbose", "verbose": true } ] }
And the screen cast:
The downside with CMake Presets in Qt Creator is that you have to be explicit about Qt versions, it doesn't know anything about Qt. It's a CMake feature.
Also the
preset
->Kit
match is global. You will get preset kits from project A to project B. -
@cristian-adam
All good, thanks. Partly the dialogs confused me as to what it was asking me to choose/confirm at various points. Anyway for now at least I have a working profiling configuration, somehow! -