* process.c (rb_clock_gettime): CLOCK_PROCESS_CPUTIME_ID emulation

using getrusage is implemented.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42640 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2013-08-21 10:18:37 +00:00
Родитель 873dd9bc45
Коммит 167419848f
2 изменённых файлов: 37 добавлений и 3 удалений

Просмотреть файл

@ -1,9 +1,14 @@
Wed Aug 21 19:17:46 2013 Tanaka Akira <akr@fsij.org>
* process.c (rb_clock_gettime): CLOCK_PROCESS_CPUTIME_ID emulation
using getrusage is implemented.
Wed Aug 21 17:34:27 2013 Tanaka Akira <akr@fsij.org>
* 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@atdot.net>
* insns.def: fix regexp's once option behavior.

Просмотреть файл

@ -6687,14 +6687,21 @@ rb_proc_times(VALUE obj)
* 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)
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)
#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));