块设备层中IO事件统计

块设备层中IO事件统计

一、可统计的内容

1、IO合并的比例(传入的IO请求/合并后传入设备驱动的请求),合并后块大小统计
2、块设备层中IO各个阶段的时间统计(生成IO请求时间、IO调度时间和驱动和硬件上消耗的时间等,分析IO性能瓶颈)

二、统计方法

blktrace + blkparse + btt

1、开启监测

text
1
2
$ blktrace -d /dev/sda
# 其中-d指定监测的设备,运行时无终端输出,使用Ctrl+C终止

2、整理结果
blktrace在执行的目录中生成sdc.blktrace.X文件,x代表CPU id,此文件无法直接读取,使用blkparse解析这些文件

text
1
2
$ blkparse -i sdc
# 解析文件,-i指定了输入文件

输出及其解释:

每一个IO的输出格式

text
1
2
3
4
5
6
7
8
9
10
8,32   1  1201902    31.648378780     7  M   W 35726288 + 8 [kworker/u49:0]
# 第一个字段:8,32 这个字段是设备号 major device ID和minor device ID。
# 第二个字段:1 表示CPU
# 第三个字段:1201902 序列号
# 第四个字段:31.648378780 Time Stamp是时间偏移
# 第五个字段:PID 本次IO对应的进程ID
# 第六个字段:Event,这个字段非常重要,反映了IO进行到了那一步
# 第七个字段:R表示 Read, W是Write,D表示block,B表示Barrier Operation
# 第八个字段:35726288 + 8,表示的是起始block number 和 number of blocks,即我们常说的Offset 和 Size
# 第九个字段: 进程名

合计格式

text
1
2
3
4
5
6
7
8
9
10
11
Total (sdc):
Reads Queued:           0,       0KiB Writes Queued:       1,392K,    5,570MiB
Read Dispatches:        0,       0KiB Write Dispatches:   10,869,    5,564MiB
Reads Requeued:         0               Writes Requeued:         0
Reads Completed:        0,       0KiB Writes Completed:   10,869,    5,564MiB
Read Merges:            0,       0KiB Write Merges:        1,381K,    5,527MiB
PC Reads Queued:        0,       0KiB PC Writes Queued:        0,       0KiB
PC Read Disp.:         15,       0KiB PC Write Disp.:          0,       0KiB
PC Reads Req.:          0               PC Writes Req.:          0
PC Reads Compl.:        7               PC Writes Compl.:        0
IO unplugs:           743               Timer unplugs:           0

blkparse的结果不是很清晰,可以使用btt进行进一步的分析
先将blktrace生成的文件整合到一个文件中:

text
1
$ blkparse -O -i sdc -d sdc.blktrace.bin

使用btt进行进一步的分析:

text
1
$ btt -A -i sdc.blktrace.bin

输出和解释:

text
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
一、IO请求到写入设备各个阶段的时间统计
==================== All Devices ====================
ALL           MIN           AVG           MAX           N

--------------- ------------- ------------- ------------- -----------
• Q2Q               0.000000808   0.000022550   0.169176199     5017599
• Q2G               0.000000606   0.002694758   0.169166444       39199
• S2G               0.070385850   0.086264639   0.169165248        1224
• G2I               0.000146627   0.001615914   0.006145786       39199
• Q2M               0.000000149   0.000000254   0.000217874     4978400
• I2D               0.227251891   0.313646256   0.464198624       39103
• M2D               0.230494332   0.315166061   0.464387299     4966208
• D2C               0.056031007   0.095095120   0.210527104     5001087
• Q2C               0.338179300   0.410294069   0.596850606     5001087
其中各个阶段说明:
• Q2Q — time between requests sent to the block layer
• Q2G — time from a block I/O is queued to the time it gets a request allocated for it
• G2I — time from a request is allocated to the time it is Inserted into the device's queue
• Q2M — time from a block I/O is queued to the time it gets merged with an existing request
• I2D — time from a request is inserted into the device's queue to the time it is actually issued to the device
• M2D — time from a block I/O is merged with an exiting request until the request is issued to the device
• D2C — service time of the request by the device
• Q2C — total time spent in the block layer for a request
问题:Q2I + I2D + D2C = Q2C,实际测出来的并不相等,资料中的结果也是

二、IO请求到写入设备各个阶段的占比
==================== Device Overhead ====================
DEV |       Q2G       G2I       Q2M       I2D       D2C
---------- | --------- --------- --------- --------- ---------
(  8, 32) |   0.0051%   0.0031%   0.0001%   0.5977%  23.1773%
---------- | --------- --------- --------- --------- ---------
Overall |   0.0051%   0.0031%   0.0001%   0.5977%  23.1773%
当I2D很大的时候可以试试其它的IO调度策略

三、IO合并的比例,Q表示传入的IO请求数量,D表示合并后发出的请求数量,Ratio表示合并的比例,BLKxxx表示合并后的块的大小最大值、最小值和平均值
==================== Device Merge Information ====================
DEV |       #Q       #D   Ratio |   BLKmin   BLKavg   BLKmax   Total
---------- | -------- -------- ------- | -------- -------- -------- --------
(  8, 32) |  5017600    39198   128.0 |     1024     1024     1024 40138752

四、IO调度相关,当一个IO 进入Q状态时,调度器中此时有多少IO处于D状态
==================== Active Requests At Q Information ====================
DEV | Avg Reqs @ Q
---------- | -------------
(  8, 32) |          14.9

五、每一个进程的时间段统计
==================== Per Process ====================
Q2C           MIN           AVG           MAX           N
--------------- ------------- ------------- ------------- -----------
kworker           0.339407380   0.414291058   0.595046589     1374335

六、Q2D统计直方图数据
==================== Q2D Histogram ====================
DEV | <.005 <.010 <.025 <.050 <.075 <.100 <.250 <.500 < 1.0 >=1.0
--------- | ===== ===== ===== ===== ===== ===== ===== ===== ===== =====
(  8, 32) |   0.0   0.0   0.0   0.0   0.0   0.0   0.0 100.0   0.0   0.0
========== | ===== ===== ===== ===== ===== ===== ===== ===== ===== =====
AVG |   0.0   0.0   0.0   0.0   0.0   0.0   0.0 100.0   0.0   0.0

七、设备驱动或者设备具有IO的时间,与iostat的util类似
==================== I/O Active Period Information ====================
DEV |     # Live     Avg. Act     Avg. !Act % Live
---------- | ---------- ------------- ------------- ------
(  8, 32) |          1   0.000000000   0.000000000 100.00
---------- | ---------- ------------- ------------- ------
Total Sys |          1   0.000000000   0.000000000 100.00

参考文档

[0] btt
[1] 切换调度器-Linux
[2] 切换调度器-Redhat
[3] 单队列的移除