Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

oprofile的Q&A:https://oprofile.sourceforge.io/faq/

先operf

SYNOPSIS

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

One (and only one) of either command , --pid or --system-wide is required.

des

Operf is an OProfile tool that can be used in place of opcontrol for profiling. Operf uses
the Linux Performance Events Subsystem, and hence, does not require the use of the opcon‐
trol daemon – in fact, operf and opcontrol usage are mutually exclusive.

By default, operf uses /oprofile_data as the session-dir and stores profiling
data there. You can change this by way of the –session-dir option.

The usual post-profiling analysis tools such as opreport(1) and opannotate(1) can be used
to generate profile reports.

The post-processing analysis tools will search for samples
in /oprofile_data first. If that directory does not exist, the post-process‐
ing tools use the standard session-dir of /var/lib/oprofile.

Statistics, such as total samples received and lost samples, are written to the operf.log
file that can be found in the /samples directory.

options

指定监控对象

command[args] 这部分和你普通运行程序时的命令一样
The command or application to be profiled. args are the input arguments that the
command or application requires. One (and only one) of either command , --pid or --system-wide is required.
--pid / -p PID
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. If you run operf –pid as a
background job (i.e., with the &), you must stop it in a controlled manner in order
for it to process the profile data it has collected. Use kill -SIGINT
for this purpose.

其他选项

--vmlinux / k vmlinux_path 不带这个参数的话不会统计内核的symbols

Why do the profile tools fail to open the vmlinux kernel image ?
Probably because you have accidentally specified the vmlinuz not vmlinux file. If you don’t have a vmlinux file, most Linux distributions provide a kernel debuginfo package that includes it. Otherwise, you need to recompile your kernel from source. If you’re not interested in kernel samples, then don’t use the –vmlinux option (and for legacy profiling, use opcontrol –no-vmlinux).

          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.

--callgraph / -g
This option enables the callgraph to be saved during profiling. NOTE: The full
callchain is recorded, so there is no depth limit.

--separate-thread / -t
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 conjunction with the ‘–system-wide’ option,
the ‘–separate-thread’ option 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.

--separate-cpu / -c
This option categorizes samples by cpu.

--session-dir / -d path
This option specifies the session path to hold the sample data. If not specified,
the data is saved in the oprofile_data directory on the current path

--lazy-conversion / -l
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-con‐
version option directs operf to wait until profiling is completed to do the conver‐
sion of profile data.

opreport

输出解释 https://oprofile.sourceforge.io/doc/opreport.html

opgprof

-p不给的话默认是当前目录下的oprofile_data目录,
-o不给的话默认生成gmon.out,可以作为gprof的输入

opgprof -o <output_file> -p <oprofile_data_path>

举例

先收集统计数据,在当前目录下运行(-g希望能够输出call graph)

sudo operf -g ./main -m msbfs -g test_graph.txt -o . -p vertex_pairs.txt

在当前目录下oprofile_data得到数据(此目录可以备份起来,之后还可以基于此目录生成各种分析文件),
然后输出分析结果,在当前目录下运行(如果operf是command指定监控对象的,则用opreport不需要指定command)
将所有符号的统计结果(按占用cpu周期的降序)symbols.txt中,还有调用图

opreport -o symbols.txt -l 
opreport -o call_graph.txt -c 

opreport -l中输出的image name列中,no-vmlinux表示the time spent by the linux kernelthe application binary itself 在这里的名字就是可执行文件的名字,比如mainxxx.so
time spent in the shared libraries your application uses

同时profile内核举例

获取vmlinux文件

安装kernel debuginfo

centos: https://iwmj.wordpress.com/2018/09/15/how-to-download-and-install-debuginfo-packages-for-centos/

# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) # this means your system is centos 7, which determines download address
wget http://debuginfo.centos.org/7/x86_64/kernel-debuginfo-common-x86_64-$(uname -r).rpm
wget http://debuginfo.centos.org/7/x86_64/kernel-debuginfo-$(uname -r).rpm
  • Then install them
yum install kernel-debuginfo-common-x86_64-$(uname -r).rpm
yum install kernel-debuginfo-$(uname -r).rpm

然后会得到/usr/lib/debug/lib/modules/$(uname -r)/vmlinux,这个可以-k传递给opref

extract-vmlinux脚本解压vmlinuz operf报告解压得到的文件invalid

https://blog.packagecloud.io/how-to-extract-and-disassmble-a-linux-kernel-image-vmlinuz/
centos:

  • 获取解压脚本sudo yum install kernel-devel-$(uname -r) You will be able to find the extract-linux script at /usr/src/kernels/$(uname -r)/scripts/extract-vmlinux
  • 解压:
    • A good first step is to create a temporary directory and copy the kernel image to it:
    mkdir ~/tmp/kernel-extract
    sudo cp /boot/vmlinuz-$(uname -r) ~/tmp/kernel-extract/
    
    • Now, run the extract-vmlinux script to extract the image.
     cd ~/tmp/kernel-extract/
    sudo <path_to_extract-vmlinux> vmlinuz-$(uname -r) > vmlinux
    

    比如/usr/src/kernels/$(uname -r)/scripts/extract-vmlinux

operf

加上-k指定vmlinux file的路径

sudo operf -k /tmp/kernel-extract/vmlinux-$(uname -r) ./main -m msbfs -g test_graph.txt -o . -p vertex_pairs.txt

适用应用

  • 多线程可
  • cpu占用型的可
  • 太小的程序统计的不完全

评论