Linux iotop命令介绍
简介
在一般运维工作中经常会遇到这么一个场景,服务器的IO负载很高(iostat中的util),但是无法快速的定位到IO负载的来源进程和来源文件导致无法进行相应的策略来解决问题。
Windows操作系统可以通过鲁大师等硬盘检测工具来查看硬盘读写速度,那么linux下测试硬盘IO读写情况怎么看?iotop是linux系统下测试硬盘IO读写的工具,简单的说,iotop是一个用来监视磁盘I/O使用状况的 top 类工具,可监测到哪一个程序使用的磁盘IO的信息(requires 2.6.20 or later)。
iotop命令 是一个用来监视磁盘I/O使用状况的top类工具。iotop具有与top相似的UI,其中包括PID、用户、I/O、进程等相关信息。Linux下的IO统计工具如iostat,nmon等大多数是只能统计到per设备的读写情况,如果你想知道每个进程是如何使用IO的就比较麻烦,使用iotop命令可以很方便的查看。
iotop使用Python语言编写而成,要求Python2.5(及以上版本)和Linux kernel2.6.20(及以上版本)。iotop提供有源代码及rpm包,可从其官方主页下载。
安装
1 | yum install -y iotop |
帮助
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | [root@docker35 ~]# iotop -h 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, i to change I/O priority, 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) |
各个参数说明:
本人提供Oracle(OCP、OCM)、MySQL(OCP)、PostgreSQL(PGCA、PGCE、PGCM)等数据库的培训和考证业务,私聊QQ646634621或微信db_bao,谢谢!-o, --only只显示正在产生I/O的进程或线程。除了传参,可以在运行过程中按o生效。
-b, --batch非交互模式,一般用来记录日志。
-n NUM, --iter=NUM设置监测的次数,默认无限。在非交互模式下很有用。
-d SEC, --delay=SEC设置每次监测的间隔,默认1秒,接受非整形数据例如1.1。
-p PID, --pid=PID指定监测的进程/线程。
-u USER, --user=USER指定监测某个用户产生的I/O。
-P, --processes仅显示进程,默认iotop显示所有线程。
-a, --accumulated显示累积的I/O,而不是带宽。
-k, --kilobytes使用kB单位,而不是对人友好的单位。在非交互模式下,脚本编程有用。
-t, --time 加上时间戳,非交互非模式。
-q, --quiet 禁止头几行,非交互模式。有三种指定方式。
-q 只在第一次监测时显示列名
-qq 永远不显示列名。
-qqq 永远不显示I/O汇总。交互按键:
和top命令类似,iotop也支持以下几个交互按键。
left和right方向键:改变排序。
r:反向排序。
o:切换至选项--only,只显示正在产生I/O的进程或线程。除了传参,可以在运行过程中按o生效。
p:切换至--processes选项。仅显示进程,默认iotop显示所有线程。
a:切换至--accumulated选项。
q:退出。
i:改变线程的优先级。
示例
时间刷新间隔2秒,输出5次
1 | iotop -d 2 -n 5 -o |
第一行:
- Total DISK READ : 0.00 B/s | Total DISK WRITE : 0.00 B/s
READ和WRITE速率总计。
第二行:
- Actual DISK READ: 0.00 B/s | Actual DISK WRITE: 59.41 K/s
实际读取和写入磁盘速率。
第三行:
- PID:进程,按p可切换线程tid
- PRIO:优先级
- USER:执行进程的用户
- DISK READ:磁盘读取速率
- DISK WRITE:磁盘写入速率
- SWAPIN:swap交换百分比
- IO>:IO等待所占用百分比,它显示每个进程的 I/O 利用率,包含磁盘和交换。
- COMMAND:线程/进程详细信息
非交互式,输出5次,间隔2秒,输出到屏幕,也可输出到日志文本,用于监控某时间段的io信息
1 | iotop -botq -n 5 -d 2 |
非交互式,输出pid为903的进程信息
1 | iotop -botq -p 903 |
统计累计的io
1 | iotop -oPa |