折腾了好久,到目前还是没有让 gperftools cpu profiler 能在多进程环境下跑起来,问题如下:
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
运行过程
$ 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. $
先记下来吧,后面再想办法搞。
小广告贴的到处都是:http://stackoverflow.com/quest…,https://groups.google.com/foru…,http://www.searchtb.com/2012/1…,https://code.google.com/p/gper…
另外有一个讲性能分析的 stackoverflow 问答,顺便记下来:http://stackoverflow.com/quest…
最后的结果是这样的:
Aliaksey Kandratsenka
9月9日
发送至 google-perftoo.
It looks like profiler sets up timer for main thread early and then fork (which creates new thread) does not inherit this. I’m not very familiar with that code but I find it weird.
Anyways I was able to enable profiling in child by calling ProfilerRegisterThread right after ProfilerStart. Which set up profiling signal for child and made it work.
Pingback: 动态链接库中的全局变量 | ZRJ