JRuby (please use github issues at http://bugs.jruby.org)
  1. JRuby (please use github issues at http://bugs.jruby.org)
  2. JRUBY-5380

Use Hotspot-specific features for calculating CPU time when available

    Details

    • Type: Improvement Improvement
    • Status: Resolved Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: JRuby 1.6RC1
    • Fix Version/s: JRuby 1.7.0.RC1
    • Component/s: Core Classes/Modules
    • Labels:
      None
    • Number of attachments :
      0

      Description

      It turns out there's a special OperatingSystemMXBean on Hotspot that provides a way to get actual full-process CPU time. The patch below utilizes it, but probably would not work on non-Hotspot JVMs unless they license the same code.

      diff --git a/src/org/jruby/RubyProcess.java b/src/org/jruby/RubyProcess.java
      index 4d16468..7382a38 100644
      --- a/src/org/jruby/RubyProcess.java
      +++ b/src/org/jruby/RubyProcess.java
      @@ -30,6 +30,7 @@
       package org.jruby;
       
       import com.kenai.constantine.platform.Errno;
      +import com.sun.management.UnixOperatingSystemMXBean;
       import org.jruby.anno.JRubyClass;
       import org.jruby.anno.JRubyMethod;
       import org.jruby.anno.JRubyModule;
      @@ -919,12 +920,14 @@ public class RubyProcess {
           public static IRubyObject times(ThreadContext context, IRubyObject recv, Block unusedBlock) {
               return times(context.getRuntime());
           }
      +
      +    private static final UnixOperatingSystemMXBean threadBean = (UnixOperatingSystemMXBean)sun.management.ManagementFactory.getOperatingSystemMXBean();
      +    
           public static IRubyObject times(Ruby runtime) {
      -        double currentTime = System.currentTimeMillis() / 1000.0;
      -        double startTime = runtime.getStartTime() / 1000.0;
      +        double userTime = threadBean.getProcessCpuTime() / 1000000000.0;
               RubyFloat zero = runtime.newFloat(0.0);
               return RubyStruct.newStruct(runtime.getTmsStruct(), 
      -                new IRubyObject[] { runtime.newFloat(currentTime - startTime), zero, zero, zero }, 
      +                new IRubyObject[] { runtime.newFloat(userTime), zero, zero, zero },
                       Block.NULL_BLOCK);
           }
      

      It would be great to make this fix and have our CPU times actually reflect something in reality, rather than just using wall clock for everything.

        Activity

        Hide
        Charles Oliver Nutter added a comment -

        Process.times actually uses jnr-posix now, so it's native where possible and wall-clock otherwise.

        Show
        Charles Oliver Nutter added a comment - Process.times actually uses jnr-posix now, so it's native where possible and wall-clock otherwise.

          People

          • Assignee:
            Charles Oliver Nutter
            Reporter:
            Charles Oliver Nutter
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: