0

I'm looking for a way to measure CPU usage of a process that finishes in 1 second.

Since it's so fast, top doesn't do it justice. From what I understand, top takes snapshots, so the process could be done in between the updates.

The program is in C++, and I'm running on Linux. If there's some easy code that I can copy and paste into my program that prints out CPU usage at end of main(), that'd work. Or, if there's some profiling tool, I could use that too.

EDIT - It seems like people have some misunderstanding of what I want.

I'm not looking for duration. I know the duration. It's around 1 second. What I want to know is CPU usage. If it's 100%, then it means my CPU was running the entire 1 second.

If it's 50%, then it means the CPU was idle 50% of the time. It's possible that it's waiting for IO the other 50%.

If my program ran for a long time, top is good, since it shows stuff like this

%Cpu(s): 0.6 us, 0.3 sy, 0.0 ni, 99.1 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

This says my cpu is in user space 0.6% of the time, kernel space 0.3% of the time, and simply idle 99.1% of the time.

But - as I said before, top doesn't work for fast processes. So what should I do?

thanks

user3240688
  • 1,188
  • 3
  • 13
  • 34
  • Er...`time ./a.out`? – Wintermute Apr 17 '15 at 22:09
  • have a look at this as well: http://nadeausoftware.com/articles/2012/03/c_c_tip_how_measure_cpu_time_benchmarking – IndieTech Solutions Apr 17 '15 at 22:10
  • possible duplicate of [Benchmarking code - am I doing it right?](http://stackoverflow.com/questions/17860492/benchmarking-code-am-i-doing-it-right) – Gyuri Apr 17 '15 at 22:16
  • Start the timer. Run your code for 1E06 iterations. Stop the timer. Calculate elapsed time. Divide elapsed time by 1E06 for an average execution time. Due to variances beyond your program's control, you won't be able to get any more accurate. Sorry, that was profiling. Same idea for CPU usage: run the program many times, then divide by the number of iterations. – Thomas Matthews Apr 17 '15 at 22:27
  • Isn't cpu usage 100% while it runs? The other comments (and 1 answer) seem to think you are trying to measure duration ... but top does report %cpu. Please clarify what you are trying to measure. – 2785528 Apr 17 '15 at 22:39

1 Answers1

0

Try this:

#include <unistd.h>

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>
#include <boost/date_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>


//This function reads /proc/stat and returns the idle value for each cpu in a vector
std::vector<long long> get_idle() {

    //Virtual file, created by the Linux kernel on demand
    std::ifstream in( "/proc/stat" );

    std::vector<long long> result;

    //This might broke if there are not 8 columns in /proc/stat
    boost::regex reg("cpu(\\d+) (\\d+) (\\d+) (\\d+) (\\d+) (\\d+) (\\d+) (\\d+) (\\d+)");

    std::string line;
    while ( std::getline(in, line) ) {

        boost::smatch match;
        if ( boost::regex_match( line, match, reg ) ) {

            long long idle_time = boost::lexical_cast<long long>(match[5]);

            result.push_back( idle_time );
        }


    }
    return result;
}

//This function returns the avarege load in the next interval_seconds for each cpu in a vector
//get_load() halts this thread for interval_seconds
std::vector<float> get_load(unsigned interval_seconds) {

    boost::posix_time::ptime current_time_1 = boost::date_time::microsec_clock<boost::posix_time::ptime>::universal_time();
    std::vector<long long> idle_time_1 = get_idle();

    sleep(interval_seconds);

    boost::posix_time::ptime current_time_2 = boost::date_time::microsec_clock<boost::posix_time::ptime>::universal_time();
    std::vector<long long> idle_time_2 = get_idle();

    //We have to measure the time, beacuse sleep is not accurate
    const float total_seconds_elpased = float((current_time_2 - current_time_1).total_milliseconds()) / 1000.f;

    std::vector<float> cpu_loads;

    for ( unsigned i = 0; i < idle_time_1.size(); ++i ) {

        //This might get slightly negative, because our time measurment is not accurate
        const float load = 1.f - float(idle_time_2[i] - idle_time_1[i])/(100.f * total_seconds_elpased);
        cpu_loads.push_back( load );

    }
    return cpu_loads;
}

int main() {

    const unsigned measurement_count = 5;
    const unsigned interval_seconds = 5;

    for ( unsigned i = 0; i < measurement_count; ++i ) {

        std::vector<float> cpu_loads = get_load(interval_seconds);
        for ( unsigned i = 0; i < cpu_loads.size(); ++i ) {
            std::cout << "cpu " << i << " : " << cpu_loads[i] * 100.f << "%" << std::endl;
        }
    }


    return 0;
}
IndieTech Solutions
  • 2,527
  • 1
  • 21
  • 36