We have a few threads in a particular process that increase CPU usage over time on FreeBSD 12.3. Reviewing the source code associated with these threads did not identify anything that should be taking a lot of CPU time, much less anything that would increase for a longer-running process.
On Windows, I would debug using the Visual Studio CPU Usage profiler, which shows the time spent in each method (and some system calls). Is there a tool or feature that would allow something like this on FreeBSD for C++ built with Clang? I know there are plenty of instrumentation features available, but have not found the right one yet.
If there is a tool known to be useful for CPU usage profiling of C++ code, are there any particular Clang compile flags needed, and is there any documentation with some usage examples?
On Windows, I would debug using the Visual Studio CPU Usage profiler, which shows the time spent in each method (and some system calls). Is there a tool or feature that would allow something like this on FreeBSD for C++ built with Clang? I know there are plenty of instrumentation features available, but have not found the right one yet.
If there is a tool known to be useful for CPU usage profiling of C++ code, are there any particular Clang compile flags needed, and is there any documentation with some usage examples?
I have experience developing primarily on Windows, and have used Microsoft Visual Studio's Performance Profiler to track down issues. Documentation includes step-by-step instructions and screenshots of example results. I have very little experience with either Linux or FreeBSD. Our software is compiled on Windows; we have makefiles that references clang in a MinGW cross-compiler (originally prepared according to this page) to build our C++ code base for FreeBSD. After deploying to our FreeBSD 12.3 test environment, we interact with it using an SSH session. This test environment currently has very few packages that are not installed out-of-the-box with the FreeBSD installer.
Problem
We need to identify a similar tool to run CPU profiling on FreeBSD, also with step-by-step instructions and screenshots of example results (to confirm that this was the right tool for the job).
Solution
The solution that worked for me (see all posts for breadcrumbs):
- If not already done, compile C++ code with the
-g
compiler option - Install the valgrind() package with
sudo pkg install valgrind
- For documentation with screenshots, see the article by Paul Floyd: Valgrind Part 4: Cachegrind and Callgrind
- Use
valgrind --tool=callgrind --instr-atstart=no --log-file=cg.out ./<my_app> <my_app_args>
to start the app with collection disabled.- Expect a process slowdown, even with collection disabled
- Use
callgrind_control --instr=on
to enable collection - Use QCacheGrind on Windows to process and view results from valgrind() on FreeBSD.
Other Suggestions
These may work for others (see all posts for breadcrumbs):
- When using valgrind, use the lighter-weight cachegrind tool.
- My attempt did not give meaningful results
- Use dtrace() for collection and for Flame Graphs for processing (per cracauer@ )
- My attempt encountered an error:
dtrace: failed to initialize dtrace: DTrace device not available on system
- My attempt encountered an error:
- Use pmcstat() for collection (per Paul Floyd)
- My attempt encountered an error:
pmcstat: ERROR: Initialization of the pmc(3) library failed: No such file or directory
- My attempt encountered an error:
- Use google perftools / pprof (per Paul Floyd)
- My attempt encountered error compiling with the
-lprofiler
(using the cross-compiler) - I did not install the package, so not sure if this requires the "google-perftools" or the "pprof" package
- My attempt encountered error compiling with the
Last edited: