阿里云服务器网站:文本来源:1、查看Linux环境中的CPU信息1. 1、查看CPU详细信息
通过cat / proc / cpuinfo命令,您可以查看与CPU相关的信息:
[root@rh~]$cat/proc/cpuinfo processor:0 vendor_id:GenuineIntel cpufamily:6 model:44 modelname:Intel(R)Xeon(R)CPUE5620@2.40GHz stepping:2 cpuMHz:1596.000 cachesize:12288KB physicalid:0 siblings:8 coreid:0 cpucores:4 apicid:0 initialapicid:0 fpu:yes fpu_exception:yes cpuidlevel:11 wp:yes flags:fpuvmedepsetscmsrpaemcecx8apicsepmtrrpgemcacmovpatpse36clflushdtsacpimmxfxsrssesse2sshttmpbesyscallnxpdpe1gbrdtscplmconstant_tscarch_perfmonpebsbtsrep_goodxtopologynonstop_tscaperfmperfpnipclmulqdqdtes64monitords_cplvmxsmxesttm2ssse3cx16xtprpdcmpciddcasse4_1sse4_2popcntaeslahf_lmaratepbdtstpr_shadowvnmiflexpriorityeptvpid bogomips:4800.15 clflushsize:64 cache_alignment:64 addresssizes:40bitsphysical,48bitsvirtual powermanagement: ......
在查看的相关信息中,某些信息通常令人困惑。这里有一些解释:
我们通常可以使用以下命令来获取有关这些参数的信息:
[root@rh ~]$ cat /proc/cpuinfo | grep "physical id"| sort|uniq physical id :0 physical id :1 [root@rh ~]$ cat /proc/cpuinfo | grep "cpu cores"| sort|uniq cpu cores :4 [root@rh ~]# cat /proc/cpuinfo | grep "core id" | sort|uniq core id :0 core id :1 core id :10 core id :9 [root@rh ~]$ cat /proc/cpuinfo | grep "siblings"| sort|uniq siblings :8 [root@rh ~]$ cat /proc/cpuinfo | grep "processor"| sort|uniq processor :0 processor :1 processor :10 processor :11 processor :12 processor :13 processor :14 processor :15 processor :2 processor :3 processor :4 processor :5 processor :6 processor :7 processor :8 processor :9
通过以上结果,您可以看到这台机器:
超线程技术:超线程技术使用特殊的硬件指令将两个逻辑内核仿真为两个物理芯片,以便单个处理器可以使用线程级并行计算,并且与多线程操作系统和软件兼容。减少了CPU的空闲时间,提高了CPU的工作效率。
1. 2、查看多核CPU信息
您可以使用mpstat命令或sar命令进行查看。可以通过man mpstat / sar检查具体用法。
2、计算Linux环境中进程的CPU占用率2. 1、通过/ proc / stat文件查看所有CPU活动信息
以下示例数据来自内核2. 6. 24-24及更高版本:
[root@rh ~]$ cat /proc/stat cpu 22344724045041824108021655975341258620900 cpu0 17625111934142575516534590721678000 cpu1 124129139234258609121139271569700 cpu2 14953055861825310182230738785100 cpu3 40880138057258734793404191586200 cpu4 13417171611232582975610429221715500 cpu5 23935533183725605479369202609500 cpu6 193200319132256197282060183069000 cpu7 184300324300256161424170203308100 cpu8 319400661718252319111474156766100 cpu9 80798715203525829339237191630800 cpu10 42472414005225869331168181505900 cpu11 16305440147325567904207143467400 cpu12 8227015781525853353319101480800 cpu13 1889446479709253542796532615788500 cpu14 70003020476025769743159162105900 cpu15 4567114089925855452138161553600 intr 114673438499700110001000001302607002620870712704000<...省略若干数据...> ctxt 89793364 btime 1366591448 processes 27283 procs_running 1 procs_blocked 0 softirq 1262462448063122856507893291074176388225020046121395355817613064075931
第一行中的数据代表CPU的总使用量。让我们解释一下此数据行中每个值的含义:
从以上信息中,我们可以获得CPU活动的总时间:
totalCPUTime =用户+好的+系统+空闲+ iowait + irq + softirq +被盗+来宾
2. 2、通过/ proc / [PID] / stat文件查看进程的CPU活动信息
2. 2. 1、文件目录存储过程信息
Linux系统实现了``一切都是文件''的思想,通过读取文件也可以获取所有进程的运行状态。 / proc文件系统是伪文件系统,它仅存在于内存中,并且不占用外部内存空间。它以文件系统的形式在内核和进程之间提供通信接口。用户和应用程序可以通过/ proc获取系统信息,并且可以更改内核的某些参数。
/ proc / [PID] /目录中的文件记录了此过程的各种操作指标。是进程号。
2. 2. 2、查看正在运行的进程的详细信息
通过查看/ proc / [PID] / stat文件,您可以获得有关正在运行的进程的详细信息,包括CPU使用率信息。例如:
[root@rh ~]$ cat /proc/1/stat 1(init) S 0110-1420275230262635222948351651023463188016200101198205443841844674407370955161511000004096536962595184467440737095516150004003400
/ proc / [PID] / stat文件信息说明

看到以上信息,我一定会很困惑。我不知道每个字段的含义。
1)我们可以使用man 5 proc命令来查看文档,找到/ proc / [pid] / stat节点,然后我们可以看到每个字段的含义。如:
/proc/[pid]/stat Status information about the process.Thisis used by ps(1).Itisdefinedin/usr/src/linux/fs/proc/array.c. The fields,in order,with their proper scanf(3) format specifiers, are: pid %d The process ID. comm %s The filename of the executable,in parentheses.Thisis visible whether ornot the executable is swapped out. state %c One character from the string"RSDZTW"where R is running, S is sleeping in an interruptible wait, D is waiting in uninterruptible disk sleep, Z is zombie, T is traced or stopped (on a signal),and W is paging. ppid %d The PID of the parent. ......
2)具体说明,例如:
2. 2. 3、有关进程的CPU使用率的信息
在上述时间内,此信息将在计算CPU使用率时使用:
进程的CPU使用时间(此值包括其所有线程的CPU时间):
processCPUTime = utime + stime +剪辑+ cstime
2. 3、通过/ proc / [PID] / task / [TID] / stat文件查看进程中线程的活动信息
此文件包含轻量级进程(lwp,通常称为线程)的所有活动信息。该文件中的所有值都是从系统启动到当前时刻的累积值。文件的内容格式和每个字段的含义类似于/ proc / [PID] / stat文件。此文件中的tid字段代表轻量级线程号。
此线程的CPU使用时间:
threadCPUTime = utime + stime
2. 4、单核情况下的CPU使用率计算
2. 4. 1、基本概念
首先,通过读取/ proc / stat文件获取总的CPU时间,读取/ proc / [PID] / stat获取进程的CPU时间,然后读取/ proc / [PID] / task / [TID] / stat获取线程的CPU时间。然后,对两个足够短的CPU快照和进程或线程快照的时间间隔进行采样,以计算其CPU使用率。
2. 4. 2、计算总CPU使用率totalCPUUse
1)以足够短的时间间隔对两个CPU快照进行采样,即读取/ proc / stat文件以在两个时间点获取以下数据:
2)计算总CPU时间totalCPUTime:
totalCPUTime = CPUTime2 – CPUTime1;
3)计算CPU空闲时间idleCPUTime:
idleCPUTime = idle2 – idle1;
4)计算CPU总使用量totalCPUUse:
totalCPUUse =(totalCPUTime – idleCPUTime)/ totalCPUTime;
2. 4. 3、计算进程processCPUUse的CPU使用率
1)对CPU快照和相应的进程快照的两个足够短的时间间隔进行采样,即读取/ proc / stat文件以在两个时间点获取以下数据:
读取/ proc / [PID] / stat文件以在两个时间点获取以下数据:
2)计算总CPU时间totalCPUTime和处理时间processTime:
totalCPUTime = CPUTime2 – CPUTime1;
processTime = processTime2 – processTime1;
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shoujiruanjian/article-375750-1.html
期待期待
靠实力增长慢慢扭转局势并最终胜利