免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2175 | 回复: 1
打印 上一主题 下一主题

[C] linux系统CPU利用率计算(C语言版)【来源于网络】 [复制链接]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-06-26 16:32 |只看该作者 |倒序浏览
本帖最后由 miniuinx 于 2013-06-26 16:58 编辑

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;
   
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.x = 0; // FIXME: can't tell by kernel version number

      cpus.y = 0; // FIXME: can't tell by kernel version number

      cpus.z = 0; // FIXME: can't tell by kernel version number

      num = sscanf(buf, "cpu%u %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu",
         &cpus.id,
         &cpus.u, &cpus.n, &cpus.s, &cpus.i, &cpus.w, &cpus.x, &cpus.y, &cpus.z
      );
      if (num < 4)
            std_err("failed /proc/stat read");
   }
   return cpus;
}
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
}

论坛徽章:
1
子鼠
日期:2013-08-23 16:36:37
2 [报告]
发表于 2013-06-26 20:35 |只看该作者
能不能加点说明,更容易理解
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP