Chinaunix首页 | 论坛 | 博客
  • 博客访问: 219714
  • 博文数量: 28
  • 博客积分: 398
  • 博客等级: 一等列兵
  • 技术积分: 1109
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-07 22:28
文章分类
文章存档

2017年(1)

2014年(3)

2013年(7)

2012年(4)

2011年(13)

分类: LINUX

2011-05-10 16:24:02

读取cpu负载信息,get_cpu_real返回多核负载情况,
返回参数:第一个是cpu总负载,后续是各个核心的负载。

  1. /*
  2. *读取cpu负载信息,get_cpu_real返回多核负载情况,
  3. *返回参数:第一个是cpu总负载,后续是各个核心的负载。
  4. */

  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>

  8. int get_cpu_real( float *cpu_load );

  9. int main( int argc, char *argv[] )
  10. {
  11.     int i = 0;
  12.     while ( i < 10000 )
  13.     {
  14.         float cpu_f[16];
  15.         get_cpu_real( cpu_f );
  16.         int cpu_all = (int)(100*cpu_f[0]);
  17.         int cpu_i1 = (int)(100*cpu_f[1]);
  18.         fprintf( stdout,"all cpu load=%d, core1= %d\n", cpu_all, cpu_i1 );
  19.         usleep( 1000*500);
  20.     }

  21.     return 0;
  22. }


  23. int get_cpu_load_new( )
  24. {
  25.     char cpu_load[1024] = {0x00};
  26.     int buflen = sizeof(cpu_load);
  27.     unsigned int total, user, nice, system, idle;

  28.     char cpu[32] = {0x00};
  29.     char text[512] = {0x00};

  30.     FILE *fp;
  31.     fp = fopen("/proc/stat", "r");
  32.     if ( fp == NULL )
  33.         return -1;

  34.     while (fgets(text, 200, fp))
  35.     {
  36.         if (strstr(text, "cpu") == NULL )
  37.             break;

  38.         sscanf(text, "%s %u %u %u %u", cpu, &user, &nice, &system, &idle);
  39.         total = (user + nice + system + idle);

  40.         snprintf(cpu_load+strlen(cpu_load), buflen-strlen(cpu_load),
  41.                 "%s %u %u %u %u %u\n",
  42.                 cpu, user, nice, system, idle,total);
  43.         memset( text, 0x00, sizeof(text) );
  44.     }
  45.     fclose(fp);

  46.     FILE *fp_cpu = NULL;
  47.     fp_cpu = fopen("/tmp/taw_cpu_load_info", "w+");
  48.     if ( fp_cpu == NULL )
  49.         return -1;
  50.     fwrite( cpu_load, 1, strlen( cpu_load), fp_cpu );
  51.     fclose(fp_cpu);

  52.     return 0;
  53. }

  54. #define TAW_CPU_KERNEL_MAX 16 // 最多支持16核
  55. int get_cpu_real( float *cpu_load )
  56. {
  57.     FILE *fp = NULL;
  58.     char text[512] = {0x00};
  59.     char cpu[32] = {0x00};
  60.     int i = 0;
  61.     unsigned int total_old[TAW_CPU_KERNEL_MAX ] = {0x00};
  62.     unsigned int total_new[TAW_CPU_KERNEL_MAX ] = {0x00};
  63.     unsigned int user_old[TAW_CPU_KERNEL_MAX ] = {0x00};
  64.     unsigned int user_new[TAW_CPU_KERNEL_MAX ] = {0x00};
  65.     unsigned int nice_old[TAW_CPU_KERNEL_MAX ] = {0x00};
  66.     unsigned int nice_new[TAW_CPU_KERNEL_MAX ] = {0x00};
  67.     unsigned int system_old[TAW_CPU_KERNEL_MAX ] = {0x00};
  68.     unsigned int system_new[TAW_CPU_KERNEL_MAX ] = {0x00};
  69.     unsigned int idle_old[TAW_CPU_KERNEL_MAX ] = {0x00};
  70.     unsigned int idle_new[TAW_CPU_KERNEL_MAX ] = {0x00};


  71.     // 读取上次cpu信息
  72.     fp = fopen("/tmp/taw_cpu_load_info", "r");
  73.     i=0;
  74.     while ( fp != NULL && fgets(text, 200, fp) )
  75.     {
  76.         if (strstr(text, "cpu") == NULL )
  77.             break;

  78.         sscanf(text, "%s %u %u %u %u %u", cpu, &user_old[i],
  79.                 &nice_old[i], &system_old[i], &idle_old[i], &total_old[i] );
  80.         i++;
  81.     }
  82.     if ( fp != NULL )
  83.         fclose(fp);

  84.     // 读取新的cpu信息
  85.     get_cpu_load_new();
  86.     fp = fopen("/tmp/taw_cpu_load_info", "r");
  87.     if ( fp == NULL )
  88.         return -1;

  89.     i = 0;
  90.     while (fgets(text, 200, fp))
  91.     {
  92.         if (strstr(text, "cpu") == NULL )
  93.             break;

  94.         sscanf(text, "%s %u %u %u %u %u", cpu, &user_new[i], &nice_new[i],
  95.                 &system_new[i], &idle_new[i], &total_new[i] );

  96.         if (total_new[i] == total_old[i])
  97.             cpu_load[i] = 0.0f;
  98.         else
  99.         {
  100.             cpu_load[i] = ( (user_new[i] + nice_new[i] + system_new[i] ) -
  101.                     (user_old[i] + nice_old[i] + system_old[i] ) ) / (float)( total_new[i] - total_old[i] );
  102.         }

  103.         i++;
  104.     }
  105.     fclose(fp);

  106.     return 0;
  107. }

阅读(2129) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~