Skip to content

Commit d8ce19c

Browse files
author
Andrija Kolic
committed
[GR-68166] Add ImageHeapMetadataDumpFileName native image build option and NativeImageHeap.dumpMetadata method
PullRequest: graal/21918
2 parents 6ef1b72 + b4f1b23 commit d8ce19c

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,11 @@ public Boolean getValue(OptionValues values) {
13701370
@Option(help = "Create a heap dump and exit.")//
13711371
public static final RuntimeOptionKey<Boolean> DumpHeapAndExit = new RuntimeOptionKey<>(false, Immutable);
13721372

1373+
@Option(help = "Name of a csv file into which image heap metadata should be dumped. " +
1374+
"This csv file will be located in the same directory as the image. " +
1375+
"If this option is an empty string, the metadata will not be dumped.") //
1376+
public static final HostedOptionKey<String> ImageHeapMetadataDumpFileName = new HostedOptionKey<>("");
1377+
13731378
@Option(help = "Print some VM information and exit.")//
13741379
public static final RuntimeOptionKey<Boolean> PrintVMInfoAndExit = new RuntimeOptionKey<>(false, Immutable);
13751380

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImage.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ public void build(String imageName, DebugContext debug) {
561561
// We print the heap statistics after the heap was successfully written because this
562562
// could modify objects that will be part of the image heap.
563563
printHeapStatistics(heap.getLayouter().getPartitions());
564+
heap.dumpMetadata();
564565
}
565566
}
566567

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageHeap.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@
2626

2727
import static com.oracle.svm.core.util.VMError.shouldNotReachHereUnexpectedInput;
2828

29+
import java.io.BufferedWriter;
30+
import java.io.File;
31+
import java.io.FileWriter;
32+
import java.io.IOException;
2933
import java.lang.reflect.Array;
3034
import java.lang.reflect.Modifier;
35+
import java.nio.file.Path;
3136
import java.util.ArrayDeque;
3237
import java.util.Arrays;
3338
import java.util.Collection;
@@ -41,6 +46,7 @@
4146
import java.util.Set;
4247
import java.util.function.Predicate;
4348

49+
import com.oracle.svm.core.option.HostedOptionValues;
4450
import org.graalvm.nativeimage.ImageSingletons;
4551
import org.graalvm.nativeimage.c.function.RelocatedPointer;
4652
import org.graalvm.word.UnsignedWord;
@@ -792,6 +798,35 @@ public ObjectInfo addLateToImageHeap(Object object, Object reason) {
792798
return addToImageHeap(object, (HostedClass) type, getSize(object, type), System.identityHashCode(object), reason);
793799
}
794800

801+
/**
802+
* Dumps metadata for every object in the image heap.
803+
*/
804+
public void dumpMetadata() {
805+
String metadataFileName = SubstrateOptions.ImageHeapMetadataDumpFileName.getValue();
806+
if (metadataFileName == null || metadataFileName.isEmpty()) {
807+
// Do not dump metadata if the file name isn't set
808+
return;
809+
}
810+
811+
Path metadataFilePath = SubstrateOptions.getImagePath(HostedOptionValues.singleton()).resolve(metadataFileName);
812+
File metadataFile = metadataFilePath.toFile();
813+
String metadataDir = metadataFile.getParent();
814+
if (!new File(metadataDir).exists()) {
815+
throw VMError.shouldNotReachHere("Image heap metadata directory does not exist: " + metadataDir);
816+
}
817+
818+
try (FileWriter metadataOut = new FileWriter(metadataFile);
819+
BufferedWriter metadataBw = new BufferedWriter(metadataOut)) {
820+
metadataBw.write("class-name,partition,offset-in-heap,size\n");
821+
for (ObjectInfo info : getObjects()) {
822+
String csvLine = info.getClazz().getName() + "," + info.getPartition().getName() + "," + info.getOffset() + "," + info.getSize() + System.lineSeparator();
823+
metadataBw.write(csvLine);
824+
}
825+
} catch (IOException ex) {
826+
throw new RuntimeException("Failed to dump image heap metadata to " + metadataFile, ex);
827+
}
828+
}
829+
795830
private long getSize(Object object, HostedType type) {
796831
if (type.isInstanceClass()) {
797832
HostedInstanceClass clazz = (HostedInstanceClass) type;

0 commit comments

Comments
 (0)