导语:看到资料有计算磁盘延迟的。写一个类似的shell脚本 以防后续要用。
主要通过命令 cat /proc/diskstats
来获取磁盘的信息
fields:
1 - major number
2 - minor mumber
3 - device name
4 - reads completed successfully
5 - reads merged
6 - sectors read
7 - time spent reading (ms)
8 - writes completed
9 - writes merged
10 - sectors written
11 - time spent writing (ms)
12 - I/Os currently in progress
13 - time spent doing I/Os (ms)
14 - weighted time spent doing I/Os (ms)
# 第七列为 读花的时间(ms),这是所有读操作所花费的毫秒数(用__make_request()到end_that_request_last()测量)
# 第十三列为 输入输入花的时间(ms),花在I/O操作上的毫秒数,这个域会增长只要field 9不为0。
(number of milliseconds spent doing I/Os. This field is increased so long as field 9 is nonzero.)
cat /proc/diskstats
8 0 sda 208010923 6190 51045399672 78018748 40801137 2919609 20073260568 1403219000 0 49226220 1481074792
8 1 sda1 208010836 6190 51045397248 78018568 40801137 2919609 20073260568 1403219000 0 49225668 1481044656
8 16 sdb 35632370 1766 2055298292 83563540 11908017 4001307 3088071384 791169568 0 20208620 874718584
通过获取磁盘60秒内写盘花的时间(毫秒) 和写磁盘的次数 相除来获得每次写需要的耗时。
1分钟内写操作花费的毫秒数/写入完成 iops的数量
while :
do
# clear
current_time=`date "+%Y-%m-%dT%H:%M:%S"` #当前时间
echo current_time: $current_time
current_time_spent_reading=`cat /proc/diskstats |grep sdb |awk '{print $11}' |tail -1`
echo current_time_spent_reading $current_time_spent_reading
current_writes_completed=`cat /proc/diskstats |grep sdb |awk '{print $8}'|tail -1`
echo current_writes_completed $current_writes_completed
sleep 60
time_now=`date "+%Y-%m-%dT%H:%M:%S"` #当前时间
echo time_now $time_now
time_spent_reading_now=`cat /proc/diskstats |grep sdb |awk '{print $11}'|tail -1`
echo time_spent_reading_now $time_spent_reading_now
writes_completed_now=`cat /proc/diskstats |grep sdb |awk '{print $8}'|tail -1`
echo writes_completed_now $writes_completed_now
if [ "$current_writes_completed" != "$writes_completed_now" ];then
write_counts=$(($writes_completed_now - $current_writes_completed))
#aaa=$(($a - $b))
time_spent_reading=$(($time_spent_reading_now - $current_time_spent_reading))
#$[num1 - num2] $[num1 / num2]
delay_time=$(($time_spent_reading / $write_counts))
echo ------------
echo write_counts $write_counts
echo time_spent_reading $time_spent_reading
echo delay_time $delay_time ms
echo ++++++++++++
else
echo "没有写"
fi
done
测试
cat /proc/diskstats | grep sdb1
dd if=kube-bench of=/dev/null
# 测试方式:使用dd指令,对磁盘进行连续写入,不使用内存缓冲区,每次写入8k的数据,总共写入20万次,产生1.6G大小的文件。
# if=FILE read from FILE instead of stdin of=FILE write to FILE instead of stdout
# 测试指令:dd if=/dev/zero of=/data01/test.dbf bs=8k count=200000 conv=fdatasync
cat /proc/diskstats | grep sdb1