summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2022-02-17 08:58:49 +0100
committerHenning Baldersheim <balder@yahoo-inc.com>2022-02-17 08:59:17 +0100
commit02055efafa5b24e6ac9a738d8e6a756ee8681526 (patch)
tree950d9a8354f124e534fcd839fcbad50fcad7f79d /vespajlib
parent78494eabeb01bb4f95d86b16c389850efc0fd7de (diff)
Allow control of vespa-system-timer frequency in the range [1...1000] hz with environment variable VESPA_TIMER_HZ.
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/src/main/java/com/yahoo/concurrent/SystemTimer.java25
1 files changed, 23 insertions, 2 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/concurrent/SystemTimer.java b/vespajlib/src/main/java/com/yahoo/concurrent/SystemTimer.java
index 7e32ca25dc4..123625b1533 100644
--- a/vespajlib/src/main/java/com/yahoo/concurrent/SystemTimer.java
+++ b/vespajlib/src/main/java/com/yahoo/concurrent/SystemTimer.java
@@ -2,6 +2,8 @@
package com.yahoo.concurrent;
import java.util.concurrent.TimeUnit;
+import java.util.logging.Level;
+import java.util.logging.Logger;
/**
* This is an implementation of {@link Timer} that is backed by an actual system timer.
@@ -12,9 +14,27 @@ public enum SystemTimer implements Timer {
INSTANCE;
+ private static final Logger log = Logger.getLogger(SystemTimer.class.getName());
+
private volatile long millis;
- private SystemTimer() {
+ private static int detectHz() {
+ String hzEnv = System.getenv("VESPA_TIMER_HZ");
+ int hz = 1000;
+ if ((hzEnv != null) && !hzEnv.isBlank()) {
+ try {
+ hz = Integer.parseInt(hzEnv);
+ } catch (NumberFormatException e) {
+ log.log(Level.WARNING, "Failed parsing VESPA_TIMER_HZ='" + hzEnv + "'", e);
+ }
+ };
+ hz = Math.min(1000, Math.max(1, hz)); // Capping to valid range [1...1000]hz
+ log.fine("vespa-system-timer running at " + hz + "hz. VESPA_TIMER_HZ='" + hzEnv + "'");
+ return hz;
+ }
+
+ SystemTimer() {
+ int napTime = 1000 / detectHz();
millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
Thread thread = new Thread() {
@@ -23,7 +43,7 @@ public enum SystemTimer implements Timer {
while (true) {
millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
try {
- Thread.sleep(1);
+ Thread.sleep(napTime);
} catch (InterruptedException e) {
break;
}
@@ -31,6 +51,7 @@ public enum SystemTimer implements Timer {
}
};
thread.setDaemon(true);
+ thread.setName("vespa-system-timer");
thread.start();
}