Chinaunix首页 | 论坛 | 博客
  • 博客访问: 922217
  • 博文数量: 63
  • 博客积分: 568
  • 博客等级: 中士
  • 技术积分: 3435
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-05 11:44
文章分类
文章存档

2016年(4)

2015年(6)

2014年(3)

2013年(27)

2012年(23)

分类: LINUX

2016-12-03 16:54:51

perf profiling 分析程序性能

程序性能分析

perf 有一个功能就是按一定频率采集某一个程序的调用栈,然后对调用栈进行统计分析。如果某一个代码路径在采集结果中出现的越平凡,说明程序消耗在这个代码路径上的时间也就越多。这样我们就能很快找到程序调用最频繁的代码路径。

perf命令

perf record -F 99 -p $(pidof test1) -g -- sleep 300 这个命令是采集test1程序相关的调用栈,采样频率为99Hz,-g表示将调用栈打印出来,-- sleep 30表示采样时间是300秒

perf report -g --stdio 这个命令是查看采用的结果

例子分析

为了了解这个工具有什么样的作用,我特意写了一个程序来进行分析。

#include

int functiona(void)
{
    int c = 0;
    int count = 0;
    for(c = 1; c < 20000; c++)
    {
        count++;
    }
    return;
}

int functionb(void)
{
    int c = 0;
    int count = 0;
    for(c = 1; c < 40000; c++)
    {
        count++;
    }
    return;
}


int main(int argc, char * argv[])
{
    for(;;)
    {
        functiona();
        printf("call function a\n");
        functionb();
        printf("call function b\n");
    }
    return;
}

通过编译命令gcc -g -o test1 test1.c

测试过程

  1. test1 > /de v/null &
  2. perf record -F 99 -p $(pidof test1) -g -- sleep 300
  3. perf script > out.perf
  4. ./stackcollapse-perf.pl out.perf > out.folded
  5. ./flamegraph.pl out.folded > out.svg

测试结果分析


阅读(12831) | 评论(3) | 转发(2) |
给主人留下些什么吧!~~

l4950512752017-02-17 20:27:54

谢谢分享!

oplain2017-02-08 16:15:07

谢谢分享,[Python 字符串(String)总结](http://www.niaogejc.com/python/python-string.html)<a href=\'http://www.niaogejc.com/python/python-string.html\'>Python 字符串(String)总结</a>,这个也总结的不错。

oska8742017-02-05 23:21:09

./stackcollapse-perf.pl out.perf > out.folded
./flamegraph.pl out.folded > out.svg

这两个脚本是?