How to get the system ioWait

9.9k Views Asked by At

How can i get the systems ioWait? This would be preferable by using info the /proc interface( i guess its written somewhere in there) so an app could detect this, but an external call to exec() from my app would be acceptable.

2

There are 2 best solutions below

0
On BEST ANSWER

This is available in /proc/stat.

From the documentation in the kernel source:

The very first "cpu" line aggregates the numbers in all of the other "cpuN" lines. These numbers identify the amount of time the CPU has spent performing different kinds of work. Time units are in USER_HZ (typically hundredths of a second). The meanings of the columns are as follows, from left to right:

  • user: normal processes executing in user mode
  • nice: niced processes executing in user mode
  • system: processes executing in kernel mode
  • idle: twiddling thumbs
  • iowait: waiting for I/O to complete
  • irq: servicing interrupts
  • softirq: servicing softirqs
  • steal: involuntary wait
0
On

Another way to get the iowait is using iostat (Debian: sysstat), for example:

iostat -c 5

will return (every 5 seconds):

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           8.39    0.00   11.42    4.74    0.00   75.46

or in JSON format:

iostat -o JSON -c 5
{
   "sysstat":{
      "hosts":[
         {
            "nodename":"host.example.com",
            "sysname":"Linux",
            "release":"4.15.0-159-generic",
            "machine":"x86_64",
            "number-of-cpus":64,
            "date":"2021/10/27",
            "statistics":[
               {
                  "avg-cpu":{
                     "user":7.32,
                     "nice":0.07,
                     "system":5.60,
                     "iowait":13.59,
                     "steal":0.00,
                     "idle":73.43
                  }
               }
            ]
         }
      ]
   }
}

In here, iowait is the same percentage reported by top, glances, etc.

Note:

If you run it once, you will get the same values each time, so a way to "fix" it, is to run it at least 2 times and use the last values:

iostat -c 1 2

which means: run it 2 times separated by 1 second

In bash you can get only the iowait value with:

iostat -c 1 2 | awk 'NF { print $4 }' | tail -n 1

In the case of JSON, it will return two children under statistics (use the last one).