Linux ,c/c++, web,前端,php,js
分类:
2012-03-19 18:40:03
原文地址:linux c程序获取cpu使用率及内存使用情况 作者:mutes
想获取一下目标机运行时linux系统的硬件占用情况,写了这几个小程序,以后直接用了。 方法就是读取proc下的文件来获取了。 cpu使用率: /proc/stat ,内存使用情况: /proc/meminfo 看程序 : /*************************************************************** * @file: statusinfo.c * * @brief: 从linux系统获取cpu及内存使用情况 * * @version 1.0 * * @author 抹黑 * * @date 2009年3月17日 * ***************************************************************/ typedef struct PACKED //定义一个cpu occupy的结构体 { char name[20]; //定义一个char类型的数组名name有20个元素 unsigned int user; //定义一个无符号的int类型的user unsigned int nice; //定义一个无符号的int类型的nice unsigned int system;//定义一个无符号的int类型的system unsigned int idle; //定义一个无符号的int类型的idle }CPU_OCCUPY; typedef struct PACKED //定义一个mem occupy的结构体 { char name[20]; //定义一个char类型的数组名name有20个元素 unsigned long total; char name2[20]; unsigned long free; }MEM_OCCUPY; get_memoccupy (MEM_OCCUPY *mem) //对无类型get函数含有一个形参结构体类弄的指针O { FILE *fd; int n; char buff[256]; MEM_OCCUPY *m; m=mem; fd = fopen ("/proc/meminfo", "r"); fgets (buff, sizeof(buff), fd); fgets (buff, sizeof(buff), fd); fgets (buff, sizeof(buff), fd); fgets (buff, sizeof(buff), fd); sscanf (buff, "%s %u %s", m->name, &m->total, m->name2); fgets (buff, sizeof(buff), fd); //从fd文件中读取长度为buff的字符串再存到起始地址为buff这个空间里 sscanf (buff, "%s %u", m->name2, &m->free, m->name2); fclose(fd); //关闭文件fd } int cal_cpuoccupy (CPU_OCCUPY *o, CPU_OCCUPY *n) { unsigned long od, nd; unsigned long id, sd; int cpu_use = 0; od = (unsigned long) (o->user + o->nice + o->system +o->idle);//第一次(用户+优先级+系统+空闲)的时间再赋给od nd = (unsigned long) (n->user + n->nice + n->system +n->idle);//第二次(用户+优先级+系统+空闲)的时间再赋给od id = (unsigned long) (n->user - o->user); //用户第一次和第二次的时间之差再赋给id sd = (unsigned long) (n->system - o->system);//系统第一次和第二次的时间之差再赋给sd if((nd-od) != 0) cpu_use = (int)((sd+id)*10000)/(nd-od); //((用户+系统)乖100)除(第一次和第二次的时间差)再赋给g_cpu_used else cpu_use = 0; //printf("cpu: %u\n",cpu_use); return cpu_use; } get_cpuoccupy (CPU_OCCUPY *cpust) //对无类型get函数含有一个形参结构体类弄的指针O { FILE *fd; int n; char buff[256]; CPU_OCCUPY *cpu_occupy; cpu_occupy=cpust; fd = fopen ("/proc/stat", "r"); fgets (buff, sizeof(buff), fd); sscanf (buff, "%s %u %u %u %u", cpu_occupy->name, &cpu_occupy->user, &cpu_occupy->nice,&cpu_occupy->system, &cpu_occupy->idle); fclose(fd); } int main() { CPU_OCCUPY cpu_stat1; CPU_OCCUPY cpu_stat2; MEM_OCCUPY mem_stat; int cpu; //获取内存 get_memoccupy ((MEM_OCCUPY *)&mem_stat); //第一次获取cpu使用情况 get_cpuoccupy((CPU_OCCUPY *)&cpu_stat1); sleep(10); //第二次获取cpu使用情况 get_cpuoccupy((CPU_OCCUPY *)&cpu_stat2); //计算cpu使用率 cpu = cal_cpuoccupy ((CPU_OCCUPY *)&cpu_stat1, (CPU_OCCUPY *)&cpu_stat2); return 0; } 另一参考: http://www.cppblog.com/dyj057/archive/2007/01/31/18221.aspx 在Linux中如果要监视一个进程的运行情况,如查看它的CPU使用效率和内存使用情况,就需要从系统的/proc目录的读取一些系统信息。然后分析得到结果,特别是在嵌入式中的应用程序这个功能就很重要。本文中的代码是从top命令的源代码分析中获得,并做了部分修改,在FC6+GCC4.1调试通过。从这个工程中我也获得一些感悟。 void get_system_info(info) struct system_info * info; { char buffer[ 4096 + 1 ]; int fd, len; char * p; int i; /* get load averages */ { fd = open( " loadavg " , O_RDONLY); len = read(fd, buffer, sizeof (buffer) - 1 ); close(fd); buffer[len] = ' \0 ' ; info -> load_avg[ 0 ] = strtod(buffer, & p); info -> load_avg[ 1 ] = strtod(p, & p); info -> load_avg[ 2 ] = strtod(p, & p); p = skip_token(p); /* skip running/tasks */ p = skip_ws(p); if ( * p) info -> last_pid = atoi(p); else info -> last_pid = - 1 ; } /* get the cpu time info */ { fd = open( " stat " , O_RDONLY); len = read(fd, buffer, sizeof (buffer) - 1 ); close(fd); buffer[len] = ' \0 ' ; p = skip_token(buffer); /* "cpu" */ cp_time[ 0 ] = strtoul(p, & p, 0 ); cp_time[ 1 ] = strtoul(p, & p, 0 ); cp_time[ 2 ] = strtoul(p, & p, 0 ); cp_time[ 3 ] = strtoul(p, & p, 0 ); /* convert cp_time counts to percentages */ percentages( 4 , cpu_states, cp_time, cp_old, cp_diff); } /* get system wide memory usage */ { char * p; fd = open( " meminfo " , O_RDONLY); len = read(fd, buffer, sizeof (buffer) - 1 ); close(fd); buffer[len] = ' \0 ' ; /* be prepared for extra columns to appear be seeking to ends of lines */ p = buffer; p = skip_token(p); memory_stats[ 0 ] = strtoul(p, & p, 10 ); /* total memory */ p = strchr(p, ' \n ' ); p = skip_token(p); memory_stats[ 1 ] = strtoul(p, & p, 10 ); /* free memory */ p = strchr(p, ' \n ' ); p = skip_token(p); memory_stats[ 2 ] = strtoul(p, & p, 10 ); /* buffer memory */ p = strchr(p, ' \n ' ); p = skip_token(p); memory_stats[ 3 ] = strtoul(p, & p, 10 ); /* cached memory */ for (i = 0 ; i < 8 ;i ++ ) { p ++ ; p = strchr(p, ' \n ' ); } p = skip_token(p); memory_stats[ 4 ] = strtoul(p, & p, 10 ); /* total swap */ p = strchr(p, ' \n ' ); p = skip_token(p); memory_stats[ 5 ] = strtoul(p, & p, 10 ); /* free swap */ } /* set arrays and strings */ info -> cpustates = cpu_states; info -> memory = memory_stats; } mytop.zip |