3

In section 3.9 of the classic APUE(Advanced Programming in the UNIX Environment), the author measured the user/system time consumed in his sample program which runs against varying buffer size(an I/O read/write program).

The result table goes kinda like(all the time are in the unit of second):

BUFF_SIZE  USER_CPU   SYSTEM_CPU  CLOCK_TIME  LOOPS
1          124.89     161.65      288.64      103316352
...        
512        0.27       0.41        7.03        201789
...

I'm curious about and really wondering how to measure the USER/SYSTEM CPU time for a piece of program?

And in this example, what does the CLOCK TIME mean and how to measure it?

Obviously it isn't simply the sum of user CPU time and system CPU time.

Tuxdude
  • 47,485
  • 15
  • 109
  • 110
snowfox
  • 1,978
  • 1
  • 21
  • 21
  • So to be clear, you want C code, which will get those values for you? – hyde Mar 15 '13 at 07:56
  • What do you really mean? Computing the cycle time into your program, or using command line tool to get the whole cpu time consumed info for a program? You can get cpu info with "time" command line. By the way you should show us the command line tool name the author has used to get this log. – Mr Bonjour Mar 15 '13 at 08:01
  • The author didn't mention how he did the measurement. That's why it provoked my curiosity. And just as you mentioned and answered by Tuxdude, the job could be done by using the command line tool "time". Thanks. – snowfox Mar 15 '13 at 08:34

2 Answers2

4

You could easily measure the running time of a program using the time command under *nix:

$ time myprog

real        0m2.792s
user        0m0.099s
sys         0m0.200s

The real or CLOCK_TIME refers to the wall clock time i.e the time taken from the start of the program to finish and includes even the time slices taken by other processes when the kernel context switches them. It also includes any time, the process is blocked (on I/O events, etc.)

The user or USER_CPU refers to the CPU time spent in the user space, i.e. outside the kernel. Unlike the real time, it refers to only the CPU cycles taken by the particular process.

The sys or SYSTEM_CPU refers to the CPU time spent in the kernel space, (as part of system calls). Again this is only counting the CPU cycles spent in kernel space on behalf of the process and not any time it is blocked.

In the time utility, the user and sys are calculated from either times() or wait() system calls. The real is usually calculated using the time differences in the 2 timestamps gathered using the gettimeofday() system call at the start and end of the program.

One more thing you might want to know is real != user + sys. On a multicore system the user or sys or their sum can quite easily exceed the real time.

Tuxdude
  • 47,485
  • 15
  • 109
  • 110
2

Partial answer:

Well, CLOCK_TIME is same as time shown by a clock, time passed in the so called "real world".

One way to measure that is to use gettimeofday POSIX function, which stores time to caller's struct timeval, containing UNIX seconds field and a microsecond field (actual accuracy is often less). Example for using that in typical benchmark code (ignoring errors etc):

struct timeval tv1, tv2;
gettimeofday(&tv1, NULL);

do_operation_to_measure();

gettimeofday(&tv2, NULL);

// get difference, fix value if microseconds became negative
struct timeval tvdiff = { tv2.tv_sec - tv1.tv_sec, tv2.tv_usec - tv1.tv_usec };
if (tvdiff.tv_usec < 0) { tvdiff.tv_usec += 1000000; tvdiff.tv_sec -= 1; }

// print it
printf("Elapsed time: %ld.%06ld\n", tvdiff.tv_sec, tvdiff.tv_usec);
hyde
  • 60,639
  • 21
  • 115
  • 176