Skip to content

Commit cdcfe38

Browse files
[GR-69565] Copy open type world data to the metaspace.
PullRequest: graal/22097
2 parents 4d79a97 + df6ee20 commit cdcfe38

File tree

7 files changed

+45
-12
lines changed

7 files changed

+45
-12
lines changed

substratevm/src/com.oracle.svm.core.genscavenge/src/com/oracle/svm/core/genscavenge/metaspace/MetaspaceImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ public byte[] allocateByteArray(int length) {
111111
return allocator.allocateByteArray(length);
112112
}
113113

114+
@Override
115+
public int[] allocateIntArray(int length) {
116+
return allocator.allocateIntArray(length);
117+
}
118+
114119
@Override
115120
public void walkObjects(ObjectVisitor visitor) {
116121
assert VMOperation.isInProgress() : "prevent other threads from manipulating the metaspace";

substratevm/src/com.oracle.svm.core.genscavenge/src/com/oracle/svm/core/genscavenge/metaspace/MetaspaceObjectAllocator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public byte[] allocateByteArray(int length) {
7373
return (byte[]) allocateArrayLikeObject(hub, length);
7474
}
7575

76+
public int[] allocateIntArray(int length) {
77+
DynamicHub hub = DynamicHub.fromClass(int[].class);
78+
return (int[]) allocateArrayLikeObject(hub, length);
79+
}
80+
7681
@Uninterruptible(reason = "Holds uninitialized memory.")
7782
private Object allocateArrayLikeObject(DynamicHub hub, int arrayLength) {
7883
UnsignedWord size = LayoutEncoding.getArrayAllocationSize(hub.getLayoutEncoding(), arrayLength);

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/heap/InstanceReferenceMapEncoder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.oracle.svm.core.config.ConfigurationValues;
3636
import com.oracle.svm.core.heap.InstanceReferenceMapDecoder.InstanceReferenceMap;
3737
import com.oracle.svm.core.snippets.KnownIntrinsics;
38+
import com.oracle.svm.core.util.DuplicatedInNativeCode;
3839
import com.oracle.svm.core.util.VMError;
3940

4041
import jdk.graal.compiler.core.common.NumUtil;
@@ -56,6 +57,7 @@
5657
* </ul>
5758
*/
5859
public class InstanceReferenceMapEncoder extends ReferenceMapEncoder {
60+
@DuplicatedInNativeCode //
5961
public static final int REFERENCE_MAP_COMPRESSED_OFFSET_SHIFT = 2;
6062

6163
public static final int MAP_HEADER_SIZE = 4;

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHub.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ public final class DynamicHub implements AnnotatedElement, java.lang.reflect.Typ
282282

283283
/**
284284
* Array containing this type's type check id information. During a type check, these slots are
285-
* searched for a matching typeID.
285+
* searched for a matching typeID. This array may be used by the garbage collector and therefore
286+
* needs to live in the image heap or {@link Metaspace}.
286287
*/
287288
@UnknownObjectField(availability = AfterHostedUniverse.class)//
288289
private int[] openTypeWorldTypeCheckSlots;
@@ -303,7 +304,8 @@ public final class DynamicHub implements AnnotatedElement, java.lang.reflect.Typ
303304
/**
304305
* HashTable used for interface hashing under open type world if
305306
* {@link SubstrateOptions#useInterfaceHashing()} is enabled. See TypeCheckBuilder for a general
306-
* documentation.
307+
* documentation. This array may be used by the garbage collector and therefore needs to live in
308+
* the image heap or {@link Metaspace}.
307309
*/
308310
@UnknownObjectField(availability = AfterHostedUniverse.class)//
309311
private int[] openTypeWorldInterfaceHashTable;
@@ -477,8 +479,8 @@ public static DynamicHub allocate(String name, DynamicHub superHub, Object inter
477479
short numClassTypes,
478480
short typeIDDepth,
479481
short numIterableInterfaceTypes,
480-
int[] openTypeWorldTypeCheckSlots,
481-
int[] openTypeWorldInterfaceHashTable,
482+
int[] typeCheckSlotsHeapArray,
483+
int[] interfaceHashTableHeapArray,
482484
int openTypeWorldInterfaceHashParam,
483485
int vTableEntries,
484486
int afterFieldsOffset, boolean valueBased) {
@@ -582,16 +584,18 @@ public static DynamicHub allocate(String name, DynamicHub superHub, Object inter
582584
// GR-61330: only write if the field exists according to analysis
583585
// companion.metaType = null;
584586

585-
int referenceMapCompressedOffset = RuntimeInstanceReferenceMapSupport.singleton().getOrCreateReferenceMap(superHub);
586-
587587
// GR-57813
588588
companion.hubMetadata = null;
589589
companion.reflectionMetadata = null;
590590

591+
/* Allocate memory in the metaspace and copy data from the Java heap to the metaspace. */
591592
DynamicHub hub = Metaspace.singleton().allocateDynamicHub(vTableEntries);
593+
int[] openTypeWorldTypeCheckSlots = Metaspace.singleton().copyToMetaspace(typeCheckSlotsHeapArray);
594+
int[] openTypeWorldInterfaceHashTable = Metaspace.singleton().copyToMetaspace(interfaceHashTableHeapArray);
595+
int referenceMapCompressedOffset = RuntimeInstanceReferenceMapSupport.singleton().getOrCreateReferenceMap(superHub);
592596

593-
DynamicHubOffsets dynamicHubOffsets = DynamicHubOffsets.singleton();
594597
/* Write fields in defining order. */
598+
DynamicHubOffsets dynamicHubOffsets = DynamicHubOffsets.singleton();
595599
writeObject(hub, dynamicHubOffsets.getNameOffset(), name);
596600
writeByte(hub, dynamicHubOffsets.getHubTypeOffset(), hubType);
597601
writeByte(hub, dynamicHubOffsets.getReferenceTypeOffset(), referenceType.getValue());

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/RuntimeInstanceReferenceMapSupport.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,8 @@ private synchronized int putIfAbsent(byte[] newHeapMap) {
106106
return toCompressedOffset(existingMetaspaceMapHolder.refMap);
107107
}
108108

109-
/* Copy the data to the metaspace. */
110-
byte[] newMetaspaceMap = Metaspace.singleton().allocateByteArray(newHeapMap.length);
111-
System.arraycopy(newHeapMap, 0, newMetaspaceMap, 0, newHeapMap.length);
112-
113-
/* Store the new reference map in the hash map. */
109+
/* Copy the data to the metaspace and store the new reference map in the hash map. */
110+
byte[] newMetaspaceMap = Metaspace.singleton().copyToMetaspace(newHeapMap);
114111
ReferenceMapHolder newMetaspaceMapHolder = new ReferenceMapHolder(newMetaspaceMap);
115112
refMaps.put(newMetaspaceMapHolder, newMetaspaceMapHolder);
116113
return toCompressedOffset(newMetaspaceMapHolder.refMap);

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/metaspace/Metaspace.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@ static boolean isSupported() {
8989
/** Allocates a byte array. */
9090
byte[] allocateByteArray(int length);
9191

92+
/** Allocates an int array. */
93+
int[] allocateIntArray(int length);
94+
95+
default byte[] copyToMetaspace(byte[] heapArray) {
96+
byte[] result = Metaspace.singleton().allocateByteArray(heapArray.length);
97+
System.arraycopy(heapArray, 0, result, 0, heapArray.length);
98+
return result;
99+
}
100+
101+
default int[] copyToMetaspace(int[] heapArray) {
102+
int[] result = Metaspace.singleton().allocateIntArray(heapArray.length);
103+
System.arraycopy(heapArray, 0, result, 0, heapArray.length);
104+
return result;
105+
}
106+
92107
/** Walks all metaspace objects. May only be called at a safepoint. */
93108
void walkObjects(ObjectVisitor visitor);
94109
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/metaspace/NoMetaspace.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ public byte[] allocateByteArray(int length) {
6868
throw VMError.shouldNotReachHere("Must not be called if metaspace support is not available.");
6969
}
7070

71+
@Override
72+
public int[] allocateIntArray(int length) {
73+
throw VMError.shouldNotReachHere("Must not be called if metaspace support is not available.");
74+
}
75+
7176
@Override
7277
public void walkObjects(ObjectVisitor visitor) {
7378
/* Nothing to do. */

0 commit comments

Comments
 (0)