1

Lets say, I have a function (or functions) which takes a long time (wall time) to execute, for example:

#include "stdafx.h"
#include <math.h>
#include <windows.h>

void fun()
{
  long sum = 0L;
  for (long long i = 1; i < 10000000; i++){
        sum += log((double)i);
    }
 }

double cputimer()
{
    FILETIME createTime;
    FILETIME exitTime;
    FILETIME kernelTime;
    FILETIME userTime;


    if ( GetProcessTimes( GetCurrentProcess( ),
        &createTime, &exitTime, &kernelTime, &userTime ) != -1 )
    {
        SYSTEMTIME userSystemTime;
        if ( FileTimeToSystemTime( &userTime, &userSystemTime ) != -1 )
            return (double)userSystemTime.wHour * 3600.0 +
            (double)userSystemTime.wMinute * 60.0 +
            (double)userSystemTime.wSecond +
            (double)userSystemTime.wMilliseconds / 1000.0;
    }

}

int _tmain(int argc, _TCHAR* argv[])
{
    double start, stop;

    start = cputimer();
    fun();
    stop = cputimer();


    printf("Time taken: %f [seconds]\n", stop - start);

    return 0;
}

I would like to measure a CPU load of this function and RAM usage that this function call uses. Is that possible? How can I do this? Im interested in Windows and Linux solutions.

Paul R
  • 208,748
  • 37
  • 389
  • 560
Brian Brown
  • 3,873
  • 16
  • 48
  • 79

2 Answers2

2

On POSIX you can try using getrusage in the manner similar to how you check the wall time. Not sure about windows.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
1

There is GetProcessTimes function for windows which can give you the CPU time. Also check the Process Status API

Also there is SIGAR which is platform independent.

On Linux you can try with getrusage

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • But it all will give me memory per process, not per function call, right? – Brian Brown Oct 19 '13 at 19:16
  • 2
    Memory is not allocated to function. You can either compare before and after or you can link with your own alloc. – Michael Krelin - hacker Oct 19 '13 at 19:16
  • @MichaelKrelin-hacker:- Very correct! Was about to write the same :) – Rahul Tripathi Oct 19 '13 at 19:18
  • @Michael Krelin - hacker: OK, I see. What do you mean by 'my own alloc'? – Brian Brown Oct 19 '13 at 19:18
  • Something that replaces c library allocator (or wraps around it). I think it's quite implementation dependent. – Michael Krelin - hacker Oct 19 '13 at 19:19
  • @BrianBrown:- You can use:- % CPU usage = (CPU time) / (# of cores) / (wall time) to get the CPU consumption and to get the # of logical cores in linux you can use this:- sysconf(_SC_NPROCESSORS_ONLN) – Rahul Tripathi Oct 19 '13 at 19:21
  • 1
    @RahulTripathi, which comes down to cpu time since number of cores is too constant and wall time is too variable ;) – Michael Krelin - hacker Oct 19 '13 at 19:25
  • @MichaelKrelin-hacker:- Yes exactly! Good catch! +1:) – Rahul Tripathi Oct 19 '13 at 19:26
  • @RahulTripathi: hmm, so any other way of obtaining cpu load? – Brian Brown Oct 19 '13 at 19:26
  • @BrianBrown, I think you need to understand what do you call cpu load and why you think you want it. – Michael Krelin - hacker Oct 19 '13 at 19:28
  • @BrianBrown:- I agree with @Michael! Does this help:- http://stackoverflow.com/questions/1420426/calculating-cpu-usage-of-a-process-in-linux/1420431#1420431 and http://stackoverflow.com/questions/4450961/computation-of-cpu-percentage-by-a-single-process-in-unix-by-the-top-command? – Rahul Tripathi Oct 19 '13 at 19:29
  • @Michael Krelin - hacker: I would like to measure how my function call influences CPU load: and get a number of % of it - the CPU usage like we get in the, for example, TaskManager in Windows. I would like to check it before and after I call my function. Is that correct? – Brian Brown Oct 19 '13 at 19:31
  • @BrianBrown:- Type man 2 getrusage. ru_idrss is what you need for memory usage. The difference in ru_utime before and after the calculation will give you the CPU time – Rahul Tripathi Oct 19 '13 at 19:33
  • Yes, the metric you want seems to be cpu time. (see my last comment under my answer) – Michael Krelin - hacker Oct 19 '13 at 19:35
  • Hmm, guys, I dont know how to explain it well. One thing I know for sure is that Im not interested in measuring cpu time;) did it before - with `getrusage`. What I really want here is to do something that @RahulTripathi gave in his link: http://stackoverflow.com/questions/4450961/computation-of-cpu-percentage-by-a-single-process-in-unix-by-the-top-command – Brian Brown Oct 19 '13 at 19:37
  • But @BrianBrown, there's no such thing like cpu load of the function. It's about cpu. When you see cpu load of processes shown as percentage it's basically about how much cpu (time) each of them used. Relative to the sum thereof + cpu idle. – Michael Krelin - hacker Oct 19 '13 at 19:43
  • You can set up another process that will check your process "load" and record with timestamps and then correlate it with timestamps of function execution beginning and end. (assuming there's not too much threading involved). – Michael Krelin - hacker Oct 19 '13 at 19:43