zoukankan      html  css  js  c++  java
  • Linux性能调优之gprof和oprofile

    为了更好的优化程序性能,我们必须找到性能瓶颈点,“好钢用在刀刃上”才能取 得好的效果,否则可能白做工作。

    为了找到关键路径,我们可以使用profilng技术,在linux平台上,我们可以使用gprof和oprofile工 具。

    • gprof是GNU工具之一,它在编译的时候在每个函数的出入口加入了profiling的代码,运行时统计程序在用户态的 执行信息,可以得到每个函数的调用次数,执行时间,调用关系等信息,简单易懂。适合于查找用户级程序的性能瓶颈,对于很多时间都在内核态执行的程 序,gprof不适合。
    • oprofile也是一个开源的profiling工具,它使用硬件调试寄存器来统计信息,进 行profiling的开销比较小,而且可以对内核进行profiling。它统计的信息非常的多,可以得到cache的缺失率,memory的访存信 息,分支预测错误率等等,这些信息gprof是得不到的,但是对于函数调用次数,它是不能够得到的。。

    简单来说,gprof简单,适合于查找用户级程序的瓶颈,而oprofile稍显复杂,但是得到的信息更多,更适合调试系统软件。
    我们以编译运行hello.c为例,来说明如何使用这两个工具,这里不解释具体结果的含义,要想详细了解每个结果代表什么意思,可以看一下参考资料中官方 站点上的doc信息,里面会给你详尽的解释。
    gprof Quick Start
    gprof是gnu binutils工具之一,默认情况下linux系统当中都带有这个工具。

    1. 使用 -pg 选项来编译hello.c,如果要得到带注释的源码清单,则需要增加 -g 选项。运行: gcc -pg -g -o hello hello.c
    2. 运行应用程序: ./hello 会在当前目录下产生gmon.out文件
    3. 使用gprof来分析 gmon.out文件,需要把它和产生它的应用程序关联起来:
      1. gprof hello gmon.out -p 得到每个函数占用的执行时间
      2. gprof hello gmon.out -q 得到call graph,包含了每个函数的调用关系,调用次数,执行时间等信息。
      3. gprof hello gmon.out -A 得到一个带注释的“源代码清单”,它会注释源码,指出每个函数的执行次数。这需要在编译的时候增加 -g选项。

    oprofile Quick Start
    oprofile是sourceforge上面的一个开源项目,在2.6内核上带有这个工具,好像只有smp系统才有。比较老的系统,需要自己安装,重新 编译内核。
        oprofile是一套工具,分别完成不同的事情。
    op_help: 列出所有支持的事件。
    opcontrol:设置需要收集的 事件。
    opreport: 对结果进行统计输出。
    opannaotate:产生带注释的源/汇编文件,源语言级的注释需要编译源文件时的 支持。
    opstack:    产生调用图profile,但要求x86/2.6的平台,并且linux2.6安装了call-graph patch
    opgprof:    产生如gprof相似的结果。
    oparchive: 将所有的原始数据文件收集打包,可以到另一台机器上进行分析。
    op_import: 将采样的数据库文件从另一种abi转化成本地格式。    运行oprofile需要root权限,因为它要加载profile模块,启动oprofiled后台程序等。所以在运行之前,就需要切换到root。

    1. opcontrol --init 加载模块,mout /dev/oprofile 创建必需的文件和目录
    2. opcontrol --no-vmlinux 或者 opcontrol --vmlinux=/boot/vmlinux-`uname -r` 决定是否对kernel进行profiling
    3. opcontrol --reset 清楚当前会话中的数据
    4. opcontrol --start 开始profiling
    5. ./hello 运行应用程序,oprofile会对它进行profiling
    6. opcontrol --dump 把收集到的数据写入文件
    7. opcontrol --stop 停止profiling
    8. opcotrol -h 关闭守护进程oprofiled
    9. opcontrol --shutdown 停止oprofiled
    10. opcontrol --deinit 卸载模块

    常用的是3→7这几个过程,得到性能数据之后,可以使用 opreport, opstack, opgprof, opannotate几个工具进行分析,我常用的是opreport, opannotate进行分析。

    1. opreport使用 http://oprofile.sourceforge.net/doc/opreport.html
    2. opannotate使用 http://oprofile.sourceforge.net/doc/opannotate.html
    3. opgprof使用 http://oprofile.sourceforge.net/doc/opgprof.html

    最常用的是 opreport,这个可以给出image和symbols的信息,比如我想得到每个函数的执行时间占用比例等信息,用来发现系统性能瓶颈。 opannotate可以对源码进行注释,指出哪个地方占用时间比较多。常用命令如下:

    • opreport -l /bin/bash --exclude-depand --threshold 1 , 用来发现系统瓶颈。
    • opannotate --source --output-dir=annotated /usr/local/oprofile-pp/bin/oprofiled
    • opannotate --source --base-dirs=/tmp/build/libfoo/ --search-dirs=/home/user/libfoo/ --output-dir=annotated/ /lib/libfoo.so

    网络资源

        1. gprof 用户手册 http://sourceware.org/binutils/docs-2.17/gprof/index.html
        2. oprofile官方站点 http://oprofile.sourceforge.net/
        3. Linux性能评测工具之一:gprof篇 https://blog.csdn.net/stanjiang2010/article/details/5655143
        4. 使用 GNU profiler 来提高代码运行速度 http://www-128.ibm.com/developerworks/cn/linux/l-gnuprof.html
        5. 使用 OProfile for Linux on POWER 识别性能瓶颈 http://www-128.ibm.com/developerworks/cn/linux/l-pow-oprofile/
  • 相关阅读:
    一个网络狂人的财富轨迹
    婚姻的精髓
    软件史上最伟大的十大程序员
    由瓜子理论引出的人力资源管理启示
    感情裂缝的"维修工" 在生活抛锚的地方起航
    寻找更新过的数据
    asp.net mvc中TempData和ViewData的区别
    SQL Server Backup
    VS字符串时间转换用法
    SQL Server 根据动态条件insert,update语句
  • 原文地址:https://www.cnblogs.com/klcf0220/p/9987398.html
Copyright © 2011-2022 走看看