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

ruby-changes:30561

From: akr <ko1@a...>
Date: Wed, 21 Aug 2013 19:18:43 +0900 (JST)
Subject: [ruby-changes:30561] akr:r42640 (trunk): * process.c (rb_clock_gettime): CLOCK_PROCESS_CPUTIME_ID emulation

akr	2013-08-21 19:18:37 +0900 (Wed, 21 Aug 2013)

  New Revision: 42640

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

  Log:
    * process.c (rb_clock_gettime): CLOCK_PROCESS_CPUTIME_ID emulation
      using getrusage is implemented.

  Modified files:
    trunk/ChangeLog
    trunk/process.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42639)
+++ ChangeLog	(revision 42640)
@@ -1,9 +1,14 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Aug 21 19:17:46 2013  Tanaka Akira  <akr@f...>
+
+	* process.c (rb_clock_gettime): CLOCK_PROCESS_CPUTIME_ID emulation
+	  using getrusage is implemented.
+
 Wed Aug 21 17:34:27 2013  Tanaka Akira  <akr@f...>
 
 	* gc.c (getrusage_time): Fallback clock_gettime to getrusage when
 	  clock_gettime fails.
 	  Reported by Eric Saxby.  [ruby-core:56762] [Bug #8805]
-	  
+
 Wed Aug 21 02:32:32 2013  Koichi Sasada  <ko1@a...>
 
 	* insns.def: fix regexp's once option behavior.
Index: process.c
===================================================================
--- process.c	(revision 42639)
+++ process.c	(revision 42640)
@@ -6687,14 +6687,21 @@ rb_proc_times(VALUE obj) https://github.com/ruby/ruby/blob/trunk/process.c#L6687
  *  For example, Process::CLOCK_REALTIME is defined as
  *  +:POSIX_GETTIMEOFDAY_CLOCK_REALTIME+ when clock_gettime() is not available.
  *
- *  Emulations for +:CLOCK_REALTIME+:
+ *  Emulations for +CLOCK_REALTIME+:
  *  [:POSIX_GETTIMEOFDAY_CLOCK_REALTIME] Use gettimeofday().  The resolution is 1 micro second.
  *  [:ISO_C_TIME_CLOCK_REALTIME] Use time().  The resolution is 1 second.
  *
- *  Emulations for +:CLOCK_MONOTONIC+:
+ *  Emulations for +CLOCK_MONOTONIC+:
  *  [:MACH_ABSOLUTE_TIME_CLOCK_MONOTONIC] Use mach_absolute_time(), available on Darwin.
  * 					  The resolution is CPU dependent.
  *
+ *  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.
+ *    The result is addition of ru_utime and ru_stime.
+ *    The resolution is 1 micro second.
+ *
  *  If the given +clock_id+ is not supported, Errno::EINVAL is raised.
  *
  *  +unit+ specifies a type of the return value.
@@ -6764,6 +6771,26 @@ rb_clock_gettime(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/process.c#L6771
             goto success;
         }
 
+#ifdef RUSAGE_SELF
+#define RUBY_SUS_GETRUSAGE_SELF_USER_AND_SYSTEM_TIME_CLOCK_PROCESS_CPUTIME_ID \
+        ID2SYM(rb_intern("SUS_GETRUSAGE_SELF_USER_AND_SYSTEM_TIME_CLOCK_PROCESS_CPUTIME_ID"))
+        if (clk_id == RUBY_SUS_GETRUSAGE_SELF_USER_AND_SYSTEM_TIME_CLOCK_PROCESS_CPUTIME_ID) {
+            struct rusage usage;
+            long usec;
+            ret = getrusage(RUSAGE_SELF, &usage);
+            if (ret != 0)
+                rb_sys_fail("getrusage");
+            ts.tv_sec = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec;
+            usec = usage.ru_utime.tv_usec + usage.ru_stime.tv_usec;
+            if (1000000 <= usec) {
+                ts.tv_sec++;
+                usec -= 1000000;
+            }
+            ts.tv_nsec = usec * 1000;
+            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) {
@@ -7109,6 +7136,8 @@ Init_process(void) https://github.com/ruby/ruby/blob/trunk/process.c#L7136
 #endif
 #ifdef CLOCK_PROCESS_CPUTIME_ID
     rb_define_const(rb_mProcess, "CLOCK_PROCESS_CPUTIME_ID", CLOCKID2NUM(CLOCK_PROCESS_CPUTIME_ID));
+#elif defined(RUBY_SUS_GETRUSAGE_SELF_USER_AND_SYSTEM_TIME_CLOCK_PROCESS_CPUTIME_ID)
+    rb_define_const(rb_mProcess, "CLOCK_PROCESS_CPUTIME_ID", RUBY_SUS_GETRUSAGE_SELF_USER_AND_SYSTEM_TIME_CLOCK_PROCESS_CPUTIME_ID);
 #endif
 #ifdef CLOCK_THREAD_CPUTIME_ID
     rb_define_const(rb_mProcess, "CLOCK_THREAD_CPUTIME_ID", CLOCKID2NUM(CLOCK_THREAD_CPUTIME_ID));

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

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