I would like to use gcc's gprof line-by-line profiling. But after compiling my program, running it and executing gprof -l binary_name I get messages like:
gprof: somebody miscounted: ltab.len=9403 instead of 9391
gprof binary_name runs without error but I have not been able to run with the -l option. I was not able to find documentation on this. Google autocomplete indicates I am not the first to get this error but I could not find any threads on it. I have tried g++-9 g++-10 and g++-11. I have tried compiling with -g and -ggdb. I considered that maybe gprof did not like my program being multithreaded but even removing the OpenMP dependency and telling libtorch to use 1 thread I get the same behavior.
I have the suspicion that this error was not meant to be seen by the end user as it gives no hints on resolution and mentions ltab.len which is most likely a variable in gcc code no user would be familiar with.
I don't know if this is a general solution, but for me I realized this error occurs when you enable optimizations. If I compile with:
g++ -g -pg -O3 main.cpp # gprof: somebody miscounted: ltab.len=1034 instead of 1027
g++ -g -pg -O2 main.cpp # gprof: somebody miscounted: ltab.len=775 instead of 768
g++ -g -pg -O1 main.cpp # gprof: somebody miscounted: ltab.len=640 instead of 633
g++ -g -pg -O0 main.cpp # this works
I guess after compiler optimizations, gprof cannot map profiler output to the lines.
I was able to get g++ -pg -g with -02 optimizations to work by removing the -l from gprof command.
So first compile and run the program. Then move gmon.out and the binary compiled with debug options into the same directory. Then run
sudo gprof binary_name gmon.out binary_name > output.txt
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