日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

《Linux调优工具oprofile的演示分析》

發(fā)布時(shí)間:2025/7/14 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 《Linux调优工具oprofile的演示分析》 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

根據(jù)CPU架構(gòu)oprofile采樣的觸發(fā)有兩種模式:
1) NMI模式: 利用處理器的performance counter功能, 指定counter的類型type和累進(jìn)數(shù)量count. 比如

type=DTLB_MISS, count=500, 代表"Data TLB miss"每發(fā)生500次, 會(huì)觸發(fā)一次中斷. Oprofile.ko模塊會(huì)相應(yīng)這個(gè)中斷, 然后看當(dāng)前正在執(zhí)行的是什么指令,那個(gè)函數(shù), 那個(gè)模塊(或者app, lib), 并進(jìn)行計(jì)數(shù). 不同的處理器支持的counter類型和count取值范圍各不相同. 可以通過(guò)ophelp來(lái)查看當(dāng)前cpu所支持的counter類型和參數(shù). 例如, "--event=MISALIGN_MEM_REF:1000:0:1:0"表示非對(duì)齊的內(nèi)存訪問(wèn), 每1000次觸發(fā)一個(gè)中斷, 1000后面的0表示改特定counter的mask, 具體含義看ophelp的內(nèi)容; 最后的1:0表示只對(duì)kernel采樣, 不對(duì)用戶空間程序采樣. "--event=CPU_CLK_UNHALTED:10000:0:1:0"代表cpu時(shí)鐘周期事件.每10000個(gè)cycle觸發(fā)一次中斷. "--event=L1I_MISSES:500:0:1:0"表示一級(jí)指令緩存的cache miss.


2) Timer Interrupt模式: 在沒(méi)有performance counter支持的情況下(例如Vmware虛擬機(jī)下), 可以利用時(shí)鐘中斷來(lái)采樣. 這時(shí)候就沒(méi)有performance counter的概念了. 或者可以當(dāng)成近似的cpu時(shí)鐘周期事件. 要使用timer interrupt模式, 需要在加載oprofile.ko模塊的時(shí)候,傳遞"timer=1"參數(shù). modprobe oprofile timer=1

?

一)用opcontrol控制profile
要打開(kāi)oprofile,需要用start選項(xiàng)來(lái)調(diào)用opconrol,當(dāng)?shù)谝淮握{(diào)用opcontrol時(shí),必須告訴它想統(tǒng)計(jì)內(nèi)核還是用戶空間數(shù)據(jù).
因?yàn)槲覀兊睦邮窃谟脩艨臻g工作,應(yīng)該用--no-vmlinux選項(xiàng)來(lái)取消內(nèi)核統(tǒng)計(jì),如下:

1.首先需要加載profile:

opcontrol --init

初始化oprofile,可以通過(guò)demsg查看oprofile使用的是哪一種模式:
[root@compiler /]# dmesg | grep oprofile
oprofile: using NMI interrupt.
oprofile: using NMI interrupt.
oprofile: using NMI interrupt.
oprofile: using NMI interrupt.
oprofile: using NMI interrupt.

?

2.然后start 調(diào)用開(kāi)始獲取CPU采樣:


[root@compiler /]# opcontrol --start --no-vmlinux
ATTENTION: Use of opcontrol is discouraged. Please see the man page for operf.
Using default event: CPU_CLK_UNHALTED:100000:0:1:1
Using 2.6+ OProfile kernel interface.
Using log file /var/lib/oprofile/samples/oprofiled.log
Daemon started.
Profiler running.


每次啟動(dòng)都會(huì)在/root/.oprofile/目錄下生成一個(gè)配置文件daemonrc,如下:

[root@compiler /]# cat /root/.oprofile/daemonrc
SESSION_DIR=/var/lib/oprofile
NR_CHOSEN=0
SEPARATE_LIB=0
SEPARATE_KERNEL=0
SEPARATE_THREAD=0
SEPARATE_CPU=0
VMLINUX=none
IMAGE_FILTER=
CPU_BUF_SIZE=0
CALLGRAPH=0
XENIMAGE=none

注:
以上的每個(gè)配置項(xiàng)是通過(guò)啟動(dòng)oprofile時(shí)加的參數(shù)生成的.

?

清理統(tǒng)計(jì)數(shù)據(jù),重置統(tǒng)計(jì)量,如下:
[root@compiler /]# opcontrol --reset


3.這個(gè)時(shí)候啟動(dòng)需要監(jiān)控的程序,如果是內(nèi)核,就直接sleep一段時(shí)間即可

[root@compiler /]# /usr/local/nginx/sbin/nginx

?

4.接下來(lái)我們用--dump選項(xiàng)告訴oprofiled輸出統(tǒng)計(jì)量,如下:
[root@compiler /]# opcontrol --dump

(可選)停止后臺(tái)程序
[root@compiler /]# opcontrol --shutdown

?

5.再下來(lái)我們就可以用opreport,opgprof或者opannotate來(lái)觀察它們,任何用戶都可以看到輸出,下面我們用opreport來(lái)查看統(tǒng)計(jì)量,如下:
(1)opreport

[root@compiler /]# opreport |grep nginx
Using /var/lib/oprofile/samples/ for samples directory.
39 1.0e-04 nginx

觀察對(duì)應(yīng)進(jìn)程里面函數(shù)在運(yùn)行時(shí)候的的占用百分比
[root@compiler /]# opreport -l /usr/local/nginx/sbin/nginx
Using /var/lib/oprofile/samples/ for samples directory.
CPU: Intel Core/i7, speed 2.128e+06 MHz (estimated)
Counted CPU_CLK_UNHALTED events (Clock cycles when not halted) with a unit mask of 0x00 (No unit mask) count 100000
samples % symbol name
20 39.2157 ngx_hash_init
10 19.6078 ngx_conf_parse
2 3.9216 ngx_init_setproctitle
1 1.9608 _fini
1 1.9608 main
1 1.9608 ngx_array_push
1 1.9608 ngx_cpuinfo
1 1.9608 ngx_event_process_init
1 1.9608 ngx_hash_key_lc
1 1.9608 ngx_http_core_merge_loc_conf
1 1.9608 ngx_http_core_server_name
1 1.9608 ngx_http_core_type
1 1.9608 ngx_http_limit_conn_merge_conf
1 1.9608 ngx_http_log_compile_format
1 1.9608 ngx_http_log_init
1 1.9608 ngx_http_merge_locations
1 1.9608 ngx_http_upstream_keepalive_create_conf
1 1.9608 ngx_http_uwsgi_merge_loc_conf
1 1.9608 ngx_signal_handler
1 1.9608 ngx_strlow
1 1.9608 ngx_vslprintf
1 1.9608 ngx_worker_process_init
[root@compiler /]#


(2)opannotate
[root@compiler /]# opannotate --source /usr/local/nginx/sbin/nginx > 1.txt

可以針對(duì)某一個(gè)進(jìn)程,里面所有函數(shù)的執(zhí)行采樣統(tǒng)計(jì),用于優(yōu)化函數(shù)。

????????
注:
輸出顯示了一系列對(duì)象文件,文件包含了當(dāng)profile測(cè)試時(shí)正在運(yùn)行的代碼.
oprofiled 從調(diào)用opcontrol --start時(shí)開(kāi)始記錄,到調(diào)用opcontrol --dump時(shí)會(huì)截取當(dāng)前的統(tǒng)計(jì)量,但是oprofiled仍會(huì)繼續(xù)搜集并積累統(tǒng)計(jì)數(shù)據(jù),之后再調(diào)用opconrol --dump就會(huì)有舊的積累數(shù)據(jù)產(chǎn)生.直到調(diào)用opcontrol --stop時(shí),才會(huì)停止搜集.

?

(3)opgprof

我們可以通過(guò)gprof來(lái)產(chǎn)生一個(gè)gmon.out文件,如下:
[root@compiler /]# opgprof /usr/local/nginx/sbin/nginx

不調(diào)用圖像數(shù)據(jù)得出gmon.out結(jié)果

[root@compiler /]# gprof --no-graph /usr/local/nginx/sbin/nginx
Flat profile:

Each sample counts as 1 samples.
% cumulative self self total
time samples samples calls T1/call T1/call name
39.22 20.00 20.00 ngx_hash_init
19.61 30.00 10.00 ngx_conf_parse
3.92 32.00 2.00 ngx_init_setproctitle
1.96 33.00 1.00 _fini
1.96 34.00 1.00 main
1.96 35.00 1.00 ngx_array_push
1.96 36.00 1.00 ngx_cpuinfo
1.96 37.00 1.00 ngx_event_process_init
1.96 38.00 1.00 ngx_hash_key_lc
1.96 39.00 1.00 ngx_http_core_merge_loc_conf
1.96 40.00 1.00 ngx_http_core_server_name
1.96 41.00 1.00 ngx_http_core_type
1.96 42.00 1.00 ngx_http_limit_conn_merge_conf
1.96 43.00 1.00 ngx_http_log_compile_format
1.96 44.00 1.00 ngx_http_log_init
1.96 45.00 1.00 ngx_http_merge_locations
1.96 46.00 1.00 ngx_http_upstream_keepalive_create_conf
1.96 47.00 1.00 ngx_http_uwsgi_merge_loc_conf
1.96 48.00 1.00 ngx_signal_handler
1.96 49.00 1.00 ngx_strlow
1.96 50.00 1.00 ngx_vslprintf
1.96 51.00 1.00 ngx_worker_process_init

% the percentage of the total running time of the
time program used by this function.

cumulative a running sum of the number of seconds accounted
seconds for by this function and those listed above it.

self the number of seconds accounted for by this
seconds function alone. This is the major sort for this
listing.

calls the number of times this function was invoked, if
this function is profiled, else blank.

self the average number of milliseconds spent in this
ms/call function per call, if this function is profiled,
else blank.

total the average number of milliseconds spent in this
ms/call function and its descendents per call, if this
function is profiled, else blank.

name the name of the function. This is the minor sort
for this listing. The index shows the location of
the function in the gprof listing. If the index is
in parenthesis it shows where it would appear in
the gprof listing if it were to be printed.
[root@compiler /]#

?

6.
opcontrol --stop? #停止profiling
opcontrol --shutdown? #關(guān)閉守護(hù)進(jìn)程oprofiled
opcontrol --deinit? #卸載模塊

?

?

二)關(guān)于oprofile的最終論述

這里只提到了oprofile的表面功能.
默認(rèn)情況下,oprofile不支持在虛擬機(jī)上進(jìn)行調(diào)試,我們可以通過(guò)下面的方法讓oprofile可以在虛擬機(jī)上跑,如下:


echo "options oprofile timer=1" >> /etc/modprobe.conf
此時(shí)再重啟虛擬機(jī)就可以工作了.

轉(zhuǎn)載于:https://www.cnblogs.com/jkkkk/p/6520381.html

總結(jié)

以上是生活随笔為你收集整理的《Linux调优工具oprofile的演示分析》的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。