使用带有排序的 gperftools 时,分析计时器已过期

Profiling timer expired when using gperftools with sort

本文关键字:计时器 过期 gperftools 排序      更新时间:2023-10-16

我花了一整天的时间试图让gperftools工作:/

厌倦了不同的libunwind版本,但是当我成功安装它时,每当我使用std::system时,我都会收到以下错误"分析计时器已过期"。

主.cpp:

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

我厌倦了执行以下分析:

$ 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

然后我做到了:

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

当我LD_PRELOAD设置为"/usr/local/lib/libprofiler.so"时,排序不起作用!

然后我尝试使用库的静态版本:

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

什么也没发生,也没有创建Out.Prof!

所以我想知道为什么当我使用 std::system(sort) 时我得到"分析计时器已过期"? 这是使用静态版本的 gperftools 库的正确方法吗?

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

问题似乎是,当你设置LD_PRELOAD时,你实际上是为当前shell作业中的所有内容设置的,包括程序生成的子进程。同样的情况也发生在 CPUPROFILE 环境变量上。因此,cpu 分析器也会被激活以进行排序。看起来排序程序中的某些东西正在将 SIGPROF 信号处理程序重置为默认值,而无需实际重置相应的间隔计时器。因此,当排序完成足够的工作时,它会得到信号,默认处理程序退出程序。简单的解决方法是围绕排序程序取消设置CPUPROFILE。例如:

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

至于为什么静态链接不起作用,那是因为程序中没有任何内容会拉入探查器符号,因此实际上变成了无操作 w.r.t. 分析。