Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vmstat: What exactly does cpu wait mean?

today I had a linux system with 100% wait in the vmstat wait Column.

My question here is: What exactly does a processor do when it's waiting for I/O?

To my understanding a cpu can't really wait - it has to run some code! So is it running some tight "wait until the disk interrupt comes" code? Why is it not running other code and returning when the interrupt from the disk arrives?

To my knowledge a disk is soo slow that a cpu can do many, many cycles until the disk is ready.

Why does linux let the cpu wait until the I/O is done instead of giving it some work to do?

Thomas

like image 838
Thomas C. Avatar asked Apr 22 '16 14:04

Thomas C.


People also ask

What does CPU wait mean?

CPU wait is a somewhat broad and nuanced term for the amount of time that a task has to wait to access CPU resources. This term is popularly used in virtualized environments, where multiple virtual machines compete for processor resources.

What causes CPU wait time?

Waiting CPU time indicates the amount of time the CPU spends waiting for disk I/O or network I/O operations to complete. A high waiting time indicates that the CPU is *stranded* because of the I/O operations on that device. For optimal performance, one should aim to keep the I/O waiting CPU time as low as possible.

What is CPU wait in Linux?

For a given CPU, the I/O wait time is the time during which that CPU was idle (i.e. didn't execute any tasks) and there was at least one outstanding disk I/O operation requested by a task scheduled on that CPU (at the time it generated that I/O request).

How do I read vmstat?

The vmstat command (short for virtual memory statistics) is a built-in monitoring utility in Linux. The command is used to obtain information about memory, system processes, paging, interrupts, block I/O, disk, and CPU scheduling. Users can observe system activity virtually in real-time by specifying a sampling period.


2 Answers

I've found the answer here: http://veithen.github.io/2013/11/18/iowait-linux.html

So "waiting for I/O" on a processor level means: The Processor is doing nothing than waiting for I/O. While waiting for I/O the Processor can run user code in which case the waiting for I/O disappears and CPU% goes up.

Here the test case from the linked page.

Just run a task which is doing lot of I/O on the first CPU:

taskset 1 dd if=/dev/sda of=/dev/null bs=1MB

You can see that the first CPU is now either running kernel code (sy) or waiting for I/O (wa):

%Cpu0  :  0.0 us,  2.9 sy,  0.0 ni,  0.0 id, 97.1 wa,  0.0 hi,  0.0 si,  0.0 st

Now we start a tight cpu loop on the same cpu:

taskset 1 sh -c "while true; do true; done"

And -look at that- the I/O Wait is gone because the CPU has now some other stuff to do than wait for I/O - it runs userland code!

%Cpu0  : 91.3 us,  1.3 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  7.3 si,  0.0 st

Hope that helps someone else!

Grettings,

Thomas

like image 131
Thomas C. Avatar answered Oct 29 '22 03:10

Thomas C.


IO wait happens if a process is in 'uninterruptible'-states while waiting for the IO-device.

A process is 'uninterruptible' if it currently executes certain system-calls -- a normal read waiting for a disc to spin up won't lead to IO-wait I think -- that would lead to buggy behaviour in the application or possible data-loss if the process were to be interrupted (due to e.g. limitations in the driver used to access special hardware).

Further reading is available after searching the web for e.g. 'linux uninterruptible kernel sleep explained'

like image 32
Tom Regner Avatar answered Oct 29 '22 01:10

Tom Regner