summaryrefslogtreecommitdiffstats
path: root/src/java/com/jogamp/common
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2023-03-06 22:16:58 +0100
committerSven Gothel <[email protected]>2023-03-06 22:16:58 +0100
commit0836295b52aaad1dce10a31a13cb544802d7de03 (patch)
tree22c3993b50a2783d8ac87084936f24661ae0ff1c /src/java/com/jogamp/common
parenta26445909b3677a8c2de669992a13de2053fa821 (diff)
Fix Clock for performance counter: Add currentTimeNanos() since module startup, retrievable via getMonotonicStartupTime(). (performance)
Settings two long fields in getMonotonicTime() and creating Instant and using Duration for high-frequency counter is too expensive. currentTimeNanos() subtracts the startup time from the current monotonic time and returns the resulting duration in nanoseconds, which lasts for 292 years since module startup. This satisfies performance counter requirements.
Diffstat (limited to 'src/java/com/jogamp/common')
-rw-r--r--src/java/com/jogamp/common/os/Clock.java98
-rw-r--r--src/java/com/jogamp/common/util/PerfCounterCtrl.java14
2 files changed, 85 insertions, 27 deletions
diff --git a/src/java/com/jogamp/common/os/Clock.java b/src/java/com/jogamp/common/os/Clock.java
index 756d1d1..466b3fc 100644
--- a/src/java/com/jogamp/common/os/Clock.java
+++ b/src/java/com/jogamp/common/os/Clock.java
@@ -27,21 +27,35 @@ package com.jogamp.common.os;
import java.time.Instant;
public class Clock {
- private static long t0;
+ private static final Instant t0;
static {
Platform.initSingleton(); // loads native gluegen_rt library
- t0 = currentTimeMillis();
+ {
+ final long[/*2*/] val = { 0, 0 };
+ getMonotonicStartupTimeImpl(val);
+ t0 = Instant.ofEpochSecond(val[0], val[1]);
+ }
}
/**
* Returns current monotonic time since Unix Epoch `00:00:00 UTC on 1970-01-01`.
- *
- * Returned fraction_timespec is passing machine precision and range of the underlying native API.
- *
+ * <p>
+ * Returned timespec is passing machine precision and range of the underlying native API.
+ * </p>
+ * <p>
* Monotonic time shall be used for high-performance measurements of durations,
* since the underlying OS shall support fast calls.
- *
- * @see getWallClockTime()
+ * </p>
+ * <p>
+ * Note that {@link #currentTimeNanos()} and {@link #getMonotonicNanos()}
+ * perform much better than this method, since they only return one long nanosecond value
+ * since module startup. <br/>
+ * The implementation of this method needs to write two long values into an array.
+ * </p>
+ * @see #getMonotonicStartupTime()
+ * @see #currentTimeNanos()
+ * @see #getMonotonicNanos()
+ * @see #getWallClockTime()
*/
public static Instant getMonotonicTime() {
final long[/*2*/] val = { 0, 0 };
@@ -52,13 +66,17 @@ public class Clock {
/**
* Returns current wall-clock real-time since Unix Epoch `00:00:00 UTC on 1970-01-01`.
- *
+ * <p>
* Returned Instant is passing machine precision and range of the underlying native API.
- *
+ * </p>
+ * <p>
* Wall-Clock time shall be used for accurate measurements of the actual time only,
* since the underlying OS unlikely supports fast calls.
- *
- * @see getMonotonicTime()
+ * </p>
+ * @see #getMonotonicStartupTime()
+ * @see #currentTimeNanos()
+ * @see #getMonotonicNanos()
+ * @see #getMonotonicTime()
*/
public static Instant getWallClockTime() {
final long[/*2*/] val = { 0, 0 };
@@ -68,29 +86,63 @@ public class Clock {
private static native void getWallClockTimeImpl(final long[/*2*/] val);
/**
- * Returns current monotonic time in milliseconds.
+ * Returns the monotonic startup time since module startup as used in {@link #currentTimeNanos()} and {@link #getMonotonicNanos()}.
+ * @see #currentTimeNanos()
+ * @see #getMonotonicNanos()
*/
- public static native long currentTimeMillis();
+ public static Instant getMonotonicStartupTime() { return t0; }
+ private static native void getMonotonicStartupTimeImpl(final long[/*2*/] val);
/**
- * Returns current wall-clock system `time of day` in seconds since Unix Epoch
- * `00:00:00 UTC on 1 January 1970`.
+ * Returns current monotonic time in nanoseconds since start of this application.
+ * <p>
+ * Monotonic time shall be used for high-performance measurements of durations,
+ * since the underlying OS shall support fast calls.
+ * </p>
+ * <p>
+ * Since the returned nanoseconds are counted not from Unix Epoch but start of this application,
+ * it lasts for 9'223'372'036 seconds or 292 years using the 64-bit type `long`.
+ * </p>
+ * @see #getMonotonicStartupTime()
+ * @see #getMonotonicNanos()
*/
- public static native long wallClockSeconds();
+ public static native long currentTimeNanos();
/**
- * Returns the startup time in monotonic time in milliseconds of the native module.
+ * Returns the Instant presentation of monotonic {@link #currentTimeNanos()}.
+ * <p>
+ * Monotonic time shall be used for high-performance measurements of durations,
+ * since the underlying OS shall support fast calls.
+ * </p>
+ * <p>
+ * Note that the represented time is not from Unix epoch as claimed,
+ * but monotonic module startup time.
+ * </p>
+ * @see #getMonotonicStartupTime()
+ * @see #currentTimeNanos()
*/
- public static long startupTimeMillis() { return t0; }
+ public static Instant getMonotonicNanos() {
+ final long nanos = currentTimeNanos();
+ return Instant.ofEpochSecond(nanos/1000000000L, nanos%1000000000L);
+ }
/**
- * Returns current elapsed monotonic time in milliseconds since module startup, see {@link #startupTimeMillis()}.
+ * Returns current monotonic time in milliseconds.
+ *
+ * @see #getMonotonicStartupTime()
+ * @see #currentTimeNanos()
+ * @see #getMonotonicNanos()
*/
- public static long elapsedTimeMillis() { return currentTimeMillis() - t0; }
+ public static native long currentTimeMillis();
/**
- * Returns elapsed monotonic time in milliseconds since module startup comparing against the given timestamp, see {@link #startupTimeMillis()}.
+ * Returns current wall-clock system `time of day` in seconds since Unix Epoch
+ * `00:00:00 UTC on 1 January 1970`.
+ *
+ * @see #getWallClockTime()
+ * @see #getMonotonicTime()
+ * @see #currentTimeNanos()
+ * @see #getMonotonicNanos()
*/
- public static long elapsedTimeMillis(final long current_ts) { return current_ts - t0; }
-
+ public static native long wallClockSeconds();
}
diff --git a/src/java/com/jogamp/common/util/PerfCounterCtrl.java b/src/java/com/jogamp/common/util/PerfCounterCtrl.java
index 486c248..30290f8 100644
--- a/src/java/com/jogamp/common/util/PerfCounterCtrl.java
+++ b/src/java/com/jogamp/common/util/PerfCounterCtrl.java
@@ -26,9 +26,15 @@
package com.jogamp.common.util;
import java.io.PrintStream;
-import java.time.Duration;
+import com.jogamp.common.os.Clock;
-/** Simple performance counter controller. */
+/**
+ * Simple performance counter controller.
+ * <p>
+ * Implementation is expected to utilize nanosecond counter since module start,
+ * e.g. {@link Clock#currentTimeNanos()}.
+ * </p>
+ */
public interface PerfCounterCtrl {
/** Enable or disable performance counter. */
void enable(final boolean enable);
@@ -36,8 +42,8 @@ public interface PerfCounterCtrl {
/** Clear performance counter. */
void clear();
- /** Return the total duration, covering all sub-counter. */
- Duration getTotalDuration();
+ /** Return the total duration in nanoseconds, covering all sub-counter. */
+ long getTotalDuration();
/** Print performance counter. */
void print(final PrintStream out);