Linux Cpu 利用率计算
转载:http://blog.chinaunix.net/uid-20057401-id-1979033.html
前几天要写一个取得linux performance的函数。查询了一些资料。发现有几种计算cpu利用率的方法。但是都不怎么正确。最后查了以下top的源代码。现列出其计算cpu利用率的关键函数
c代码如下:
typedef struct CPU_t {
TIC_t u, n, s, i, w, x, y, z; // as represented in /proc/stat
TIC_t u_sav, s_sav, n_sav, i_sav, w_sav, x_sav, y_sav, z_sav; // in the order of our display
unsigned id; // the CPU ID number
} CPU_t;
c代码实现如下:
static CPU_t *cpus_refresh (CPU_t *cpus)
{
static FILE *fp = NULL;
int i;
int num;
// enough for a /proc/stat CPU line (not the intr line)
char buf[SMLBUFSIZ];
/* by opening this file once, we'll avoid the hit on minor page faults
(sorry Linux, but you'll have to close it for us) */
if (!fp) {
if (!(fp = fopen("/proc/stat", "r")))
std_err(fmtmk("Failed /proc/stat open: %s", strerror(errno)));
/* note: we allocate one more CPU_t than Cpu_tot so that the last slot
can hold tics representing the /proc/stat cpu summary (the first
line read) -- that slot supports our View_CPUSUM toggle */
cpus = alloc_c((1 + Cpu_tot) * sizeof(CPU_t));
}
rewind(fp);
fflush(fp);
// first value the last slot with the cpu summary line
if (!fgets(buf, sizeof(buf), fp)) std_err("failed /proc/stat read");
cpus[Cpu_tot].x = 0; // FIXME: can't tell by kernel version number
cpus[Cpu_tot].y = 0; // FIXME: can't tell by kernel version number
cpus[Cpu_tot].z = 0; // FIXME: can't tell by kernel version number
num = sscanf(buf, "cpu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu",
&cpus[Cpu_tot].u,
&cpus[Cpu_tot].n,
&cpus[Cpu_tot].s,
&cpus[Cpu_tot].i,
&cpus[Cpu_tot].w,
&cpus[Cpu_tot].x,
&cpus[Cpu_tot].y,
&cpus[Cpu_tot].z
);
if (num < 4)
std_err("failed /proc/stat read");
// and just in case we're 2.2.xx compiled without SMP support...
if (Cpu_tot == 1) {
cpus[1].id = 0;
memcpy(cpus, &cpus[1], sizeof(CPU_t));
}
// now value each separate cpu's tics
for (i = 0; 1 < Cpu_tot && i < Cpu_tot; i++) {
if (!fgets(buf, sizeof(buf), fp)) std_err("failed /proc/stat read");
cpus[i].x = 0; // FIXME: can't tell by kernel version number
cpus[i].y = 0; // FIXME: can't tell by kernel version number
cpus[i].z = 0; // FIXME: can't tell by kernel version number
num = sscanf(buf, "cpu%u %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu",
&cpus[i].id,
&cpus[i].u, &cpus[i].n, &cpus[i].s, &cpus[i].i, &cpus[i].w, &cpus[i].x, &cpus[i].y, &cpus[i].z
);
if (num < 4)
std_err("failed /proc/stat read");
}
return cpus;
}
c 代码实现如下:
static void summaryhlp (CPU_t *cpu, const char *pfx)
{
// we'll trim to zero if we get negative time ticks,
// which has happened with some SMP kernels (pre-2.4?)
#define TRIMz(x) ((tz = (SIC_t)(x)) < 0 ? 0 : tz)
SIC_t u_frme, s_frme, n_frme, i_frme, w_frme, x_frme, y_frme, z_frme, tot_frme, tz;
float scale;
u_frme = cpu->u - cpu->u_sav;
s_frme = cpu->s - cpu->s_sav;
n_frme = cpu->n - cpu->n_sav;
i_frme = TRIMz(cpu->i - cpu->i_sav);
w_frme = cpu->w - cpu->w_sav;
x_frme = cpu->x - cpu->x_sav;
y_frme = cpu->y - cpu->y_sav;
z_frme = cpu->z - cpu->z_sav;
tot_frme = u_frme + s_frme + n_frme + i_frme + w_frme + x_frme + y_frme + z_frme;
if (tot_frme < 1) tot_frme = 1;
scale = 100.0 / (float)tot_frme;
// display some kinda' cpu state percentages
// (who or what is explained by the passed prefix)
show_special(
0,
fmtmk(
States_fmts,
pfx,
(float)u_frme * scale,
(float)s_frme * scale,
(float)n_frme * scale,
(float)i_frme * scale,
(float)w_frme * scale,
(float)x_frme * scale,
(float)y_frme * scale,
(float)z_frme * scale
)
);
Msg_row += 1;
// remember for next time around
cpu->u_sav = cpu->u;
cpu->s_sav = cpu->s;
cpu->n_sav = cpu->n;
cpu->i_sav = cpu->i;
cpu->w_sav = cpu->w;
cpu->x_sav = cpu->x;
cpu->y_sav = cpu->y;
cpu->z_sav = cpu->z;
#undef TRIMz
}
Linux Cpu 利用率计算相关推荐
- linux下如何计算cpu利用率,Linux下的CPU利用率计算原理详解
我们在搞性能测试的时候,对后台服务器的CPU利用率监控是一个常用的手段.服务器的CPU利用率高,则表明服务器很繁忙.如果前台响应时间越来越大,而后台CPU利用率始终上不去,说明在某个地方有瓶颈了,系统 ...
- linux cpu使用率计算
转自linux 进程的cpu计算,linux环境下cpu利用率的计算_美自的博客-CSDN博客,已经收藏但怕丢失所以直接复制过来 目前linux统计cpu利用率时,所用到的信息,大多数是从 /proc ...
- linux shell数字怎么比较大小,Linux Shell 数字计算与比较
直接上脚本, 使用$(())以及$[]进行数字计算 数值比较: n1 -eq n2检查n1是否等于n2 n1 -le n2检查n1是否小于等于n2 n1 -ge n2检查n1是否大于等 ...
- 计算linux服务器CPU利用率
文章目录 一 通过top查看cpu各类率占用信息 二 通过/proc/stat文件查看cpu信息 三 cpu占用率计算公式 四 代码实现 一 通过top查看cpu各类率占用信息 如下图所示: us U ...
- linux /proc/stat 计算线程cpu,Linux下用/proc/stat文件来计算cpu的利用率(附源码)
总的Cpu使用率计算 计算方法: 1.采样两个足够短的时间间隔的Cpu快照,分别记作t1,t2,其中t1.t2的结构均为: (user.nice.system.idle.iowait.irq.soft ...
- linux cpu平均利用率st,理解 CPU 利用率
从 top 命令说起 在 Linux shell 上执行 top 命令,可以看到这样一行 CPU 利用率的数据: %Cpu(s): 0.1 us, 0.0 sy, 0.0 ni, 99.9 id, 0 ...
- Linux如何统计进程的CPU利用率
1.0 概述 在Linux的/proc文件系统,可以看到自启动时候开始,所有CPU消耗的时间片:对于个进程,也可以看到进程消耗的时间片.这是一个累计值,可以"非阻塞"的输出.获得一 ...
- Linux系统中的CPU利用率
CPU利用率是系统性能监控的重要指标.CPU利用率是开发人员系统性能优化的重要参考指标.当CPU总体利用率过高时,开发过程中需要根据具体情况进行考虑,在从程序层面优化还是从部署层面优化.程序层面通过降 ...
- linux. 获得cpu利用率 arm,Linux如何统计进程的CPU利用率
0. 为什么写这篇博客 Linux的top或者ps都可以查看进程的cpu利用率,那为什么还需要了解这个细节呢.编写这篇文章呢有如下三个原因: * 希望在脚本中,能够以过"非阻塞"的 ...
最新文章
- kettle中通过 时间戳(timestamp)方式 来实现数据库的增量同步操作(一)
- Hyperledger Fabric安装问题备忘(二)
- MVC中实现订单表和订单详细表联动新增的一种方法
- FFT和Matlab中操作FFT
- java如何把查到的对象集合放入 展示对象list中_Java面试整理-基础篇8.集合1
- pico8 掌机_使用Pico-8构建自己的复古游戏
- [JLOI2015]管道连接(斯坦纳树)
- SpringSecurity +Jwt 实现权限管理
- 写个类操作窗口(句柄操作)
- thinkphp5项目--企业单车网站(二)
- JAVA游戏死神之谜下载_诸神战纪二-死神之谜BT版
- c语言程序设计课程表,c语言怎样编写课程表,请问程序怎样写?
- 安卓手机修改ntp服务器,修改安卓手机ntp服务器地址
- ACDSee 15/ACDSee Pro 6简体中文版破解注册机
- java实现pdf旋转_Java实现PDF文本旋转倾斜的方法
- 澳门科技大学计算机专业研究生,澳门科技大学 计算机专业
- 难得轻闲-海豚湾恋人
- 抖音创作规范_抖音创作内容调整提示怎么办?应该怎么解决
- 递推算法—逆推案例(大学生存款)(C语言)
- Python之CSV文件操作
热门文章
- 开启市场新格局 且看新华三计算与存储新品发布会
- Linux : shell基础(慕课网Linux达人养成计划课程笔记)
- Spring4.0之四:Meta Annotation(元注解)
- Supermap 组合单值专题图与标签专题图演示样例
- Lync Server 2010标准版系列PART6:启用Lync
- 计算机操作系统指导书,《计算机操作系统》实验指导书-2015
- python cmath模块_cmath模块-PYTHON
- Python 之 Python2 和 Python3 的区别
- java 数据结构_Java版-数据结构-队列(数组队列)
- python *args和**kwargs以及序列解包