aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/concurrent/SystemTimer.java
blob: c2fca806a858d3bed3223df3894b9fc588d3e4e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.concurrent;

import java.time.Duration;
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.
 *
 * @author Simon Thoresen Hult
 */
public enum SystemTimer implements Timer {

    INSTANCE;

    private volatile long millis;

    public static long detectHz() {
        Logger log = Logger.getLogger(SystemTimer.class.getName());
        String hzEnv = System.getenv("VESPA_TIMER_HZ");
        long 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;
    }

    public static Duration adjustTimeoutByDetectedHz(Duration timeout) {
        return  adjustTimeoutByDetectedHz(timeout, detectHz());
    }
    public static Duration adjustTimeoutByDetectedHz(Duration timeout, long hz) {
        return  timeout.multipliedBy(1000).dividedBy(hz);
    }

    SystemTimer() {
        long napTime = adjustTimeoutByDetectedHz(Duration.ofMillis(1)).toMillis();
        long creationNanos = System.nanoTime();
        millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - creationNanos);
        Thread thread = new Thread(() -> {
            while (true) {
                millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - creationNanos);
                try {
                    Thread.sleep(napTime);
                } catch (InterruptedException e) {
                    break;
                }
            }
        });
        thread.setDaemon(true);
        thread.setName("vespa-system-timer");
        thread.start();
    }

    @Override
    public long milliTime() {
        return millis;
    }
}