使用 gprof 监测程序性能 (1)
来源:互联网 发布:网络带给我们的好处 编辑:程序博客网 时间:2024/06/05 16:21
代码如下 5.c:
#include <stdio.h>#include <pthread.h>#include <gperftools/profiler.h>void foo(int i){printf("%d\n", i);}void *thread( void *arg){ProfilerRegisterThread();int i = 0;while(1){foo(i++);if(i == 100000){break;}}return 0;}int main(void){//ProfilerStart("5.prof");pthread_t tid;pthread_create(&tid, NULL, thread, NULL);pthread_join(tid, NULL);//ProfilerStop();return 0;}
编译:
gcc 5.c -lpthread -lprofiler -g -o 5 -L /home/charles/install/lib -I /home/charles/install/include/
运行:
LD_PRELOAD=/home/charles/install/lib/libprofiler.so CPUPROFILE=5.prof ./5
会生成结果文件 5.prof.
使用 pprof 分析结果:
$ pprof --text ./5 5.profUsing local file ./5.Using local file 5.prof.Total: 26 samples 16 61.5% 61.5% 16 61.5% 0xb7783c31 6 23.1% 84.6% 6 23.1% 0xb7783c2f 2 7.7% 92.3% 2 7.7% _IO_vfprintf_internal 1 3.8% 96.2% 1 3.8% _IO_new_file_overflow 1 3.8% 100.0% 1 3.8% _IO_new_file_write 0 0.0% 100.0% 2 7.7% __clone 0 0.0% 100.0% 2 7.7% __printf 0 0.0% 100.0% 2 7.7% start_thread 0 0.0% 100.0% 2 7.7% thread
$ pprof --pdf ./5 5.prof > 5.pdfUsing local file ./5.Using local file 5.prof.
如果在代码中使用了
ProfilerStart("5.prof")
ProfilerStop();直接执行
./5
就会自动生成结果文件 5.prof
注意点:
1. 分析线程的时候,需要在线程入口函数调用
ProfilerRegisterThread()2. 使用 gperf 的时候,程序必须能正常退出。
当程序无法正常退出时,可以发信号给程序,在信号处理函数里面调用
ProfilerStop();
之后, prof文件才会有数据输出。#include <stdio.h>#include <pthread.h>#include <signal.h>#include <stdlib.h>#include <gperftools/profiler.h>void signal_handler(int signo){if( signo == SIGTERM){ProfilerStop();exit(0);}}void foo(int i){printf("%d\n", i);}void *thread( void *arg){ProfilerRegisterThread();int i = 0;while(1){foo(i++);}return 0;}int main(void){ProfilerStart("5.prof");signal(SIGTERM, &signal_handler);pthread_t tid;pthread_create(&tid, NULL, thread, NULL);pthread_join(tid, NULL);ProfilerStop();return 0;}
阅读全文
0 0
- 使用 gprof 监测程序性能 (1)
- 使用gprof对程序的性能分析(集合贴)
- gprof使用介绍-优化程序性能
- GPROF(性能监视工具)使用详解
- Linux C++程序进行性能分析工具gprof使用入门
- Linux C++程序进行性能分析工具gprof使用入门
- 使用gprof来对程序的性能分析总结
- 用gprof分析程序性能
- 学习使用Jwebap来监测程序性能
- 使用gprof分析程序(转贴)
- 程序性能调优之 怎样使用gprof和oprofile来分析 linux程序的性能(每个函数的调用次数与耗时)
- 程序性能调优之 怎样使用gprof和oprofile来分析 linux程序的性能(每个函数的调用次数与耗时)
- (转)程序性能调优之 怎样使用gprof和oprofile来分析 linux程序的性能(每个函数的调用次数与耗时)
- gprof---Linux程序性能分析工具
- 程序性能分析工具gprof介绍
- 利用gprof对Ocaml程序性能分析
- gprof && oprofile 分析linux程序性能瓶颈
- 系统性能监测(使用nmon、nmonanalyser)
- 科普系列之Linux内核中断
- 数据结构实验之查找六:顺序查找
- [LOJ6257][CodePlus 2017 12 月赛]可做题2-矩阵快速幂-乘法逆元
- Lua、Luajit、Python、Node.js和Java性能测试对比
- Tampermonkey(油猴子)插件安装、使用
- 使用 gprof 监测程序性能 (1)
- LeetCode-19-Remove Nth Node From End of List(C语言实现)
- Python scrapy 爬取安软市场
- c++实现二叉树的先序遍历,中序遍历,后序遍历(递归方法)
- java 开发模式之七 : 适配器模式
- LEDE SAMBA
- 佛爷芸: 机器学习算法原理总结系列---算法基础之(1)机器学习介绍
- 2.尾部的零
- LeetCode #743 Network Delay Time