ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

转载 Linux性能优化gprof使用

2022-06-06 16:31:55  阅读:220  来源: 互联网

标签:函数 Linux void gprof wrapper pthread 转载 data


  • gprof用于分析函数调用耗时,可用之抓出最耗时的函数,以便优化程序。
  • gcc链接时也一定要加-pg参数,以使程序运行结束后生成gmon.out文件,供gprof分析。
  • gprof默认不支持多线程程序,默认不支持共享库程序。
  1. gcc -pg 编译程序
  2. 运行程序,程序退出时生成 gmon.out
  3. gprof ./prog gmon.out -b 查看输出

要想产生gmon.out文件,必须在编译和链接时,都加上-pg  -g选项。

 

1 简介

改进应用程序的性能是一项非常耗时耗力的工作,但是究竟程序中是哪些函数消耗掉了大部分执行时间,这通常都不是非常明显的。GNU 编译器工具包所提供了一种剖析工具 GNU profiler(gprof)。gprof 可以为 Linux平台上的程序精确分析性能瓶颈。gprof精确地给出函数被调用的时间和次数,给出函数调用关系。

 

gprof 用户手册网站 http://sourceware.org/binutils/docs-2.17/gprof/index.html

 

2 功能

Gprof 是GNU gnu binutils工具之一,默认情况下linux系统当中都带有这个工具。

1. 可以显示“flat profile”,包括每个函数的调用次数,每个函数消耗的处理器时间,

2. 可以显示“Call graph”,包括函数的调用关系,每个函数调用花费了多少时间。

3. 可以显示“注释的源代码”--是程序源代码的一个复本,标记有程序中每行代码的执行次数。

 

3 原理

通过在编译和链接程序的时候(使用 -pg 编译和链接选项),gcc 在你应用程序的每个函数中都加入了一个名为mcount ( or  “_mcount”  , or  “__mcount” , 依赖于编译器或操作系统)的函数,也就是说你的应用程序里的每一个函数都会调用mcount, 而mcount 会在内存中保存一张函数调用图,并通过函数调用堆栈的形式查找子函数和父函数的地址。这张调用图也保存了所有与函数相关的调用时间,调用次数等等的所有信息。

 

4 使用流程

1. 在编译和链接时 加上-pg选项。一般我们可以加在 makefile 中。

2. 执行编译的二进制程序。执行参数和方式同以前。

3. 在程序运行目录下 生成 gmon.out 文件。如果原来有gmon.out 文件,将会被重写。

4. 结束进程。这时 gmon.out 会再次被刷新。

5. 用 gprof 工具分析 gmon.out 文件。

 

5 参数说明

l -b 不再输出统计图表中每个字段的详细描述。

l -p 只输出函数的调用图(Call graph的那部分信息)。

l -q 只输出函数的时间消耗列表。

l -e Name 不再输出函数Name 及其子函数的调用图(除非它们有未被限制的其它父函数)。可以给定多个 -e 标志。一个 -e 标志只能指定一个函数。

l -E Name 不再输出函数Name 及其子函数的调用图,此标志类似于 -e 标志,但它在总时间和百分比时间的计算中排除了由函数Name 及其子函数所用的时间。

l -f Name 输出函数Name 及其子函数的调用图。可以指定多个 -f 标志。一个 -f 标志只能指定一个函数。

l -F Name 输出函数Name 及其子函数的调用图,它类似于 -f 标志,但它在总时间和百分比时间计算中仅使用所打印的例程的时间。可以指定多个 -F 标志。一个 -F 标志只能指定一个函数。-F 标志覆盖 -E 标志。

l -z 显示使用次数为零的例程(按照调用计数和累积时间计算)。

 

一般用法: gprof –b 二进制程序 gmon.out >report.txt

 

6 报告说明

Gprof 产生的信息解释:

  %time

Cumulative

seconds

Self 

Seconds

Calls

Self

TS/call

Total

TS/call

name

该函数消耗时间占程序所有时间百分比

程序的累积执行时间

(只是包括gprof能够监控到的函数)

该函数本身执行时间

(所有被调用次数的合共时间)

函数被调用次数

函数平均执行时间

(不包括被调用时间)

(函数的单次执行时间)

函数平均执行时间

(包括被调用时间)

 

(函数的单次执行时间)

函数名

 

Call Graph 的字段含义:

Index

%time

Self

Children

Called

Name

索引值

函数消耗时间占所有时间百分比

函数本身执行时间

执行子函数所用时间

被调用次数

函数名

 

注意:

程序的累积执行时间只是包括gprof能够监控到的函数。工作在内核态的函数和没有加-pg编译的第三方库函数是无法被gprof能够监控到的,(如sleep()等)

Gprof 的具体参数可以 通过 man gprof 查询。

 

7 共享库的支持

对于代码剖析的支持是由编译器增加的,因此如果希望从共享库中获得剖析信息,就需要使用 -pg 来编译这些库。提供已经启用代码剖析支持而编译的 C 库版本(libc_p.a)。

如果需要分析系统函数(如libc库),可以用 –lc_p替换-lc。这样程序会链接libc_p.so或libc_p.a。这非常重要,因为只有这样才能监控到底层的c库函数的执行时间,(例如memcpy(),memset(),sprintf()等)。

gcc example1.c –pg -lc_p -o example1

注意要用ldd ./example | grep libc来查看程序链接的是libc.so还是libc_p.so

 

8 用户时间与内核时间

gprof 的最大缺陷:它只能分析应用程序在运行过程中所消耗掉的用户时间,无法得到程序内核空间的运行时间。通常来说,应用程序在运行时既要花费一些时间来运行用户代码,也要花费一些时间来运行 “系统代码”,例如内核系统调用sleep()。

有一个方法可以查看应用程序的运行时间组成,在 time 命令下面执行程序。这个命令会显示一个应用程序的实际运行时间、用户空间运行时间、内核空间运行时间。

如 time ./program

输出:

real    2m30.295s

user    0m0.000s

sys     0m0.004s

 

9 注意事项

1. g++在编译和链接两个过程,都要使用-pg选项。

2. 只能使用静态连接libc库,否则在初始化*.so之前就调用profile代码会引起“segmentation fault”,解决办法是编译时加上-static-libgcc或-static。

3. 如果不用g++而使用ld直接链接程序,要加上链接文件/lib/gcrt0.o,如ld -o myprog /lib/gcrt0.o myprog.o utils.o -lc_p。也可能是gcrt1.o

4. 要监控到第三方库函数的执行时间,第三方库也必须是添加 –pg 选项编译的。

5. gprof只能分析应用程序所消耗掉的用户时间.

6. 程序不能以demon方式运行。否则采集不到时间。(可采集到调用次数)

7. 首先使用 time 来运行程序从而判断 gprof 是否能产生有用信息是个好方法。

8. 如果 gprof 不适合您的剖析需要,那么还有其他一些工具可以克服 gprof 部分缺陷,包括 OProfile 和 Sysprof。

9. gprof对于代码大部分是用户空间的CPU密集型的程序用处明显。对于大部分时间运行在内核空间或者由于外部因素(例如操作系统的 I/O 子系统过载)而运行得非常慢的程序难以进行优化。

10. gprof 不支持多线程应用,多线程下只能采集主线程性能数据。原因是gprof采用ITIMER_PROF信号,在多线程内只有主线程才能响应该信号。但是有一个简单的方法可以解决这一问题:http://sam.zoy.org/writings/programming/gprof.html

11. gprof只能在程序正常结束退出之后才能生成报告(gmon.out)。

a) 原因: gprof通过在atexit()里注册了一个函数来产生结果信息,任何非正常退出都不会执行atexit()的动作,所以不会产生gmon.out文件。

b) 程序可从main函数中正常退出,或者通过系统调用exit()函数退出。

 

 

10 多线程应用

gprof 不支持多线程应用,多线程下只能采集主线程性能数据。原因是gprof采用ITIMER_PROF信号,在多线程内只有主线程才能响应该信号。

采用什么方法才能够分析所有线程呢?关键是能够让各个线程都响应ITIMER_PROF信号。可以通过桩子函数来实现,重写pthread_create函数。

  1. gprof-helper.c
  2. #define _GNU_SOURCE
  3. #include <sys/time.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <dlfcn.h>
  7. #include <pthread.h>
  8. static void * wrapper_routine(void *);
  9. /* Original pthread function */
  10. static int (*pthread_create_orig)(pthread_t *__restrict,
  11. __const pthread_attr_t *__restrict,
  12. void *(*)(void *),
  13. void *__restrict) = NULL;
  14. /* Library initialization function */
  15. void wooinit(void) __attribute__((constructor));
  16. void wooinit(void)
  17. {
  18. pthread_create_orig = dlsym(RTLD_NEXT, "pthread_create");
  19. fprintf(stderr, "pthreads: using profiling hooks for gprof/n");
  20. if(pthread_create_orig == NULL)
  21. {
  22. char *error = dlerror();
  23. if(error == NULL)
  24. {
  25. error = "pthread_create is NULL";
  26. }
  27. fprintf(stderr, "%s/n", error);
  28. exit(EXIT_FAILURE);
  29. }
  30. }
  31. /* Our data structure passed to the wrapper */
  32. typedef struct wrapper_s
  33. {
  34. void * (*start_routine)(void *);
  35. void * arg;
  36. pthread_mutex_t lock;
  37. pthread_cond_t wait;
  38. struct itimerval itimer;
  39. } wrapper_t;
  40. /* The wrapper function in charge for setting the itimer value */
  41. static void * wrapper_routine(void * data)
  42. {
  43. /* Put user data in thread-local variables */
  44. void * (*start_routine)(void *) = ((wrapper_t*)data)->;start_routine;
  45. void * arg = ((wrapper_t*)data)->;arg;
  46. /* Set the profile timer value */
  47. setitimer(ITIMER_PROF, &((wrapper_t*)data)->;itimer, NULL);
  48. /* Tell the calling thread that we don't need its data anymore */
  49. pthread_mutex_lock(&((wrapper_t*)data)->;lock);
  50. pthread_cond_signal(&((wrapper_t*)data)->;wait);
  51. pthread_mutex_unlock(&((wrapper_t*)data)->;lock);
  52. /* Call the real function */
  53. return start_routine(arg);
  54. }
  55. /* Our wrapper function for the real pthread_create() */
  56. int pthread_create(pthread_t *__restrict thread,
  57. __const pthread_attr_t *__restrict attr,
  58. void * (*start_routine)(void *),
  59. void *__restrict arg)
  60. {
  61. wrapper_t wrapper_data;
  62. int i_return;
  63. /* Initialize the wrapper structure */
  64. wrapper_data.start_routine = start_routine;
  65. wrapper_data.arg = arg;
  66. getitimer(ITIMER_PROF, &wrapper_data.itimer);
  67. pthread_cond_init(&wrapper_data.wait, NULL);
  68. pthread_mutex_init(&wrapper_data.lock, NULL);
  69. pthread_mutex_lock(&wrapper_data.lock);
  70. /* The real pthread_create call */
  71. i_return = pthread_create_orig(thread,
  72. attr,
  73. &wrapper_routine,
  74. &wrapper_data);
  75. /* If the thread was successfully spawned, wait for the data
  76. * to be released */
  77. if(i_return == 0)
  78. {
  79. pthread_cond_wait(&wrapper_data.wait, &wrapper_data.lock);
  80. }
  81. pthread_mutex_unlock(&wrapper_data.lock);
  82. pthread_mutex_destroy(&wrapper_data.lock);
  83. pthread_cond_destroy(&wrapper_data.wait);
  84. return i_return;
  85. }
  86. ///
  87. 然后编译成动态库 gcc -shared -fPIC gprof-helper.c -o gprof-helper.so -lpthread -ldl
  88. 使用例子:
  89. /a.c/
  90. #include <stdio.h>;
  91. #include <stdlib.h>;
  92. #include <unistd.h>;
  93. #include <pthread.h>;
  94. #include <string.h>;
  95. void fun1();
  96. void fun2();
  97. void* fun(void * argv);
  98. int main()
  99. {
  100. int i =0;
  101. int id;
  102. pthread_t thread[100];
  103. for(i =0 ;i< 100; i++)
  104. {
  105. id = pthread_create(&thread[i], NULL, fun, NULL);
  106. printf("thread =%d/n",i);
  107. }
  108. printf("dsfsd/n");
  109. return 0;
  110. }
  111. void* fun(void * argv)
  112. {
  113. fun1();
  114. fun2();
  115. return NULL;
  116. }
  117. void fun1()
  118. {
  119. int i = 0;
  120. while(i<100)
  121. {
  122. i++;
  123. printf("fun1/n");
  124. }
  125. }
  126. void fun2()
  127. {
  128. int i = 0;
  129. int b;
  130. while(i<50)
  131. {
  132. i++;
  133. printf("fun2/n");
  134. //b+=i;
  135. }
  136. }
  137. ///
  138. gcc -pg a.c gprof-helper.so
  139. 运行程序:
  140. ./a.out
  141. 分析gmon.out:
  142. gprof -b a.out gmon.out

 

标签:函数,Linux,void,gprof,wrapper,pthread,转载,data
来源: https://www.cnblogs.com/ooouliwa/p/16348666.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有