CPU:Cores, and Hyper-Threading
超線程(Hyper-Threading )
超線程是Intel最早提出一項(xiàng)技術(shù),最早出現(xiàn)在2002年的Pentium4上。單個(gè)采用超線程的CPU對于操作系統(tǒng)來說就像有兩個(gè)邏輯CPU,為此P4處理器需要多加入一個(gè)Logical CPU Pointer(邏輯處理單元)。
雖然采用超線程技術(shù)能同時(shí)執(zhí)行兩個(gè)線程,但它并不像兩個(gè)真正的CPU那樣,每個(gè)CPU都具有獨(dú)立的資源。當(dāng)兩個(gè)線程都同時(shí)需要某一個(gè)資源時(shí),其中一個(gè)要暫時(shí)停止,并讓出資源,直到這些資源閑置后才能繼續(xù)。因此超線程的性能并不等于兩顆CPU的性能。
多核(multi-cores)
最開始CPU只有一個(gè)核(core),為了提高性能,引入了雙核CPU,四核CPU等,雙核CPU能同時(shí)執(zhí)行兩個(gè)線程。和超線程不同的是,雙核CPU是實(shí)打?qū)嵉挠袃蓚€(gè)central processing units在一個(gè)CPU chip。
上圖顯示主板上有1個(gè)插槽(socket),這個(gè)插槽插著一個(gè)CPU,這個(gè)CPU有4個(gè)核(core),每個(gè)核都使用超線程技術(shù),所以這臺(tái)機(jī)器總共有8個(gè)邏輯核。
CPU使用率計(jì)算
CPU使用率測試
一臺(tái)擁有8個(gè)logic core CPU的機(jī)器,執(zhí)行如下程序:
#include <pthread.h> const int num = 9; pthread_t threads[num]; void *func(void* arg) { while(1) {} return ((void *)0); } int main(int argc, char* argv[]) { for (int i = 0; i < num; i++) { pthread_create(&threads[i], NULL, func, NULL); } for (int i = 0; i < num; i++) { pthread_join(threads[i], NULL); } return 0; }
該程序開啟9個(gè)線程每個(gè)線程都執(zhí)行一個(gè)死循環(huán)。執(zhí)行后用top查看cpu使用情況:
332 root 20 0 84312 612 416 S 800.0 0.0 7:18.41 cputest
可以看到cputest的CPU使用情況為800%,也就是8個(gè)logic core都在執(zhí)行cputest這個(gè)進(jìn)程。
而在一個(gè)只有1個(gè)logic的CPU上跑的結(jié)果如下:
13812 ubuntu 20 0 80284 708 628 S 97.7 0.1 0:10.14 cputest
可以看到,縱使開啟了9個(gè)線程,每個(gè)線程都執(zhí)行死循環(huán),CPU使用率只有97.7%。
如何計(jì)算CPU使用率
1. %CPU -- CPU Usage The task's share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time. In a true SMP environment, if a process is multi-threaded and top is not operating in Threads mode, amounts greater than 100% may be reported. You toggle Threads mode with the `H' interactive command. Also for multi-processor environments, if Irix mode is Off, top will operate in Solaris mode where a task's cpu usage will be divided by the total number of CPUs. You toggle Irix/Solaris modes with the `I' interactive command.
以上截取自man top中對于CPU使用率的定義,總結(jié)來說某個(gè)進(jìn)程的CPU使用率就是這個(gè)進(jìn)程在一段時(shí)間內(nèi)占用的CPU時(shí)間占總的CPU時(shí)間的百分比。
比如某個(gè)開啟多線程的進(jìn)程1s內(nèi)占用了CPU0 0.6s, CPU1 0.9s, 那么它的占用率是150%。這樣就不難理解上例中cputest進(jìn)程CPU占用率為800%這個(gè)結(jié)果了。
實(shí)現(xiàn)CPU使用率統(tǒng)計(jì)程序
某進(jìn)程cpu使用率 = 該進(jìn)程cpu時(shí)間 / 總cpu時(shí)間。
/proc/pid/stat中可以得出進(jìn)程自啟動(dòng)以來占用的cpu時(shí)間。以bash進(jìn)程為例:
79 (bash) S 46 79 79 34816 0 0 0 0 0 0 46 135 387954 4807 20 0 1 0 6114 232049254400 873 18446744073709551615 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
第14項(xiàng)utime和第15項(xiàng)stime分別表示bash自啟動(dòng)起來,執(zhí)行用戶代碼態(tài)占用的時(shí)間和執(zhí)行內(nèi)核態(tài)代碼占用的時(shí)間,單位是clock tick,clock tick是時(shí)間單位。這兩項(xiàng)的詳細(xì)解釋如下(摘自man proc):
(14) utime %lu Amount of time that this process has been scheduled in user mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK)). This includes guest time, guest_time (time spent running a virtual CPU, see below), so that Applications that are not aware of the guest time field do not lose that time from their calculations. (15) stime %lu Amount of time that this process has been scheduled in kernel mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK)).
每個(gè)clock tick占用多少時(shí)間呢?
可以通過sysconf(_SC_CLK_TCK)獲取1秒內(nèi)有多少個(gè)clock tick(通常是100)。也就是說1 clock tick為1 / 100秒。
有了上面的基礎(chǔ),
我們可以每隔period秒讀取/proc/pid/stat,解析其中的utime和stime,將其和(utime+stime)減去上一次采樣時(shí)這兩項(xiàng)的和(lastutime + laststime),這就是period秒內(nèi)該進(jìn)程占用CPU的時(shí)間,單位為clock tick。
總的CPU時(shí)間為period * sysconf(_SC_CLK_TCK),單位也為clock tick。
所以公式如下:
某進(jìn)程cpu使用率 = ((utime+stime) - (lastutime + laststime)) / period * sysconf(_SC_CLK_TCK)
以下是實(shí)現(xiàn):
#include <unistd.h> #include <stdio.h> #include <sys/time.h> #include <string.h> #include <signal.h> #include <stdlib.h> #include <fstream> #include <IOStream> #include <sstream> using namespace std; struct StatData { void parse(const string& content) { size_t rp = content.rfind(')'); std::istringstream iss(content.data() + rp + 1); // 0 1 2 3 4 5 6 7 8 9 11 13 15 // 3770 (cat) R 3718 3770 3718 34818 3770 4202496 214 0 0 0 0 0 0 0 20 // 16 18 19 20 21 22 23 24 25 // 0 1 0 298215 5750784 81 18446744073709551615 4194304 4242836 140736345340592 // 26 // 140736066274232 140575670169216 0 0 0 0 0 0 0 17 0 0 0 0 0 0 iss >> state; iss >> ppid >> pgrp >> session >> tty_nr >> tpgid >> flags; iss >> minflt >> cminflt >> majflt >> cmajflt; iss >> utime >> stime >> cutime >> cstime; iss >> priority >> nice >> num_threads >> itrealvalue >> starttime; } string name; char state; int ppid; int pgrp; int session; int tty_nr; int tpgid; int flags; long minflt; long cminflt; long majflt; long cmajflt; long utime; long stime; long cutime; long cstime; long priority; long nice; long num_threads; long itrealvalue; long starttime; }; int clockTicks = static_cast<int>(::sysconf(_SC_CLK_TCK)); const int period = 2; int pid; int ticks; StatData lastStatData; bool processExists(pid_t pid) { char filename[256]; snprintf(filename, sizeof filename, "/proc/%d/stat", pid); return ::access(filename, R_OK) == 0; } //read /proc/pid/stat string readProcFile(int pid) { char filename[256]; snprintf(filename, sizeof filename, "/proc/%d/stat", pid); ifstream in; in.open(filename); stringstream ss; ss << in.rdbuf(); string ret = ss.str(); return ret; } double cpuUsage(int userTicks, int sysTicks, double kPeriod, double kClockTicksPerSecond) { return (userTicks + sysTicks) / (kClockTicksPerSecond * kPeriod); //CPU使用率計(jì)算 } void tick(int num) { string content = readProcFile(pid); StatData statData; memset(&statData, 0, sizeof statData); statData.parse(content); if (ticks > 0) { int userTicks = std::max(0, static_cast<int>(statData.utime - lastStatData.utime)); int sysTicks = std::max(0, static_cast<int>(statData.stime - lastStatData.stime)); printf("pid %d cpu usage:%.1f%%n", pid, cpuUsage(userTicks, sysTicks, period, clockTicks) * 100); } ticks++; lastStatData = statData; } int main(int argc, char* argv[]) { if (argc < 2) { printf("Usage: %s pidn", argv[0]); return 0; } pid = atoi(argv[1]); if (!processExists(pid)) { printf("Process %d doesn't exist.n", pid); return 1; } if (signal(SIGALRM, tick) == SIG_ERR) { exit(0); } struct itimerval tick; memset(&tick, 0, sizeof tick); tick.it_value.tv_sec = period; tick.it_value.tv_usec = 0; tick.it_interval.tv_sec = period; tick.it_interval.tv_usec = 0; setitimer(ITIMER_REAL, &tick, NULL); while (1) { pause(); } return 0; }
代碼很簡單,每隔兩秒采一次樣,計(jì)算這兩秒內(nèi)指定進(jìn)程的CPU使用率。
為了測試,先將前文的cputest運(yùn)行起來,該程序會(huì)占滿8個(gè)logic core。
./cputest &,然后top看下CPU使用率,大約占用了800%的CPU。
867 root 20 0 84312 616 416 S 800.0 0.0 17:44.60 cputest
接著用我們的自己的寫的程序看下,pid是867,
./cpumon 867
pid 867 cpu usage:786.0% pid 867 cpu usage:785.5% pid 867 cpu usage:787.5% pid 867 cpu usage:759.5% pid 867 cpu usage:781.5% pid 867 cpu usage:791.5% pid 867 cpu usage:743.5% pid 867 cpu usage:782.0% pid 867 cpu usage:777.5% pid 867 cpu usage:785.0% pid 867 cpu usage:790.5% pid 867 cpu usage:786.0% ^C
可以看到每隔兩秒都會(huì)計(jì)算一次,使用率略低于800%,也可以理解,因?yàn)楝F(xiàn)在cpumon也會(huì)占用一定的CPU時(shí)間。
參考資料:https://www.howtogeek.com/194756/cpu-basics-multiple-cpus-cores-and-hyper-threading-explained/
原文:https://www.cnblogs.com/gatsby123/p/11127158.html