0

I have tried using this program (adapted from this SO answer):

// First segment (down to L49) is from SO https://stackoverflow.com/a/26930298/1876983
#include "stdlib.h"
#include "stdio.h"
#include "string.h"

using namespace std;

static unsigned long long lastTotalUser, lastTotalUserLow, lastTotalSys, lastTotalIdle;

void init(){
    FILE* file = fopen("/proc/stat", "r");
    fscanf(file, "cpu %llu %llu %llu %llu", &lastTotalUser, &lastTotalUserLow,
        &lastTotalSys, &lastTotalIdle);
    fclose(file);
}

double getCurrentValue(){
    double percent;
    FILE* file;
    unsigned long long totalUser, totalUserLow, totalSys, totalIdle, total;

    file = fopen("/proc/stat", "r");
    fscanf(file, "cpu %llu %llu %llu %llu", &totalUser, &totalUserLow,
        &totalSys, &totalIdle);
    fclose(file);

    if (totalUser < lastTotalUser || totalUserLow < lastTotalUserLow ||
        totalSys < lastTotalSys || totalIdle < lastTotalIdle){
        //Overflow detection. Just skip this value.
        percent = -1.0;
    }
    else{
        total = (totalUser - lastTotalUser) + (totalUserLow - lastTotalUserLow) +
            (totalSys - lastTotalSys);
        percent = total;
        total += (totalIdle - lastTotalIdle);
        percent /= total;
        percent *= 100;
    }

    lastTotalUser = totalUser;
    lastTotalUserLow = totalUserLow;
    lastTotalSys = totalSys;
    lastTotalIdle = totalIdle;

    return percent;
}

int main() {
    double CPU = getCurrentValue();
    printf("%.2f", CPU);

    return 0;
}

To determine current total CPU usage on Linux, but it seems to be determining average total CPU usage. At the moment the KDE CPU monitor is telling me my CPU usage is just 17% and this program tells me it is 60.19%. Now earlier today I was compiling a heap of software using the Portage package manager so the 60.19% seems to be more accurately represent my average CPU usage since I turned this PC on. This CPU usage figure has been going down since I stopped compiling software, further supporting the idea this is the average CPU usage, not the current CPU usage.

Community
  • 1
  • 1
Josh Pinto
  • 1,453
  • 4
  • 20
  • 37

1 Answers1

0

One way could be to use exec function in C++ to execute any relevant Linux command to get these values. You can execute Linux commands like top or uptime with > operator.
For example: uptime > output.txt and then can later read output.txt file.

sv_jan5
  • 1,543
  • 16
  • 42
  • And what command would I use to determine CPU usage? If you don't tell me that I can't see how you can expect me to accept this answer. – Josh Pinto May 21 '17 at 04:23
  • That is quite possible right?. One way could be to use `mpstat | awk '$3 ~ /CPU/ { for(i=1;i<=NF;i++) { if ($i ~ /%idle/) field=i } } $3 ~ /all/ { print 100 - $field }'` . Or you can refer to this link to http://stackoverflow.com/questions/9229333/how-to-get-overall-cpu-usage-e-g-57-on-linux – sv_jan5 May 21 '17 at 04:32
  • That command returns nothing for me; while `grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'` and `top -bn1 | grep "Cpu(s)" | \ sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | \ awk '{print 100 - $1"%"}'` both give ~58% which is now what the C++ program is telling me, making them seem like average CPU usage. – Josh Pinto May 21 '17 at 04:35
  • I just gave you a possible way to use several different things. If you know how to get cpu utilization using some other way like python etc. then you can execute that from c++ and store its output in a file and then can read it. – sv_jan5 May 21 '17 at 05:31
  • @BrentonHorne Was it helpful? – sv_jan5 May 21 '17 at 13:02