[PATCH] CPU-time clocks
The attached patches implement POSIX CPU-time clock support: http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getcpuclockid.html http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_getcpuclockid.html http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_gettime.html http://www.kernel.org/doc/man-pages/online/pages/man3/clock_getcpuclockid.3.html http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_getcpuclockid.3.html http://www.kernel.org/doc/man-pages/online/pages/man3/clock_gettime.3.html There are several things I need to note: 1) Unfortunately newlib's variable naming doesn't currently comply with POSIX. Fixing this will affect RTEMS, so this will need to be worked out on the newlib list. 2) On Linux, clockid_t is a signed int, and glibc uses negative clock IDs for processes and threads (e.g. clock_getcpuclockid returns -6 - pid*8 for the given pid). However, newlib's clockid_t is a unsigned long, so I've chosen a slightly-different numbering scheme which makes the code a bit cleaner. While the scheme is arbitrary and could be changed at any time, that should only be necessary if several more fixed clock IDs are required in the future (a (clockid_t)10 would be mistaken for a CPU-time clock for PID 1); right now POSIX defines only four fixed clock IDs. 3) timer_create(2) needs to be reworked to support clocks other than CLOCK_REALTIME. For now, I'm falling back to the ENOTSUP case allowed by POSIX, but this should be reexamined after reworking timer_create. 4) As noted in the comments, POSIX says that the permissions required to set any particular clock are implementation-defined. On Linux, CPU-time clocks are not settable (IOW no process has such permissions); I have done the same here. 5) The clock_getres(3) code is based on my findings on my system (156001 100ns on W7 x64); I'd appreciate some confirmation for other systems. 6) On Linux, clock_getres(3) does not appear to verify if a (potential) CPU-time clock actually exists. Patches for newlib, winsup/cygwin, and winsup/doc, along with test programs for the new and affected functions, attached. Yaakov 2011-05-12 Yaakov Selkowitz * libc/include/time.h (CLOCK_PROCESS_CPUTIME_ID): Rename from CLOCK_PROCESS_CPUTIME. (CLOCK_THREAD_CPUTIME_ID): Rename from CLOCK_THREAD_CPUTIME. * libc/include/sys/features.h [__CYGWIN__] (_POSIX_CPUTIME): Define. (_POSIX_THREAD_CPUTIME): Define. Index: libc/include/time.h === RCS file: /cvs/src/src/newlib/libc/include/time.h,v retrieving revision 1.19 diff -u -r1.19 time.h --- libc/include/time.h 16 Oct 2008 21:53:58 - 1.19 +++ libc/include/time.h 8 May 2011 23:56:27 - @@ -212,7 +212,7 @@ the identifier of the CPU_time clock associated with the PROCESS making the function call. */ -#define CLOCK_PROCESS_CPUTIME (clockid_t)2 +#define CLOCK_PROCESS_CPUTIME_ID (clockid_t)2 #endif @@ -222,7 +222,7 @@ the identifier of the CPU_time clock associated with the THREAD making the function call. */ -#define CLOCK_THREAD_CPUTIME (clockid_t)3 +#define CLOCK_THREAD_CPUTIME_ID (clockid_t)3 #endif Index: libc/include/sys/features.h === RCS file: /cvs/src/src/newlib/libc/include/sys/features.h,v retrieving revision 1.24 diff -u -r1.24 features.h --- libc/include/sys/features.h 2 May 2011 16:05:06 - 1.24 +++ libc/include/sys/features.h 8 May 2011 23:56:27 - @@ -103,7 +103,7 @@ /* #define _POSIX_BARRIERS -1 */ #define _POSIX_CHOWN_RESTRICTED 1 /* #define _POSIX_CLOCK_SELECTION -1 */ -/* #define _POSIX_CPUTIME -1 */ +#define _POSIX_CPUTIME 200112L #define _POSIX_FSYNC200112L #define _POSIX_IPV6200112L #define _POSIX_JOB_CONTROL 1 @@ -130,7 +130,7 @@ #define _POSIX_SYNCHRONIZED_IO 200112L /* #define _POSIX_THREAD_ATTR_STACKADDR -1 */ #define _POSIX_THREAD_ATTR_STACKSIZE 200112L -/* #define _POSIX_THREAD_CPUTIME -1 */ +#define _POSIX_THREAD_CPUTIME 200112L /* #define _POSIX_THREAD_PRIO_INHERIT -1 */ /* #define _POSIX_THREAD_PRIO_PROTECT -1 */ #define _POSIX_THREAD_PRIORITY_SCHEDULING 200112L 2011-05-15 Yaakov Selkowitz * cygwin.din (clock_getcpuclockid): Export. (pthread_getcpuclockid): Export. * posix.sgml (std-notimpl): Add clock_getcpuclockid and pthread_getcpuclockid from here... (std-susv4): ... to here. (std-notes): Remove limitations of clock_getres and clock_gettime. Note limitation of timer_create to CLOCK_REALTIME. * sysconf.cc (sca): Set _SC_CPUTIME to _POSIX_CPUTIME, and _SC_THREAD_CPUTIME to _POSIX_THREAD_CPUTIME. * thread.cc (pthread_getcpuclockid): New function. * timer.cc (timer_create): Set errno to ENOTSUP for CPU-time clocks. * times.cc (clock_gettime): Handle CLOCK_PROCESS_CPUTIME_ID and CLOCK_THREAD_CPUTIME_ID. (clock_getres): Ditto. (clock_settime): Set errno to EPERM for
Re: [PATCH] CPU-time clocks
Hi Yaakov, On May 15 13:37, Yaakov (Cygwin/X) wrote: > The attached patches implement POSIX CPU-time clock support: I just applied a patch to implement pthread_attr_setstack etc. This affects your patch in that it won't apply cleanly anymore. Would you mind to regenerate your patches relative to CVS HEAD? Thanks, Corinna -- Corinna Vinschen Please, send mails regarding Cygwin to Cygwin Project Co-Leader cygwin AT cygwin DOT com Red Hat
Re: [PATCH] CPU-time clocks
On Sun, 2011-05-15 at 21:11 +0200, Corinna Vinschen wrote: > I just applied a patch to implement pthread_attr_setstack etc. Yes, I just saw that, thank you. > This affects your patch in that it won't apply cleanly anymore. > Would you mind to regenerate your patches relative to CVS HEAD? Attached. Yaakov 2011-05-15 Yaakov Selkowitz * cygwin.din (clock_getcpuclockid): Export. (pthread_getcpuclockid): Export. * posix.sgml (std-notimpl): Add clock_getcpuclockid and pthread_getcpuclockid from here... (std-susv4): ... to here. (std-notes): Remove limitations of clock_getres and clock_gettime. Note limitation of timer_create to CLOCK_REALTIME. * sysconf.cc (sca): Set _SC_CPUTIME to _POSIX_CPUTIME, and _SC_THREAD_CPUTIME to _POSIX_THREAD_CPUTIME. * thread.cc (pthread_getcpuclockid): New function. * timer.cc (timer_create): Set errno to ENOTSUP for CPU-time clocks. * times.cc (clock_gettime): Handle CLOCK_PROCESS_CPUTIME_ID and CLOCK_THREAD_CPUTIME_ID. (clock_getres): Ditto. (clock_settime): Set errno to EPERM for CPU-time clocks. (clock_getcpuclockid): New function. * winsup.h (PID_TO_CLOCKID): New macro. (CLOCKID_TO_PID): New macro. (CLOCKID_IS_PROCESS): New macro. (THREADID_TO_CLOCKID): New macro. (CLOCKID_TO_THREADID): New macro. (CLOCKID_IS_THREAD): New macro. * include/pthread.h (pthread_getcpuclockid): Declare. * include/cygwin/version.h (CYGWIN_VERSION_API_MINOR): Bump. Index: cygwin.din === RCS file: /cvs/src/src/winsup/cygwin/cygwin.din,v retrieving revision 1.241 diff -u -r1.241 cygwin.din --- cygwin.din 15 May 2011 18:49:39 - 1.241 +++ cygwin.din 15 May 2011 19:24:11 - @@ -217,6 +217,7 @@ _clearerr = clearerr SIGFE clock SIGFE _clock = clock SIGFE +clock_getcpuclockid SIGFE clock_getres SIGFE clock_gettime SIGFE clock_setres SIGFE @@ -1212,6 +1213,7 @@ pthread_exit SIGFE pthread_getattr_np SIGFE pthread_getconcurrency SIGFE +pthread_getcpuclockid SIGFE pthread_getschedparam SIGFE pthread_getsequence_np SIGFE pthread_getspecific SIGFE Index: posix.sgml === RCS file: /cvs/src/src/winsup/cygwin/posix.sgml,v retrieving revision 1.63 diff -u -r1.63 posix.sgml --- posix.sgml 15 May 2011 18:49:39 - 1.63 +++ posix.sgml 15 May 2011 19:24:11 - @@ -90,8 +90,9 @@ cimagf clearerr clock -clock_getres (see chapter "Implementation Notes") -clock_gettime (see chapter "Implementation Notes") +clock_getcpuclockid +clock_getres +clock_gettime clock_settime (see chapter "Implementation Notes") clog clogf @@ -564,6 +565,7 @@ pthread_equal pthread_exit pthread_getconcurrency +pthread_getcpuclockid pthread_getschedparam pthread_getspecific pthread_join @@ -836,7 +838,7 @@ tgamma tgammaf time -timer_create +timer_create (see chapter "Implementation Notes") timer_delete timer_gettime timer_settime @@ -1292,7 +1294,6 @@ ceill cexpl cimagl -clock_getcpuclockid clogl conjl copysignl @@ -1386,7 +1387,6 @@ pthread_barrier[...] pthread_condattr_getclock pthread_condattr_setclock -pthread_getcpuclockid pthread_mutexattr_getrobust pthread_mutexattr_setrobust pthread_mutex_consistent @@ -1441,9 +1441,8 @@ related function calls. A real chroot functionality is not supported by Windows however. -clock_getres and clock_gettime -only support CLOCK_REALTIME and CLOCK_MONOTONIC for now. clock_setres -and clock_settime only support CLOCK_REALTIME. +clock_setres, clock_settime, and +timer_create only support CLOCK_REALTIME. BSD file locks created via flock are not propagated to the parent process and sibling processes. The locks are Index: sysconf.cc === RCS file: /cvs/src/src/winsup/cygwin/sysconf.cc,v retrieving revision 1.57 diff -u -r1.57 sysconf.cc --- sysconf.cc 15 May 2011 18:49:39 - 1.57 +++ sysconf.cc 15 May 2011 19:24:12 - @@ -160,7 +160,7 @@ {cons, {c:BC_STRING_MAX}}, /* 60, _SC_BC_STRING_MAX */ {cons, {c:-1L}}, /* 61, _SC_CLOCK_SELECTION */ {nsup, {c:0}}, /* 62, _SC_COLL_WEIGHTS_MAX */ - {cons, {c:-1L}}, /* 63, _SC_CPUTIME */ + {cons, {c:_POSIX_CPUTIME}}, /* 63, _SC_CPUTIME */ {cons, {c:EXPR_NEST_MAX}}, /* 64, _SC_EXPR_NEST_MAX */ {cons, {c:HOST_NAME_MAX}}, /* 65, _SC_HOST_NAME_MAX */ {cons, {c:IOV_MAX}}, /* 66, _SC_IOV_MAX */ @@ -177,7 +177,7 @@ {cons, {c:-1L}}, /* 77, _SC_SPORADIC_SERVER */ {nsup, {c:0}}, /* 78, _SC_SS_REPL_MAX */ {cons, {c:SYMLOOP_MAX}}, /* 79, _SC_SYMLOOP_MAX */ - {cons, {c:-1L}}, /* 80, _SC_THREAD_CPUTIME */ + {cons, {c:_POSIX_THREAD_CPUTIME}}, /* 80, _SC_THREAD_CPUTIME */ {cons, {c:-1L}}, /* 81, _SC_THREAD_SPORADIC_SERVER */ {cons, {c:-1L}}, /* 82, _SC_TIMEOUTS */ {cons, {c: