In the last blog post, we talked about how to get the results of executing the command line in Linux, and eventually get CPU usage, but unfortunately it is:
1) via Top-n 1 | The CPU usage obtained by the GREP CPU is not refreshed, and the results of the first execution are only available for each read.
2) may cause a conflict on signal
The new code solves the principle:
Calculate CPU usage by reading data from/proc/stat
#include <stdio.h><stdlib.h><sys/stat.h><sys/types.h> <string.h><fcntl.h><unistd.h><errno.h> <sys/statfs.h><sys/vfs.h>
#define FILEBUFFER_LENGTH 5000
#define EMPTY_STR ""
#define Cpu_part "/proc/stat"
struct Cpu_cost//storing CPU data structure body
{
long int user;
long int nice;
long int system;
long int idle;
long int iowait;
a long int IRQ;
long int softirq;
};
Opens the file specified by filename, from which to read the linenumber line
Return value: Successfully returned 1, failed to return 0
int Get_file_line (char *result,char *filename,int linenumber)
{
FILE *filepointer;
int i=0;
Char Buffer[filebuffer_length];
Char *temp;
memset (buffer, ' n ', filebuffer_length*sizeof (char));
strcpy (BUFFER,EMPTY_STR);
if ((filename==null) | | | (Result==null))
{
return 0;
}
if (! ( Filepointer=fopen (FileName, "RB")))
{return 0;}
while ((!feof (Filepointer)) && (I<linenumber))
{
if (!fgets (Buffer,filebuffer_length,filepointer))
{
return 0;
}
i++;//almost forgot to add this sentence again.
}
/* printf ("\n%d\n", sizeof (*result));
if (strlen (buffer) >sizeof (*result))//Can not write this, although Fgets read a line will be added to the end of ' ", but sizeof (result) results are the size of the type of result itself, So I can't count that. When a static array is passed into a function, it is only known inside the function that it is a pointer
{
return 0;
}*/
if (0!=fclose (Filepointer))
{
return 0;
}
if (0!=strcmp (BUFFER,EMPTY_STR))
{
while (null!= (temp=strstr (buffer, "\ n")))
{
*temp= ' + ';
}
while (null!= (temp=strstr (buffer, "\ r")))
{
*temp= ' + ';
}
strcpy (Result,buffer);
}else
{
strcpy (RESULT,EMPTY_STR);
return 0;
}
return 1;
}
CPU-related data is read from/proc/stat and stored in the structure
int GetCpuInfo2 (Pcpu_cost cpuInfo)
{
Char cpustr[500];
if (null==cpuinfo)
{
printf ("\ngetcpuinfo param null!\n");
return 0;
}
if (1==get_file_line (cpustr,cpu_part,1))
{
SSCANF (Cpustr, "%*s%ld%ld-%ld%ld%ld%ld",& (cpuinfo->user),& (cpuinfo->nice),& (cpuinfo-> System),& (Cpuinfo->idle),& (cpuinfo->iowait),& (CPUINFO->IRQ),& (CPUINFO->SOFTIRQ)); /modified more than one%ld, resulting in an error
return 1;
}
return 0;
}
Calculates CPU usage based on two read CPU data, resulting in a result of between 0~1000 and 1000 representing a utilization rate of 100%
Parameter two: The first read CPU data structure body
Parameter three: The second read CPU data structure body
int CalCpuInfo2 (int *cpuusage,pcpu_cost cpu1,pcpu_cost CPU2)
{
long int total=0,total1=0,total2=0,idle=0,idle1=0,idle2=0,result=0;
if ((null==cpuusage) && (NULL==CPU1) && (NULL==CPU2))
{
printf ("\ncalcpuinfo2 param null!\n");
return 0;
}
Total1=cpu1->user+cpu1->nice+cpu1->system+cpu1->idle+cpu1->iowait+cpu1->irq+cpu1->softirq ;//Total CPU time read for the first time
Total2=cpu2->user+cpu2->nice+cpu2->system+cpu2->idle+cpu2->iowait+cpu2->irq+cpu2->softirq ;//The total CPU time of the second read
if (TOTAL2>TOTAL1)
{
total=total2-total1;//Total time Difference
idle1=cpu1->idle;//idle time for first read
idle2=cpu2->idle;//idle time for second read
idle=idle2-idle1;//Idle time Difference
result=100000* (Total-idle);//Busy time difference
result=result/total;//utilization = busy time/Total time
*cpuusage= (int) result;
return 1;
}
return 0;
}
Interval 0.6s reads CPU data and calculates CPU usage, and returns the percentage of CPU usage
Float Getcpuusage ()
{
Cpu_cost cpu1,cpu2;
int usage_i=0;
float usage_f=0;
if (1==getcpuinfo2 (&CPU1))
{
Usleep (600000);
if (1==getcpuinfo2 (&CPU2))
{
if (1==calcpuinfo2 (&USAGE_I,&CPU1,&CPU2))
{
usage_f= (float) usage_i/1000;
printf ("%d%0.3f\n", usage_i,usage_f);
return usage_f;
}
}
}
return-1;
}
Of course, because it is copied and pasted from a file, the above code may have problems on the header file, and the code is not necessarily accurate comments, to be screened by users themselves.
Linux C Get CPU Utilization (2)