# CPU

> over all check commands - sar, vmstat, top
>
> Detailed check - mpstat

Points

* CPU usage   -   哪些进程占用CPU
* CPU过载 - 需求的CPU  大于实际CPU数目
* CPU 本身故障

### Granter CPU info

`cat /proc/cpuinfo | grep processor`&#x20;

`lscpu`

### Troubleshooting steps

```
#top show overall views  and 1 show all cpus status
top  +  1  
# vmstat check r and b 
vmstat 2
# refreah in 2 seconds, check cpus 
sar -u  2
# mpstat check all or each cpu status
mpstat -P ALL 2
mpstat -P 2 2  # check processor 2 status
```

## Top

默认进入top 是按照CPU 排序的

```
top - 15:20:33 up 7 days, 12:15,  1 user,  load average: 0.01, 0.02, 0.00
Tasks: 107 total,   1 running,  59 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.1 us,  0.2 sy,  0.0 ni, 99.6 id,  0.2 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   933940 total,   132316 free,   107456 used,   694168 buff/cache
KiB Swap:        0 total,        0 free,        0 used.   806876 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
26022 rjin      20   0    9252   3752   3152 R   0.7  0.4   0:00.09 top
    1 root      20   0  161128   8548   6088 S   0.0  0.9   1:05.65 systemd
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.87 kthreadd
    4 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/0:0H
    6 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 mm_percpu_wq
    7 root      20   0       0      0      0 S   0.0  0.0   0:00.96 ksoftirqd/0
```

按下数字“1”，显示所有CPU 状况

```
top - 15:18:17 up 7 days, 12:13,  1 user,  load average: 0.08, 0.04, 0.00
Tasks: 107 total,   1 running,  59 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  :  0.3 us,  0.3 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   933940 total,   132160 free,   107620 used,   694160 buff/cache
KiB Swap:        0 total,        0 free,        0 used.   806712 avail Mem
```

> shift + p list highest cpu pid                     &#x20;
>
> top -Hp pid     # -H threads

> top -u userid

## Sar

sar -u 2&#x20;

```
 root@shubuntu  ~  sar -u 2                                                                            2 ↵  ⚡  23  05:07:31
Linux 4.15.0-1031-raspi2 (shubuntu) 	07/18/19 	_aarch64_	(4 CPU)

05:07:36        CPU     %user     %nice   %system   %iowait    %steal     %idle
05:07:38        all      0.00      0.00      0.13      0.00      0.00     99.87
05:07:40        all      0.00      0.00      0.25      0.00      0.00     99.75
05:07:42        all      0.00      0.00      0.13      0.25      0.00     99.62
```

```
%user 用户模式下消耗的CPU时间的比例；
%nice 通过nice改变了进程调度优先级的进程，在用户模式下消耗的CPU时间的比例
%system 系统模式下消耗的CPU时间的比例；
%iowait CPU等待磁盘I/O导致空闲状态消耗的时间比例；
%steal 利用Xen等操作系统虚拟化技术，等待其它虚拟CPU计算占用的时间比例；
%idle CPU空闲时间比例；
```

useful command&#x20;

> sar -u 2 &#x20;
>
> sar -u -f  /va/log/sa/sa17

## Vmstat

> overall to check CPU status

```
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  1      0 3081060 110020 869352    0    0     2    20   32   46  1  0 99  0  0
 0  0      0 3081036 110024 869352    0    0     0     2   80   93  0  0 86 14  0
 0  0      0 3080912 110024 869352    0    0     0     0  121  166  0  0 100  0  0
 0  0      0 3080912 110024 869360    0    0     0     0   89  105  0  0 100  0  0
```

> 关注几点 r,  b&#x20;

```
r 表示运行队列(就是说多少个进程真的分配到CPU)
b 表示阻塞的进程
si  每秒从磁盘读入虚拟内存的大小，如果这个值大于0，表示物理内存不够用或者内存泄露了，要查找耗内存进程解决掉。我的机器内存充裕，一切正常
so  每秒虚拟内存写入磁盘的大小，如果这个值大于0，同上。
bi  块设备每秒接收的块数量，这里的块设备是指系统上所有的磁盘和其他块设备，默认块大小是1024byte，我本机上没什么IO操作，所以一直是0，但是我曾在处理拷贝大量数据(2-3T)的机器上看过可以达到140000/s，磁盘写入速度差不多140M每秒
bo 块设备每秒发送的块数量，例如我们读取文件，bo就要大于0。bi和bo一般都要接近0，不然就是IO过于频繁，需要调整。
in 每秒CPU的中断次数，包括时间中断
cs 每秒上下文切换次数，例如我们调用系统函数，就要进行上下文切换，线程的切换，也要进程上下文切换，这个值要越小越好，太大了，要考虑调低线程或者进程的数目,例如在apache和nginx这种web服务器中，我们一般做性能测试时会进行几千并发甚至几万并发的测试，选择web服务器的进程可以由进程或者线程的峰值一直下调，压测，直到cs到一个比较小的值，这个进程和线程数就是比较合适的值了。系统调用也是，每次调用系统函数，我们的代码就会进入内核空间，导致上下文切换，这个是很耗资源，也要尽量避免频繁调用系统函数。上下文切换次数过多表示你的CPU大部分浪费在上下文切换，导致CPU干正经事的时间少了，CPU没有充分利用，是不可取的。
us 用户CPU时间，我曾经在一个做加密解密很频繁的服务器上，可以看到us接近100,r运行队列达到80(机器在做压力测试，性能表现不佳)。
sy 系统CPU时间，如果太高，表示系统调用时间长，例如是IO操作频繁。
id  空闲 CPU时间，一般来说，id + us + sy = 100,一般我认为id是空闲CPU使用率，us是用户CPU使用率，sy是系统CPU使用率。
wt 等待IO CPU时间
```

## mpstat  - Multiprocessor Statistics

> Check more info of CPU

```
 root@shubuntu  ~  mpstat -P ALL 2                                                                       ✔  ⚡  21  05:06:06
Linux 4.15.0-1031-raspi2 (shubuntu) 	07/18/19 	_aarch64_	(4 CPU)

05:06:10     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
05:06:12     all    0.00    0.00    0.00    0.25    0.00    0.00    0.00    0.00    0.00   99.75
05:06:12       0    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
05:06:12       1    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
05:06:12       2    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
05:06:12       3    0.00    0.00    0.00    1.01    0.00    0.00    0.00    0.00    0.00   98.99
```

```
%user      在internal时间段里，用户态的CPU时间(%)，不包含nice值为负进程  (usr/total)*100
%nice      在internal时间段里，nice值为负进程的CPU时间(%)   (nice/total)*100
%sys       在internal时间段里，内核时间(%)       (system/total)*100
%iowait    在internal时间段里，硬盘IO等待时间(%) (iowait/total)*100
%irq       在internal时间段里，硬中断时间(%)     (irq/total)*100
%soft      在internal时间段里，软中断时间(%)     (softirq/total)*100
%idle      在internal时间段里，CPU除去等待磁盘IO操作外的因为任何原因而空闲的时间闲置时间(%) (idle/total)*100
```

> Useful command:

mpstat -P ALL 2&#x20;

mpstat -P 2 2  # check cpu 2 status, refresh every 2 seconds&#x20;
