This is the mail archive of the
libc-alpha@sources.redhat.com
mailing list for the glibc project.
CLOCK_PROCESS_CPUTIME_ID question
- From: Henry Baldursson <henry dot baldursson at gmail dot com>
- To: libc-alpha at sources dot redhat dot com
- Date: Thu, 10 Feb 2005 13:04:52 +0000
- Subject: CLOCK_PROCESS_CPUTIME_ID question
- Reply-to: Henry Baldursson <henry dot baldursson at gmail dot com>
Hi,
The function of the clock represented by CLOCK_PROCESS_CPUTIME_ID
seems sort of vague to me. According to version 3 of the single unix
specification:
"When this value of the type clockid_t is used in a clock() or
timer*() function
call, it is interpreted as the identifier of the CPU-time clock
associated with
the process making the function call."
Does that merely mean the clock gets created at process creation time
and thus reflects the seconds elapsing since the program started, or
does it also mean it gets adjusted to reflect the time the CPU spends
on the running process, ala times(2)'s tms_stime?
Tests reveal that the difference between CLOCK_REALTIME and
CLOCK_PROCESS_CPUTIME_ID is just a matter of some milliseconds of
difference even though the process is made to sleep for some seconds:
henry@U-113:~> /lib/libc.so.6
GNU C Library stable release version 2.3.3 (20040405), by Roland McGrath et al.
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Configured for i686-suse-linux.
Compiled by GNU CC version 3.3.3 (SuSE Linux).
Compiled on a Linux 2.6.4 system on 2004-04-05.
Available extensions:
GNU libio by Per Bothner
crypt add-on version 2.1 by Michael Glad and others
linuxthreads-0.10 by Xavier Leroy
GNU Libidn by Simon Josefsson
NoVersion patch for broken glibc 2.0 binaries
BIND-8.2.3-T5B
libthread_db work sponsored by Alpha Processor Inc
NIS(YP)/NIS+ NSS modules 0.19 by Thorsten Kukuk
Thread-local storage support included.
Report bugs using the `glibcbug' script to <bugs@gnu.org>.
henry@U-113:~> cat timetst.c
#include <time.h>
#include <stdio.h>
int
main(void)
{
struct timespec user1,user2;
struct timespec sys1,sys2;
double user_elapsed;
double sys_elapsed;
clock_gettime(CLOCK_REALTIME, &user1);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &sys1);
sleep(10);
clock_gettime(CLOCK_REALTIME, &user2);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &sys2);
user_elapsed = user2.tv_sec + user2.tv_nsec/1E9;
user_elapsed -= user1.tv_sec + user1.tv_nsec/1E9;
printf("CLOCK_REALTIME: %f\n", user_elapsed);
sys_elapsed = sys2.tv_sec + sys2.tv_nsec/1E9;
sys_elapsed -= sys1.tv_sec + sys1.tv_nsec/1E9;
printf("CLOCK_PROCESS_CPUTIME_ID: %f\n", sys_elapsed);
}
henry@U-113:~> gcc timetst.c -lrt
henry@U-113:~> ./a.out
CLOCK_REALTIME: 10.000837
CLOCK_PROCESS_CPUTIME_ID: 9.999535
henry@U-113:~>
So I'm just wondering if someone can clarify the precise
functionality of this clock_id.
Henry.