一、通过脚本分析IO的读/写数量、最大延迟、延迟的分布情况、块大小及数量
#!/bin/sh
#
# File Name :  count_io.sh
# Time      :  2020-07-29-11:24:28
# Author    :  Lucky
# QQ        :  1151574975
if [ $UID != "0" ];then
  echo "Please execute as root user."
  exit 1
fi
if [ $# -ne 1 ];then
  echo "Usage: $0 <block_device_name>"
  exit 1
fi
if [ ! -b $1 ];then
  echo "Can't find block device"
  exit 1
fi
DEVICE_NAME=$(basename $1)
blkparse -i $DEVICE_NAME | sort -g -k8 -k10 -k4 | awk '
BEGIN{
  total_read=0;
  total_write=0;
  maxwait_read=0;
  maxwait_write=0;
}
# 计算Q--C中间的IO等待延时,此处Q赋值
{
  if ($6=="Q") {
    start_time=$4;
    block=$8;
    update_block=$10;
    action=$7;
  };
  # 此处C值比较,得出IO等待值
  if ($6=="C" && $8==block && $10==update_block && $7==action) {
    await=$4-update_block;
    # IO读等待赋值
    if (action=="R") {
     if (await>maxwait_read) maxwait_read=await;
       total_read++;
       read_count_block[update_block]++;
       if (await>0.001) read_count1++;
       if (await>0.01) read_count10++;
       if (await>0.02) read_count20++;
       if (await>0.03) read_count30++;
    }
   # IO写等待赋值
   if (action=="W") {
    if (await>maxwait_write) maxwait_write=await;
      total_write++;
      write_count_block[update_block]++;
      if (await>0.001) write_count1++;
      if (await>0.01) write_count10++;
      if (await>0.02) write_count20++;
      if (await>0.03) write_count30++;
                }
   }
} END {
        printf("========\nResult:\n========\n");
        printf("total number of reads: %d\n", total_read);
        printf("total number of writes: %d\n", total_write);
        printf("slowest read : %.6f second\n", maxwait_read);
        printf("slowest write: %.6f second\n", maxwait_write);
        printf("reads\n> 1ms: %d\n>10ms: %d\n>20ms: %d\n>30ms: %d\n", read_count1, read_count10, read_count20, read_count30);
        printf("writes\n> 1ms: %d\n>10ms: %d\n>20ms: %d\n>30ms: %d\n", write_count1, write_count10, write_count20, write_count30);
        printf("\nblock size:%16s\n","Read Count");
        for (i in read_count_block)
          printf("%10d:%16d\n", i, read_count_block[i]);
        printf("\nblock size:%16s\n","Write Count");
        for (i in write_count_block)
          printf("%10d:%16d\n", i, write_count_block[i]);
}'
 
1.1 执行结果
# ./count_io.sh /dev/sda
========
Result:
========
total number of reads: 0
total number of writes: 0
slowest read : 0.000000 second
slowest write: 0.000000 second
reads
> 1ms: 0
>10ms: 0
>20ms: 0
>30ms: 0
writes
> 1ms: 0
>10ms: 0
>20ms: 0
>30ms: 0
block size:      Read Count
block size:     Write Count