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

ruby-changes:30565

From: akr <ko1@a...>
Date: Wed, 21 Aug 2013 22:34:04 +0900 (JST)
Subject: [ruby-changes:30565] akr:r42644 (trunk): * process.c (rb_clock_gettime): clock() based CLOCK_PROCESS_CPUTIME_ID

akr	2013-08-21 22:33:59 +0900 (Wed, 21 Aug 2013)

  New Revision: 42644

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

  Log:
    * process.c (rb_clock_gettime): clock() based CLOCK_PROCESS_CPUTIME_ID
      emulation implemented.

  Modified files:
    trunk/ChangeLog
    trunk/process.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42643)
+++ ChangeLog	(revision 42644)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Aug 21 22:30:51 2013  Tanaka Akira  <akr@f...>
+
+	* process.c (rb_clock_gettime): clock() based CLOCK_PROCESS_CPUTIME_ID
+	  emulation implemented.
+
 Wed Aug 21 21:02:37 2013  Tanaka Akira  <akr@f...>
 
 	* process.c (rb_proc_times): Use RB_GC_GUARD to guard objects from GC.
Index: process.c
===================================================================
--- process.c	(revision 42643)
+++ process.c	(revision 42644)
@@ -6713,14 +6713,21 @@ rb_proc_times(VALUE obj) https://github.com/ruby/ruby/blob/trunk/process.c#L6713
  *    getrusage() is defined by Single Unix Specification.
  *    The result is addition of ru_utime and ru_stime.
  *    The resolution is 1 micro second.
+ *  [:ISO_C_CLOCK_CLOCK_PROCESS_CPUTIME_ID]
+ *    Use clock() defined by ISO C.
+ *    The resolution is 1/CLOCKS_PER_SEC.
+ *    CLOCKS_PER_SEC is the C-level macro defined by time.h.
+ *    Single Unix Specification defines CLOCKS_PER_SEC is 1000000.
+ *    Non-Unix systems may define it a different value, though.
+ *    If CLOCKS_PER_SEC is 1000000 as SUS, 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.
+ *    Use times() 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 it is 100, the resolution is 10 milli second.
  *
  *  If the given +clock_id+ is not supported, Errno::EINVAL is raised.
  *
@@ -6811,6 +6818,21 @@ rb_clock_gettime(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/process.c#L6818
         }
 #endif
 
+#define RUBY_ISO_C_CLOCK_CLOCK_PROCESS_CPUTIME_ID \
+        ID2SYM(rb_intern("ISO_C_CLOCK_CLOCK_PROCESS_CPUTIME_ID"))
+        if (clk_id == RUBY_ISO_C_CLOCK_CLOCK_PROCESS_CPUTIME_ID) {
+            double ns;
+            clock_t c;
+            c = clock();
+            errno = 0;
+            if (c == (clock_t)-1)
+                rb_sys_fail("clock");
+            ns = c * (1e9 / CLOCKS_PER_SEC);
+            ts.tv_sec = (time_t)(ns*1e-9);
+            ts.tv_nsec = ns - ts.tv_sec*1e9;
+            goto success;
+        }
+
 #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"))
@@ -6819,7 +6841,7 @@ rb_clock_gettime(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/process.c#L6841
             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();
+            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;

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

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