首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用C++计算Linux下的磁盘读写

用C++计算Linux下的磁盘读写
EN

Stack Overflow用户
提问于 2013-01-25 23:57:17
回答 1查看 3.1K关注 0票数 2

我的要求是用总的磁盘读/写操作(或读/写的数据量)来分析当前进程的磁盘读/写操作。我需要每秒采集样本,然后在这两者之间绘制一张图表。我需要在c++的Linux (Ubuntu12.10)上这样做。

是否有可用于此任务的API/工具?我发现了一个叫做iotop的工具,但我不确定如何将其用于当前进程与系统范围内的使用。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-26 00:29:15

您可以每秒读取文件/proc/diskstats。每行代表一个设备。

来自内核的文档/iostat.txt:

代码语言:javascript
复制
Field  1 -- # of reads completed
    This is the total number of reads completed successfully.
Field  2 -- # of reads merged, field 6 -- # of writes merged
    Reads and writes which are adjacent to each other may be merged for
    efficiency.  Thus two 4K reads may become one 8K read before it is
    ultimately handed to the disk, and so it will be counted (and queued)
    as only one I/O.  This field lets you know how often this was done.
Field  3 -- # of sectors read
    This is the total number of sectors read successfully.
Field  4 -- # of milliseconds spent reading
    This is the total number of milliseconds spent by all reads (as
    measured from __make_request() to end_that_request_last()).
Field  5 -- # of writes completed
    This is the total number of writes completed successfully.
Field  7 -- # of sectors written
    This is the total number of sectors written successfully.
Field  8 -- # of milliseconds spent writing
    This is the total number of milliseconds spent by all writes (as
    measured from __make_request() to end_that_request_last()).
Field  9 -- # of I/Os currently in progress
    The only field that should go to zero. Incremented as requests are
    given to appropriate struct request_queue and decremented as they finish.
Field 10 -- # of milliseconds spent doing I/Os
    This field increases so long as field 9 is nonzero.
Field 11 -- weighted # of milliseconds spent doing I/Os
    This field is incremented at each I/O start, I/O completion, I/O
    merge, or read of these stats by the number of I/Os in progress
    (field 9) times the number of milliseconds spent doing I/O since the
    last update of this field.  This can provide an easy measure of both
    I/O completion time and the backlog that may be accumulating.

对于每个进程,您可以使用/proc/<pid>/io,它会产生如下内容:

代码语言:javascript
复制
rchar: 2012
wchar: 0
syscr: 7
syscw: 0
read_bytes: 0
write_bytes: 0
cancelled_write_bytes: 0

  • rchar,wchar:读取/写入的字节数。number of read/write system calls.
  • read_bytes,write_bytes:读取/写入存储介质的字节数。据我所知,
  • cancelled_write_bytes:是由调用"ftruncate“来取消对同一文件的挂起写操作引起的。最常见的可能是0。
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14525390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档