理解 CPU 利用率

从 top 命令说起

在 Linux shell 上执行 top 命令,可以看到这样一行 CPU 利用率的数据:

%Cpu(s):  0.1 us,  0.0 sy,  0.0 ni, 99.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

这里引用一下 top 命令的 Linux man-pages 里面的介绍:

us, user: time running un-niced user processes

sy, system: time running kernel processes

ni, nice: time running niced user processes

id, idle: time spent in the kernel idle handler

wa, IO-wait: time waiting for I/O completion

hi: time spent servicing hardware interrupts

si: time spent servicing software interrupts

st: time stolen from this vm by the hypervisor

/proc/stat

简单介绍一下 Linux 计算 CPU 利用率的基本方法。

/proc/stat 存储的是系统的一些统计信息。在我的机器上的某一时刻,内容如下:

[linjinhe@localhost ~]$ cat /proc/stat 
cpu  117450 5606 72399 476481991 1832 0 2681 0 0 0
cpu0 31054 90 19055 119142729 427 0 1706 0 0 0
cpu1 22476 3859 18548 119155098 382 0 272 0 0 0
cpu2 29208 1397 19750 119100548 462 0 328 0 0 0
cpu3 34711 258 15045 119083615 560 0 374 0 0 0
intr 41826673 113 102 0 0 0 0 0 0 1 134 0 0 186 0 0 0 81 0 0 256375 0 0 0 29 0 602 143 16 442 94859 271462 25609 4618 8846 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 58634924
btime 1540055659
processes 5180062
procs_running 1
procs_blocked 0
softirq 49572367 5 22376247 238452 3163482 257166 0 4492 19385190 0 414733

我们只关注第一行 cpu (下面第二行是我加上的注释)。

cpu  117450 5606   72399   476481991  1832     0    2681   0      0         0
      (us)  (ni)    (sy)     (id)      (wa)   (hi)  (si)  (st) (guest) (guest_nice)

前面一节,对于 CPU 利用率描述,Linux man-pages 用的都是 time( time running, time spent,time stolen)这个单词。这里的统计数据,其实就是 CPU 从系统启动至当前,各项(us, sy, ni, id, wa, hi, si, st)占用的时间,单位是 jiffies。通过 sysconf(_SC_CLK_TCK) 可以获得 1 秒被分成多少个 jiffies 。一般是 100,即 1 jiffies == 0.01 s。(st、guest、guest_nice 和虚拟化/虚拟机相关,如果这些值太高,说明虚拟化的实现或者宿主机有问题。不是本文关注的重点。)

计算 CPU 使用率的基本原理就是从 /proc/stat 进行采样和计算。最简单的方法,一秒采样一次 /proc/stat,如:

第 N 秒采样得到 cpu_total1 = us1 + ni1 + sy1 + id1 + wa1 + hi1 + si1 + st1 + guest1 + guest_nice1
第 N+1 秒采样得到 cpu_total2 = us2 + ni2 + sy2 + id2 + wa2 + hi2 + si2 + st2 + guest2 + guest_nice2
us 的占比为 (us2 - us1) / (cpu_total2 - cpu_total1)

nice

nice - run a program with modified scheduling priority

nice 是一个可以修改进程调度优先级的命令,具体可以参考 man-pages。在 Linux 中,一个进程有一个 nice 值,代表的是这个进程的调度优先级。

越 nice (nice 值越大)的进程,调度优先级越低。怎么理解这句话?进程调度本质上是进程间对 CPU 这一有限资源的争抢,越 nice 的进程,越会“谦让”,所以它的获得 CPU 的机会就越低。

上面的 CPU 利用率里面,将用户态进程使用的 CPU 分成 niced 和 un-niced 两部分,没什么本质差别。平时很少遇到要使用 nice 命令的场景(我个人从来没遇到过)。

理解 us

知道了 us 代表的意义后,简单写个代码控制一下 us

#include <pthread.h>
#include <stdio.h>
#include <assert.h>
#include <vector>
#include <string>

void* CpuUsWorker(void* arg)
{
    uint64_t i = 0;
    while (true)
    {
        i++;
    }   
    return nullptr;
}

void CpuUs(int n)
{
    std::vector<pthread_t> pthreads(n);
    for (int i = 0; i < n; i++)
    {
        assert(pthread_create(&pthreads[i], nullptr, CpuUsWorker, nullptr) == 0);
    }

    for (const auto& tid : pthreads)
    {
        assert(pthread_join(tid, nullptr) == 0);
    }
}

int main(int argc, char** argv)
{
    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s threads\n", argv[0]);
        return -1;
    }   
    CpuUs(std::stoi(argv[1]));
    return 0;
}

测试的机器是 4 个核。代码比较简单,一个线程可以跑满一个核。下面是我的测试结果:

./cpu_us 1
%Cpu(s): 25.0 us,  0.0 sy,  0.0 ni, 75.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
./cpu_us 2
%Cpu(s): 50.0 us,  0.1 sy,  0.0 ni, 49.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
./cpu_us 3
%Cpu(s): 75.1 us,  0.0 sy,  0.0 ni, 24.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

理解 ni

  1. ni 代表的是 niced 用户态进程消耗的 CPU。
  2. nice 可以调整运行程序的 nice 值。

下面是我的测试结果,可以看出 ni 变成 25%,符合预期。

nice ./cpu_us 1
%Cpu(s):  0.1 us,  0.0 sy, 25.0 ni, 74.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

下面是 top 命令显示的 nice ./cpu_us 1 的进程信息,NI 这一列就是 nice 值,其值为 10。

PID    USER    PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND          
6905 linjinhe  30  10   23024    844    700 S 100.0  0.0   0:03.06 cpu_us 

下面是 ./cpu_us 1 的进程信息,其值为 0。

PID   USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND  
6901 linjinhe   20   0   23024    844    700 S 100.0  0.0   0:12.36 cpu_us  

理解 sy

一般情况下,如果 sy 过高,说明程序调用 Linux 系统调用的开销很大。这个也可以简单写个程序验证一下。

#include <pthread.h>
#include <stdio.h>
#include <assert.h>
#include <vector>
#include <string>

void* NoopWorker(void* arg)
{
    return nullptr;
}

void* CpuSyWorker(void* arg)
{
    while (true)
    {
        pthread_t tid;
        assert(pthread_create(&tid, nullptr, NoopWorker, nullptr) == 0);
        assert(pthread_detach(tid) == 0);
    }
}

void CpuSy(int n)
{
    std::vector<pthread_t> pthreads(n); 
    for (int i = 0; i < n; i++)
    {
        assert(pthread_create(&pthreads[i], nullptr, CpuSyWorker, nullptr) == 0);
    }
    for (const auto& tid : pthreads)
    {
        assert(pthread_join(tid, nullptr) == 0);
    }
}

int main(int argc, char** argv)
{
    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s threads\n", argv[0]);
        return -1;
    }

    CpuSy(std::stoi(argv[1]));
}

测试结果:

./cpu_sy 1
%Cpu(s):  8.8 us, 59.3 sy,  0.0 ni, 31.3 id,  0.0 wa,  0.0 hi,  0.6 si,  0.0 st

大量的系统调用让 sy 飙升。不同的系统调用开销不一样,pthread_create 的开销比较大。

理解 wa

wa 这一项,连相关的 Linux man-pages 都说它不太靠谱。所以千万不要看到 wa 很高就觉得系统的 I/O 有问题。

  1. The CPU will not wait for I/O to complete; iowait is the time that a task is waiting for I/O to complete. When a CPU goes into idle state for outstanding task I/O, another task will be scheduled on this CPU.
  2. On a multi-core CPU, the task waiting for I/O to complete is not running on any CPU, so the iowait of each CPU is difficult to calculate.
  3. The value in this field may decrease in certain conditions.

说一下我的理解:

  1. 假设有个单核的系统。CPU 并不会真的“死等” I/O。此时的 CPU 实际是 idle 的,如果有其它进程可以运行,则运行其它进程,此时 CPU 时间就不算入 iowait。如果此时系统没有其它进程需要运行,则 CPU 需要“等”这次 I/O 完成才可以继续运行,此时“等待”的时间算入 iowait。
  2. 对于多核系统,如果有 iowait,要算给哪个 CPU?这是个问题。
  3. wa 高,不能说明系统的 I/O 有问题。如果整个系统只有简单任务不停地进行 I/O,此时的 wa 可能很高,而系统磁盘的 I/O 也远远没达到上限。
  4. wa 低,也不能说明系统的 I/O 没问题。假设机器进行大量的 I/O 任务把磁盘带宽打得慢慢的,同时还有计算任务把 CPU 也跑得满满的。此时 wa 很低,但系统 I/O 压力很大。
#include <pthread.h>
#include <stdio.h>
#include <assert.h>
#include <vector>
#include <string>
#include <fcntl.h>
#include <unistd.h>

void* CpuWaWorker(void* arg)
{
    std::string filename = "test_" + std::to_string(pthread_self());
    int fd = open(filename.c_str(), O_CREAT | O_WRONLY);
    assert(fd > 0);
    while (true)
    {
        assert(write(fd, filename.c_str(), filename.size()) > 0);
        assert(write(fd, "\n", 1) > 0);
        assert(fsync(fd) == 0);
    }
    return nullptr;
}

void CpuWa(int n)
{
    std::vector<pthread_t> pthreads(n);
    for (int i = 0; i < n; i++)
    {
        assert(pthread_create(&pthreads[i], nullptr, CpuWaWorker, nullptr) == 0);
    }

    for (const auto& tid : pthreads)
    {
        assert(pthread_join(tid, nullptr));
    }
}

int main(int argc, char** argv)
{
    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s threads\n", argv[0]);
        return -1;
    }   
    CpuWa(std::stoi(argv[1]));
    return 0;
}
./cpu_wa 10
%Cpu(s):  0.3 us,  6.3 sy,  0.0 ni, 50.0 id, 41.1 wa,  0.0 hi,  2.3 si,  0.0 s

在上面这个例子中,我用多个线程不停地进行小 I/O 把 wa 的值刷上去了,但是其实占用的 I/O 带宽很小,我的测试机是 SSD 的,此时的 I/O 压力并不大。

再看一个例子:

./cpu_wa 10
./cpu_us 3
%Cpu(s): 75.3 us,  3.5 sy,  0.0 ni,  8.2 id, 10.3 wa,  0.0 hi,  2.7 si,  0.0 st

可以看到,明明同样执行了 ./cpu_wa 10, wa 竟然因为同时进行 ./cpu_us 3 而降下来!!!参考上面第 4 点。

理解 si 和 hi

系统调用会触发软中断,所以在上面的一些例子执行时,si 也会有所变化,如:

./cpu_wa 10
%Cpu(s):  0.3 us,  6.3 sy,  0.0 ni, 50.0 id, 41.1 wa,  0.0 hi,  2.3 si,  0.0 s

网卡收到数据包后,网卡驱动会通过软中断通知 CPU。这里用 iperf 网络性能测试工具做一下实验。

$ iperf -s -i 1  # 服务端

$ iperf -c 192.168.1.4 -i 1 -t 60 # 客户端,可以开几个 terminal 执行多个客户端,这样 si 的变化才会比较明显
%Cpu(s):  1.7 us, 74.1 sy,  0.0 ni,  8.0 id,  0.0 wa,  0.0 hi, 16.2 si,  0.0 st

硬件中断的话,暂时找不到什么测试方法,实际应用中应该也比较少遇到。

理解 st

st 和虚拟化相关,这里说说我的理解。

利用虚拟化技术,一台 32 CPU 核心的物理机,可以创建出几十上百个单 CPU 核心的虚拟机。这在公有云场景下,简称“超卖”。
大部分情况下,物理服务器的资源有大量是闲置的。此时,“超卖”并不会造成明显影响。
当很多虚拟机的 CPU 压力变大,此时物理机的资源明显不足,就会造成各个虚拟机之间相互竞争、相互等待。
st 就是用来衡量被 Hypervisor “偷去” 给其它虚拟机使用的 CPU。这个值越高,说明这台物理服务器的资源竞争越激烈。

(云厂商会不会把他们的内核给改了,把 st 改成 0 不让你发现这种情况?)

理解 id

CPU 空闲,感觉这个从应用层的角度没什么难理解的。

这里推荐一篇文章 What's a CPU to do when it has nothing to do?,有兴趣的可以看一下。

(本文完)

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容