当前位置: 技术问答>linux和unix
谁有top命令的源码?如何获得cpu使用率
来源: 互联网 发布时间:2015-07-21
本文导语: 我写的一个程序需要获得当前cpu利用率 但是手头上没有linux源程序盘 没有找到top命令的源代码? 谁有麻烦帖一下,或者发给我 eveningglory@163.com 获得cpu使用率,我知道是通过读/proc/stat的内容获得。 但是这个文件中的...
我写的一个程序需要获得当前cpu利用率
但是手头上没有linux源程序盘
没有找到top命令的源代码?
谁有麻烦帖一下,或者发给我
eveningglory@163.com
获得cpu使用率,我知道是通过读/proc/stat的内容获得。
但是这个文件中的每一个数字都代表什么含义呢?
具体怎么实现的
谢谢
但是手头上没有linux源程序盘
没有找到top命令的源代码?
谁有麻烦帖一下,或者发给我
eveningglory@163.com
获得cpu使用率,我知道是通过读/proc/stat的内容获得。
但是这个文件中的每一个数字都代表什么含义呢?
具体怎么实现的
谢谢
|
For example: (for kernel 2.4.X)
step1: read /proc/stat file and record "cpu 1326010 7 368920 210680" to four unsigned integer(user sys nice idle ??).
step2: read /proc/stat file again after N seconds. for example, "cpu 132656 8 368930 210682"
step3: caculate cup occupy. 1 - (210682 - 210680)/ ((1326010 + 7 + 368920 + 210680) - (132656 + 8 + 368930 + 210682)) * 100%
step1: read /proc/stat file and record "cpu 1326010 7 368920 210680" to four unsigned integer(user sys nice idle ??).
step2: read /proc/stat file again after N seconds. for example, "cpu 132656 8 368930 210682"
step3: caculate cup occupy. 1 - (210682 - 210680)/ ((1326010 + 7 + 368920 + 210680) - (132656 + 8 + 368930 + 210682)) * 100%
|
Procps - The /proc file system utilities
download URL :http://procps.sourceforge.net/procps-3.2.3.tar.gz
|
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)) 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;
tot_frme = u_frme + s_frme + n_frme + i_frme + w_frme + x_frme + y_frme;
if (tot_frme
{
// 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)) 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;
tot_frme = u_frme + s_frme + n_frme + i_frme + w_frme + x_frme + y_frme;
if (tot_frme
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!