[linux]-linux通过diskstats来计算磁盘写入的延迟

导语:看到资料有计算磁盘延迟的。写一个类似的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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Linux 下,你可以通过读取 /proc/diskstats 文件来获取磁盘读写字节数。该文件包含了系统上所有磁盘的统计信息,包括读写次数、读写扇区数、读写时间、IO 请求处理时间等,每行对应一个磁盘设备。你可以使用 C 语言中的文件操作函数(如 fopen、fread、fclose 等)来读取该文件,并从中解析出你所需要的信息。 以下是一个简单的示例代码,用于获取指定磁盘设备的读写字节数: ``` #include <stdio.h> #define DISK_NAME "/dev/sda" // 指定磁盘设备 int main() { FILE *fp; char buf[256]; unsigned long long read_bytes, write_bytes; fp = fopen("/proc/diskstats", "r"); if (fp == NULL) { perror("Failed to open /proc/diskstats"); return 1; } while (fgets(buf, sizeof(buf), fp)) { char dev_name[32]; unsigned int major, minor, reads_completed, reads_merged, sectors_read, read_time; unsigned int writes_completed, writes_merged, sectors_written, write_time; if (sscanf(buf, "%u %u %s %u %u %u %u %u %u %u %u %u %u %u %llu %u %u %u %u", &major, &minor, dev_name, &reads_completed, &reads_merged, &sectors_read, &read_time, &writes_completed, &writes_merged, &sectors_written, &write_time, NULL, NULL, NULL, &read_bytes, NULL, NULL, NULL, &write_bytes) != 20) { continue; } if (strcmp(dev_name, DISK_NAME) == 0) { printf("Read bytes: %llu\nWrite bytes: %llu\n", read_bytes * 512, write_bytes * 512); break; } } fclose(fp); return 0; } ``` 需要注意的是,/proc/diskstats 文件中的所有数值都以扇区为单位,而一个扇区的大小通常为 512 字节。因此,为了获取读写字节数,你需要将读取到的扇区数乘以 512。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爷来辣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值