Skip to content

Commit

Permalink
[GR-54568] Record activity in encodeFrameDatas().
Browse files Browse the repository at this point in the history
PullRequest: graal/18034
  • Loading branch information
fniephaus committed Jun 17, 2024
2 parents bb77e9b + 9ec5f11 commit d71f1b8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -416,10 +416,10 @@ private IPData makeEntry(long ip) {
return result;
}

public void encodeAllAndInstall(CodeInfo target, ReferenceAdjuster adjuster) {
public void encodeAllAndInstall(CodeInfo target, ReferenceAdjuster adjuster, Runnable recordActivity) {
encoders.encodeAllAndInstall(target, adjuster);
encodeReferenceMaps();
frameInfoEncoder.encodeAllAndInstall(target);
frameInfoEncoder.encodeAllAndInstall(target, recordActivity);
encodeIPData();

install(target);
Expand Down Expand Up @@ -712,8 +712,7 @@ private void verifyFrame(CompilationResult compilation, BytecodeFrame expectedFr
private void verifyValue(CompilationResult compilation, JavaValue e, ValueInfo actualValue, FrameInfoQueryResult actualFrame, BitSet visitedVirtualObjects) {
JavaValue expectedValue = e;

if (expectedValue instanceof StackLockValue) {
StackLockValue lock = (StackLockValue) expectedValue;
if (expectedValue instanceof StackLockValue lock) {
assert ValueUtil.isIllegal(lock.getSlot()) : actualValue;
assert lock.isEliminated() == actualValue.isEliminatedMonitor() : actualValue;
expectedValue = lock.getOwner();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -34,7 +34,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.graalvm.collections.EconomicMap;
import org.graalvm.collections.Equivalence;
Expand Down Expand Up @@ -317,7 +316,7 @@ void encodeCompressedData(UnsafeArrayTypeWriter encodingBuffer, Encoders encoder
result = -Integer.compare(frameMaxHeight.get(f1), frameMaxHeight.get(f2));
}
return result;
}).collect(Collectors.toList());
}).toList();
for (CompressedFrameData frame : sharedFrames) {
assert !sharedEncodedFrameIndexMap.containsKey(frame) : frame;
sharedEncodedFrameIndexMap.put(frame, encodingBuffer.getBytesWritten());
Expand Down Expand Up @@ -356,7 +355,7 @@ void encodeCompressedData(UnsafeArrayTypeWriter encodingBuffer, Encoders encoder
return sharedEncodedFrameIndexMap.containsKey(frame) && (frameSuccessors == null || frameSuccessors.size() == 1);
});
if (directlyPointToSharedFrame) {
CompressedFrameData frame = slice.get(0);
CompressedFrameData frame = slice.getFirst();
assert sharedEncodedFrameIndexMap.containsKey(frame) : frame;
encodedSliceIndexMap.put(sliceIdx, sharedEncodedFrameIndexMap.get(frame));
} else {
Expand Down Expand Up @@ -552,8 +551,7 @@ private static int countVirtualObjects(DebugInfo debugInfo) {

private static void countVirtualObjects(JavaValue[] values, BitSet visitedVirtualObjects) {
for (JavaValue value : values) {
if (value instanceof VirtualObject) {
VirtualObject virtualObject = (VirtualObject) value;
if (value instanceof VirtualObject virtualObject) {
if (!visitedVirtualObjects.get(virtualObject.getId())) {
visitedVirtualObjects.set(virtualObject.getId());
countVirtualObjects(virtualObject.getValues(), visitedVirtualObjects);
Expand Down Expand Up @@ -623,8 +621,7 @@ private ValueInfo makeValueInfo(FrameData data, JavaKind kind, JavaValue v, bool
ValueInfo result = new ValueInfo();
result.kind = kind;

if (value instanceof StackLockValue) {
StackLockValue lock = (StackLockValue) value;
if (value instanceof StackLockValue lock) {
assert ValueUtil.isIllegal(lock.getSlot()) : value;
if (isDeoptEntry && lock.isEliminated()) {
throw VMError.shouldNotReachHere("Cannot have an eliminated monitor in a deoptimization entry point: value " + value + " in method " +
Expand All @@ -639,8 +636,7 @@ private ValueInfo makeValueInfo(FrameData data, JavaKind kind, JavaValue v, bool
result.type = ValueType.Illegal;
assert result.kind == JavaKind.Illegal : value;

} else if (value instanceof StackSlot) {
StackSlot stackSlot = (StackSlot) value;
} else if (value instanceof StackSlot stackSlot) {
result.type = ValueType.StackSlot;
result.data = stackSlot.getOffset(data.totalFrameSize);
// TODO BS GR-42085 The first check is needed when safe-point sampling is on. Is this
Expand All @@ -658,13 +654,12 @@ private ValueInfo makeValueInfo(FrameData data, JavaKind kind, JavaValue v, bool
result.isCompressedReference = isCompressedReference(register);
ImageSingletons.lookup(Counters.class).registerValueCount.inc();

} else if (calleeSavedRegisters != null && value instanceof RegisterValue) {
} else if (calleeSavedRegisters != null && value instanceof RegisterValue registerValue) {
if (isDeoptEntry) {
throw VMError.shouldNotReachHere("Cannot encode registers in deoptimization entry point: value " + value + " in method " +
data.debugInfo.getBytecodePosition().getMethod().format("%H.%n(%p)"));
}

RegisterValue registerValue = (RegisterValue) value;
Register register = ValueUtil.asRegister(registerValue);
if (!calleeSavedRegisters.calleeSaveable(register)) {
throw VMError.shouldNotReachHere("Register is not calleeSaveable: register " + registerValue + " in method " + data.debugInfo.getBytecodePosition().getMethod().format("%H.%n(%p)"));
Expand All @@ -679,8 +674,7 @@ private ValueInfo makeValueInfo(FrameData data, JavaKind kind, JavaValue v, bool
result.isCompressedReference = registerValue.getPlatformKind().getVectorLength() == 1 && isCompressedReference(registerValue);
ImageSingletons.lookup(Counters.class).registerValueCount.inc();

} else if (value instanceof JavaConstant) {
JavaConstant constant = (JavaConstant) value;
} else if (value instanceof JavaConstant constant) {
result.value = constant;
if (constant instanceof CompressibleConstant) {
result.isCompressedReference = ((CompressibleConstant) constant).isCompressed();
Expand Down Expand Up @@ -830,7 +824,7 @@ private void makeVirtualObject(FrameData data, VirtualObject virtualObject, bool
}
}

data.virtualObjects[id] = valueList.toArray(new ValueInfo[valueList.size()]);
data.virtualObjects[id] = valueList.toArray(new ValueInfo[0]);
ImageSingletons.lookup(Counters.class).virtualObjectsCount.inc();
}

Expand Down Expand Up @@ -887,8 +881,8 @@ private static int computeOffset(ArrayList<ValueInfo> valueInfos, int startIndex
return result;
}

protected void encodeAllAndInstall(CodeInfo target) {
NonmovableArray<Byte> frameInfoEncodings = encodeFrameDatas();
protected void encodeAllAndInstall(CodeInfo target, Runnable recordActivity) {
NonmovableArray<Byte> frameInfoEncodings = encodeFrameDatas(recordActivity);
install(target, frameInfoEncodings);
}

Expand All @@ -905,7 +899,7 @@ private static void afterInstallation(CodeInfo info) {
ConfigurationValues.getObjectLayout().getArrayElementOffset(JavaKind.Object, NonmovableArrays.lengthOf(CodeInfoAccess.getObjectConstants(info))));
}

private NonmovableArray<Byte> encodeFrameDatas() {
private NonmovableArray<Byte> encodeFrameDatas(Runnable recordActivity) {
UnsafeArrayTypeWriter encodingBuffer = UnsafeArrayTypeWriter.create(ByteArrayReader.supportsUnalignedMemoryAccess());
frameMetadata.encodeCompressedData(encodingBuffer, encoders);
for (FrameData data : allDebugInfos) {
Expand All @@ -916,6 +910,7 @@ private NonmovableArray<Byte> encodeFrameDatas() {
data.encodedFrameInfoIndex = frameMetadata.getEncodingOffset(data.frameSliceIndex);
assert frameMetadata.writeFrameVerificationInfo(data, encoders);
}
recordActivity.run();
}
NonmovableArray<Byte> frameInfoEncodings = NonmovableArrays.createByteArray(TypeConversion.asS4(encodingBuffer.getBytesWritten()), NmtCategory.Code);
encodingBuffer.toByteBuffer(NonmovableArrays.asByteBuffer(frameInfoEncodings));
Expand Down Expand Up @@ -988,14 +983,11 @@ private void encodeValues(ValueInfo[] valueInfos, UnsafeArrayTypeWriter encoding
}

protected static long encodePrimitiveConstant(JavaConstant constant) {
switch (constant.getJavaKind()) {
case Float:
return Float.floatToRawIntBits(constant.asFloat());
case Double:
return Double.doubleToRawLongBits(constant.asDouble());
default:
return constant.asLong();
}
return switch (constant.getJavaKind()) {
case Float -> Float.floatToRawIntBits(constant.asFloat());
case Double -> Double.doubleToRawLongBits(constant.asDouble());
default -> constant.asLong();
};
}

private static int encodeFlags(ValueType type, JavaKind kind, boolean isCompressedReference, boolean isEliminatedMonitor) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -268,7 +268,9 @@ private void patchDirectObjectConstants(ObjectConstantsHolder objectConstants, C
private void createCodeChunkInfos(CodeInfo runtimeMethodInfo, ReferenceAdjuster adjuster) {
CodeInfoEncoder codeInfoEncoder = new CodeInfoEncoder(new RuntimeFrameInfoCustomization(), new CodeInfoEncoder.Encoders(false, null));
codeInfoEncoder.addMethod(method, compilation, 0, compilation.getTargetCodeSize());
codeInfoEncoder.encodeAllAndInstall(runtimeMethodInfo, adjuster);
Runnable noop = () -> {
};
codeInfoEncoder.encodeAllAndInstall(runtimeMethodInfo, adjuster, noop);

assert !adjuster.isFinished() || CodeInfoEncoder.verifyMethod(method, compilation, 0, compilation.getTargetCodeSize(), runtimeMethodInfo, FrameInfoDecoder.SubstrateConstantAccess);
assert !adjuster.isFinished() || codeInfoEncoder.verifyFrameInfo(runtimeMethodInfo);
Expand All @@ -285,12 +287,10 @@ private int patchData(Map<Integer, NativeImagePatcher> patcher, ObjectConstantsH
boolean noPriorMatch = patchedOffsets.add(dataPatch.pcOffset);
VMError.guarantee(noPriorMatch, "Patching same offset twice.");
patchesHandled++;
if (dataPatch.reference instanceof DataSectionReference) {
DataSectionReference ref = (DataSectionReference) dataPatch.reference;
if (dataPatch.reference instanceof DataSectionReference ref) {
int pcDisplacement = dataOffset + ref.getOffset() - dataPatch.pcOffset;
patch.patchCode(code.rawValue(), pcDisplacement, compiledBytes);
} else if (dataPatch.reference instanceof ConstantReference) {
ConstantReference ref = (ConstantReference) dataPatch.reference;
} else if (dataPatch.reference instanceof ConstantReference ref) {
SubstrateObjectConstant refConst = (SubstrateObjectConstant) ref.getConstant();
objectConstants.add(patch.getOffset(), patch.getLength(), refConst);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -73,9 +73,9 @@
import com.oracle.svm.core.code.CodeInfoQueryResult;
import com.oracle.svm.core.code.CodeInfoTable;
import com.oracle.svm.core.code.FrameInfoDecoder;
import com.oracle.svm.core.code.FrameInfoDecoder.ConstantAccess;
import com.oracle.svm.core.code.FrameInfoEncoder;
import com.oracle.svm.core.code.FrameInfoQueryResult;
import com.oracle.svm.core.code.FrameInfoDecoder.ConstantAccess;
import com.oracle.svm.core.code.ImageCodeInfo.HostedImageCodeInfo;
import com.oracle.svm.core.config.ConfigurationValues;
import com.oracle.svm.core.configure.ConditionalRuntimeValue;
Expand Down Expand Up @@ -187,11 +187,11 @@ public void setCodeAreaSize(int codeAreaSize) {
}

public Pair<HostedMethod, CompilationResult> getFirstCompilation() {
return orderedCompilations.get(0);
return orderedCompilations.getFirst();
}

public Pair<HostedMethod, CompilationResult> getLastCompilation() {
return orderedCompilations.get(orderedCompilations.size() - 1);
return orderedCompilations.getLast();
}

protected List<Pair<HostedMethod, CompilationResult>> computeCompilationOrder(Map<HostedMethod, CompilationResult> compilationMap) {
Expand Down Expand Up @@ -268,9 +268,7 @@ public Map<Constant, Object> initAndGetEmbeddedConstants() {

public void addConstantsToHeap() {
VMError.guarantee(!embeddedConstants.isEmpty(), "Embedded constants should already be computed.");
embeddedConstants.forEach((constant, reason) -> {
addConstantToHeap(constant, reason instanceof BytecodePosition position ? position.getMethod().getName() : reason);
});
embeddedConstants.forEach((constant, reason) -> addConstantToHeap(constant, reason instanceof BytecodePosition position ? position.getMethod().getName() : reason));
}

private void addConstantToHeap(Constant constant, Object reason) {
Expand Down Expand Up @@ -425,7 +423,7 @@ protected void buildRuntimeMetadata(DebugContext debug, SnippetReflectionProvide
HostedType declaringType = hUniverse.lookup(analysisMethod.getDeclaringClass());
String name = analysisMethod.getName();
HostedType[] parameterTypes = analysisMethod.getSignature().toParameterList(null).stream() //
.map(aType -> hUniverse.lookup(aType)) //
.map(hUniverse::lookup) //
.toArray(HostedType[]::new);
int modifiers = analysisMethod.getModifiers();
HostedType returnType = hUniverse.lookup(analysisMethod.getSignature().getReturnType());
Expand Down Expand Up @@ -487,7 +485,7 @@ protected void buildRuntimeMetadata(DebugContext debug, SnippetReflectionProvide
System.out.println("encoded during call entry points ; " + frameInfoCustomization.numDuringCallEntryPoints);
}

HostedImageCodeInfo imageCodeInfo = installCodeInfo(snippetReflection, firstMethod, codeSize, codeInfoEncoder, runtimeMetadataEncoder);
HostedImageCodeInfo imageCodeInfo = installCodeInfo(snippetReflection, firstMethod, codeSize, codeInfoEncoder, runtimeMetadataEncoder, watchdog);

if (ImageSingletons.contains(CallStackFrameMethodInfo.class)) {
ImageSingletons.lookup(CallStackFrameMethodInfo.class).initialize(encoders, hMetaAccess);
Expand All @@ -513,9 +511,9 @@ protected void buildRuntimeMetadata(DebugContext debug, SnippetReflectionProvide
}

protected HostedImageCodeInfo installCodeInfo(SnippetReflectionProvider snippetReflection, CFunctionPointer firstMethod, UnsignedWord codeSize, CodeInfoEncoder codeInfoEncoder,
RuntimeMetadataEncoder runtimeMetadataEncoder) {
RuntimeMetadataEncoder runtimeMetadataEncoder, DeadlockWatchdog watchdog) {
HostedImageCodeInfo imageCodeInfo = CodeInfoTable.getImageCodeCache().getHostedImageCodeInfo();
codeInfoEncoder.encodeAllAndInstall(imageCodeInfo, new HostedInstantReferenceAdjuster(snippetReflection));
codeInfoEncoder.encodeAllAndInstall(imageCodeInfo, new HostedInstantReferenceAdjuster(snippetReflection), watchdog::recordActivity);
runtimeMetadataEncoder.encodeAllAndInstall();
imageCodeInfo.setCodeStart(firstMethod);
imageCodeInfo.setCodeSize(codeSize);
Expand Down Expand Up @@ -685,9 +683,7 @@ protected boolean verifyMethods(DebugContext debug, HostedUniverse hUniverse, Co

public void writeConstants(NativeImageHeapWriter writer, RelocatableBuffer buffer) {
ByteBuffer bb = buffer.getByteBuffer();
dataSection.buildDataSection(bb, (position, constant) -> {
writer.writeReference(buffer, position, (JavaConstant) constant, "VMConstant: " + constant);
});
dataSection.buildDataSection(bb, (position, constant) -> writer.writeReference(buffer, position, (JavaConstant) constant, "VMConstant: " + constant));
}

public abstract NativeTextSectionImpl getTextSectionImpl(RelocatableBuffer buffer, ObjectFile objectFile, NativeImageCodeCache codeCache);
Expand All @@ -700,8 +696,7 @@ public Path[] getCCInputFiles(Path tempDirectory, String imageName) {

public void printCompilationResults() {
String reportsPath = SubstrateOptions.reportsPath();
ReportUtils.report("compilation results", reportsPath, "universe_compilation", "txt",
writer -> printCompilationResults(writer));
ReportUtils.report("compilation results", reportsPath, "universe_compilation", "txt", this::printCompilationResults);
}

private void printCompilationResults(PrintWriter writer) {
Expand Down

0 comments on commit d71f1b8

Please sign in to comment.