Skip to content

Commit

Permalink
[GR-59714] Remove calls to System.currentTimeMillis().
Browse files Browse the repository at this point in the history
PullRequest: graal/19414
  • Loading branch information
christianhaeubl committed Dec 2, 2024
2 parents 83e6cf0 + cc2d234 commit 645d27a
Show file tree
Hide file tree
Showing 23 changed files with 129 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import jdk.graal.compiler.nodes.graphbuilderconf.GraphBuilderContext;
import jdk.graal.compiler.nodes.graphbuilderconf.InvocationPlugins;
import jdk.graal.compiler.nodes.java.LoadFieldNode;
import jdk.graal.compiler.nodes.java.MethodCallTargetNode;
import jdk.graal.compiler.nodes.memory.MemoryKill;
import jdk.graal.compiler.nodes.memory.MultiMemoryKill;
import jdk.graal.compiler.nodes.memory.SingleMemoryKill;
Expand All @@ -101,6 +102,7 @@
import jdk.graal.compiler.phases.tiers.HighTierContext;
import jdk.graal.compiler.phases.util.Providers;
import jdk.graal.compiler.runtime.RuntimeProvider;
import jdk.graal.compiler.serviceprovider.GraalServices;
import jdk.graal.compiler.test.AddExports;
import jdk.graal.compiler.test.SubprocessUtil;
import jdk.internal.misc.Unsafe;
Expand Down Expand Up @@ -195,8 +197,11 @@ public boolean shouldVerifyFoldableMethods() {
return true;
}

public boolean shouldVerifyCurrentTimeMillis() {
return true;
public void verifyCurrentTimeMillis(MetaAccessProvider meta, MethodCallTargetNode t, ResolvedJavaType declaringClass) {
final ResolvedJavaType services = meta.lookupJavaType(GraalServices.class);
if (!declaringClass.equals(services)) {
throw new VerificationError(t, "Should use System.nanoTime() for measuring elapsed time or GraalServices.milliTimeStamp() for the time since the epoch");
}
}

/**
Expand Down Expand Up @@ -366,9 +371,7 @@ public static void runTest(InvariantsTool tool) {
verifiers.add(foldableMethodsVerifier);
}

if (tool.shouldVerifyCurrentTimeMillis()) {
verifiers.add(new VerifyCurrentTimeMillisUsage());
}
verifiers.add(new VerifyCurrentTimeMillisUsage(tool));

tool.updateVerifiers(verifiers);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package jdk.graal.compiler.core.test;

import jdk.graal.compiler.core.test.CheckGraalInvariants.InvariantsTool;
import jdk.graal.compiler.nodes.StructuredGraph;
import jdk.graal.compiler.nodes.java.MethodCallTargetNode;
import jdk.graal.compiler.nodes.spi.CoreProviders;
Expand All @@ -40,6 +41,12 @@
public class VerifyCurrentTimeMillisUsage extends VerifyPhase<CoreProviders> {
private static final String CURRENT_TIME_MILLIS_NAME = "currentTimeMillis";

private final InvariantsTool tool;

public VerifyCurrentTimeMillisUsage(InvariantsTool tool) {
this.tool = tool;
}

@Override
protected void verify(StructuredGraph graph, CoreProviders context) {
final ResolvedJavaType systemType = context.getMetaAccess().lookupJavaType(System.class);
Expand All @@ -48,11 +55,8 @@ protected void verify(StructuredGraph graph, CoreProviders context) {
if (callee.getDeclaringClass().equals(systemType)) {
String calleeName = callee.getName();
if (calleeName.equals(CURRENT_TIME_MILLIS_NAME)) {
final ResolvedJavaType services = context.getMetaAccess().lookupJavaType(GraalServices.class);
if (graph.method().getDeclaringClass().equals(services)) {
return;
}
throw new VerificationError(t, "Should use System.nanoTime for measuring elapsed time or GraalServices.milliTimeStamp for the time since the epoch");
final ResolvedJavaType declaringClass = graph.method().getDeclaringClass();
tool.verifyCurrentTimeMillis(context.getMetaAccess(), t, declaringClass);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public final class GCImpl implements GC {
private final CollectionPolicy policy;
private boolean completeCollection = false;
private UnsignedWord collectionEpoch = WordFactory.zero();
private long lastWholeHeapExaminedTimeMillis = -1;
private long lastWholeHeapExaminedNanos = -1;

@Platforms(Platform.HOSTED_ONLY.class)
GCImpl() {
Expand Down Expand Up @@ -332,7 +332,7 @@ private boolean doCollectOnce(GCCause cause, long requestingNanoTime, boolean co

doCollectCore(!complete);
if (complete) {
lastWholeHeapExaminedTimeMillis = System.currentTimeMillis();
lastWholeHeapExaminedNanos = System.nanoTime();
}

accounting.afterCollectOnce(completeCollection);
Expand Down Expand Up @@ -446,7 +446,7 @@ private void printHeapSizeChange(String text, UnsignedWord before, UnsignedWord
}

private Log printGCPrefixAndTime() {
long uptimeMs = Isolates.getCurrentUptimeMillis();
long uptimeMs = Isolates.getUptimeMillis();
return Log.log().string("[").rational(uptimeMs, TimeUtils.millisPerSecond, 3).string("s").string("] GC(").unsigned(collectionEpoch).string(") ");
}

Expand Down Expand Up @@ -1145,14 +1145,14 @@ public UnsignedWord getCollectionEpoch() {
}

public long getMillisSinceLastWholeHeapExamined() {
long startMillis;
if (lastWholeHeapExaminedTimeMillis < 0) {
long start;
if (lastWholeHeapExaminedNanos < 0) {
// no full GC has yet been run, use time since the first allocation
startMillis = Isolates.getCurrentStartTimeMillis();
start = Isolates.getStartTimeNanos();
} else {
startMillis = lastWholeHeapExaminedTimeMillis;
start = lastWholeHeapExaminedNanos;
}
return System.currentTimeMillis() - startMillis;
return TimeUtils.millisSinceNanos(start);
}

@Fold
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public long getOpenedTime() {
if (!wasOpened) {
/* If a timer was not opened, pretend it was opened at the start of the VM. */
assert openNanos == 0;
return Isolates.getCurrentStartNanoTime();
return Isolates.getStartTimeNanos();
}
return openNanos;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ protected void park(boolean isAbsolute, long time) {
if (time == 0) {
millis = SynchAPI.INFINITE() & 0xFFFFFFFFL;
} else if (isAbsolute) {
millis = time - System.currentTimeMillis();
millis = time - TimeUtils.currentTimeMillis();
if (millis <= 0) {
/* Already elapsed. */
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.oracle.svm.core.c.CGlobalDataFactory;
import com.oracle.svm.core.c.function.CEntryPointErrors;
import com.oracle.svm.core.os.CommittedMemoryProvider;
import com.oracle.svm.core.util.TimeUtils;
import com.oracle.svm.core.util.VMError;

import jdk.graal.compiler.nodes.NamedLocationIdentity;
Expand Down Expand Up @@ -74,8 +75,8 @@ public class Isolates {
/* Only used if SpawnIsolates is disabled. */
private static final CGlobalData<Pointer> SINGLE_ISOLATE_ALREADY_CREATED = CGlobalDataFactory.createWord();

private static long startTimeMillis;
private static long startNanoTime;
private static long startTimeNanos;
private static long initDoneTimeMillis;
private static long isolateId = -1;

/**
Expand Down Expand Up @@ -107,29 +108,30 @@ public static void assignIsolateId(boolean isFirstIsolate) {
}
}

public static void assignCurrentStartTime() {
assert startTimeMillis == 0 : startTimeMillis;
assert startNanoTime == 0 : startNanoTime;
startTimeMillis = System.currentTimeMillis();
startNanoTime = System.nanoTime();
public static void assignStartTime() {
assert startTimeNanos == 0 : startTimeNanos;
assert initDoneTimeMillis == 0 : initDoneTimeMillis;
startTimeNanos = System.nanoTime();
initDoneTimeMillis = TimeUtils.currentTimeMillis();
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public static long getCurrentStartTimeMillis() {
assert startTimeMillis != 0;
return startTimeMillis;
/** Epoch-based timestamp. If possible, {@link #getStartTimeNanos()} should be used instead. */
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
public static long getInitDoneTimeMillis() {
assert initDoneTimeMillis != 0;
return initDoneTimeMillis;
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public static long getCurrentUptimeMillis() {
assert startTimeMillis != 0;
return System.currentTimeMillis() - startTimeMillis;
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
public static long getUptimeMillis() {
assert startTimeNanos != 0;
return TimeUtils.millisSinceNanos(startTimeNanos);
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public static long getCurrentStartNanoTime() {
assert startNanoTime != 0;
return startNanoTime;
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
public static long getStartTimeNanos() {
assert startTimeNanos != 0;
return startTimeNanos;
}

@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
Expand Down Expand Up @@ -169,7 +171,7 @@ public static int create(WordPointer isolatePointer, IsolateArguments arguments)
return CEntryPointErrors.NO_ERROR;
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
@Uninterruptible(reason = CALLED_FROM_UNINTERRUPTIBLE_CODE, mayBeInlined = true)
public static PointerBase getHeapBase(Isolate isolate) {
return isolate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -915,8 +915,8 @@ public void printDiagnostics(Log log, ErrorContext context, int maxDiagnosticLev

log.string("Page size: ").unsigned(VirtualMemoryProvider.get().getGranularity()).newline();
if (!SubstrateOptions.AsyncSignalSafeDiagnostics.getValue()) {
log.string("VM uptime: ").rational(Isolates.getCurrentUptimeMillis(), TimeUtils.millisPerSecond, 3).string("s").newline();
log.string("Current timestamp: ").unsigned(System.currentTimeMillis()).newline();
log.string("VM uptime: ").rational(Isolates.getUptimeMillis(), TimeUtils.millisPerSecond, 3).string("s").newline();
log.string("Current timestamp: ").unsigned(TimeUtils.currentTimeMillis()).newline();
}

CodeInfo info = CodeInfoTable.getFirstImageCodeInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*/
package com.oracle.svm.core.code;

import jdk.graal.compiler.api.replacements.Fold;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
Expand All @@ -44,6 +43,8 @@
import com.oracle.svm.core.thread.VMThreads;
import com.oracle.svm.core.util.TimeUtils;

import jdk.graal.compiler.api.replacements.Fold;

public class RuntimeCodeInfoHistory {
private static final RingBuffer.Consumer<CodeCacheLogEntry> PRINT_WITH_JAVA_HEAP_DATA = RuntimeCodeInfoHistory::printEntryWithJavaHeapData;
private static final RingBuffer.Consumer<CodeCacheLogEntry> PRINT_WITHOUT_JAVA_HEAP_DATA = RuntimeCodeInfoHistory::printEntryWithoutJavaHeapData;
Expand Down Expand Up @@ -120,7 +121,7 @@ private static void printEntry(Object context, CodeCacheLogEntry entry, boolean
}

private static class CodeCacheLogEntry {
private long timestamp;
private long uptimeMillis;
private String kind;
private String codeName;
private CodeInfo codeInfo;
Expand All @@ -142,7 +143,7 @@ public void setValues(String kind, CodeInfo codeInfo, int codeInfoState, String
assert Heap.getHeap().isInImageHeap(kind);

this.safepointId = Safepoint.Master.singleton().getSafepointId();
this.timestamp = System.currentTimeMillis();
this.uptimeMillis = Isolates.getUptimeMillis();
this.kind = kind;
this.codeInfo = codeInfo;
this.codeInfoState = codeInfoState;
Expand All @@ -162,8 +163,7 @@ public void setValues(String kind, CodeInfo codeInfo, int codeInfoState, String

public void print(Log log, boolean allowJavaHeapAccess) {
if (kind != null) {
long uptime = timestamp - Isolates.getCurrentStartTimeMillis();
log.rational(uptime, TimeUtils.millisPerSecond, 3).string("s - ").string(kind).spaces(1);
log.rational(uptimeMillis, TimeUtils.millisPerSecond, 3).string("s - ").string(kind).spaces(1);
String name = allowJavaHeapAccess ? codeName : null;
CodeInfoAccess.printCodeInfo(log, codeInfo, codeInfoState, name, codeStart, codeEnd, hasInstalledCode, installedCodeAddress, installedCodeEntryPoint);
log.string(", safepointId: ").unsigned(safepointId).newline();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.oracle.svm.core.c.CGlobalData;
import com.oracle.svm.core.c.CGlobalDataFactory;
import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
import com.oracle.svm.core.util.TimeUtils;
import com.oracle.svm.core.util.VMError;

import jdk.graal.compiler.api.replacements.Fold;
Expand Down Expand Up @@ -124,7 +125,7 @@ public static void initialize() {
public int getActiveProcessorCount() {
VMError.guarantee(isContainerized());

long currentMs = System.currentTimeMillis();
long currentMs = TimeUtils.currentTimeMillis();
if (currentMs > activeProcessorCountTimeoutMs) {
cachedActiveProcessorCount = ContainerLibrary.getActiveProcessorCount();
activeProcessorCountTimeoutMs = currentMs + CACHE_MS;
Expand All @@ -142,7 +143,7 @@ public int getCachedActiveProcessorCount() {
public UnsignedWord getPhysicalMemory() {
VMError.guarantee(isContainerized());

long currentMs = System.currentTimeMillis();
long currentMs = TimeUtils.currentTimeMillis();
if (currentMs > physicalMemoryTimeoutMs) {
cachedPhysicalMemorySize = ContainerLibrary.physicalMemory();
physicalMemoryTimeoutMs = currentMs + CACHE_MS;
Expand All @@ -160,7 +161,7 @@ public UnsignedWord getCachedPhysicalMemory() {
public long getMemoryLimitInBytes() {
VMError.guarantee(isContainerized());

long currentMs = System.currentTimeMillis();
long currentMs = TimeUtils.currentTimeMillis();
if (currentMs > memoryLimitInBytesTimeoutMs) {
cachedMemoryLimitInBytes = ContainerLibrary.getMemoryLimitInBytes();
memoryLimitInBytesTimeoutMs = currentMs + CACHE_MS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public VMUptimeDmd() {
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-24+18/src/hotspot/share/services/diagnosticCommand.cpp#L393-L400")
public String execute(DCmdArguments args) throws Throwable {
StringBuilderLog log = new StringBuilderLog();
log.rational(Isolates.getCurrentUptimeMillis(), TimeUtils.millisPerSecond, 3).string(" s").newline();
log.rational(Isolates.getUptimeMillis(), TimeUtils.millisPerSecond, 3).string(" s").newline();
return log.getResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ protected static void writeValueInMaterializedObj(Object materializedObj, Unsign
}

private static void printDeoptimizedFrame(Log log, Pointer sp, DeoptimizedFrame deoptimizedFrame, FrameInfoQueryResult sourceFrameInfo, boolean printOnlyTopFrames) {
log.string("[Deoptimization of frame (").rational(Isolates.getCurrentUptimeMillis(), TimeUtils.millisPerSecond, 3).string("s)").newline();
log.string("[Deoptimization of frame (").rational(Isolates.getUptimeMillis(), TimeUtils.millisPerSecond, 3).string("s)").newline();

SubstrateInstalledCode installedCode = deoptimizedFrame.getSourceInstalledCode();
if (installedCode != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ private static int initializeIsolateInterruptibly1(CEntryPointCreateIsolateParam
boolean firstIsolate = Unsafe.getUnsafe().compareAndSetInt(null, initStateAddr, FirstIsolateInitStates.UNINITIALIZED, FirstIsolateInitStates.IN_PROGRESS);

Isolates.assignIsolateId(firstIsolate);
Isolates.assignCurrentStartTime();
Isolates.assignStartTime();

if (!firstIsolate) {
int state = Unsafe.getUnsafe().getInt(initStateAddr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ private void dumpHeapOnOutOfMemoryError0() {

try {
Log.log().string("Dumping heap to ").string(path).string(" ...").newline();
long start = System.currentTimeMillis();
long start = System.nanoTime();
if (dumpHeap(fd, false)) {
long fileSize = getFileSupport().size(fd);
long elapsedMs = System.currentTimeMillis() - start;
long elapsedMs = TimeUtils.millisSinceNanos(start);
long seconds = elapsedMs / TimeUtils.millisPerSecond;
long ms = elapsedMs % TimeUtils.millisPerSecond;
Log.log().string("Heap dump file created [").signed(fileSize).string(" bytes in ").signed(seconds).character('.').signed(ms).string(" secs]").newline();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import com.oracle.svm.core.thread.VMOperation;
import com.oracle.svm.core.thread.VMThreads;
import com.oracle.svm.core.threadlocal.VMThreadLocalSupport;
import com.oracle.svm.core.util.TimeUtils;
import com.oracle.svm.core.util.VMError;

import jdk.graal.compiler.api.replacements.Fold;
Expand Down Expand Up @@ -504,7 +505,7 @@ private void writeHeader() {
writeUTF8("JAVA PROFILE 1.0.2");
writeByte((byte) 0);
writeInt(wordSize());
writeLong(System.currentTimeMillis());
writeLong(TimeUtils.currentTimeMillis());
}

private void startTopLevelRecord(HProfTopLevelRecord tag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
import java.nio.file.Files;
import java.nio.file.Paths;

import jdk.graal.compiler.api.replacements.Fold;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.ProcessProperties;
import org.graalvm.nativeimage.impl.HeapDumpSupport;

import com.oracle.svm.core.SubstrateOptions;
import com.oracle.svm.core.util.TimeUtils;

import jdk.graal.compiler.api.replacements.Fold;

public abstract class HeapDumping implements HeapDumpSupport {
@Fold
Expand All @@ -58,7 +60,7 @@ public static String getHeapDumpPath(String defaultFilename) {
}

public void dumpHeap(boolean gcBefore) throws IOException {
String suffix = Long.toString(System.currentTimeMillis());
String suffix = Long.toString(TimeUtils.currentTimeMillis());
String defaultFilename = getDefaultHeapDumpFilename(suffix);
dumpHeap(getHeapDumpPath(defaultFilename), gcBefore);
}
Expand Down
Loading

0 comments on commit 645d27a

Please sign in to comment.