• 性能测试:oprofile的学习使用


    一、oprofile简介

    Oprofile是linux上的性能监测工具,有人说是性能测试的神器。通过CPU硬件提供的性能计数器对事件进行采样,从代码层面分析程序的性能消耗情况,找出程序性能的问题点。

    oProfile是Linux平台上的一个功能强大的性能分析工具,支持两种采样(sampling)方式:基于事件的采样(eventbased)和基于时间的采样(timebased)。
    基于事件的采样是oProfile只记录特定事件(比如L2 cache miss)的发生次数,当达到用户设定的定值时oProfile就记录一下(采一个样)。这种方式需要CPU内部有性能计数器(performace counter)。
    基于时间的采样是oProfile借助OS时钟中断的机制,每个时钟中断oProfile都会记录一次(采一次样),引入此种采样方式的目的在于提供对没有性能计数器的CPU的支持,其精度相对于基于事件的采样要低。因为要借助OS时钟中断的支持,对禁用中断的代码oProfile不能对其进行分析。

    oProfile在Linux上分两部分,一个是内核模块(oprofile.ko),一个为用户空间的守护进程(oprofiled)。前者负责访问性能计数器或者注册基于时间采样的函数(使用register_timer_hook注册之,使时钟中断处理程序最后执行profile_tick时可以访问之),并采样置于内核的缓冲区内。后者在后台运行,负责从内核空间收集数据,写入文件。

    二、原理

    CPU都提供一个性能计数器的东西(performance counter),大致的原理就是程序可以注册告诉CPU对什么event感兴趣(比如CPU_CYCLE,CPU经历了一次时钟周期),然后CPU在执行了相应的操作后,就会在性能计数器上加1,这样程序就可以取出。所以,使用OProfile来定位CPU使用率的问题,就变成了让oprofile收集 程序运行过程中哪个可执行程序(或是so)中的哪个function,消耗的CPU CYCLE最多。

    也就是说我们可以通过收集数据知道哪个程序的哪个函数消耗的cpu时间占比。这种测试方法结果更符合实际情况,属于代码层面的测试。

    三、Ubuntu16.04下安装oprofile

    (一)、准备工作

    Popt下载:http://www.linuxfromscratch.org/blfs/view/svn/general/popt.html

    binutils下载:http://ftp.gnu.org/gnu/binutils/?C=M;O=D

    oprofile下载:在官网上下载,我下载的版本为oprofile-1.3.0.tar.gz

    (二)、安装

    直接安装:

    /workdisk/software/# apt install oprofile

    可以先用直接安装方法,如果错误再往下看。

    /workdisk/software/# apt install popt-devel
    /workdisk/software/# apt install binutils
    /workdisk/software/# apt install binutils-devel

    还是不行的话,就下载后源码安装:

    1、popt安装

    1 解压
    2 root@ranxf-TEST:/workdisk/software/popt-1.16# ./configure --prefix=/usr 
    3 make
    4 make install

    2、binutils安装

    1 解压tar -xvf binutils-2.32.tar.xz
    2 cd binutils-2.32
    3 root@ranxf-TEST:/workdisk/software/binutils-2.32# ./configure --with-sysroots
    4 root@ranxf-TEST:/workdisk/software/binutils-2.32# make clean
    5 root@ranxf-TEST:/workdisk/software/binutils-2.32# make
    6 root@ranxf-TEST:/workdisk/software/binutils-2.32# make install

    3、oprofile安装

    oprofile安装
    /workdisk/software/oprofile-1.3.0# ./configure --prefix=/workdisk/oprofile/ /workdisk/software/oprofile-1.3.0# make /workdisk/software/oprofile-1.3.0# make install 如果按照以上安装方法,出现如下错误 Configuration error: Iberty library not found 按照以下顺序重新安装解决 /workdisk/software/oprofile-1.3.0# apt-file update /workdisk/software/oprofile-1.3.0# apt-get install apt-file /workdisk/software/oprofile-1.3.0# apt-get install libiberty-dev /workdisk/software/oprofile-1.3.0# ./configure --prefix=/workdisk/oprofile/ /workdisk/software/oprofile-1.3.0# make /workdisk/software/oprofile-1.3.0# make install

    Configuration error: Iberty library not found参见该链接:Configuration error: Iberty library not found

    安装过程也可以直接写成:./configure make & make install

    大型软件编译过程可以在make前面加一条make clean对先前编译结果进行清除

    三、验证oprofile是否安装成功

     https://oprofile.sourceforge.io/news/官网显示:1.0.0之后版本的一个重大变化是删除了基于传统opcontrol的分析器。自从operf首次引入版本0.9.8以来,旧版概要分析工具已被弃用。

    operf -version
    operf: oprofile 1.1.0 compiled on Aug 10 2016 19:53:16

    四、oprofile的使用

    使用operf进行分析可以精确定位分析(即单个过程或系统范围)。使用operf,无需初始设置 - 只需使用您需要的选项调用operf ; 然后运行OProfile后处理工具。operf语法如下:

    operf [options] [--system-wide | --pid = <PID> | [command [args]]]

    典型用法可能如下所示:

    operf ./my_test_program my_arg

    ./my_test_program完成时(或按Ctrl-C),停止,你就可以使用opreport或其他OProfile的后处理工具。

    默认情况下,operf将样本数据存储在其中<cur_dir>/oprofile_data/samples/current,而opreport和其他后处理工具将首先查找该位置的配置文件数据,除非您通过该--session-dir选项到指定位置。

    operf:
    operf -g   -p 1525
    如果多个线程区别较大,可加上 -t

    root@ranxf-TEST:/workdisk/oprofile# operf -g -p 1525
    operf: Press Ctl-c or 'kill -SIGINT 8288' to stop profiling
    operf: Profiler started
    ^C
    WARNING: Lost samples detected! See /workdisk/oprofile/oprofile_data/samples/operf.log for details.(警告:检测到丢失的样本!有关详细信息,请参阅/workdisk/oprofile/oprofile_data/samples/operf.log。)
    Lowering the sampling rate may reduce or eliminate lost samples.
    See the '--events' option description in the operf man page for help.

    Profiling done.
    opreport: opreport
    -l | more opreport -c -% -f -g -s sample | more opannotate: opannotate -s

    五、常见问题

    找不到样本:

    使用operf,请确保从收集配置文件的目录运行OProfile后处理工具(例如,opreport,opannotate)。或者,您可以使用--session-dir选项指定样本数据的位置

    调用后处理工具时图像名称(即应用程序名称)的错误规范是此错误消息的常见原因。如果您使用operf来分析单个应用程序,则无需在调用结束时附加图像名称,因为仅为给定的应用程序收集样本。

    官方文档

      1 Following is a description of the operf options.
      2 
      3 command [args]
      4 
      5     The command or application to be profiled. The [args] are the input arguments that the command or application requires. Either command, --pid or --system-wide is required, but cannot be used simultaneously.
      6 --pid / -p [PID]
      7 
      8     This option enables operf to profile a running application. PID should be the process ID of the process you wish to profile. When finished profiling (e.g., when the profiled process ends), press Ctrl-c to stop operf.
      9 --system-wide / -s
     10 
     11     This option is for performing a system-wide profile. You must have root authority to run operf in this mode. When finished profiling, Ctrl-C to stop operf. If you run operf --system-wide as a background job (i.e., with the &), you must stop it in a controlled manner in order to process the profile data it has collected. Use kill -SIGINT <operf-PID> for this purpose. It is recommended that when running operf with this option, your current working directory should be /root or a subdirectory of /root to avoid storing sample data files in locations accessible by regular users.
     12 --vmlinux / k [vmlinux_path]
     13 
     14     A vmlinux file that matches the running kernel that has symbol and/or debuginfo. Kernel samples will be attributed to this binary, allowing post-processing tools (like opreport) to attribute samples to the appropriate kernel symbols. If this option is not specified, the file /proc/kallsyms is used to obtain kernel symbol addresses correponding to sample addresses. However, the setting of /proc/sys/kernel/kptr_restrict may restrict a non-root user's access to /proc/kallsyms, in which case, all kernel samples are attributed to a pseudo binary named "no-vmlinux".
     15 --callgraph / -g
     16 
     17     This option enables the callgraph to be saved during profiling. NOTE: The full callchain is recorded, so there is no depth limit.
     18 --append / -a
     19 
     20     By default, operf moves old profile data from <session_dir>/samples/current to <session_dir>/samples/previous. If a 'previous' profile already existed, it will be replaced. If the --append option is passed, old profile data in 'current' is left in place and new profile data will be added to it, and the 'previous' profile (if one existed) will remain untouched. To access the 'previous' profile, simply add a session specification to the normal invocation of oprofile post-processing tools; for example:
     21 
     22     opreport session:previous
     23 
     24 --events / -e [event1[,event2[,...]]]
     25 
     26     This option is for passing a comma-separated list of event specifications for profiling. Each event spec is of the form:
     27 
     28     name:count[:unitmask[:kernel[:user]]]
     29 
     30     When no event specification is given, the default event for the running processor type will be used for profiling. Use ophelp to list the available events for your processor type.
     31 --separate-thread / -t
     32 
     33     This option categorizes samples by thread group ID (tgid) and thread ID (tid). The --separate-thread option is useful for seeing per-thread samples in multi-threaded applications. When used in conjuction with the --system-wide option, --separate-thread is also useful for seeing per-process (i.e., per-thread group) samples for the case where multiple processes are executing the same program during a profiling run.
     34 --separate-cpu / -c
     35 
     36     This option categorizes samples by cpu.
     37 --session-dir / -d [path]
     38 
     39     This option specifies the session directory to hold the sample data. If not specified, the data is saved in the oprofile_data directory on the current path.
     40 ---lazy-conversion / -l
     41 
     42     Use this option to reduce the overhead of operf during profiling. Normally, profile data received from the kernel is converted to OProfile format during profiling time. This is typically not an issue when profiling a single application. But when using the --system-wide option, this on-the-fly conversion process can cause noticeable overhead, particularly on busy multi-processor systems. The --lazy-conversion option directs operf to wait until profiling is completed to do the conversion of profile data.
     43 --verbose / -V [level]
     44 
     45     A comma-separated list of debugging control values used to increase the verbosity of the output. Valid values are: debug, record, convert, misc, sfile, arcs, and the special value, 'all'.
     46 --version -v
     47 
     48     Show operf version.
     49 --help / -h
     50 
     51     Show a help message
     52 
     53 
     54 Options for opreport
     55 
     56 --accumulated / -a
     57 
     58     Accumulate sample and percentage counts in the symbol list.
     59 --callgraph / -c
     60 
     61     Show callgraph information.
     62 --debug-info / -g
     63 
     64     Show source file and line for each symbol.
     65 --demangle / -D none|normal|smart
     66 
     67     none: no demangling. normal: use default demangler (default) smart: use pattern-matching to make C++ symbol demangling more readable.
     68 --details / -d
     69 
     70     Show per-instruction details for all selected symbols. Note that, for binaries without symbol information, the VMA values shown are raw file offsets for the image binary.
     71 --exclude-dependent / -x
     72 
     73     Do not include application-specific images for libraries, kernel modules and the kernel..
     74 --exclude-symbols / -e [symbols]
     75 
     76     Exclude all the symbols in the given comma-separated list.
     77 --global-percent / -%
     78 
     79     Make all percentages relative to the whole profile.
     80 --help / -? / --usage
     81 
     82     Show help message.
     83 --image-path / -p [paths]
     84 
     85     Comma-separated list of additional paths to search for binaries. This is needed to find kernel modules.
     86 --root / -R [path]
     87 
     88     A path to a filesystem to search for additional binaries.
     89 --include-symbols / -i [symbols]
     90 
     91     Only include symbols in the given comma-separated list.
     92 --long-filenames / -f
     93 
     94     Output full paths instead of basenames.
     95 --merge / -m [lib,cpu,tid,tgid,unitmask,all]
     96 
     97     Merge any profiles separated in a --separate session.
     98 --no-header
     99 
    100     Don't output a header detailing profiling parameters.
    101 --output-file / -o [file]
    102 
    103     Output to the given file instead of stdout.
    104 --reverse-sort / -r
    105 
    106     Reverse the sort from the default.
    107 --session-dir=dir_path
    108 
    109     Use sample database from the specified directory dir_path instead of the default location. If this option is not specified, then opreport will search for samples in <cur_dir>/oprofile_data first. If that directory does not exist, the standard session-dir of /var/lib/oprofile is used as the session directory.
    110 --show-address / -w
    111 
    112     Show the VMA address of each symbol (off by default).
    113 --sort / -s [vma,sample,symbol,debug,image]
    114 
    115     Sort the list of symbols by, respectively, symbol address, number of samples, symbol name, debug filename and line number, binary image filename.
    116 --symbols / -l
    117 
    118     List per-symbol information instead of a binary image summary.
    119 --threshold / -t [percentage]
    120 
    121     Only output data for symbols that have more than the given percentage of total samples. For profiles using multiple events, if the threshold is reached for any event, then all sample data for the symbol is shown.
    122 --verbose / -V [options]
    123 
    124     Give verbose debugging output.
    125 --version / -v
    126 
    127     Show version.
    128 --xml / -X
    129 
    130     Generate XML output.
    131 
    132 
    133 Usage of opannotate
    134 
    135 --assembly / -a
    136 
    137     Output annotated assembly. If this is combined with --source, then mixed source / assembly annotations are output.
    138 --base-dirs / -b [paths]/
    139 
    140     Comma-separated list of path prefixes. This can be used to point OProfile to a different location for source files when the debug information specifies an absolute path on your system for the source that does not exist. The prefix is stripped from the debug source file paths, then searched in the search dirs specified by --search-dirs.
    141 --demangle / -D none|normal|smart
    142 
    143     none: no demangling. normal: use default demangler (default) smart: use pattern-matching to make C++ symbol demangling more readable.
    144 --exclude-dependent / -x
    145 
    146     Do not include application-specific images for libraries, kernel modules and the kernel.
    147 --exclude-file [files]
    148 
    149     Exclude all files in the given comma-separated list of glob patterns. This option is supported solely with the --source option. It can be used to filter out source files in the output using the following types of specifications:
    150 
    151         filenames (basename -- i.e., no path)
    152         filename glob specifications (all files whose base filename matches the given pattern)
    153         directory segments (all source files located in the specified directory; e.g. "libio")
    154         directory segment glob specifications (e.g., "libi*")
    155 
    156 --exclude-symbols / -e [symbols]
    157 
    158     Exclude all the symbols in the given comma-separated list.
    159 --help / -? / --usage
    160 
    161     Show help message.
    162 --image-path / -p [paths]
    163 
    164     Comma-separated list of additional paths to search for binaries. This is needed to find kernel modules.
    165 --root / -R [path]
    166 
    167     A path to a filesystem to search for additional binaries.
    168 --include-file [files]
    169 
    170     Only include files in the given comma-separated list of glob patterns. The same rules apply for this option as for the --exclude-file option.
    171 --include-symbols / -i [symbols]
    172 
    173     Only include symbols in the given comma-separated list.
    174 --objdump-params [params]
    175 
    176     Pass the given parameters as extra values when calling objdump. If more than one option is to be passed to objdump, the parameters must be enclosed in a quoted string.
    177 
    178     An example of where this option is useful is when your toolchain does not automatically recognize instructions that are specific to your processor. For example, on IBM POWER7/RHEL 6, objdump must be told that a binary file may have POWER7-specific instructions. The opannotate option to show the POWER7-specific instructions is:
    179 
    180        --objdump-params=-Mpower7
    181 
    182     The opannotate option to show the POWER7-specific instructions, the source code (--source) and the line numbers (-l) would be:
    183 
    184        --objdump-params="-Mpower7 -l --source"
    185 
    186 --output-dir / -o [dir]
    187 
    188     Output directory. This makes opannotate output one annotated file for each source file. This option can't be used in conjunction with --assembly.
    189 --search-dirs / -d [paths]
    190 
    191     Comma-separated list of paths to search for source files. This is useful to find source files when the debug information only contains relative paths.
    192 --source / -s
    193 
    194     Output annotated source. This requires debugging information to be available for the binaries.
    195 --session-dir=dir_path
    196 
    197     Use sample database from the specified directory dir_path instead of the default location. If this option is not specified, then opannotate will search for samples in <cur_dir>/oprofile_data first. If that directory does not exist, the standard session-dir of /var/lib/oprofile is used as the session directory.
    198 --threshold / -t [percentage]
    199 
    200     For annotated assembly, only output data for symbols that have more than the given percentage of total samples. For profiles using multiple events, if the threshold is reached for any event, then all sample data for the symbol is shown.
    201 
    202     For annotated source, only output data for source files that have more than the given percentage of total samples. For profiles using multiple events, if the threshold is reached for any event, then all sample data for the source file is shown.
    203 --verbose / -V [options]
    204 
    205     Give verbose debugging output.
    206 --version / -v
    207 
    208     Show version. 

    参考文章

      使用oprofile-1.1.0:https://blog.csdn.net/spche/article/details/72831596

      官网OProfile manual:https://oprofile.sourceforge.io/doc/index.html

  • 相关阅读:
    【读书笔记】:MIT线性代数(1):Linear Combinations
    Adam Optimization Algorithm
    CSS 对齐方式
    CSS Position
    设置Table边框的CSS
    p_CreateAuditEntry
    Entity FramWork Code first 使用心得
    CRM 配置 ADFS后,使用自定义STS遇到的问题总结
    Sql Server Always On主库与附库遇到的问题
    ADFS 登录页面自定义
  • 原文地址:https://www.cnblogs.com/ranxf/p/11412257.html
Copyright © 2020-2023  润新知