Linux平台上用於分析系统_第1页
Linux平台上用於分析系统_第2页
Linux平台上用於分析系统_第3页
Linux平台上用於分析系统_第4页
Linux平台上用於分析系统_第5页
已阅读5页,还剩20页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、SystemTap 是 Linux平台上用於分析系統為的器,除可以追蹤 system和 library calls的執以及測系統執效能之外,最重要的是提供一個工具讓我們可以即時存取kernel資結構,對於解作業系統是如何運作的很有幫助。如何在Linux上建置使用 systemtap的環境瞭解 systemtap基本的運作原從使用範中學習揣摩怎麼用 systemtap可安裝GNU/Linux 的個人電腦皆可GNU/Linux Debian 4.0 / Fedora 8.0 / Ubuntu 8.04 (etch)GCC 4.1.2 or later(安裝 build-essential pack

2、age) 安裝 kernel-package, libncurses5, libncurses5-dev package 安裝 systemtap, linux-source package#su -l#yum install systemtap kernel-devel yum-utils #debuginfo-install kernel # stap -ve probe begin log(hello world) exit () # stap -c df -e probe syscall.* if (target()=pid() log(name. .argstr) 假設使用者已安裝好

3、 Debian Etch(4.0) ,並建置好基本軟體環境。#apt-get install kernel-package libncurses5 libncurses5-dev#apt-get install systemtap linux-source-2.6.18安裝之前要先設好以下設定: 由於使用systemtap需要 vmlinux檔,所以做好的 kernel deb要內附 vmlinux的話請修改/etc/kernel-pkg.conf並加入install_vmlinux := YES解壓縮原始檔之後,進入kernel source 的 top dir進 make menuconf

4、ig進入 Instrumentation Support,勾選 Kprobes同樣的進入Kernel hacking,勾選 Compile the kernel with debug info儲存離開建核心請執#make-kpkg -revision 2.6.18 -append-to-version -systemtap -initrd binary-arch核心編譯好之後,會自動壓縮成deb檔案,接著用dpkg i直接安裝即可重新開機之後,SystemTap的環境就建置好helloworld.stp“hello world!” script 範明 :probe begin print(“h

5、ello worldn”) exit()stap helloworld.stpSystemtap script 主要是由 probes和functions 所組成的Statements and Expressions其中的 statements 和 expresssions語法似於 C與 awk,如 if/else statement、while loop、for loop、break/continue、似 awk 的 next。和 C 語言同的是,systemtap 視分號 (;) 為 null statement 而非 statement terminator。此外,註解可以是 shell

6、-style () 、C-style(/* */) 、C+-style(/)。字則視為 atomic values 而非字陣,String concatenation 以號顯示(.)VariablesSystemtap變會自動宣告並初始化。變的形態自動由上下文推而得。變的 scope 預設為 probe handler 中,要在 probes 之間共享變,則需使用 global關鍵字,共享所產生的 shared data問題系統會自動解決Target variable除使用者自訂的變之外,另有一以$開頭稱為 target variable的變可用允許使用者存取 probe point 前後,但

7、要知道有哪些target variables可供使用,須熟悉 kernel source,並關閉最佳化選項Function另一個 systemtap script 的主要結構是function。定義 function的方式為關鍵字function後接 function name,後在小括號內放。Function definition可放 在 script 中的任何地方,可以有任意目的字或字,以call by value方式傳入,回傳值可能為字或字。支援 recursion。要注意的是,target variable可當作function的傳入Associative array除簡單的變之外,也

8、提供 associative array。以最大容固定的 hash tables實做。注意須宣告成 global。宣告方式如下: global a #宣告 global的純變或array global b400印出系統中在一段時間(5秒)內,前二十個最常被呼叫到的system calls編輯一個檔案如下,命名為 top.stp#!/usr/bin/env stap# display the top 10 syscalls called in last 5 seconds#global syscallsfunction print_top () cnt=0 log (SYSCALLttttCOU

9、NT) foreach (name in syscalls-) printf(%-20stt%5dn,name, syscallsname) if (cnt+ = 20) break printf(-n) delete syscallsprobe syscall.* syscallsprobefunc()+probe timer.ms(5000) print_top ()執行下列兩個stp , 並且將其執行結果以及程式的行為目的寫成文件回報 :kmalloc.stpprof.stp文件內容最多三頁(請註明學號與姓名)作業繳交方式 : FTPIP : 90 , port

10、 : 9876ID : cs101 , PWD : cs101檔名:systemtap繳交內容 : 報告文件報告文件Deadline : 4/20 , 12:00pm#! /usr/bin/env stap# Using statistics to examine kernel memory allocationsglobal kmallocprobe kernel.function(_kmalloc) kmalloc $size# Exit after 10 secondsprobe timer.ms(10000) exit () probe end printf(Count: %d all

11、ocationsn, count(kmalloc) printf(Sum: %d Kbytesn, sum(kmalloc)/1000) printf(Average: %d bytesn, avg(kmalloc) printf(Min: %d bytesn, min(kmalloc) printf(Max: %d bytesn, max(kmalloc) print(nAllocations by size in bytesn) print(hist_log(kmalloc)#!/usr/bin/env stap# This is an example of profiling a spe

12、cific command or pid.# It works by recording the time when a system call is entered# exited. # Usage: prof.stp -c top -n5# Will start up top and after 5 iterations, will exit.# Usage: prof.stp -x 3323# Will profile pid 3323 until it c is hit.#probe kernel.function(sys_*) if (target() = tid() calltim

13、etid() = gettimeofday_us()probe kernel.function(sys_*).return if (target() != tid() next now = gettimeofday_us() c = calltimetid() if (!c) next ttimeprobefunc() now - c delete calltimetid()probe end printf(n) foreach (x in ttime) printf(%-20stcalls:%6dtavg time (us):%5dttotal(us):%7dn, x, count(ttimex), avg(ttimex), sum(ttimex)globa

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论