Linux 环境下查看 CPU 信息
通过 cat /proc/cpuinfo 命令,可以查看 CPU 相关的信息
Processor : AArch64 Processor rev 14 (aarch64)
processor : 0
BogoMIPS : 38.40
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp
CPU implementer : 0x51
CPU architecture: 8
CPU variant : 0xd
CPU part : 0x805
CPU revision : 14
processor : 1
BogoMIPS : 38.40
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp
CPU implementer : 0x51
CPU architecture: 8
CPU variant : 0xd
CPU part : 0x805
CPU revision : 14
.....
- processor:逻辑处理器的 id,一般情况下,逻辑 cpu = 物理CPU个数 × 每颗核数,如果不相等的话,则表示 Linux 的 CPU 支持超线程技术。
- BogoMIPS:BogoMIPS 是 Linus 本人的独创,如果系统启动时,计算出 BogoMIPS 为 100,可记为 100万条伪指令每秒。
在 Linux 环境下计算进程的 CPU 占用
通过 cat /proc/stat 文件查看所有的 CPU 活动信息
127|raphael:/ $ cat /proc/stat
cpu 12123934 1617498 9092384 23034727 91741 1800835 569171 0 0 0
cpu0 2160370 295650 1854378 21252616 86920 675808 227246 0 0 0
cpu1 2200035 300779 1701774 222213 1004 324118 91918 0 0 0
cpu2 2109617 263837 1647247 221856 957 307148 90692 0 0 0
cpu3 1437121 91239 1429458 246820 615 271759 104134 0 0 0
cpu4 1257432 151080 667213 272231 613 58109 14079 0 0 0
cpu5 1237783 156192 710850 271753 600 59504 14486 0 0 0
cpu6 1217469 163612 684075 271835 631 59697 14791 0 0 0
cpu7 504104 195104 397385 275399 398 44688 11820 0 0 0
intr 1297456035 0 0 0 0 187404514 0 28792308 0 16345624 0 0 0 0 16325 199 0 51456 0 2 0 355303 0 2 0 2 62521 16 0 139 204207 0 0 4757381 3390833 0 240620 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 799 0 0 0 0 722 0 386 0 0 404501 0 0 0 0 0 0 0
0 0 0 0 15014616 49027 284 49508957 0 0 25 47 0 0 0 0 7 2 0 138924 0 2004 0 0 0 0 0 0 0 0 0 0 0 0 0 0 104347 0 0 0 26 0 0 0 0 16 0 0 0 0 0 0 0 0 0 0 358091 96339 1769 1364 0 5 0 0 0 0 0 0 0 0 0 0 0 0 11678 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 52678 2723945 42 2478477 5121142 267948 0 0 0 0 0 1149871 844753 11603324 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 157861 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 107849 0 1684 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 672822 0 14423 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 3 80866 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 104139 727776 675552 0 832894 0 3914284 0 0 0 4541419 0 34 0 0 10562515 0 0 0 0 0 0 0 0 0 0 0 6 799 1 0 1 0 0 1 0 1 0 1 0 1 0 1 0 1 0 0 38814 5259305 0 2687 0 0 0 1715 2
0 0 6 13 0 0 6 0 0 54 4 0 0 0 0 601511 0 0 0 332 0 0 0 0 0 0 0 0 26 2871 0 107 22 0 0 0 0 0 0 50 113 121 0 89 0 174 2086 0 0 0 0 0 0 0 0 0 96 0 0 89 0 0 0 7258 0 10058 0 0 0 0 0 0 0 0 0 0 0 0 242176 0 7505 9289359 13087458 980 0 0 0
0 0 0 0 36 27 10 10 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13360 0 0 4329353 0 0
ctxt 1832848701
btime 1625156783
processes 1097383
procs_running 4
procs_blocked 0
softirq 226658851 49747268 49555152 1799689 9900586 12973300 0 18627150 44256743 0 39798963
第一行的数据表示的是 CPU 总的使用情况,
- 这些数值的单位都是 jiffies,jiffies 是内核中的一个全局变量,用来记录系统启动以来产生的节拍数,在 Linux 中,一个节拍大致可以理解为操作系统进程调度的最小时间片,不同的 Linux 系统内核这个值可能不同,通常在 1ms 到 10ms 之间。
- cpu 12123934 1617498 9092384 23034727 91741 1800835 569171 0 0 0
- user(12123934) 从系统启动开始累积到当前时刻,处于用户态的运行时间,不包含 nice 值为负的进程。
- nice(1617498) 从系统启动开始累积到当前时刻,nice 值为负的进程所占用的 CPU 时间。
- system(9092384) 从系统启动开始累积到当前时刻,处于核心态的运行时间。
- idle(23034727) 从系统启动开始累积到当前时刻,除 IO 等待时间以外的其他等待时间。
- iowait(91741) 从系统启动开始累积到当前时刻,IO 等待时间。
- irq(1800835) 从系统启动开始累积到当前时刻,硬中断时间。
- softirq(569171) 从系统启动开始累积到当前时刻,软中断时间。
- totalCPUTime = user + nice + system + idle + iowait + irq + softirq
通过 /proc/[PID]/stat 文件查看某一进程的 CPU 活动信息
Linux 系统贯彻“一切都是文件”的思想,所有的进程的运行状态也都可以通过读取文件来获取。 /proc 文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间。它以文件系统的方式为内核与进程提供通信的接口。用户和应用程序可以通过 /proc 得到系统的信息,并可以改变内核的某些参数。
通过查看 cat /proc/[PID]/stat 文件,可以进程运行的详细信息,其中就包括 CPU 占用信息。
raphael:/ $ cat /proc/1/stat
1 (init) S 0 0 0 0 -1 1077952768 60985 5612487 540 5604 7335 3690 27677 66525 20 0 1 0 0 61513728 717 18446744073709551615 1 1 0 0 0 0 65536 0 1073775864 0 0 0 17 2 0 0 10141 0 0 0 0 0 0 0 0 0 0
Linux 环境查看进程运行相关信息
ps
raphael:/ $ ps --help
usage: ps [-AadefLlnwZ] [-gG GROUP,] [-k FIELD,] [-o FIELD,] [-p PID,] [-t TTY,] [-uU USER,]
List processes.
Which processes to show (-gGuUpPt selections may be comma separated lists):
-A All -a Has terminal not session leader
-d All but session leaders -e Synonym for -A
-g In GROUPs -G In real GROUPs (before sgid)
-p PIDs (--pid) -P Parent PIDs (--ppid)
-s In session IDs -t Attached to selected TTYs
-T Show threads also -u Owned by selected USERs
-U Real USERs (before suid)
Output modifiers:
-k Sort FIELDs (-FIELD to reverse) -M Measure/pad future field widths
-n Show numeric USER and GROUP -w Wide output (don't truncate fields)
Which FIELDs to show. (-o HELP for list, default = -o PID,TTY,TIME,CMD)
-f Full listing (-o USER:12=UID,PID,PPID,C,STIME,TTY,TIME,ARGS=CMD)
-l Long listing (-o F,S,UID,PID,PPID,C,PRI,NI,ADDR,SZ,WCHAN,TTY,TIME,CMD)
-o Output FIELDs instead of defaults, each with optional :size and =title
-O Add FIELDS to defaults
-Z Include LABEL
top
raphael:/ $ top --help
usage: top [-Hbq] [-k FIELD,] [-o FIELD,] [-s SORT] [-n NUMBER] [-m LINES] [-d SECONDS] [-p PID,] [-u USER,]
Show process activity in real time.
-H Show threads
-k Fallback sort FIELDS (default -S,-%CPU,-ETIME,-PID)
-o Show FIELDS (def PID,USER,PR,NI,VIRT,RES,SHR,S,%CPU,%MEM,TIME+,CMDLINE)
-O Add FIELDS (replacing PR,NI,VIRT,RES,SHR,S from default)
-s Sort by field number (1-X, default 9)
-b Batch mode (no tty)
-d Delay SECONDS between each cycle (default 3)
-m Maximum number of tasks to show
-n Exit after NUMBER iterations
-p Show these PIDs
-u Show these USERs
-q Quiet (no header lines)
Cursor LEFT/RIGHT to change sort, UP/DOWN move list, space to force
update, R to reverse sort, Q to exit.