Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How the util of iostat is computed?

iostat -x -d 

can display many i/o statistic info. For util of iostat, the explanation is :

Percentage of CPU time during which I/O requests were issued to the device (band-width utilization for the device). Device saturation occurs when this value is close to 100%

I want to know how the util was computed?

I make an experiment, (see following code), start 40 thread to randomly read 40 files. I suppose the disk util should be very high, but I am wrong, the iostat is as follow, anyone can give why? THX

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

Code:

#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;
}
like image 412
Raymond Avatar asked Dec 16 '10 06:12

Raymond


People also ask

How does iostat calculate Util?

When iostat says %util, it means "Percentage of CPU time during which I/O requests were issued to the device". The percentage of the time the drive was doing at least one thing. If it's doing 16 things at the same time, that doesn't change.

What is iostat command used for?

Description. The iostat command is used to monitor system input/output (I/O) devices (physical and logical) that are loaded, by observing the time for which these devices are active.

How do I run iostat continuously?

To enable disk I/O history, from the command line enter smit chgsys and then select true from the Continuously maintain DISK I/O history field. The first report from the iostat command shows cumulative activity since the last reset of the disk activity counters.

Which is the first report generated by the iostat command?

The iostat command generates two types of reports, the CPU Utilization report and the Device Utilization report. CPU Utilization Report The first report generated by the iostat command is the CPU Utilization Report. For multiprocessor systems, the CPU values are global averages among all processors.


1 Answers

%util is named busy in the source code of iostat: https://code.google.com/p/tester-higkoo/source/browse/trunk/Tools/iostat/iostat.c#380

Busy is counted as percent ratio of Ticks to deltams, limited to 100%

busy = 100.0 * blkio.ticks / deltams; /* percentage! */
if (busy > 100.0) busy = 100.0;

DeltaMS is sum of system load for the period of time (user time + system time + idle time + 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;

Ticks - is the Time of requests in queue for the period

blkio.ticks = new_blkio[p].ticks
                - old_blkio[p].ticks;

In more current version of sysstat the code is bit different: 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 */

xds is filled in the 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);

And there is the filling of tot_ticks from iostat.c

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

sdc.tot_ticks = ioi->tot_ticks;
sdp.tot_ticks = ioj->tot_ticks;

tot_ticks are read from "sysfs stat for current block device or partition" in read_sysfs_file_stat (iostat.c:487), and ioi and ioj are current and previous stat.

like image 178
osgx Avatar answered Sep 22 '22 20:09

osgx