如何计算iostat的util?

Ray*_*ond 15 linux linux-device-driver linux-kernel

iostat -x -d 
Run Code Online (Sandbox Code Playgroud)

可以显示许多i/o统计信息.对于iostat的util,解释是:

向设备发出I/O请求的CPU时间百分比(设备的带宽利用率).当此值接近100%时发生设备饱和

我想知道如何计算util?

我做了一个实验,(见下面的代码),启动40个线程来随机读取40个文件.我想磁盘工具应该非常高,但我错了,iostat如下,任何人都可以给出原因?谢谢

Device:         rrqm/s   wrqm/s   r/s   w/s   rsec/s   wsec/s avgrq-sz avgqu-sz   await  svctm  %util
sdb1              0.01     0.44  0.24  0.57     3.44     8.14    14.34     0.00    2.28   0.66   0.05
Run Code Online (Sandbox Code Playgroud)

码:

#include <iostream>
#include <fstream>
#include <pthread.h>

using namespace std;

void* work(void* a)
{
    int* id = (int*)a;
    string file = "sys.partition";
    char buf[100];
    sprintf(buf, "%d", *id);
    file.append(string(buf));
    ifstream in(file.c_str());
    in.seekg(0, ios_base::end);
    size_t len = in.tellg();

    cout << "open file : " << file << " , " << len << endl;
    srand(time(NULL));

    while(true)
    {
        size_t pos = rand() % len;
        in.seekg(pos);
        //cout << pos << endl;
        in.read(buf, 10);
        system("sync");
    }
    in.close();
}

int main(int argc, char** argv)
{
    static const int num = 40;
    pthread_t threads[num];
    for (int i = 0; i < num; i++)       {
        pthread_create(&threads[i], NULL, work, &i);
    }
    for (int i = 0; i < num; i++)       {
        pthread_join(threads[i], NULL);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

osg*_*sgx 20

%util在iostat的源代码中命名为busy:https://code.google.com/p/tester-higkoo/source/browse/trunk/Tools/iostat/iostat.c#380

忙被计为百分比比率Ticksdeltams,限制在100%

busy = 100.0 * blkio.ticks / deltams; /* percentage! */
if (busy > 100.0) busy = 100.0;
Run Code Online (Sandbox Code Playgroud)

DeltaMS是一段时间(用户时间+系统时间+空闲时间+ iowait)/ ncpu的系统负载之和.

double deltams = 1000.0 *
        ((new_cpu.user + new_cpu.system +
          new_cpu.idle + new_cpu.iowait) -
         (old_cpu.user + old_cpu.system +
          old_cpu.idle + old_cpu.iowait)) / ncpu / HZ;
Run Code Online (Sandbox Code Playgroud)

蜱虫 - 是Time of requests in queue时期

blkio.ticks = new_blkio[p].ticks
                - old_blkio[p].ticks;
Run Code Online (Sandbox Code Playgroud)

在更新的sysstat版本中,代码有点不同:http: //sources.debian.net/src/sysstat/10.2.0-1/iostat.c#L959

/*       rrq/s wrq/s   r/s   w/s  rsec  wsec  rqsz  qusz await r_await w_await svctm %util */
printf(" %8.2f %8.2f %7.2f %7.2f %8.2f %8.2f %8.2f %8.2f %7.2f %7.2f %7.2f %6.2f %6.2f\n",
...
       /*
        * Again: Ticks in milliseconds.
        * In the case of a device group (option -g), shi->used is the number of
        * devices in the group. Else shi->used equals 1.
        */
       shi->used ? xds.util / 10.0 / (double) shi->used
                 : xds.util / 10.0);    /* shi->used should never be null here */
Run Code Online (Sandbox Code Playgroud)

xds填入compute_ext_disk_stats(&sdc, &sdp, itv, &xds); http://sources.debian.net/src/sysstat/10.2.0-1/common.c?hl=679#L679

/*
 * Macros used to display statistics values.
 *
 * HZ is 1024 on IA64 and % should be normalized to 100.
 */
#define S_VALUE(m,n,p)  (((double) ((n) - (m))) / (p) * HZ)

xds->util  = S_VALUE(sdp->tot_ticks, sdc->tot_ticks, itv);
Run Code Online (Sandbox Code Playgroud)

并且有来自iostat.c的tot_ticks的填充

  * @ioi        Current sample statistics.
  * @ioj        Previous sample statistics.
  * @itv        Interval of time.
  ...

sdc.tot_ticks = ioi->tot_ticks;
sdp.tot_ticks = ioj->tot_ticks;
Run Code Online (Sandbox Code Playgroud)

tot_ticks在(iostat.c:487)中读取" 当前块设备或分区的sysfs stat " ,并且是当前和以前的统计信息.read_sysfs_file_statioiioj


Nic*_*lay 10

iostat -x(我在实现它之前使用了一个旧的源代码来编写它)显示来自/proc/diskstats(记录在这里)和/proc/stat(对于CPU时间;参见man proc(5))(以及其他一些,但这对于理解并不重要)的信息.

您可以在osgx的答案中看到相关的代码片段,但我无法孤立地理解它们,所以这是一个扩展的解释:

  • %util = blkio.ticks / deltams * 100%
  • deltams是自上次快照以来经过的时间(以毫秒为单位).它使用的CPU统计数据/proc/stat可能是因为它提供了比依赖系统时间更好的结果,但我不确定.(附注:由于某种原因,时间被分开HZ,而文档说明它在USER_HZ,我不明白.)
  • blkio.ticks是"从I/O花费的毫秒数",来自/proc/diskstatsdocs:

    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.
    
    Run Code Online (Sandbox Code Playgroud)

    也就是说,我的理解是ticks当任何I/O请求(对于此设备)正在进行时,滴答的数量乘以滴答之间的持续时间.

所以%util = 100%意味着每次内核看起来(我想现代内核每秒1000次,见"HZ"),I/O请求正在进行中.

以下是iostat上另一篇文章的摘录:

[%util is]存储设备有多少时间工作(忙碌).

在适当的RAID环境中,它更像是"RAID阵列中至少有一个磁盘需要多长时间才能完成".我故意在这里排除任何类型的缓存 - 如果请求可以从缓存中提供,那么它在%util中显示的几率可以忽略不计,与其他值不同.

这也意味着 - RAID子系统可以从6.25%(一个执行工作的磁盘)加载到100%(所有这些都忙).那是'100%'的单一价值的相当多的见解,不是吗?