一,iotop的作用:
iotop是監視磁盤I/O使用狀況的top類工具,
可以針對進程和線程統計io的使用情況
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,安裝iotop:
[root@database1 ~]# yum install iotop
三,查看幫助
[root@database1 ~]# iotop --help Usage: /usr/sbin/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)
四,查看版本:
[root@yjweb ~]# iotop --version iotop 0.6
五,iotop常用參數
1,只顯示有io操作的進程
[root@database1 ~]# iotop -o
說明:-o:只顯示有io操作的進程
2,顯示針對進程的統計
[root@database1 ~]# iotop -o -P
說明:默認是針對線程的統計,表頭是TID,
如果針對進程統計,表頭是PID
3,顯示啟動后累積的數據:
[root@database1 ~]# iotop -oa
說明:
只看某一個時間點的數據有時找不到消耗I/O最高的進程,
這時查看累積的數據顯示更有效
4,監控指定pid的io使用:
[root@database1 ~]# iotop -p 26474
說明:
-p 指定進程id
5,指定刷新的間隔時間
[root@database1 ~]# iotop -o -d 2
說明:默認是1秒,
-d: 指定間隔的秒數,例子中是2秒刷新一次
6,查看指定用戶的io使用
[root@database1 ~]# iotop -o -a -u mysql
說明:
-u: 指定用戶,當查詢mysql的io時很方便
6,非交互模式,批量處理 用來記錄日志
[root@database1 ~]# iotop -boqtn3
說明:
-b: batch處理,不支持交互,常用來輸出日志 -q: quiet 只輸出一次表頭 -n:用來指定輸出循環次數:例子中我們用了3次 -t: 增加一列時間 -t和-q兩個參數只適用-b
如何輸出到文件:
[root@database1 ~]# iotop -boqn3 > /root/iotop0319.txt 2>&1
六,交互命令:
o: 打開/關閉 只顯示有io的進程/線程
p: 切換按進程和按線程的統計
a: 切換是否采用累積統計模式
q:退出
七,顯示內容各表頭的說明:
tid:線程id,按p可轉換進程pid
PRIO:優先級
DISK READ:磁盤讀取速率
DISK WRITE:磁盤寫入速率
SWAPIN:swap交換百分比
IO>:IO等待所占用百分比
COMMAND:線程/進程詳細信息
八,得到pid/tid后,如何找出它正在打開的文件?
用lsof
[root@database1 ~]# lsof -p 26474 | more COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 26474 mysql cwd DIR 252,17 4096 18268161 /data/mysql/data mysqld 26474 mysql rtd DIR 252,1 4096 2 / mysqld 26474 mysql txt REG 252,1 11172576 1055031 /usr/sbin/mysqld 。。。
說明:lsof -p參數:列出指定進程id下打開的文件
九,找到了mysql大量消耗io的線程id,如何找出對應的sql?
說明:mysql在5.7版本給performance_schema.threads表增加了thread_os_id, 即系統線程字段
低於5.7版本的mysql沒辦法根據操作系統的線程id找到sql
看例子:如果線程id是:19440
執行下面的sql即可:
SELECT a.name, a.thread_id, a.thread_os_id, a.processlist_id, a.type, b.user, b.host, b.db, b.command, b.time, b.state, b.info FROM performance_schema.threads a LEFT JOIN information_schema.processlist b ON a.processlist_id = b.id where a.type = 'FOREGROUND' and a.thread_os_id =19440
十,查看當前的centos版本
[root@yjweb ~]# cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core)