centos8平台使用iotop监控磁盘io

一,iotop的作用:

iotop是监视磁盘I/O使用状况的top类工具,

可以针对进程和线程统计io的使用情况

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

         对应的源码可以访问这里获取: https://github.com/liuhongdi/

 说明:作者:刘宏缔 邮箱: [email protected]

二,安装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)

猜你喜欢

转载自www.cnblogs.com/architectforest/p/12537095.html