LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   CPU utilisation - IS THIS EQUATION CORRECT (https://www.linuxquestions.org/questions/programming-9/cpu-utilisation-is-this-equation-correct-794221/)

chamila1986 03-09-2010 08:50 AM

CPU utilisation - IS THIS EQUATION CORRECT
 
I wrote a test program to get the CPU utilisation. I hope to extend it to take cpu utilisation on multi core machines. Hear I used the equation
for taking cpu utilisation given at http://ubuntuforums.org/showthread.php?t=148781 of JmSchanck post. But when I match the output of my program against 'top' and 'system monitor' output it seems quite high. Sometimes cpu utilisation goes more than 100%.
So the given equation in that url is correct??? can anyone give me the equation to take CPU utilisation???


Code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) {

    double cpu_details1[4];
    double cpu_details2[4];

    char line_single1[256];
    char line_single2[256];

    while (1) {
        FILE *file = fopen("/proc/stat", "r");
        line_single1[256] = '\0';
        line_single2[256] = '\0';

        if (file != NULL) {
            fgets(line_single1, sizeof line_single1, file);
            fclose(file);
        } else {
            perror("Problem of opening host file :");
        }

        //  strcpy(line_single1, "cpu  115776 184 22367 306320 10571 1226 94 0 0");
        ////  printf("LINE1= %s \n", line_single1);
        char *result1 = NULL;
        result1 = strtok(line_single1, " ");

        int j = 0;
        while (result1 != NULL) {
            ////  printf("result >>  \"%s\"\n", result1);
            result1 = strtok(NULL, " ");
            if (j == 4) {
                break;
            }
            cpu_details1[j] = atof(result1);
            ////    printf("at %i=%f", j, cpu_details1[j]);
            j++;
        }

        // line_single2 = "cpu  115778 184 22373 306582 10571 1226 94 0 0";

        sleep(1);
        FILE *file2 = fopen("/proc/stat", "r");
        if (file2 != NULL) {
            fgets(line_single2, sizeof line_single1, file2);
            fclose(file2);
        } else {
            perror("Problem of opening host file :");
        }

        ////  printf("LINE2= %s \n", line_single2);
        char *result2 = NULL;
        result2 = strtok(line_single2, " ");

        int k = 0;
        while (result2 != NULL) {
            ////  printf("result >>  \"%s\"\n", result2);
            result2 = strtok(NULL, " ");
            if (k == 4) {
                break;
            }
            // if (k != 0) {
            cpu_details2[k] = atof(result2);
            ////  printf("at %i=%f", k, cpu_details2[k]);
            //  }
            k++;
        }
        int inc = 0;
        /***  for (; inc < 4; inc++) {
              printf("%d =%f %f \n", inc, cpu_details1[inc], cpu_details2[inc]);

          }**********/
        double val = (cpu_details2[0] - cpu_details1[0])+(cpu_details2[1] - cpu_details1[1])+(cpu_details2[2] - cpu_details1[2]);
        double gap = (cpu_details2[3] - cpu_details1[3]);
        ////  printf("0=%d 1=%d 2=%d 3=%d", cpu_details2[0] - cpu_details1[0], cpu_details2[1] - cpu_details1[1], cpu_details2[2] - cpu_details1[2], cpu_details2[3] - cpu_details1[3]);
        double usage = val / gap;
        //printf("\n val=%f  gap=%f \n >>>>>>>>>>>>>>>>>>>>>>>>>>> Usage= %f  \n", val, gap, usage * 100);
        printf("\n Usage= %f  \n", usage * 100);
    }
}


theNbomr 03-09-2010 04:46 PM

For each column (you seem to have three more than I was able to identify) compute the jiffy-count during the interval. You seem to be doing this. Then I think you want to sum the jiffies for each column. The idle time is the complement of the busy time, so:
Code:

# Pseudo-code
    totalJiffies = user + nice + system + idle + hwInt + swInt + yourOtherThreeColums
    busyPercent = 100.0 * ( 1.0 - ( idle / totalJiffies ) )

A quick test of this algorithm on a couple of systems shows close agreement with top running at the same time.

--- rod.

chamila1986 03-10-2010 11:05 AM

Code:

# Pseudo-code
    totalJiffies = user + nice + system + idle + hwInt + swInt + yourOtherThreeColums
    busyPercent = 100.0 * ( 1.0 - ( idle / totalJiffies ) )


What is mean by 'idle' in the Pseudo-code sir? How is that calculate sir?

totalJiffies= addition of /proc/stat file's cpu line's values?????? ISN'T IT

theNbomr 03-10-2010 11:37 AM

Each of the columns in the 'cpu ' line represents the number of jiffies used by that state. One of the states (the fourth numeric column) represents the count of idle jiffies. Anything not in the idle state is 'busy'. Your code breaks up the columns into an array where each column is numbered. I chose to name the columns in the pseudo-code to reflect the meaning of each column. The sum of all columns will be the total number of jiffies over the interval. If the interval is repeated and is a constant time, the total jiffy count will be (nearly) constant for each interval. The percentage of time spent in the idle state can then be calculated as a fraction of the total time. The complement of this would be the 'busy' time.
--- rod.


All times are GMT -5. The time now is 01:56 AM.