From 4b033d2692866f5d5944cd33b7dfa68d3dd4707a Mon Sep 17 00:00:00 2001 From: akr Date: Wed, 21 Aug 2013 11:36:43 +0000 Subject: * process.c (get_clk_tck): Extracted from rb_proc_times. (rb_clock_gettime): times() based CLOCK_PROCESS_CPUTIME_ID emulation is implemented. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42642 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- process.c | 60 +++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 15 deletions(-) (limited to 'process.c') diff --git a/process.c b/process.c index 24d53579b7..8c6935a0b4 100644 --- a/process.c +++ b/process.c @@ -6605,6 +6605,25 @@ p_gid_switch(VALUE obj) #if defined(HAVE_TIMES) +static long +get_clk_tck(void) +{ + long hertz = +#ifdef HAVE__SC_CLK_TCK + (double)sysconf(_SC_CLK_TCK); +#else +#ifndef HZ +# ifdef CLK_TCK +# define HZ CLK_TCK +# else +# define HZ 60 +# endif +#endif /* HZ */ + HZ; +#endif + return hertz; +} + /* * call-seq: * Process.times -> aStructTms @@ -6620,19 +6639,7 @@ p_gid_switch(VALUE obj) VALUE rb_proc_times(VALUE obj) { - const double hertz = -#ifdef HAVE__SC_CLK_TCK - (double)sysconf(_SC_CLK_TCK); -#else -#ifndef HZ -# ifdef CLK_TCK -# define HZ CLK_TCK -# else -# define HZ 60 -# endif -#endif /* HZ */ - HZ; -#endif + const double hertz = get_clk_tck(); struct tms buf; volatile VALUE utime, stime, cutime, sctime; @@ -6697,10 +6704,18 @@ rb_proc_times(VALUE obj) * * Emulations for +CLOCK_PROCESS_CPUTIME_ID+: * [:SUS_GETRUSAGE_SELF_USER_AND_SYSTEM_TIME_CLOCK_PROCESS_CPUTIME_ID] - * Use getrusage with RUSAGE_SELF. - * getrusage is defined by Single Unix Specification. + * Use getrusage() with RUSAGE_SELF. + * getrusage() is defined by Single Unix Specification. * The result is addition of ru_utime and ru_stime. * The resolution is 1 micro second. + * [:POSIX_TIMES_CALLING_PROCESS_USER_AND_SYSTEM_TIME_CLOCK_PROCESS_CPUTIME_ID] + * Use times(). + * times() is defined by POSIX. + * The result is addition of tms_utime and tms_stime. + * tms_cutime and tms_cstime are ignored. + * The resolution is the clock tick. + * "getconf CLK_TCK" command shows the clock ticks per second. + * (The clock ticks per second is defined by HZ macro in older systems.) * * If the given +clock_id+ is not supported, Errno::EINVAL is raised. * @@ -6791,6 +6806,21 @@ rb_clock_gettime(int argc, VALUE *argv) } #endif +#ifdef HAVE_TIMES +#define RUBY_POSIX_TIMES_CALLING_PROCESS_USER_AND_SYSTEM_TIME_CLOCK_PROCESS_CPUTIME_ID \ + ID2SYM(rb_intern("POSIX_TIMES_CALLING_PROCESS_USER_AND_SYSTEM_TIME_CLOCK_PROCESS_CPUTIME_ID")) + if (clk_id == RUBY_POSIX_TIMES_CALLING_PROCESS_USER_AND_SYSTEM_TIME_CLOCK_PROCESS_CPUTIME_ID) { + double ns; + struct tms buf; + if (times(&buf) == (clock_t)-1) + rb_sys_fail("times"); + ns = ((double)buf.tms_utime + buf.tms_stime) * 1e9 / get_clk_tck(); + ts.tv_sec = (time_t)(ns*1e-9); + ts.tv_nsec = ns - ts.tv_sec*1e9; + goto success; + } +#endif + #ifdef __APPLE__ #define RUBY_MACH_ABSOLUTE_TIME_CLOCK_MONOTONIC ID2SYM(rb_intern("MACH_ABSOLUTE_TIME_CLOCK_MONOTONIC")) if (clk_id == RUBY_MACH_ABSOLUTE_TIME_CLOCK_MONOTONIC) { -- cgit v1.2.3