# linux cpu iowait的理解
在其他地方可能会简写为`wa`、`wio`。
在linux中解释为
```
Percentage of time that the CPU or CPUs were idle during
which the system had an outstanding disk I/O request.
```
从字面理解就是:有多少的CPU时间占比是属于 **CPU空闲且还有未完成的IO请求** 这种状态
iowait本质上也是cpu空闲的一种, iowait低证明不了应用的瓶颈不在IO上,
同样的iowait高也不一定能反应应用的瓶颈在IO上,但可以作为一个侦查的方向,
这就好比体检报告,说某个指标异常但又附带了句也有可能出现在正常人身上。
举个餐厅的小例子:
当餐厅客人不多的时候,服务员(CPU)可能就会站在正在点餐的客人旁边,关注客人的点餐,
客人一点完餐就会立即去录单和推到后厨了,这段服务员花的时间就相当于iowait
当餐厅客人多的时候,服务员忙着去上菜,无暇去关注正在点餐的客人,甚至客人点完餐了,还要主动叫服务员(软中断)
这时服务员的时间就基本没花在关注正在点餐的客人身上,虽说没iowait了,但不代表没客人点餐
可参考歪果仁写的[这篇博客](https://blog.pregos.info/wp-content/uploads/2010/09/iowait.txt),
以下是截的一段cpu各指标的计算逻辑
```
The tools print out the statistics using counters that the
kernel updates periodically (on AIX, these CPU state counters
are incremented at every clock interrupt (these occur
at 10 millisecond intervals).
When the clock interrupt occurs on a CPU, the kernel
checks the CPU to see if it is idle or not. If it's not
idle, the kernel then determines if the instruction being
executed at that point is in user space or in kernel space.
If user, then it increments the 'user' counter by one. If
the instruction is in kernel space, then the 'sys' counter
is incremented by one.
If the CPU is idle, the kernel then determines if there is
at least one I/O currently in progress to either a local disk
or a remotely mounted disk (NFS) which had been initiated
from that CPU. If there is, then the 'iowait' counter is
incremented by one. If there is no I/O in progress that was
initiated from that CPU, the 'idle' counter is incremented
by one.
```