[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ..) returns bogus time



Hi,

I just found the same problem as described here:
http://sources.redhat.com/bugzilla/show_bug.cgi?id=1256

CLOCK_PROCESS_CPUTIME_ID clock returns wall-clock time since process
was started instead of CPU-time used by process.

Since I didn't find it in archives nor bugzilla I'd like to ask,
whenever it is considered to be normal behaviour.
It would also help if anynone could clarify execution time definition:
http://www.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap03.html#tag_03_117
To my understanding, time spent inside sleep() should not be counted.
But it is...

(Current testing, linux-image-2.6.15-1-686, gcc-4.0.2-8, glibc-2.3.5-13)

Thanks in advance,
	ladis

ps: below is a little testcase

#define POSIX_SOURCE 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/resource.h>
#include <errno.h>
#include <unistd.h>

static double timespec_to_double(struct timespec *ts)
{
	return (double)ts->tv_sec + (double)ts->tv_nsec / 1000000000.0;
}

int main(int argc, char *argv[])
{
	int i, j;
	double cpuTime0, cpuTime1;
	struct timespec ts;

	if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) != 0)
		printf("* %s\n", strerror(errno));
	cpuTime0 = timespec_to_double(&ts);

	for (i = 0; i != 5; ++i) {
		sleep(1);
		if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) != 0)
			printf("* %s\n", strerror(errno));
		cpuTime1 = timespec_to_double(&ts);
		printf("Time: %f\n", cpuTime1 - cpuTime0);
	}

	return 0;
}



Reply to: