diff options
author | Sven Gothel <[email protected]> | 2011-11-08 04:52:40 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2011-11-08 04:52:40 +0100 |
commit | 9f14ad29bf2d652c11328479ccb11030408c0543 (patch) | |
tree | d5299d48a66bd4458bab7b9228ba206680ec08bb /src/java/com | |
parent | e4b3cbde5bf59521ed8bf1f15f3378a7c14b4391 (diff) |
Platform: Add 'getCurrentSleepJitter()'
Diffstat (limited to 'src/java/com')
-rw-r--r-- | src/java/com/jogamp/common/os/Platform.java | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/java/com/jogamp/common/os/Platform.java b/src/java/com/jogamp/common/os/Platform.java index 27d556c..2c1a18e 100644 --- a/src/java/com/jogamp/common/os/Platform.java +++ b/src/java/com/jogamp/common/os/Platform.java @@ -35,6 +35,7 @@ import java.nio.IntBuffer; import java.nio.ShortBuffer; import java.security.AccessController; import java.security.PrivilegedAction; +import java.util.concurrent.TimeUnit; import java.util.jar.JarFile; import com.jogamp.common.nio.Buffers; @@ -560,6 +561,30 @@ public class Platform { */ public static MachineDescription getMachineDescription() { return machineDescription; - } + } + + // + // time / jitter + // + + /** + * Returns the estimated sleep jitter value in nanoseconds. + * <p> + * Includes a warm-up path, allowing hotspot to optimize the code. + * </p> + */ + public static synchronized long getCurrentSleepJitter() { + getCurrentSleepJitterImpl(TimeUnit.MILLISECONDS.toNanos(10), 10); // warm-up + return getCurrentSleepJitterImpl(TimeUnit.MILLISECONDS.toNanos(10), 10); + } + private static long getCurrentSleepJitterImpl(final long nsDuration, final int splitInLoops) { + final long nsPeriod = nsDuration / splitInLoops; + final long t0_ns = System.nanoTime(); + for(int i=splitInLoops; i>0; i--) { + try { TimeUnit.NANOSECONDS.sleep(nsPeriod); } catch (InterruptedException e) { } + } + return ( ( System.nanoTime() - t0_ns ) - nsDuration ) / splitInLoops; + } + } |