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  
}  

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/463645.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Delphi 记录类型- 结构指针

转自&#xff1a;万一老师的 Delphi 博客 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 class(TForm) Button1: TButton; Button2: TButton; …

深圳的房价跌了

我很久没有关注深圳的房价了&#xff0c;上一篇关于深圳房价的文章好像还是几个月之前的&#xff0c;几个月之前&#xff0c;我一个同学买房&#xff0c;跟我咨询了下&#xff0c;然后就写了一篇文章。现在不要着急买房这篇文章从那个时候到现在已经一年了&#xff0c;前几天跟…

Springboot 使用Mybatis对postgreSQL实现CRUD

目录结构 1、创建一个springboot项目 选择Web、Mabatis、postgreSQL 2、在application中写入配置文件 1 #配置数据源 2 spring.datasource.platformpostgres 3 spring.datasource.urljdbc:postgresql://127.0.0.1:5432/postgres 4 spring.datasource.usernamepostgres 5 spring…

boost学习之boost::lock_guard源码分析

boost::lock_guard可以说是一种比boost::unique_lock轻量级的lock, 简单一些场景可以用它就行了。源码如下&#xff1a; template<typename Mutex> class lock_guard { private: Mutex& m; explicit lock_guard(lock_guard&); lock_guard& operator(lock…

搜索连接字符串存储过程【原创】

1、CREATE PROCEDURE dbo.SearchUserInfo (Age int ) AS SELECT * FROM UserInfo WHERE Age like %cast(Age as nvarchar(50))% RETURN 2、 CREATE PROCEDURE dbo.Mfx_SearchUserInfo --用户搜索[state-审核状态(2为全部)、UserLever-用户级别(0为全部)、keyWord(关键字)] …

mysql my.cnf 配置建议

/usr/local/share/mysql下面有5个my-xxxx.cnf文件 my-small.cnf 最小配置安装&#xff0c;内存⇐64M&#xff0c;数据数量最少 my-large.cnf 内存512M my-medium.cnf 32M<内存<64M&#xff0c;或者内存有128M&#xff0c;但是数据库与web服务器公用内存 my-huge.cnf …

不得不说,这是我面过的最优秀的Linux运维!

Linux可以说是运维之“本”。无论中小企业还是大厂&#xff0c;现在的企业有95%甚至更多是使用Linux服务器。而对于Linux运维来说&#xff0c;Linux基础越扎实、会的工具越多&#xff0c;能解决的问题就越多&#xff0c;技术也能走的更远。Linux&#xff0c;甚至可以说是进入IT…

boost学习之boost::lock_guardT与boost::unique_lockT的区别

(1)、boost::lock_guard没有unlock()功能 boost::mutex mutex; void foo( ) { boost::lock_guard<boost::mutex> lock(mutex); process(data); ///没有unlock()功能&#xff0c;程序结束自动析构 }; (2)、unique_lock允许设置超时&#xff0c;推迟锁定lock以及在对象…

用友财务软件主要数据表字段含义

用友财务软件主要数据表字段含义 /*rdrecords : 收发记录子表AutoID : auto ID 自动编号 automatism identification(identity)ID : ID 与收发记录主表关联项cInvCode : c…

重叠IO之完成例程

http://hi.baidu.com/%CC%EC%D1%C4_jmf/blog/item/7718ccd99b03cb2710df9bc2.html转载于:https://www.cnblogs.com/vcdebug2010/archive/2012/06/23/2559096.html

一个中科大「差生」的8年程序员工作总结

今年终于从大菊花厂离职了&#xff0c;离职前收入大概60w不到吧&#xff01;在某乎属于比较差的&#xff0c;今天终于有空写一下自己的职场故事&#xff0c;也算是给自己近8年的工作做个总结复盘。近8年有些事情做对了&#xff0c;也有更多事情做错了&#xff0c;在这里记录一下…

Java IO File

#file file的一些方法&#xff0c;因为windows和Linux开发环境的问题&#xff0c;在file中最好统一用 / 输出流操作 转载于:https://www.cnblogs.com/cykfory/p/10294981.html

gtest使用例子

最近使用gtest进行单元测试&#xff0c;采用打桩的形式。关于gtest的详细说明就不多说了&#xff0c;网上的资料一大堆。主要讲解使用时的参数如何配置以及遇到的问题。下面的例子模拟是加、减、乘、除四则运算&#xff0c;前提是不知道加、减、乘、除四则运算是如何实现的。 …

游戏开发中的数学和物理算法(7):角度 vs 弧度

我们通常使用的笛卡尔坐标系统&#xff0c;角点通常在(0,0),即原点。初始边在x轴正半轴&#xff0c;终边与初始边成夹角。初始边逆时针旋转为正值&#xff0c;顺时针旋转为逆值。数学表示&#xff1a;角度&#xff1a;degreeradian*180/π 弧度&#xff1a;radiandegree*π/18…

医学影像PACS系统解决方案与成功案例汇总

经历过2010年一年时间&#xff0c;HC3i论坛也与大家相伴快一年了&#xff0c;这一年中&#xff0c;网友分享专业医疗信息化资料超过15000个&#xff0c;HC3i感谢大家的支持与厚爱&#xff01;岁末年初之极&#xff0c;也整理盘点一下&#xff0c;方便大家回顾一年中精华资源&am…

npm 安装包报错 rollbackFailedOptional

npm config rm proxynpm config rm https-proxy 然后使用npm install -g cnpm --registryhttps://registry.npm.taobao.org安装淘宝的cnpm 然后就可以使用cnpm命令了转载于:https://www.cnblogs.com/xtjatswc/p/10295734.html

一个小老板的春天

大家周末好继续之前说的采访专栏&#xff0c;前两天和一个做生意的朋友聊天&#xff0c;总结了一些内容分享给大家&#xff0c;觉得不错的点赞收藏起来&#xff0c;可能后面你自己当老板了会用到。我这个老板的名字和公司名字我就不说出来了&#xff0c;他现在做的是细分领域&a…

boost学习之boost::shared_ptr

Boost智能指针——shared_ptr boost::scoped_ptr虽然简单易用&#xff0c;但它不能共享所有权的特性却大大限制了其使用范围&#xff0c;而boost::shared_ptr可以解决这一局限。顾名思义&#xff0c;boost::shared_ptr是可以共享所有权的智能指针&#xff0c;首先让我们通过一…

SharePoint GridView的使用2——DataSourceView的使用

首先创建一个abstract类&#xff0c;继承Microsoft.SharePoint.WebControls.DataTableDataSourceView。之后基于这个类可以创建多个显示不同数据的的DataSourceView&#xff0c;在上文的DataSource控件中有个ViewName属性&#xff0c;可以通过这个属性来区分不同的DataSourceVi…

瞧瞧UC浏览器对CSS样式的“关怀”

瞧瞧UC浏览器对CSS样式的“关怀” UC对CSS这样的“照顾”尤其8.0以下体现的是淋漓尽致&#xff01; 不"支持"font-family属性&#xff0c;也就是说&#xff0c;在UC浏览器你只能看到一种字体&#xff1b;不"支持"font-szie属性&#xff0c;也就是说&#…