systemtap基礎安裝

爲部署systemtap,除了systemtap安裝包,還需要-devel,-debuginfo和-debuginfo-common包;如果系統有多個內核,必須爲每個版本內核各安裝-devel和-debuginfo包;

安裝systemtap
需要systemtap和systemtap-runtime安裝包
yum install systemtap systemtap-runtime

安裝相關內核包
systemtap需要內核信息以便進行probe,而這些都由
kernel-debuginfo
kernel-debuginfo-common
kernel-devel
提供
uname -r查看當前內核版本,譬如爲2.6.18-53.el5機器爲i686,則需要如下
kernel-debuginfo-2.6.18-53.1.13.el5.i686.rpm
kernel-debuginfo-common-2.6.18-53.1.13.el5.i686.rpm
kernel-devel-2.6.18-53.1.13.el5.i686.rpm
最簡單的方式
yum install kernelname-devel-version
debuginfo-install kernelname-version

運行簡單腳本測試安裝是否成功
stap -v -e 'probe vfs.read {printf("read performed\n"); exit()}'.
會看到以下輸出
Pass 1: parsed user script. and 45 library script(s) in 340usr/0sys/358real ms.
Pass 2: analyzed script. 1 probe(s), 1 function(s), 0 embed(s), 0 global(s) in
290usr/260sys/568real ms.
Pass 3: translated to C into "/tmp/stapiArgLX/stap_e5886fa50499994e6a87aacdc43cd392_399.c" in
490usr/430sys/938real ms.
Pass 4: compiled C into "stap_e5886fa50499994e6a87aacdc43cd392_399.ko" in
3310usr/430sys/3714real ms.
Pass 5: starting run.
read performed
Pass 5: run completed in 10usr/40sys/73real ms.

運行systemtap腳本時,會創建內核模塊,然後加載至內核;
然而該類腳本只能在安裝了systemtap包的系統上執行,而有時不能在所有機器都安裝;
Cross-instrumentation爲此提供折中方案:即在一臺機器集中生成指令模塊,然後複製到各個機器上使用;
步驟如下:
爲每個target server安裝systemtap-runtime,並獲取其內核版本uname -r;
在host安裝systemtap,並安裝每個target內核對應的-devel/-debuginfo/-debuginfo-common;

host構造指令模塊stap -r kernel_version script. -m module_name,複製到target執行staprun module_name.ko即可
譬如創建simple.ko,其target內核爲2.6.18-92.1.10.el5,在host執行如下命令生成simple.ko
stap -r 2.6.18-92.1.10.el5 -e 'probe vfs.read {exit()}' -m simple
將simple.ko複製到target,執行staprun simple.ko即可;


stap腳本可以使用root執行,此外還提供了兩個用戶組
stapdev:該組用戶使用stap運行systemtap腳本,或者staprun運行指令模塊;運行stap需要將腳本編譯成內核模塊並加載,此類操作需要相應root權限,因此該組用戶只能賦給信任的用戶;
stapusr:該組用戶只能調用staprun,還可以運行/lib/modules/kernel_version/systemtap目錄下的模塊

stap命令選項
-v 將輸出更具體,可以執行stap -vvv script.stp
-o 將標準輸出寫入文件
-S size,count 分別限制文件大小M和數量
-x pid 使用target()捕獲指定的進程ID
-c 'command' 將target()指向該命令
-e 'script' 將腳本作爲輸入傳輸給systemtap translator,即直接執行-e後的腳本
-F 使用flight recorder mode將腳本後臺運行,該腳本允許stap腳本長時間運行,有兩種方式存儲輸出:內存和文件模式,兩者都是後臺進程;
也可從標準輸入讀取並運行腳本 echo "probe timer.s(1) {exit()}" | stap -

什麼是flight recorder
allows you to run a SystemTap script. run for long periods and just focus on recent output.
內存模式:從內核內存申請緩衝區存放運行信息;緩存默認1M,-s可調大
stap -F iotime.stp
將看到如下信息
Disconnecting from systemtap module.
To reconnect, type "staprun -A stap_5dd0073edcb1f13f7565d8c343063e68_19556"
只需運行staprun -A stap_5dd0073edcb1f13f7565d8c343063e68_19556便可持續獲取腳本輸出信息
文件模式:
將信息存於文件,-S決定文件大小,-o輸出文件
stap -F -o /tmp/pfaults.log -S 1,2 pfaults.stp


腳本運行流程
先檢查已有的tapset庫(/usr/share/systemtap/tapset),然後使用library已定義的tapset替代腳本
將其轉化爲C語言,並編譯成內核模塊
加載該模塊,激活腳本腳本中的probe


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章