diff options
author | ulfjack <ulfjack@google.com> | 2018-07-24 00:31:37 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-07-24 00:33:17 -0700 |
commit | 1af71e53903247a061d042d71775d3eb4700848f (patch) | |
tree | 57386b3cf19ae521185805cef39239ee0c826eac /src | |
parent | dd9f60e4d2721cdfd3044a8c8a1bdfd8e444009c (diff) |
Use an interface to convert from nanos to millis since epoch
The conversion approach we were previously using is not stable - the resulting
offsets can differ based on what other things are going on on the same machine
at the same time.
By using an interface and passing it to the relevant places (and only computing
the offset once), we ensure that all conversions are consistent with each other.
PiperOrigin-RevId: 205787309
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/runtime/AbstractCriticalPathComponent.java | 42 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/runtime/BuildSummaryStatsModule.java | 5 |
2 files changed, 29 insertions, 18 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/AbstractCriticalPathComponent.java b/src/main/java/com/google/devtools/build/lib/runtime/AbstractCriticalPathComponent.java index 15fb47eca1..1508be60bf 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/AbstractCriticalPathComponent.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/AbstractCriticalPathComponent.java @@ -28,6 +28,25 @@ import javax.annotation.Nullable; */ @ThreadCompatible public class AbstractCriticalPathComponent<C extends AbstractCriticalPathComponent<C>> { + /** + * Converts from nanos to millis since the epoch. In particular, note that {@link System#nanoTime} + * does not specify any particular time reference but only notes that returned values are only + * meaningful when taking in relation to each other. + */ + public interface NanosToEpochConverter { + /** Converts from nanos to millis since the epoch. */ + long toEpoch(long timeNanos); + } + + /** + * Creates a {@link NanosToEpochConverter} from clock by taking the current time in millis and the + * current time in nanos to compute the appropriate offset. + */ + public static NanosToEpochConverter fromClock(Clock clock) { + long nowInMillis = clock.currentTimeMillis(); + long nowInNanos = clock.nanoTime(); + return (startNanos) -> nowInMillis - TimeUnit.NANOSECONDS.toMillis((nowInNanos - startNanos)); + } // These two fields are values of BlazeClock.nanoTime() at the relevant points in time. private long startNanos; @@ -113,12 +132,16 @@ public class AbstractCriticalPathComponent<C extends AbstractCriticalPathCompone } } - public long getElapsedTimeMillis() { - return TimeUnit.NANOSECONDS.toMillis(getElapsedTimeNanos()); + public long getStartTimeNanos() { + return startNanos; } - long getStartNanos() { - return startNanos; + public long getStartTimeMillisSinceEpoch(NanosToEpochConverter converter) { + return converter.toEpoch(startNanos); + } + + public Duration getElapsedTime() { + return Duration.ofNanos(getElapsedTimeNanos()); } long getElapsedTimeNanos() { @@ -176,16 +199,5 @@ public class AbstractCriticalPathComponent<C extends AbstractCriticalPathCompone } return currentTime + getActionString(); } - - /** - * When {@code clock} is the same {@link Clock} that was used for computing - * {@link #relativeStartNanos}, it returns the wall time since epoch representing when - * the action was started. - */ - public long getStartWallTimeMillis(Clock clock) { - long millis = clock.currentTimeMillis(); - long nanoElapsed = clock.nanoTime(); - return millis - TimeUnit.NANOSECONDS.toMillis((nanoElapsed - startNanos)); - } } diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BuildSummaryStatsModule.java b/src/main/java/com/google/devtools/build/lib/runtime/BuildSummaryStatsModule.java index 86f11476ee..85c1a47b87 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/BuildSummaryStatsModule.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/BuildSummaryStatsModule.java @@ -33,7 +33,6 @@ import com.google.devtools.build.lib.profiler.ProfilerTask; import com.google.devtools.build.lib.profiler.SilentCloseable; import com.google.devtools.build.lib.util.Pair; import com.google.protobuf.ByteString; -import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.logging.Logger; @@ -119,8 +118,8 @@ public class BuildSummaryStatsModule extends BlazeModule { for (SimpleCriticalPathComponent stat : criticalPath.components().reverse()) { Profiler.instance() .logSimpleTaskDuration( - stat.getStartNanos(), - Duration.ofNanos(stat.getElapsedTimeNanos()), + stat.getStartTimeNanos(), + stat.getElapsedTime(), ProfilerTask.CRITICAL_PATH_COMPONENT, stat.prettyPrintAction()); } |