gperftools cpu profiler不支持多进程?

时间:2013-08-22 15:27:35

标签: linux profiling google-perftools gperftools

根据文档http://gperftools.googlecode.com/svn/trunk/doc/cpuprofile.html,cpu配置文件支持多进程并将生成独立的输出文件:

  

如果您的程序分叉,孩子们也会被分析(因为他们   继承相同的CPUPROFILE设置)。每个过程都是分析的   分别;区分子配置文件和父配置文件   并且相互之间,所有孩子都会附加他们的进程ID   到CPUPROFILE名称。

但是当我尝试如下:

// main_cmd_argv.cpp

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <gperftools/profiler.h>

int loop(int n) {
    int sum = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            sum = i + j;
            if (sum %3 == 0) {
                sum /= 3;
            }
        }
    }
    return 0;
}

int main(int argc, char* argv[]) {

    printf("%s\n%s\n", getenv("CPUPROFILE"), getenv("CPUPROFILESIGNAL"));

    if (argc > 1 && strcmp(argv[1], "-s")==0) {
        // single process
        loop(100000);
        printf("stoped\n");
    } else if (argc > 1 && strcmp(argv[1], "-m")==0) {
        // multi process
        pid_t pid = fork();
        if (pid < 0) {
            printf("fork error\n");
            return -1;
        }
        if (pid == 0) {    
            loop(100000);
            printf("child stoped\n");
        } else if (pid > 0) {
            loop(10000);
            printf("father stoped\n");
            wait(NULL);
        }        
    }   

    return 0;
}

// makefile

GPerfTools=/home/adenzhang/tools/gperftools

CCFLAGS=-fno-omit-frame-pointer -g -Wall

ALL_BINS=main_cmd_argv
all:$(ALL_BINS)

main_cmd_argv:main_cmd_argv.o
    g++ $(CCFLAGS) -o $@ $^ -L./ -L$(GPerfTools)/lib -Wl,-Bdynamic -lprofiler -lunwind

.cpp.o:
    g++ $(CCFLAGS) -c -I./ -I$(GPerfTools)/include -fPIC -o $@ $<
clean:
    rm -f $(ALL_BINS) *.o *.prof

// shell命令

$ make
g++ -fno-omit-frame-pointer -g -Wall -c -I./ -I/home/adenzhang/tools/gperftools/include -fPIC -o main_cmd_argv.o main_cmd_argv.cpp
g++ -fno-omit-frame-pointer -g -Wall -o main_cmd_argv main_cmd_argv.o -L./ -L/home/adenzhang/tools/gperftools/lib -Wl,-Bdynamic -lprofiler -lunwind
$ env CPUPROFILE=main_cmd_argv.prof ./main_cmd_argv -s
젩n_cmd_argv.prof
(null)
stoped
PROFILE: interrupts/evictions/bytes = 6686/3564/228416
$ /home/adenzhang/tools/gperftools/bin/pprof --text ./main_cmd_argv ./main_cmd_argv.prof 
Using local file ./main_cmd_argv.
Using local file ./main_cmd_argv.prof.
Removing killpg from all stack traces.
Total: 6686 samples
    6686 100.0% 100.0%     6686 100.0% loop
       0   0.0% 100.0%     6686 100.0% __libc_start_main
       0   0.0% 100.0%     6686 100.0% _start
       0   0.0% 100.0%     6686 100.0% main
$ rm main_cmd_argv.prof 
$ env CPUPROFILE=main_cmd_argv.prof ./main_cmd_argv -m
젩n_cmd_argv.prof
(null)
father stoped
child stoped
PROFILE: interrupts/evictions/bytes = 0/0/64
PROFILE: interrupts/evictions/bytes = 68/36/2624
$ ls 
main_cmd_argv  main_cmd_argv.cpp  main_cmd_argv.o  main_cmd_argv.prof  Makefile
$ /home/adenzhang/tools/gperftools/bin/pprof --text ./main_cmd_argv ./main_cmd_argv.prof 
Using local file ./main_cmd_argv.
Using local file ./main_cmd_argv.prof.
$

认为gperf不支持多进程,任何人都可以解释一下吗?谢谢!

2 个答案:

答案 0 :(得分:3)

很老,不知道你是否找到答案,但是......

似乎每个线程/ fork都应该使用ProfilerRegisterThread()注册自己; 您可以在以下两个问题中找到更多信息:HereHere

此处还有一个示例代码,类似于您的分支可以是registered的测试用例。

答案 1 :(得分:0)

我目前正在使用gperftools来分析mpi程序并遇到此问题。在谷歌搜索后,我发现在执行每个子流程时应该调用ProfilerStart(_YOUR_PROF_FILE_NAME_)ProfilerStop(),并且_YOUR_PRO_FILE_NAME_在不同流程之间必须是不同的。然后你可以分析每个过程的表现。

链接(也由ZRJ询问):
https://groups.google.com/forum/#!topic/google-perftools/bmysZILR4ik