ruby-changes:46820
From: k0kubun <ko1@a...>
Date: Sun, 28 May 2017 10:48:14 +0900 (JST)
Subject: [ruby-changes:46820] k0kubun:r58935 (trunk): process.c: Use getrusage(2) in Process.times
k0kubun 2017-05-28 10:48:11 +0900 (Sun, 28 May 2017) New Revision: 58935 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58935 Log: process.c: Use getrusage(2) in Process.times if getrusage(2) is available, to improve precision of Process.times and its user like lib/benchmark.rb. On macOS, since getrusage(2) has better precision than times(3), they are much improved like: * Before Process.times => #<struct Process::Tms utime=0.56, stime=0.35, cutime=0.04, cstime=0.03> puts Benchmark.measure { "a" * 1_000_000_000 } 0.340000 0.310000 0.650000 ( 0.674025) * After Process.times => #<struct Process::Tms utime=0.561899, stime=0.35076, cutime=0.046483, cstime=0.038929> puts Benchmark.measure { "a" * 1_000_000_000 } 0.343223 0.310037 0.653260 ( 0.674025) On Linux, since struct rusage from getrusage(2) is used instead of struct tms from times(2), they are slightly improved like: * Before Process.times => #<struct Process::Tms utime=0.43, stime=0.11, cutime=0.0, cstime=0.0> puts Benchmark.measure { "a" * 1_000_000_000 } 0.120000 0.040000 0.170000 ( 0.171621) * After Process.times => #<struct Process::Tms utime=0.432, stime=0.116, cutime=0.0, cstime=0.0> puts Benchmark.measure { "a" * 1_000_000_000 } 0.124000 0.048000 0.172000 ( 0.171621) [ruby-dev:49471] [Feature #11952] Modified files: trunk/NEWS trunk/process.c Index: process.c =================================================================== --- process.c (revision 58934) +++ process.c (revision 58935) @@ -6887,15 +6887,26 @@ get_clk_tck(void) https://github.com/ruby/ruby/blob/trunk/process.c#L6887 VALUE rb_proc_times(VALUE obj) { + VALUE utime, stime, cutime, cstime, ret; +#if defined(RUSAGE_SELF) && defined(RUSAGE_CHILDREN) + struct rusage usage_s, usage_c; + + if (getrusage(RUSAGE_SELF, &usage_s) != 0 || getrusage(RUSAGE_CHILDREN, &usage_c) != 0) + rb_sys_fail("getrusage"); + utime = DBL2NUM((double)usage_s.ru_utime.tv_sec + (double)usage_s.ru_utime.tv_usec/1e6); + stime = DBL2NUM((double)usage_s.ru_stime.tv_sec + (double)usage_s.ru_stime.tv_usec/1e6); + cutime = DBL2NUM((double)usage_c.ru_utime.tv_sec + (double)usage_c.ru_utime.tv_usec/1e6); + cstime = DBL2NUM((double)usage_c.ru_stime.tv_sec + (double)usage_c.ru_stime.tv_usec/1e6); +#else const double hertz = get_clk_tck(); struct tms buf; - VALUE utime, stime, cutime, cstime, ret; times(&buf); utime = DBL2NUM(buf.tms_utime / hertz); stime = DBL2NUM(buf.tms_stime / hertz); cutime = DBL2NUM(buf.tms_cutime / hertz); cstime = DBL2NUM(buf.tms_cstime / hertz); +#endif ret = rb_struct_new(rb_cProcessTms, utime, stime, cutime, cstime); RB_GC_GUARD(utime); RB_GC_GUARD(stime); Index: NEWS =================================================================== --- NEWS (revision 58934) +++ NEWS (revision 58935) @@ -59,6 +59,10 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L59 of #coerce. Return nil in #coerce if the coercion is impossible. [Feature #7688] +* Process + + * Precision of Process.times is improved if getrusage(2) exists. [Feature #11952] + * Range * Range#initialize no longer rescue exceptions when comparing begin and end with #<=> and raise a "bad value for range" ArgumentError -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/