Skip to content

Commit 35647e1

Browse files
committed
[GR-63242] Added feature to dump runtime compiled methods on SVM.
PullRequest: graal/20336
2 parents edee1ed + e9d48ba commit 35647e1

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/PosixRawFileOperationSupport.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.File;
2828
import java.nio.ByteOrder;
2929

30+
import com.oracle.svm.core.jdk.SystemPropertiesSupport;
3031
import jdk.graal.compiler.word.Word;
3132
import org.graalvm.nativeimage.ImageSingletons;
3233
import org.graalvm.nativeimage.Platform;
@@ -82,6 +83,11 @@ public RawFileDescriptor create(CCharPointer cPath, FileCreationMode creationMod
8283
return open0(cPath, flags);
8384
}
8485

86+
@Override
87+
public String getTempDirectory() {
88+
return SystemPropertiesSupport.singleton().getInitialProperty("java.io.tmpdir");
89+
}
90+
8591
@Override
8692
public RawFileDescriptor open(File file, FileAccessMode mode) {
8793
String path = file.getPath();

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,14 @@ protected void onValueUpdate(EconomicMap<OptionKey<?>, Object> values, Integer o
11601160
@OptionMigrationMessage("Use the '-o' option instead.")//
11611161
@Option(help = "Directory of the image file to be generated", type = OptionType.User)//
11621162
public static final HostedOptionKey<String> Path = new HostedOptionKey<>(null);
1163+
1164+
/** Use {@link SubstrateOptions#hasDumpRuntimeCompiledMethodsSupport()} instead. */
1165+
@Option(help = "Dump the instructions of runtime compiled methods in temporary files.") //
1166+
public static final RuntimeOptionKey<Boolean> DumpRuntimeCompiledMethods = new RuntimeOptionKey<>(false, key -> {
1167+
if (key.hasBeenSet() && Platform.includedIn(Platform.WINDOWS.class)) {
1168+
throw UserError.invalidOptionValue(key, key.getValue(), "Dumping runtime compiled code is not supported on Windows.");
1169+
}
1170+
});
11631171
}
11641172

11651173
@Fold
@@ -1471,4 +1479,9 @@ private static void validateRelativeCodePointers(HostedOptionKey<Boolean> option
14711479
UserError.guarantee(!PLTGOTConfiguration.isEnabled(), "%s cannot be used together with PLT/GOT.", enabledOption);
14721480
}
14731481
}
1482+
1483+
public static boolean hasDumpRuntimeCompiledMethodsSupport() {
1484+
return !Platform.includedIn(Platform.WINDOWS.class) && ConcealedOptions.DumpRuntimeCompiledMethods.getValue();
1485+
}
1486+
14741487
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/code/RuntimeCodeCache.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,20 @@
2626

2727
import static com.oracle.svm.core.deopt.Deoptimizer.Options.LazyDeoptimization;
2828
import static com.oracle.svm.core.option.RuntimeOptionKey.RuntimeOptionKeyFlag.RelevantForCompilationIsolates;
29+
import static com.oracle.svm.core.os.RawFileOperationSupport.FileAccessMode.WRITE;
30+
import static com.oracle.svm.core.os.RawFileOperationSupport.FileCreationMode.CREATE_OR_REPLACE;
2931
import static com.oracle.svm.core.snippets.KnownIntrinsics.readCallerStackPointer;
3032

33+
import com.oracle.svm.core.SubstrateOptions;
34+
import com.oracle.svm.core.log.Log;
35+
import com.oracle.svm.core.os.RawFileOperationSupport;
3136
import org.graalvm.collections.EconomicMap;
3237
import org.graalvm.nativeimage.CurrentIsolate;
3338
import org.graalvm.nativeimage.IsolateThread;
3439
import org.graalvm.nativeimage.Platform;
3540
import org.graalvm.nativeimage.Platforms;
3641
import org.graalvm.nativeimage.c.function.CodePointer;
42+
import org.graalvm.nativeimage.c.type.CCharPointer;
3743
import org.graalvm.word.Pointer;
3844
import org.graalvm.word.UnsignedWord;
3945

@@ -59,6 +65,8 @@
5965
import jdk.graal.compiler.options.OptionType;
6066
import jdk.graal.compiler.word.Word;
6167

68+
import java.text.SimpleDateFormat;
69+
6270
public class RuntimeCodeCache {
6371

6472
public static class Options {
@@ -161,11 +169,45 @@ private static int binarySearch(NonmovableArray<UntetheredCodeInfo> a, int fromI
161169
return -(low + 1); // key not found.
162170
}
163171

172+
private static RawFileOperationSupport getFileSupport() {
173+
return RawFileOperationSupport.bigEndian();
174+
}
175+
176+
private static void dumpMethod(CodeInfo info) {
177+
String methodName = CodeInfoAccess.getName(info);
178+
int tier = CodeInfoAccess.getTier(info);
179+
String date = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new java.util.Date());
180+
String tmpDirPath = getFileSupport().getTempDirectory();
181+
String filePath = tmpDirPath + "/" + date + "_" + methodName + "_" + tier + ".bin";
182+
183+
RawFileOperationSupport.RawFileDescriptor fd = getFileSupport().create(filePath, CREATE_OR_REPLACE, WRITE);
184+
if (!getFileSupport().isValid(fd)) {
185+
Log.log().string("Failed to dump runtime compiled code: '").string(filePath).string("' could not be created.").newline();
186+
return;
187+
}
188+
189+
try {
190+
CCharPointer codeStart = (CCharPointer) CodeInfoAccess.getCodeStart(info);
191+
192+
UnsignedWord codeSize = CodeInfoAccess.getCodeSize(info);
193+
if (!RawFileOperationSupport.bigEndian().write(fd, (Pointer) codeStart, codeSize)) {
194+
Log.log().string("Dumping method to ").string(filePath).string(" failed").newline();
195+
}
196+
} finally {
197+
getFileSupport().close(fd);
198+
}
199+
}
200+
164201
public void addMethod(CodeInfo info) {
165202
assert VMOperation.isInProgressAtSafepoint() : "invalid state";
166203
InstalledCodeObserverSupport.activateObservers(RuntimeCodeInfoAccess.getCodeObserverHandles(info));
204+
167205
addMethod0(info);
168206
RuntimeCodeInfoHistory.singleton().logAdd(info);
207+
208+
if (SubstrateOptions.hasDumpRuntimeCompiledMethodsSupport()) {
209+
dumpMethod(info);
210+
}
169211
}
170212

171213
@Uninterruptible(reason = "Modifying code tables that are used by the GC")

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/os/RawFileOperationSupport.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ static RawFileOperationSupport nativeByteOrder() {
110110
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
111111
RawFileDescriptor create(CCharPointer path, FileCreationMode creationMode, FileAccessMode accessMode);
112112

113+
/** Returns the path to the platform-specific temporary directory. */
114+
String getTempDirectory();
115+
113116
/**
114117
* Opens a file with the specified {@link FileAccessMode access mode}.
115118
*

0 commit comments

Comments
 (0)