读取cpu负载信息,get_cpu_real返回多核负载情况,
返回参数:第一个是cpu总负载,后续是各个核心的负载。
- /*
-
*读取cpu负载信息,get_cpu_real返回多核负载情况,
-
*返回参数:第一个是cpu总负载,后续是各个核心的负载。
-
*/
-
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
int get_cpu_real( float *cpu_load );
-
-
int main( int argc, char *argv[] )
-
{
-
int i = 0;
-
while ( i < 10000 )
-
{
-
float cpu_f[16];
-
get_cpu_real( cpu_f );
-
int cpu_all = (int)(100*cpu_f[0]);
-
int cpu_i1 = (int)(100*cpu_f[1]);
-
fprintf( stdout,"all cpu load=%d, core1= %d\n", cpu_all, cpu_i1 );
-
usleep( 1000*500);
-
}
-
-
return 0;
-
}
-
-
-
int get_cpu_load_new( )
-
{
-
char cpu_load[1024] = {0x00};
-
int buflen = sizeof(cpu_load);
-
unsigned int total, user, nice, system, idle;
-
-
char cpu[32] = {0x00};
-
char text[512] = {0x00};
-
-
FILE *fp;
-
fp = fopen("/proc/stat", "r");
-
if ( fp == NULL )
-
return -1;
-
-
while (fgets(text, 200, fp))
-
{
-
if (strstr(text, "cpu") == NULL )
-
break;
-
-
sscanf(text, "%s %u %u %u %u", cpu, &user, &nice, &system, &idle);
-
total = (user + nice + system + idle);
-
-
snprintf(cpu_load+strlen(cpu_load), buflen-strlen(cpu_load),
-
"%s %u %u %u %u %u\n",
-
cpu, user, nice, system, idle,total);
-
memset( text, 0x00, sizeof(text) );
-
}
-
fclose(fp);
-
-
FILE *fp_cpu = NULL;
-
fp_cpu = fopen("/tmp/taw_cpu_load_info", "w+");
-
if ( fp_cpu == NULL )
-
return -1;
-
fwrite( cpu_load, 1, strlen( cpu_load), fp_cpu );
-
fclose(fp_cpu);
-
-
return 0;
-
}
-
-
#define TAW_CPU_KERNEL_MAX 16 // 最多支持16核
-
int get_cpu_real( float *cpu_load )
-
{
-
FILE *fp = NULL;
-
char text[512] = {0x00};
-
char cpu[32] = {0x00};
-
int i = 0;
-
unsigned int total_old[TAW_CPU_KERNEL_MAX ] = {0x00};
-
unsigned int total_new[TAW_CPU_KERNEL_MAX ] = {0x00};
-
unsigned int user_old[TAW_CPU_KERNEL_MAX ] = {0x00};
-
unsigned int user_new[TAW_CPU_KERNEL_MAX ] = {0x00};
-
unsigned int nice_old[TAW_CPU_KERNEL_MAX ] = {0x00};
-
unsigned int nice_new[TAW_CPU_KERNEL_MAX ] = {0x00};
-
unsigned int system_old[TAW_CPU_KERNEL_MAX ] = {0x00};
-
unsigned int system_new[TAW_CPU_KERNEL_MAX ] = {0x00};
-
unsigned int idle_old[TAW_CPU_KERNEL_MAX ] = {0x00};
-
unsigned int idle_new[TAW_CPU_KERNEL_MAX ] = {0x00};
-
-
-
// 读取上次cpu信息
-
fp = fopen("/tmp/taw_cpu_load_info", "r");
-
i=0;
-
while ( fp != NULL && fgets(text, 200, fp) )
-
{
-
if (strstr(text, "cpu") == NULL )
-
break;
-
-
sscanf(text, "%s %u %u %u %u %u", cpu, &user_old[i],
-
&nice_old[i], &system_old[i], &idle_old[i], &total_old[i] );
-
i++;
-
}
-
if ( fp != NULL )
-
fclose(fp);
-
-
// 读取新的cpu信息
-
get_cpu_load_new();
-
fp = fopen("/tmp/taw_cpu_load_info", "r");
-
if ( fp == NULL )
-
return -1;
-
-
i = 0;
-
while (fgets(text, 200, fp))
-
{
-
if (strstr(text, "cpu") == NULL )
-
break;
-
-
sscanf(text, "%s %u %u %u %u %u", cpu, &user_new[i], &nice_new[i],
-
&system_new[i], &idle_new[i], &total_new[i] );
-
-
if (total_new[i] == total_old[i])
-
cpu_load[i] = 0.0f;
-
else
-
{
-
cpu_load[i] = ( (user_new[i] + nice_new[i] + system_new[i] ) -
-
(user_old[i] + nice_old[i] + system_old[i] ) ) / (float)( total_new[i] - total_old[i] );
-
}
-
-
i++;
-
}
-
fclose(fp);
-
-
return 0;
-
}
阅读(2210) | 评论(0) | 转发(0) |