获取linux系统CPU使用率
来源:互联网 发布:金山毒霸网络卸载密码 编辑:程序博客网 时间:2024/05/29 05:55
linux中监视系统运行信息,需要研究/proc下的一些信息,/proc/stat/给出了cpu的运行信息,先来分析一下
[work@builder ~]$ cat /proc/stat
cpu 432661 13295 86656 422145968 171474 233 5346
cpu0 123075 2462 23494 105543694 16586 0 4615
cpu1 111917 4124 23858 105503820 69697 123 371
cpu2 103164 3554 21530 105521167 64032 106 334
cpu3 94504 3153 17772 105577285 21158 4 24
intr 1065711094 1057275779 92 0 6 6 0 4 0 3527 0 0 0 70 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 7376958 0 0 0 0 0 0 0 1054602 0 0 0 0 0 0 0 30 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 19067887
btime 1139187531
processes 270014
procs_running 1
procs_blocked 0
输出解释
CPU 以及CPU0、CPU1、CPU2、CPU3每行的每个参数意思(以第一行为例)为:
参数 解释
user (432661) 从系统启动开始累计到当前时刻,用户态的CPU时间(单位:jiffies) ,不包含 nice值为负进程。1jiffies=0.01秒
nice (13295) 从系统启动开始累计到当前时刻,nice值为负的进程所占用的CPU时间(单位:jiffies)
system (86656) 从系统启动开始累计到当前时刻,核心时间(单位:jiffies)
idle (422145968) 从系统启动开始累计到当前时刻,除硬盘IO等待时间以外其它等待时间(单位:jiffies)
iowait (171474) 从系统启动开始累计到当前时刻,硬盘IO等待时间(单位:jiffies) ,
irq (233) 从系统启动开始累计到当前时刻,硬中断时间(单位:jiffies)
softirq (5346) 从系统启动开始累计到当前时刻,软中断时间(单位:jiffies)
CPU时间=user+system+nice+idle+iowait+irq+softirq
“intr”这行给出中断的信息,第一个为自系统启动以来,发生的所有的中断的次数;然后每个数对应一个特定的中断自系统启动以来所发生的次数。
“ctxt”给出了自系统启动以来CPU发生的上下文交换的次数。
“btime”给出了从系统启动到现在为止的时间,单位为秒。
“processes (total_forks) 自系统启动以来所创建的任务的个数目。
“procs_running”:当前运行队列的任务的数目。
“procs_blocked”:当前被阻塞的任务的数目。
通过两次读取/proc/stat文件,计算出cpu的busy和idle时间差。
代码只是简单的获取出CPU使用率,如果用到系统里,还是要用线程的。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <errno.h>
#define STATES_line2x4
static const char *States_fmts = STATES_line2x4;
//Total number of CPU
static int Cpu_tot;
// These typedefs attempt to ensure consistent 'ticks' handling
typedef unsigned long long TIC_t;
// This structure stores a frame's cpu tics used in history
// calculations.
// all environments.
typedef struct CPU_t {
} CPU_t;
// This routine simply formats whatever the caller wants and
// returns a pointer to the resulting 'const char' string...
static const char *fmtmk (const char *fmts, ...)
{
}
static CPU_t *cpus_refresh (CPU_t *cpus)
{
}
static void summaryhlp (CPU_t *cpu, const char *pfx)
{
#define TRIMz(x)
printf("u = %Lu, u_sav = %Lu\n", cpu->u, cpu->u_sav);
#undef TRIMz
}
int main()
{
static CPU_t * smpcpu = NULL;
}
转自:http://blog.163.com/sxs_solo/blog/static/2633338201011129811606/
- 获取linux系统CPU使用率
- 获取系统CPU 使用率
- 获取系统的CPU使用率、内存使用率
- 【Delphi】获取系统CPU使用率
- [系统级] 获取CPU使用率
- [Java学习] Java获取Linux系统cpu使用率
- linux下实现CPU使用率和内存使用率获取方法
- linux下获取内存使用率及cpu使用率
- Linux下,java获取CPU使用率、内存使用率
- Linux下,java获取CPU使用率、内存使用率
- linux下获取cpu和内存使用率
- LINUX下获取CPU和内存使用率
- linux获取当前进程的CPU使用率
- Linux 下使用Sigar 获取CPU 使用率
- linux下分析系统cpu使用率
- Shell脚本监控Linux系统CPU使用率
- 【系统信息获取】5,获取CPU当前使用率
- 获取进程cpu使用率
- java properties文件操作
- mfc摘录
- POJ 1509 Glass Beads(SAM 求最小表示法)
- IIS7配置PHP图解
- Spring结合Quartz配置
- 获取linux系统CPU使用率
- 总结: c++ 多重继承,虚拟继承,内存布局
- poj 1986LCA离线dfs+并查集
- 50个c/c++源代码网站
- URAL 1244
- MATLAB 的大括号(转自小小IT空间)
- TCP快速重传与快速恢复原理分析
- 装配线调度
- 【AsynTask】android异步任务详解 AsynTask