diff --git a/NEWS b/NEWS index 5bfe01b3a0..3f9f620850 100644 --- a/NEWS +++ b/NEWS @@ -59,6 +59,10 @@ with all sufficient information, see the ChangeLog file or Redmine 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 diff --git a/process.c b/process.c index c8964dff69..67f51db847 100644 --- a/process.c +++ b/process.c @@ -6887,15 +6887,26 @@ get_clk_tck(void) 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);