這篇文章主要介紹了Linux系統IO分析工具之iotop參數詳解(查看IO占用),本文着重注解了iotop工具的參數,以及可操作命令,需要的朋友可以參考下
簡介:
iotop – simple top-like I/O monitor
iotop是一個用來監視磁盤I/O使用狀況的 top 類工具,可監測到哪一個程序使用的磁盤IO的信息(requires 2.6.20 or later)
安裝:
yum -y install iotop
用法:
iotop [OPTIONS]
選項:
--version #顯示版本號
-h, --help #顯示幫助信息
-o, --only #顯示進程或者線程實際上正在做的I/O,而不是全部的,可以隨時切換按o
-b, --batch #運行在非交互式的模式
-n NUM, --iter=NUM #在非交互式模式下,設置顯示的次數,
-d SEC, --delay=SEC #設置顯示的間隔秒數,支持非整數值
-p PID, --pid=PID #只顯示指定PID的信息
-u USER, --user=USER #顯示指定的用戶的進程的信息
-P, --processes #只顯示進程,一般為顯示所有的線程
-a, --accumulated #顯示從iotop啟動后每個線程完成了的IO總數
-k, --kilobytes #以千字節顯示
-t, --time #在每一行前添加一個當前的時間
-q, --quiet #suppress some lines of header (implies --batch). This option can be specified up to three times to remove header lines.
-q column names are only printed on the first iteration,
-qq column names are never printed,
-qqq the I/O summary is never printed.
可用的命令(在運行iotop命令后按相應鍵位):
使用left和right改變排序(方向鍵改變排序列),還可使用以下命令:
r:反向排序,
o:切換至選項--only,
p:切換至--processes選項,
a:切換至--accumulated選項
q:退出
i:改變線程的優先級
例子:
$ iotop
$ iotop –b –n 3 –d 5
介紹
Linux下的IO統計工具如iostat, nmon等大多數是只能統計到per設備的讀寫情況, 如果你想知道每個進程是如何使用IO的就比較麻煩.
iotop 是一個用來監視磁盤 I/O 使用狀況的 top 類工具。iotop 具有與 top 相似的 UI,其中包括 PID、用戶、I/O、進程等相關信息。
實例
直接運行就好
iotop
輸出如下
Total DISK READ: 6.01 M/s | Total DISK WRITE: 3.85 K/s TID PRIO USER DISK READ DISK WRITE SWAPIN IO> COMMAND 20074 be/4 hadoop 6.01 M/s 0.00 B/s 0.00 % 4.54 % java -Dproc_datanode -Xmx8192m -server org.apache.hadoop.hdfs.server.datanode.DataNode 6371 be/4 hadoop 0.00 B/s 3.25 M/s 0.00 % 0.00 % java -Dproc_datanode -Xmx8192m -server org.apache.hadoop.hdfs.server.datanode.DataNode 8497 be/4 hadoop 0.00 B/s 3.67 M/s 0.00 % 0.00 % java -Dproc_datanode -Xmx8192m -server org.apache.hadoop.hdfs.server.datanode.DataNode 1 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % init 2 be/4 root 0.00 B/s 0.00 B/s 0.00 % 0.00 % [kthreadd]
通過輸出結果,我們可以清楚地知曉是什么程序在讀寫磁盤,速度以及命令行,pid 等信息。
在使用LINUX時經常會遇到系統IO占用很高,系統IO占用可以使用iostat -x 1查看
但很多時候知道磁盤IO負載高,但並不知道是什么程序占用的,是PHP,還是MYSQL,還是其它的,這就不好查看了。iotop工具可以實現
以ubuntu為例:
安裝
root@ubuntu111064:~# apt-get install iotop
安裝完成后
執行iotop
就可以看到類似top一樣的顯示
iotop 詳細參數:
root@ubuntu111064:~# iotop -h
unable to set locale, falling back to the default locale
Usage: /usr/bin/iotop [OPTIONS]
DISK READ and DISK WRITE are the block I/O bandwidth used during the sampling
period. SWAPIN and IO are the percentages of time the thread spent respectively
while swapping in and waiting on I/O more generally. PRIO is the I/O priority at
which the thread is running (set using the ionice command).
Controls: left and right arrows to change the sorting column, r to invert the
sorting order, o to toggle the –only option, p to toggle the –processes
option, a to toggle the –accumulated option, q to quit, any other key to force
a refresh.
Options:
–version show program’s version number and exit
-h, –help show this help message and exit
-o, –only only show processes or threads actually doing I/O
-b, –batch non-interactive mode
-n NUM, –iter=NUM number of iterations before ending [infinite]
-d SEC, –delay=SEC delay between iterations [1 second]
-p PID, –pid=PID processes/threads to monitor [all]
-u USER, –user=USER users to monitor [all]
-P, –processes only show processes, not all threads
-a, –accumulated show accumulated I/O instead of bandwidth
-k, –kilobytes use kilobytes instead of a human friendly unit
-t, –time add a timestamp on each line (implies –batch)
-q, –quiet suppress some lines of header (implies –batch)
-p 指定進程ID,顯示該進程的IO情況
-u 指定用戶名,顯示該用戶所有的進程IO情況
linux系統中,管理員可以使用top來監控進程的cpu和內存的使用情況,但是對於磁盤的i/o則只能使用iostat籠統地進行監視,不能對應進程這對於管理員來說很不方便。比如你的服務器負載很低,內存使用也很少,但是硬盤狂轉,但你就是無法准確確定是哪個進程在搞鬼。最近在google這個問題時,偶然發現了io監控這缺失的一環的工具。這就是iotop ,當前的最新版本是0.2.1,官方網站提供了源碼和rpm包下載。
官方網站: http://guichaz.free.fr/iotop/
iotop 需要python版本>=2.5,且系統內核>=2.6.20
iotop可以顯示磁盤讀寫的速率,交換分區進出情況和整體磁盤性能狀況,這些都是按照每個進程使用情況來統計。進程列表按照I/O使用狀態排序並每秒刷新一次。
IO負載高的來源定位
前言:
在一般運維工作中經常會遇到這么一個場景,服務器的IO負載很高(iostat中的util),但是無法快速的定位到IO負載的來源進程和來源文件導致無法進行相應的策略來解決問題。
這個現象在MySQL上更為常見,在5.6(performance_schema提供io instrument)之前,我們通常只能猜到是MySQL導致的高IO,但是沒法定位具體是哪個文件帶來的負載。
例如是ibdata的刷寫?還是冷門ibd的隨機讀取?
本文就將介紹一個比較簡單的定位IO高負載的流程。
工具准備:
iotop: http://guichaz.free.fr/iotop/
pt-ioprofile:http://www.percona.com/downloads/percona-toolkit/2.2.1/
Step1 : iostat 查看IO情況
iostat -x 1 查看IO情況,從下圖可以看到dfa這個磁盤的IO負載較高,接下來我們就來定位具體的負載來源
Step2: iotop定位負載來源進程
iotop的本質是一個python腳本,從proc中獲取thread的IO信息,進行匯總。
從下圖可以看出大部分的IO來源都來自於mysqld進程。因此可以確定dfa的負載來源是數據庫
Step3 pt-ioprofile定位負載來源文件
pt-ioprofile的原理是對某個pid附加一個strace進程進行IO分析。
以下是摘自官網的一段警示:
However, it works by attaching strace to the process using ptrace(), which will make it run very slowly until strace detaches. In addition to freezing the server, there is also some risk of the process crashing or performing badly after strace detaches from it, or indeed of strace not detaching cleanly and leaving the process in a sleeping state. As a result, this should be considered an intrusive tool, and should not be used on production servers unless you are comfortable with that.
通過ps aux|grep mysqld 找到 mysqld進程對應的進程號,通過pt-ioprofile查看哪個文件的IO占用時間最多。
默認參數下該工具展示的是IO占用的時間。
對於定位問題更有用的是通過IO的吞吐量來進行定位。使用參數 --cell=sizes,該參數將結果已 B/s 的方式展示出來
從上圖可以看出IO負載的主要來源是sbtest (sysbench的IO bound OLTP測試)。
並且壓力主要集中在讀取上。
iotop命令是專門顯示硬盤IO的命令,界面風格類似top命令。這個命令只有在kernelv2.6.20及以后的版本中才有。python版本需要 python2.7及以上版本
官方的說明:
Linux has always been able to show how much I/O was going on (the bi and bo columns of the vmstat 1 command).
Iotop is a Python program with a top like UI used to show of behalf of which process is the I/O going on. It requires Python ≥ 2.7 and a Linux kernel ≥ 2.6.20 with the TASK_DELAY_ACCT CONFIG_TASKSTATS, TASK_IO_ACCOUNTING and CONFIG_VM_EVENT_COUNTERS options on.
iotop安裝:
直接yum安裝。
yum install iotop
在Ubuntu里安裝命令是: sudo apt-get install iotop
安裝好之后在終端輸入:iotop就可以了
或者:
git clone git://repo.or.cz/iotop.git
iotop使用說明:
可以用左右箭頭操作,按 r 是相反方向,按 o 是動態切換
用法 iotop -參數
–version 查看版本信息的
-h, –help 查看幫助信息的
-o, –only 只顯示在划硬盤的程序
-b, –batch 批量處理 用來記錄日志的
-n NUM 設定循環幾次
-d SEC, –delay=SEC 設定顯示時間間隔