Linux中怎么精确获取linux查询cpu主频频和cpu周期

推荐这篇日记的豆列
······博客访问: 51198
博文数量: 18
注册时间:
ITPUB论坛APP
ITPUB论坛APP
APP发帖 享双倍积分
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: Linux
linux下查看cpu和内核的个数
linux下查看cpu的个数及内核信息
[root@qhtlnx28 ~]# cat /proc/cpuinfoprocessor : 0vendor_id : GenuineIntelcpu family : 6model : 15model name : Intel(R) Xeon(R) CPU E5310 @ 1.60GHzstepping : 11cpu MHz : 1595.974cache size : 4096 KBphysical id : 0siblings : 4core id : 0cpu cores : 4fdiv_bug : nohlt_bug : nof00f_bug : nocoma_bug : nofpu : yesfpu_exception : yescpuid level : 10wp : yesflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm pni monitor ds_cpl tm2 xtprbogomips : 3194.02processor : 1vendor_id : GenuineIntelcpu family : 6model : 15model name : Intel(R) Xeon(R) CPU E5310 @ 1.60GHzstepping : 11cpu MHz : 1595.974cache size : 4096 KBphysical id : 0siblings : 4core id : 1cpu cores : 4fdiv_bug : nohlt_bug : nof00f_bug : nocoma_bug : nofpu : yesfpu_exception : yescpuid level : 10wp : yesflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm pni monitor ds_cpl tm2 xtprbogomips : 3191.88processor : 2vendor_id : GenuineIntelcpu family : 6model : 15model name : Intel(R) Xeon(R) CPU E5310 @ 1.60GHzstepping : 11cpu MHz : 1595.974cache size : 4096 KBphysical id : 0siblings : 4core id : 2cpu cores : 4fdiv_bug : nohlt_bug : nof00f_bug : nocoma_bug : nofpu : yesfpu_exception : yescpuid level : 10wp : yesflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm pni monitor ds_cpl tm2 xtprbogomips : 3191.96processor : 3vendor_id : GenuineIntelcpu family : 6model : 15model name : Intel(R) Xeon(R) CPU E5310 @ 1.60GHzstepping : 11cpu MHz : 1595.974cache size : 4096 KBphysical id : 0siblings : 4core id : 3cpu cores : 4fdiv_bug : nohlt_bug : nof00f_bug : nocoma_bug : nofpu : yesfpu_exception : yescpuid level : 10wp : yesflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm pni monitor ds_cpl tm2 xtprbogomips : 3191.93processor : 4vendor_id : GenuineIntelcpu family : 6model : 15model name : Intel(R) Xeon(R) CPU E5310 @ 1.60GHzstepping : 11cpu MHz : 1595.974cache size : 4096 KBphysical id : 1siblings : 4core id : 4cpu cores : 4fdiv_bug : nohlt_bug : nof00f_bug : nocoma_bug : nofpu : yesfpu_exception : yescpuid level : 10wp : yesflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm pni monitor ds_cpl tm2 xtprbogomips : 3191.99processor : 5vendor_id : GenuineIntelcpu family : 6model : 15model name : Intel(R) Xeon(R) CPU E5310 @ 1.60GHzstepping : 11cpu MHz : 1595.974cache size : 4096 KBphysical id : 1siblings : 4core id : 5cpu cores : 4fdiv_bug : nohlt_bug : nof00f_bug : nocoma_bug : nofpu : yesfpu_exception : yescpuid level : 10wp : yesflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm pni monitor ds_cpl tm2 xtprbogomips : 3191.90processor : 6vendor_id : GenuineIntelcpu family : 6model : 15model name : Intel(R) Xeon(R) CPU E5310 @ 1.60GHzstepping : 11cpu MHz : 1595.974cache size : 4096 KBphysical id : 1siblings : 4core id : 6cpu cores : 4fdiv_bug : nohlt_bug : nof00f_bug : nocoma_bug : nofpu : yesfpu_exception : yescpuid level : 10wp : yesflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm pni monitor ds_cpl tm2 xtprbogomips : 3191.96processor : 7vendor_id : GenuineIntelcpu family : 6model : 15model name : Intel(R) Xeon(R) CPU E5310 @ 1.60GHzstepping : 11cpu MHz : 1595.974cache size : 4096 KBphysical id : 1siblings : 4core id : 7cpu cores : 4fdiv_bug : nohlt_bug : nof00f_bug : nocoma_bug : nofpu : yesfpu_exception : yescpuid level : 10wp : yesflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm pni monitor ds_cpl tm2 xtprbogomips : 3192.00以上就是我的cpu的信息哈哈 ,这里说明一下, 这是IBM 3550的cpu信息。通过上面信息我们可以知道,有8个核。从processor& 0&& 到processor&& 7可以看出来。有两个cpu,从
physical id : 0和
physical id : 1可以看出有两个cpu。从
cpu cores : 4可以看出每个cpu有四个内核
:/proc/cpuinfo
  linux 下
  /proc/cpuinfo
  文件会显示cpu的信息
  processor 会从0开始记数 继续下去多个cpu
  flags 如果有 ht 说明支持超线程技术
  判断物理CPU的个数可以查看physical :/proc/cpuinfo
  linux 下
  /proc/cpuinfo
  文件会显示cpu的信息
  processor 会从0开始记数 继续下去多个cpu
  flags 如果有 ht 说明支持超线程技术
  判断物理CPU的个数可以查看physical id 的值,相同则为同一个物理 CPU
id 的值,相同则为同一个物理 CPU
阅读(19274) | 评论(0) | 转发(0) |
相关热门文章
给主人留下些什么吧!~~
请登录后评论。c语言获取CPU主频信息报告_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
c语言获取CPU主频信息报告
上传于||暂无简介
阅读已结束,如果下载本文需要使用2下载券
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩3页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢31947人阅读
linux足迹(25)
最近在研究linux系统负载的时候,接触到一些关于CPU信息查看的知识,和大家分享一下。通过对/proc/cpuinfo文件中的参数的分析,也学到了不少东西。
在linux操作系统中,CPU的信息在启动的过程中被装载到虚拟目录/proc下的cpuinfo文件中,我们可以通过 cat /proc/cpuinfo 查看一下:
上图是本人电脑的CPU信息,下面我们来分析其中几个比较重要的指标:
processor& & &逻辑处理器的id。physical id& & 物理封装的处理器的id。core id& & & & & &每个核心的id。cpu cores& & &位于相同物理封装的处理器中的内核数量。siblings& & & & &位于相同物理封装的处理器中的逻辑处理器的数量。
我们返回去看上图,我的physical id 都为0,说明我只有一个物理处理器;
我的processor有两个不同的编号,并且同属于一个physical id,同时cpu cores 的值为2,这也就说明了我的CPU是双核心的,并且每个核心只有一个逻辑处理器。
为了加深大家对这几个参数的理解,我们再来看另外一张图片,这是一台工作站的服务器:
在这个服务器上,'cpu cores' 为4,physical id 有两个,core id有8个,siblings的值为8,总共有16个processor。
所以这个服务器主机的CPU为2个物理封装的处理器,每个处理器又有4个处理核心(cpu cores),每个cpu core有可划分为2个逻辑处理器(超线程技术),因此,每个物理处理器上有8个逻辑处理器,总共就有16个processor。这回明白了吧。大体的结构如下图:
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:140253次
积分:1619
积分:1619
排名:第19326名
原创:33篇
转载:18篇
评论:13条
(4)(1)(2)(7)(3)(4)(2)(8)(2)(6)(6)(2)(3)(1)Load和CPU利用率是如何算出来的
本文内容遵从, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: /tech/system/how_to_calc_load_cpu.html
相信很多人都对Linux中top命令里“load average”这一栏困惑过,到底什么是Load,Load代表了什么含义,Load高会有什么后果?“%CPU”这一栏为什么会超过100%,它是如何计算的?
带着这些问题,我们通过一些测试,来探索下其中的不解之处。
首先,我们通过实验来大概确定其计算方式:
测试服务器:4核Xeon处理器
测试软件:MySQL 5.1.40
服务器上除了MySQL没有运行其他任何非系统自带软件。因为MySQL只能单线程运行单条SQL,所以可以很好的通过增加查询并发来控制使用的CPU核数。
空载时,top的信息为:
top &#:47 up 35 days,
load average: 0.00, 0.00, 0.00
1 running,
75 sleeping,
0 stopped,
0.0%ni, 99.5%id,
在数据库中启动一个大查询:
top &#:09 up 35 days,
load average: 0.99, 0.92, 0.67
1 running,
79 sleeping,
0 stopped,
0.0%ni, 96.3%id,
0.0%ni,100.0%id,
: 98.7%us,
0.0%ni,100.0%id,
同时可以看到%CPU也是在100%
SHR S %CPU %MEM
308m 137m 4644 S 99.9
15:13.28 mysqld
然后开启第二个大查询,不久就可以看到top信息的变化,Load到了2:
top &#:44 up 35 days,
load average: 1.99, 1.62, 1.08
1 running,
79 sleeping,
0 stopped,
0.0%ni, 97.7%id,
: 99.0%us,
0.0%ni,100.0%id,
: 99.0%us,
也可以观察到%CPU增加到了200%:
SHR S %CPU %MEM
312m 141m 4644 S 199.8
22:31.27 mysqld
由此可以简单的做出如下临时结论:
1. %CPU是由每个核的CPU占用律之和算出来的。
2. load跟执行的任务数有关
不过要想准确的知道其含义,还是必须从源码入手。
CPU利用率的计算方法
下载busybox的源码,在procps目录下有top.c的源码,查看第293行附近(1.17.1版),可以看到
if (prev_hist_count) do {
if (prev_hist[i].pid == pid) {
cur-&pcpu = cur-&ticks - prev_hist[i].
total_pcpu += cur-&
i = (i+1) % prev_hist_
/* hist_iterations++; */
} while (i != last_i);
这就是计算%CPU的代码,很明显total_pcpu就是累加了每个线程对每个核的使用率,所以%CPU的最大值就是核数*100%。
而CPU利用率又是怎么计算的呢,跟踪代码可以发现,是从系统的/proc/stat这里读取的,这个文件的格式可以参考:http://www.linuxhowtos.org/System/procstat.htm,下面是我笔记本上读出来的内容。
plx@plinux-Laptop:~/busybox-1.17.1$ cat /proc/stat
0 0 0 0 0 0 1
0 93 0 5 0 183 0 20 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
processes 6707
procs_running 2
procs_blocked 0
cpuN的含义从左到右分别是:user、system、nice、idle、iowait、irq、softirq,具体含义可以看文档。
在下面几行的含义是:
“intr”这行给出中断的信息,第一个为自系统启动以来,发生的所有的中断的次数;然后每个数对应一个特定的中断自系统启动以来所发生的次数。
“ctxt”给出了自系统启动以来CPU发生的上下文交换的次数。
“btime”给出了从系统启动到现在为止的时间,单位为秒。
“processes (total_forks) 自系统启动以来所创建的任务的个数目。
“procs_running”:当前运行队列的任务的数目。
“procs_blocked”:当前被阻塞的任务的数目。
那么CPU利用率可以使用以下方法,先取两个采样点,然后计算其差值:
cpu usage=(idle2-idle1)/(cpu2-cpu1)*100 cpu usage=[(user_2 +sys_2+nice_2) - (user_1 + sys_1+nice_1)]/(total_2 - total_1)*100;
这是一段Bash代码采集利用率的,摘自网络:
##echo user nice system idle iowait irq softirq
CPULOG_1=$(cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
SYS_IDLE_1=$(echo $CPULOG_1 | awk '{print $4}')
Total_1=$(echo $CPULOG_1 | awk '{print $1+$2+$3+$4+$5+$6+$7}')
CPULOG_2=$(cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
SYS_IDLE_2=$(echo $CPULOG_2 | awk '{print $4}')
Total_2=$(echo $CPULOG_2 | awk '{print $1+$2+$3+$4+$5+$6+$7}')
SYS_IDLE=`expr $SYS_IDLE_2 - $SYS_IDLE_1`
Total=`expr $Total_2 - $Total_1`
SYS_USAGE=`expr $SYS_IDLE/$Total*100 |bc -l`
SYS_Rate=`expr 100-$SYS_USAGE |bc -l`
Disp_SYS_Rate=`expr "scale=3; $SYS_Rate/1" |bc`
echo $Disp_SYS_Rate%
还有一段Perl的代码,也是摘自网络:
#!/usr/bin/perl
$SLEEPTIME=5;
if (-e "/tmp/stat") {
unlink "/tmp/stat";
open (JIFF_TMP, "&&/tmp/stat") || die "Can't open /proc/stat file!\n";
open (JIFF, "/proc/stat") || die "Can't open /proc/stat file!\n";
print JIFF_TMP $jiff_0[0] ;
close (JIFF);
sleep $SLEEPTIME;
open (JIFF, "/proc/stat") || die "Can't open /proc/stat file!\n";
print JIFF_TMP $jiff_1[0];
close (JIFF);
close (JIFF_TMP);
@USER=`awk '{print \$2}' "/tmp/stat"`;
@NICE=`awk '{print \$3}' "/tmp/stat"`;
@SYSTEM=`awk '{print \$4}' "/tmp/stat"`;
@IDLE=`awk '{print \$5}' "/tmp/stat"`;
@IOWAIT=`awk '{print \$6}' "/tmp/stat"`;
@IRQ=`awk '{print \$7}' "/tmp/stat"`;
@SOFTIRQ=`awk '{print \$8}' "/tmp/stat"`;
$JIFF_0=$USER[0]+$NICE[0]+$SYSTEM[0]+$IDLE[0]+$IOWAIT[0]+$IRQ[0]+$SOFTIRQ[0];
$JIFF_1=$USER[1]+$NICE[1]+$SYSTEM[1]+$IDLE[1]+$IOWAIT[1]+$IRQ[1]+$SOFTIRQ[1];
$SYS_IDLE=($IDLE[0]-$IDLE[1]) / ($JIFF_0-$JIFF_1) * 100;
$SYS_USAGE=100 - $SYS_IDLE;
printf ("The CPU usage is %1.2f%%\n",$SYS_USAGE);
Load的计算方法
跟踪busybox的代码可以知道,load是从/proc/loadavg中读取的。
我本机的一次抓取内容如下:
plx@plinux-Laptop:~/busybox-1.17.1$ cat /proc/loadavg
0.64 0.81 0.86 3/364 6930
每个值的含义依次为:
lavg_1 (0.64) 1-分钟平均负载
lavg_5 (0.81) 5-分钟平均负载
lavg_15(0.86) 15-分钟平均负载
nr_running (3) 在采样时刻,运行队列的任务的数目,与/proc/stat的procs_running表示相同意思
nr_threads (364) 在采样时刻,系统中活跃的任务的个数(不包括运行已经结束的任务)
last_pid(6930) 最大的pid值,包括轻量级进程,即线程。
假设当前有两个CPU,则每个CPU的当前任务数为0.64/2=0.32
我们可以在Linux内核中找到loadavg文件的源码:
tatic int loadavg_read_proc(char *page, char **start, off_t off,
int count, int *eof, void *data)
a = avenrun[0] + (FIXED_1/200);
b = avenrun[1] + (FIXED_1/200);
c = avenrun[2] + (FIXED_1/200);
len = sprintf(page,"%d.%02d %d.%02d %d.%02d %ld/%d %d\n",
LOAD_INT(a), LOAD_FRAC(a),
LOAD_INT(b), LOAD_FRAC(b),
LOAD_INT(c), LOAD_FRAC(c),
nr_running(), nr_threads, last_pid);
return proc_calc_metrics(page, start, off, count, eof, len);
以及计算load的代码:
#define FSHIFT
/* nr of bits of precision */
#define FIXED_1
(1<>= FSHIFT;
unsigned long avenrun[3];
EXPORT_SYMBOL(avenrun);
* calc_load - given tick count, update the avenrun load estimates.
* This is called while holding a write_lock on xtime_lock.
static inline void calc_load(unsigned long ticks)
unsigned long active_ /* fixed-point */
static int count = LOAD_FREQ;
if (count < 0) {
count += LOAD_FREQ;
active_tasks = count_active_tasks();
CALC_LOAD(avenrun[0], EXP_1, active_tasks);
CALC_LOAD(avenrun[1], EXP_5, active_tasks);
CALC_LOAD(avenrun[2], EXP_15, active_tasks);
看了,理解了这些代码。
所以可以明白:Linux的系统负载指运行队列的平均长度,也就是等待CPU的平均进程数。 Linux的系统负载指运行队列的平均长度,也就是等待CPU的平均进程数。因为Linux内禁止浮点运算,因此系统的负载只能通过计算变化的次数这一修正值来计算。Linux内核定义一个长度为3的双字数组avenrun,双字的低11位用于存放负载的小数部分,高21位用于存放整数部分。当进程所耗的 CPU时间片数超过CPU在5秒内能够提供的时间片数时,内核计算上述的三个负载。负载初始化为0,假设最近1、5、15分钟内的平均负载分别为 load1、load5和load15,那么下一个计算时刻到来时,内核通过下面的算式计算负载:
load1 -= load1 -* exp(-5 / 60) -+ n * (1 - exp(-5 / 60 ))
load5 -= load5 -* exp(-5 / 300) + n * (1 - exp(-5 / 300))
load15 = load15 * exp(-5 / 900) + n * (1 - exp(-5 / 900))
其中,exp(x)为e的x次幂,n为当前运行队列的长度。Linux内核认为进程的生存时间服从参数为1的指数分布,指数分布的概率密度为:以内核计算负载load1为例,设相邻两个计算时刻之间系统活动的进程集合为S0。从1分钟前到当前计算时刻这段时间里面活动的load1个进程,设他们的集合是 S1,内核认为的概率密度是:λe-λx,而在当前时刻活动的n个进程,设他们的集合是Sn内核认为的概率密度是1-λe-λx。其中x = 5 / 60,因为相邻两个计算时刻之间进程所耗的CPU时间为5秒,而考虑的时间段是1分钟(60秒)。那么可以求出最近1分钟系统运行队列的长度:
load1 = |S1| -* λe-λx + |Sn| * (1-λe-λx) = load1 * λe-λx + n * (1-λe-λx)
其中λ = 1, x = 5 / 60, |S1|和|Sn|是集合元素的个数,这就是Linux内核源文件shed.c的函数calc_load()计算负载的数学依据。
所以“Load值=CPU核数”,这是最理想的状态,没有任何竞争,一个任务分配一个核。
由于数据是每隔5秒钟检查一次活跃的进程数,然后根据这个数值算出来的。如果这个数除以CPU的核数,结果高于5的时候就表明系统在超负荷运转了。
类似的文章
分类目录分类目录
选择分类目录
生活轨迹&&(66)
&&&随笔日记&&(23)
&&&心灵感触&&(18)
技术笔记&&(174)
&&&数据库&&(84)
&&&操作系统&&(34)
&&&程序设计&&(39)
&&&架构设计&&(7)
&&&项目管理&&(3)
&&&嵌入式&&(4)
&&&人工智能&&(3)
学习研究&&(11)
&&&自然科学&&(6)
&&&经济管理&&(4)
娱乐活动&&(4)

我要回帖

更多关于 单摆周期精确公式 的文章

 

随机推荐