gpt4 book ai didi

linux - gperftools cpu profiler 不支持多进程?

转载 作者:IT王子 更新时间:2023-10-29 00:35:00 28 4
gpt4 key购买 nike

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

If your program forks, the children will also be profiled (since they inherit the same CPUPROFILE setting). Each process is profiled separately; to distinguish the child profiles from the parent profile and from each other, all children will have their process-id appended to the CPUPROFILE name.

但是当我尝试如下时:

//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;
}

//生成文件

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.
$

好像gperf不支持多进程,谁能解释一下?谢谢!

最佳答案

很老了,不知道你有没有找到答案,但是...

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

这里还有一个示例代码,类似于您的测试用例,其中 fork 可以是 registered .

关于linux - gperftools cpu profiler 不支持多进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18384760/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com