Use Hotspot-specific features for calculating CPU time when available
---------------------------------------------------------------------
Key: JRUBY-5380
URL: http://jira.codehaus.org/browse/JRUBY-5380
Project: JRuby
Issue Type: Improvement
Components: Core Classes/Modules
Affects Versions: JRuby 1.6RC1
Reporter: Charles Oliver Nutter
Fix For: JRuby 1.7
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.
{noformat}
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);
}
{noformat}
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.
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://jira.codehaus.org/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email