0

For the following code getrusage returning zeros in ru_utime.tv_usec and ru_utime.tv_sec.

Code:

#include "stdlib.h"
#include "stdio.h"
#include "sys/time.h"
#include "sys/resource.h"

int getr_return, who = RUSAGE_SELF;
struct rusage usage;

main()
{
    getr_return = getrusage(who, &usage);
    printf(" getr_return = %d\n", getr_return);

    printf(" time taken in seconds = %.61f\n", usage.ru_utime.tv_sec);
    printf(" time taken in seconds = %.61f\n", usage.ru_utime.tv_usec);


        Some_Mips_consuming_code().


        getr_return = getrusage(who, &usage);
        printf(" getr_return = %d\n", getr_return);

        printf(" time taken in seconds = %.61f\n", usage.ru_utime.tv_sec);
        printf(" time taken in seconds = %.61f\n", usage.ru_utime.tv_usec);

    exit;
 } 

output:

getr_return = 0
time taken in seconds =  0.0000000000000000000000000000000000000000000000000000000000000
time taken in seconds =  0.0000000000000000000000000000000000000000000000000000000000000
getr_return = 0
time taken in seconds =  0.0000000000000000000000000000000000000000000000000000000000000
time taken in seconds =  0.0000000000000000000000000000000000000000000000000000000000000

Compiled code on Linux version 2.6.18-308

Ran executable on ARM board and it's Linux version 3.8.1-2.0

user3370219
  • 1
  • 1
  • 2
  • So what is your expected output? – Yaroslav Shabalin Mar 02 '14 at 12:26
  • Additional information: I have tried putting code inbetween two getrusage() as well, but still got those values as zeroes. getr_return = getrusage(who, &usage); printf(" getr_return = %d\n", getr_return); printf(" time taken in seconds = %.61f\n", usage.ru_utime.tv_sec); printf(" time taken in seconds = %.61f\n", usage.ru_utime.tv_usec); some getr_return = getrusage(who, &usage); printf(" time taken in seconds = %.61f\n", usage.ru_utime.tv_sec); printf(" time taken in seconds = %.61f\n", usage.ru_utime.tv_usec); – user3370219 Mar 02 '14 at 13:19
  • Add a long enough computation. Something taking at least half a second of CPU time. – Basile Starynkevitch Mar 02 '14 at 19:10
  • Yes, I have done that I have added a high MIPS consuming Decoder but still the result is same. – user3370219 Mar 03 '14 at 05:37

2 Answers2

1

The user usage time is calculated between two instants of the logic to find out the time consumed which should have a start and end usage of timeval. Some thing like below sample,

 struct timeval start, end;
 getrusage(RUSAGE_SELF, &usage);
 start = usage.ru_utime;
 /* Code to check the usage consumed */
 getrusage(RUSAGE_SELF, &usage);
 end = usage.ru_utime;

ru_utime & ru_stime are of structures of type timeval. If you look its declaration both members tv_sec & tv_usec are of type long. So change the format specifier when printing to %ld. Note when on success getrusage() returns 0.

Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
0

Time measurement is limited in precision, resolution and accuracy. Read time(7). Don't expect significant measures for computation less than e.g. half a second.

Often, CPU time measurement is done by counting jiffies or timer interrupts (related to HZ if you have one in your kernel).

Since you call getrusage(2) at the beginning of your main, not much computing has happened before it (basically, only pre-main initialization in e.g. crt0.o). So you should expect it to be near zero.

You might try using clock_gettime(2) with CLOCK_REALTIME or CLOCK_PROCESS_CPUTIME_ID.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547