Profiling timer expired when using gperftools with sort

好久不见. 提交于 2019-12-12 04:24:39

问题


I spent the whole day trying to make gperftools working :/

I tired different libunwind versions but when I successed in installing it I got the following error "Profiling timer expired" whenever I used std::system.

main.cpp:

#include <cstdlib>
int main(int argc, char** argv) {
    std::system("cut -f1 anyExistingFile | sort > newSortedFile");
    return 0;
}

I tired to perform profiling as following:

$ g++ main.cpp -o myApp -std=c++11
$ env CPUPROFILE=out.prof    LD_PRELOAD="/usr/local/lib/libprofiler.so" ./myApp
Profiling timer expired
PROFILE: interrupts/evictions/bytes = 0/0/64

then I did:

$ env LD_PRELOAD="/usr/local/lib/libprofiler.so" 
$ sort file
$ env LD_PRELOAD=
$ sort file

sort was not working when I had LD_PRELOAD set to "/usr/local/lib/libprofiler.so" !!

then I tried to use the static versions of the library:

$ g++ main.cpp -o myApp -std=c++11 /usr/local/lib/libtcmalloc_and_profiler.a
$ env CPUPROFILE=out.prof ./myApp

nothing happened, and out.prof was not created!

so I am wondering why I get "Profiling timer expired" when I use std::system(sort)? and is it the right way to use the static version of gperftools library?

P.S: 64-bit, gperftools=2.5, libunwind=1.1, linux Ubuntu 16.04.1


回答1:


The issue seems to be that when you set LD_PRELOAD you actually set it for everything in current shell's job, including childs processes that your program spawns. Same happens to CPUPROFILE environment variable. So cpu profiler gets activated for sort as well. And it looks like something within sort program is resetting SIGPROF signal handler to default without actually resetting corresponding interval timer. So when sort completes enough work it gets signal and default handler exits the program. Simple workaround is to unsetenv CPUPROFILE around your sort program. E.g.:

#include <cstdlib>
int main(int argc, char** argv) {
    std::system("cut -f1 /usr/share/dict/american-english-insane | CPUPROFILE='' sort  > /tmp/insane");
    return 0;
}

As for why static linking didn't work, it is because nothing in your program pulls in profiler symbols, so becomes effectively no-op w.r.t. profiling.



来源:https://stackoverflow.com/questions/42024496/profiling-timer-expired-when-using-gperftools-with-sort

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!