自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

聊聊 Top 命令中的 CPU 使用率

商務辦公
當系統(tǒng)中沒有可運行的進程時,將會執(zhí)行 idle 進程。也就是說,當系統(tǒng)執(zhí)行 idle 進程時,表示系統(tǒng)正處于空閑狀態(tài)。

[[402755]]

本文轉(zhuǎn)載自微信公眾號「Linux內(nèi)核那些事」,作者songsong001。轉(zhuǎn)載本文請聯(lián)系Linux內(nèi)核那些事公眾號。

平常我們使用 top 命令來查看系統(tǒng)的性能情況,在 top 命令中可以看到很多不同類型的 CPU 使用率,如下圖紅框中標出部分:

下面,我們來介紹一下這些 CPU 使用率的意義:

  • us:user time,表示 CPU 執(zhí)行用戶進程的時間,包括 nice 時間。通常都是希望用戶空間CPU越高越好。
  • sy:system time,表示 CPU 在內(nèi)核運行的時間,包括 IRQ 和 softirq。系統(tǒng) CPU 占用越高,表明系統(tǒng)某部分存在瓶頸。通常這個值越低越好。
  • ni:nice time,具有優(yōu)先級的用戶進程執(zhí)行時占用的 CPU 利用率百分比。
  • id:idle time,表示系統(tǒng)處于空閑期,等待進程運行。
  • wa:waiting time,表示 CPU 在等待 IO 操作完成所花費的時間。系統(tǒng)不應該花費大量的時間來等待 IO 操作,否則就說明 IO 存在瓶頸。
  • hi:hard IRQ time,表示系統(tǒng)處理硬中斷所花費的時間。
  • si:soft IRQ time,表示系統(tǒng)處理軟中斷所花費的時間。
  • st:steal time,被強制等待(involuntary wait)虛擬 CPU 的時間,此時 Hypervisor 在為另一個虛擬處理器服務。

當然,單靠上面的解釋來理解它們的意義還是比較困難的。所以,本文主要從源碼的角度來分析它們到底代表什么。

時鐘中斷

首先,我們要知道統(tǒng)計 CPU 使用情況在什么地方執(zhí)行的。在分析之前,我們先來了解下 時鐘中斷:

時鐘中斷:是一種硬中斷,由時間硬件(系統(tǒng)定時器,一種可編程硬件)產(chǎn)生。當 CPU 接收到時鐘中斷信號后,會在處理完當前指令后調(diào)用 時鐘中斷處理程序 來完成更新系統(tǒng)時間、執(zhí)行周期性任務等。

可以發(fā)現(xiàn),統(tǒng)計 CPU 使用情況是在 時鐘中斷處理程序 中完成的。

每個 CPU 的使用情況通過 cpu_usage_stat 結(jié)構(gòu)來記錄,我們來看看其定義:

  1. struct cpu_usage_stat { 
  2.     cputime64_t user
  3.     cputime64_t nice; 
  4.     cputime64_t system; 
  5.     cputime64_t softirq; 
  6.     cputime64_t irq; 
  7.     cputime64_t idle; 
  8.     cputime64_t iowait; 
  9.     cputime64_t steal; 
  10.     cputime64_t guest; 
  11. }; 

從 cpu_usage_stat 結(jié)構(gòu)的定義可以看出,其每個字段與 top 命令的 CPU 使用率類型一一對應。在內(nèi)核初始化時,會為每個 CPU 創(chuàng)建一個 cpu_usage_stat 結(jié)構(gòu),用于統(tǒng)計 CPU 的使用情況。

OK,現(xiàn)在我們來分析下內(nèi)核是怎么統(tǒng)計 CPU 的使用情況的。

每次執(zhí)行 時鐘中斷處理程序 都會調(diào)用 account_process_tick 函數(shù)進行 CPU 使用情況統(tǒng)計,我們來分析一下 account_process_tick 函數(shù)的實現(xiàn):

  1. void account_process_tick(struct task_struct *p, int user_tick) 
  2.     cputime_t one_jiffy_scaled = cputime_to_scaled(cputime_one_jiffy); 
  3.     struct rq *rq = this_rq(); 
  4.  
  5.     // 說明:user_tick 變量標識當前是否處于執(zhí)行用戶應用程序 
  6.  
  7.     if (user_tick) { 
  8.         // 1. 如果 CPU 在執(zhí)行用戶程序, 那么調(diào)用 account_user_time 進行統(tǒng)計 
  9.         account_user_time(p, cputime_one_jiffy, one_jiffy_scaled); 
  10.     } else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET)) { 
  11.         // 2. 如果 CPU 在執(zhí)行內(nèi)核代碼, 那么調(diào)用 account_system_time 進行統(tǒng)計 
  12.         account_system_time(p, HARDIRQ_OFFSET, cputime_one_jiffy, 
  13.                             one_jiffy_scaled); 
  14.     } else { 
  15.         // 3. 否則說明 CPU 在執(zhí)行 idle 進程(也就是處于空閑狀態(tài)), 那么調(diào)用 account_idle_time 進行統(tǒng)計 
  16.         account_idle_time(cputime_one_jiffy); 
  17.     } 

account_process_tick 函數(shù)主要分 3 種情況進行統(tǒng)計,如下:

如果 CPU 在執(zhí)行用戶程序,那么調(diào)用 account_user_time 進行統(tǒng)計。

如果 CPU 在執(zhí)行內(nèi)核代碼,那么調(diào)用 account_system_time 進行統(tǒng)計。

否則說明 CPU 在執(zhí)行 idle 進程(也就是處于空閑狀態(tài)),那么調(diào)用 account_idle_time 進行統(tǒng)計。

CPU 使用情況統(tǒng)計

下面我們分別對這 3 種統(tǒng)計進行分析。

1. 統(tǒng)計用戶程序執(zhí)行時間

統(tǒng)計用戶程序的執(zhí)行時間是通過 account_user_time 函數(shù)來完成的,我們來看看其實現(xiàn):

  1. void account_user_time(struct task_struct *p, cputime_t cputime, 
  2.                        cputime_t cputime_scaled) 
  3.     // 獲取 CPU 的統(tǒng)計結(jié)構(gòu)(每個CPU一個 cpu_usage_stat 結(jié)構(gòu)) 
  4.     struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;  
  5.     cputime64_t tmp; 
  6.     ... 
  7.  
  8.     // 分 2 種情況統(tǒng)計 CPU 的使用情況 
  9.     // 1. 如果進程的 nice 值大于0, 那么將會統(tǒng)計到 nice 字段中 
  10.     // 2. 如果進程的 nice 值小于等于0, 那么將會統(tǒng)計到 user 字段中 
  11.     if (TASK_NICE(p) > 0) 
  12.         cpustat->nice = cputime64_add(cpustat->nice, tmp); 
  13.     else 
  14.         cpustat->user = cputime64_add(cpustat->user, tmp); 
  15.     ... 

account_user_time 函數(shù)主要分兩種情況統(tǒng)計:

  • 如果進程的 nice 值大于0,那么將會增加到 CPU 統(tǒng)計結(jié)構(gòu)的 nice 字段中。
  • 如果進程的 nice 值小于等于0,那么增加到 CPU 統(tǒng)計結(jié)構(gòu)的 user 字段中。

這里說明一下進程 nice 值的作用,nice 值越大,說明進程的優(yōu)先級越低。所以,nice 統(tǒng)計值主要用來統(tǒng)計低優(yōu)先級進程的占使用 CPU 的情況。也說明了,user 和 nice 統(tǒng)計值都屬于執(zhí)行用戶程序的 CPU 時間。

2. 統(tǒng)計內(nèi)核代碼執(zhí)行時間

如果在發(fā)生時鐘中斷前,CPU 處于內(nèi)核態(tài),也就是說在執(zhí)行內(nèi)核代碼。那么將會調(diào)用 account_system_time 函數(shù)進行統(tǒng)計,account_system_time 函數(shù)實現(xiàn)如下:

  1. void account_system_time(struct task_struct *p, int hardirq_offset, 
  2.                          cputime_t cputime, cputime_t cputime_scaled) 
  3.     // 獲取 CPU 的統(tǒng)計結(jié)構(gòu)(每個CPU一個 cpu_usage_stat 結(jié)構(gòu)) 
  4.     struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; 
  5.     cputime64_t tmp; 
  6.     ... 
  7.  
  8.     // 主要分 3 種情況進行統(tǒng)計 
  9.     // 1. 如果當前處于硬中斷執(zhí)行上下文, 那么統(tǒng)計到 irq 字段中 
  10.     // 2. 如果當前處于軟中斷執(zhí)行上下文, 那么統(tǒng)計到 softirq 字段中 
  11.     // 3. 否則統(tǒng)計到 system 字段中 
  12.     if (hardirq_count() - hardirq_offset) 
  13.         cpustat->irq = cputime64_add(cpustat->irq, tmp); 
  14.     else if (softirq_count()) 
  15.         cpustat->softirq = cputime64_add(cpustat->softirq, tmp); 
  16.     else 
  17.         cpustat->system = cputime64_add(cpustat->system, tmp); 
  18.     ... 

account_system_time 函數(shù)主要分 3 種情況進行統(tǒng)計:

如果當前處于硬中斷執(zhí)行上下文,那么增加到 CPU 統(tǒng)計結(jié)構(gòu)的 irq 字段中。

如果當前處于軟中斷執(zhí)行上下文,那么增加到 CPU 統(tǒng)計結(jié)構(gòu)的 softirq 字段中。

否則增加到 CPU 統(tǒng)計結(jié)構(gòu)的 system 字段中。

從上面代碼可以看出,irq 和 softirq 統(tǒng)計值也算是內(nèi)核代碼執(zhí)行時間。

3. idle 進程執(zhí)行時間統(tǒng)計

當系統(tǒng)中沒有可運行的進程時,將會執(zhí)行 idle 進程。也就是說,當系統(tǒng)執(zhí)行 idle 進程時,表示系統(tǒng)正處于空閑狀態(tài)。

idle 進程執(zhí)行時間統(tǒng)計由 account_idle_time 函數(shù)完成,其實現(xiàn)如下:

  1. void account_idle_time(cputime_t cputime) 
  2.     struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; 
  3.     cputime64_t cputime64 = cputime_to_cputime64(cputime); 
  4.     struct rq *rq = this_rq(); 
  5.  
  6.     // 分 2 種情況統(tǒng)計 CPU 的使用情況 
  7.     // 1. 如果系統(tǒng)有進程正在等待 I/O 操作完成, 那么將統(tǒng)計到 iowait 字段中 
  8.     // 2. 否則將統(tǒng)計到 idle 字段中 
  9.     if (atomic_read(&rq->nr_iowait) > 0) 
  10.         cpustat->iowait = cputime64_add(cpustat->iowait, cputime64); 
  11.     else 
  12.         cpustat->idle = cputime64_add(cpustat->idle, cputime64); 

account_idle_time 函數(shù)也分兩種情況進行統(tǒng)計:

  • 如果系統(tǒng)中有正在等待 I/O 操作完成的進程,那么增加到 CPU 統(tǒng)計結(jié)構(gòu)的 iowait 字段中。
  • 否則增加到 CPU 統(tǒng)計結(jié)構(gòu)的 idle 字段中。

從上面的分析可以看出,iowait 統(tǒng)計值也屬于空閑時間的一種。

top 命令的 CPU 使用率

通過源碼分析,我們知道 top 命令中 CPU 使用率各種類型的意思,現(xiàn)在我們來介紹一下 top 命令是怎么計算各種類型的 CPU 使用率。

要獲取各個 CPU 的使用情況信息,可以通過讀取 /proc/stat 文件獲取,如下:

  1. [vagrant@localhost ~]$ cat /proc/stat 
  2. cpu  245 10 1142 1097923 95 0 28 0 0 0 
  3. cpu0 245 10 1142 1097923 95 0 28 0 0 0 
  4. ... 

上面的結(jié)果顯示了 CPU 的使用情況信息,第一行代表所有 CPU 的總和,而第二行開始表示每個 CPU 核心的使用情況信息。因為我的電腦只有一個核,所以只有一條數(shù)據(jù)。

下面說說這些數(shù)據(jù)的意義,從第一個數(shù)值開始分別代表:user ,nice,system,idle,iowait, irq,softirq,steal。

所以,top 命令的 CPU 使用率計算公式如下:

  1. CPU總時間 = user + nice + system + idle + wait + irq + softirq + steal 
  2. %us = user / CPU總時間 
  3. %ni = nice / CPU總時間 
  4. %sy = system / CPU總時間 
  5. %id = idel / CPU總時間 
  6. %wa = wait / CPU總時間 
  7. %hi = irq / CPU總時間 
  8. %si = softirq / CPU總時間 
  9. %st = steal / CPU總時間 

嗯,看起來還是挺簡單的。

總結(jié)

本文主要分析了 top 命令中的 CPU 使用率的意義和實現(xiàn)原理,希望通過本文,能夠幫助大家對 top 命令有更深的認識。

 

責任編輯:武曉燕 來源: Linux內(nèi)核那些事
相關(guān)推薦

2021-08-10 11:45:57

topCPULinux

2024-04-11 13:27:19

Linuxtop命令

2019-01-15 15:04:54

CPU電腦使用率

2009-11-16 17:03:20

Oracle優(yōu)化CPU

2021-11-11 16:46:02

CPU使用率 .NET

2013-04-12 09:39:01

Top監(jiān)控系統(tǒng)

2020-07-08 07:00:00

LinuxCPU應用程序

2022-07-23 21:31:24

KubernetesLinux開源

2014-12-01 13:44:03

cgroupscpulimitlinux

2010-04-27 10:32:54

Oracle優(yōu)化CPU

2022-04-11 07:50:58

top 命令Java 系統(tǒng)JVM 進程

2019-09-24 14:52:35

CPU負載Linux

2010-04-12 11:21:35

Oracle優(yōu)化CPU

2023-03-06 08:41:32

CPU使用率排查

2010-01-18 10:13:08

VB.NET獲取CPU

2009-12-15 15:12:05

Linux限制進程cp

2017-05-03 11:10:14

Linux進程監(jiān)控cpustat

2017-03-23 19:27:55

LinuxCPUCPU利用率

2013-09-18 11:00:14

JavaCPU

2012-07-02 16:38:22

Linux服務器閏秒
點贊
收藏

51CTO技術(shù)棧公眾號