linux CPU loading calculate
http://hi.baidu.com/lionpanther/item/e908c37abdaf1c3f71442380
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
struct occupy
{
char name[20];
unsigned int user;
unsigned int nice;
unsigned int system;
unsigned int idle;
};
float g_cpu_used;
int cpu_num;
void cal_occupy(struct occupy *, struct occupy *);
void get_occupy(struct occupy *);
int main()
{
struct occupy ocpu[10];
struct occupy ncpu[10];
int i;
cpu_num = sysconf(_SC_NPROCESSORS_ONLN);
for(;;)
{
sleep(1);
get_occupy(ocpu);
sleep(1);
get_occupy(ncpu);
for (i=0; i<cpu_num; i++)
{
cal_occupy(&ocpu[i], &ncpu[i]);
printf("%f \n", g_cpu_used);
}
}
}
void cal_occupy (struct occupy *o, struct occupy *n)
{
double od, nd;
double id, sd;
double scale;
od = (double) (o->user + o->nice + o->system +o->idle);//第一次(用户+优先级+系统+空闲)的时间
nd = (double) (n->user + n->nice + n->system +n->idle);//第二次(用户+优先级+系统+空闲)的时间
scale = 100.0 / (float)(nd-od);
id = (double) (n->user - o->user);
sd = (double) (n->system - o->system);
g_cpu_used = ((sd+id)*100.0)/(nd-od);
}
void get_occupy (struct occupy *o)
{
FILE *fd;
int n;
char buff[1024];
fd = fopen ("/proc/stat", "r");
fgets (buff, sizeof(buff), fd);
for(n=0;n<cpu_num;n++)
{
fgets (buff, sizeof(buff),fd);
sscanf (buff, "%s %u %u %u %u", &o[n].name, &o[n].user, &o[n].nice,&o[n].system, &o[n].idle);
fprintf (stderr, "%s %u %u %u %u\n", o[n].name, o[n].user, o[n].nice,o[n].system, o[n].idle);
}
fclose(fd);
}
linux CPU loading calculate的更多相关文章
- Linux CPU亲缘性详解
前言 在淘宝开源自己基于nginx打造的tegine服务器的时候,有这么一项特性引起了笔者的兴趣.“自动根据CPU数目设置进程个数和绑定CPU亲缘性”.当时笔者对CPU亲缘性没有任何概念,当时作者只是 ...
- 查看线程linux cpu使用率
Linux下如何查看高CPU占用率线程 LINUX CPU利用率计算 转 http://www.cnblogs.com/lidabo/p/4738113.html目录(?)[-] proc文件系统 p ...
- Linux CPU数量判断,通过/proc/cpuinfo.
Linux CPU数量判断,通过/proc/cpuinfo. 相同 physical id :决定一个物理处理器 如果“siblings”和“cpu cores”一致,则说明不支持超线程,或者超线程未 ...
- How do I Find Out Linux CPU Utilization?
From:http://www.cyberciti.biz/tips/how-do-i-find-out-linux-cpu-utilization.html Whenever a Linux sys ...
- Linux CPU监控指标
Linux CPU监控指标 Linux提供了非常丰富的命令可以进行CPU相关数据进行监控,例如:top.vmstat等命令.top是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执 ...
- 转载: 一、linux cpu、内存、IO、网络的测试工具
来源地址: http://blog.csdn.net/wenwenxiong/article/details/77197997 记录一下 以后好找.. 一.linux cpu.内存.IO.网络的测试工 ...
- Linux CPU使用率含义及原理
相关概念 在Linux/Unix下,CPU利用率分为用户态.系统态和空闲态,分别表示CPU处于用户态执的时间,系统内核执行的时间,和空闲系统进程执行的时间. 下面是几个与CPU占用率相关的概念. CP ...
- Understanding Linux CPU stats
Your Linux server is running slow, so you follow standard procedure and run top. You see the CPU met ...
- Linux CPU Load Average
理解Linux系统负荷 LINUX下CPU Load Average的一点研究 Linux load average负载量分析与解决思路 Understanding Linux CPU Load - ...
随机推荐
- oom日志查看
这通常会触发 Linux 内核里的 Out of Memory (OOM) killer,OOM killer 会杀掉某个进程以腾出内存留给系统用,不致于让系统立刻崩溃.如果检查相关的日志文件(/va ...
- 【六】PHP正则表达式方法
PHP中正则表达式的声明格式有两种方式,一种是POSIX老版模式,已经不常用.还有一种是其他语言中常见的PCRE方法. 1.正则表达式的匹配方法并返回匹配的项:array preg_grep(stri ...
- POJ 2541 Binary Witch(逆序KMP,好题)
逆序KMP,真的是强大! 参考链接,下面有题意解释:http://blog.sina.com.cn/s/blog_6ec5c2d00100tphp.htmlhttp://blog.csdn.net/s ...
- iOS生成本地随机验证码
原文链接:http://www.cnblogs.com/jerehedu/p/4527707.html 效果图:
- POJ 1573
#include<iostream> #include<stdio.h> #define MAXN 15 using namespace std; char _m[MAXN][ ...
- **app后端设计(10)--数据增量更新(省流量)
在新浪微博的app中,从别的页面进入主页,在没有网络的情况下,首页中的已经收到的微博还是能显示的,这显然是把相关的数据存储在app本地. 使用数据的app本地存储,能减少网络的流量,同时极大提高了用户 ...
- eclipse创建python项目
http://jingyan.baidu.com/article/19192ad8173300e53f570757.html
- Linux操作系统下的Sudo命令
查看.修改或者执行某些命令需要root用户的权限,如果不想直接切换到root用户,就可以使用sudo命令.sudo命令用于针对单个命令授予临时权限.sudo仅在需要时授予用户权限,减少了用户因为错误执 ...
- android-non-ui-ui-thread-communications-part-5-5
This is the last post in my series regarding Android thread communications. Parts 1 through 4 are l ...
- ks全自动安装centos
1. 全新安装centos,选择好所需定制包 2. 完成安装后会在root下面生成一个install.log(rpm包列表)anaconda-ks.cfg(下文的ks.cfg基于此文件修改) 3. 将 ...