[前][次][番号順一覧][スレッド一覧]

ruby-changes:30563

From: akr <ko1@a...>
Date: Wed, 21 Aug 2013 20:36:48 +0900 (JST)
Subject: [ruby-changes:30563] akr:r42642 (trunk): * process.c (get_clk_tck): Extracted from rb_proc_times.

akr	2013-08-21 20:36:43 +0900 (Wed, 21 Aug 2013)

  New Revision: 42642

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42642

  Log:
    * process.c (get_clk_tck): Extracted from rb_proc_times.
      (rb_clock_gettime): times() based CLOCK_PROCESS_CPUTIME_ID emulation
      is implemented.

  Modified files:
    trunk/ChangeLog
    trunk/process.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42641)
+++ ChangeLog	(revision 42642)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Aug 21 20:33:01 2013  Tanaka Akira  <akr@f...>
+
+	* process.c (get_clk_tck): Extracted from rb_proc_times.
+	  (rb_clock_gettime): times() based CLOCK_PROCESS_CPUTIME_ID emulation
+	  is implemented.
+
 Wed Aug 21 19:31:48 2013  Tanaka Akira  <akr@f...>
 
 	* process.c: POSIX_GETTIMEOFDAY_CLOCK_REALTIME is renamed to
Index: process.c
===================================================================
--- process.c	(revision 42641)
+++ process.c	(revision 42642)
@@ -6605,6 +6605,25 @@ p_gid_switch(VALUE obj) https://github.com/ruby/ruby/blob/trunk/process.c#L6605
 
 
 #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) https://github.com/ruby/ruby/blob/trunk/process.c#L6639
 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) https://github.com/ruby/ruby/blob/trunk/process.c#L6704
  *
  *  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.
  *
@@ -6790,6 +6805,21 @@ rb_clock_gettime(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/process.c#L6805
             goto success;
         }
 #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"))

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]