Skip to content

Commit

Permalink
refactor code so it's closer to Hotspot implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
roberttoyonaga committed Mar 22, 2024
1 parent cadcc00 commit 621569d
Show file tree
Hide file tree
Showing 24 changed files with 704 additions and 553 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.heap.OutOfMemoryUtil;
import com.oracle.svm.core.nmt.NmtCategory;
import com.oracle.svm.core.os.VirtualMemoryProvider;
import com.oracle.svm.core.util.VMError;

Expand Down Expand Up @@ -112,7 +113,7 @@ private Pointer prepareTrampolines(PinnedObject mhsArray, PinnedObject stubsArra
VirtualMemoryProvider memoryProvider = VirtualMemoryProvider.get();
UnsignedWord pageSize = allocationSize();
/* We request a specific alignment to guarantee correctness of getAllocationBase */
Pointer page = memoryProvider.commit(WordFactory.nullPointer(), pageSize, VirtualMemoryProvider.Access.WRITE | VirtualMemoryProvider.Access.FUTURE_EXECUTE);
Pointer page = memoryProvider.commit(WordFactory.nullPointer(), pageSize, VirtualMemoryProvider.Access.WRITE | VirtualMemoryProvider.Access.FUTURE_EXECUTE, NmtCategory.Internal);
if (page.isNull()) {
throw OutOfMemoryUtil.reportOutOfMemoryError(new OutOfMemoryError("Could not allocate memory for trampolines."));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
import com.oracle.svm.core.feature.InternalFeature;
import com.oracle.svm.core.nmt.NativeMemoryTracking;
import com.oracle.svm.core.nmt.NmtCategory;
import com.oracle.svm.core.nmt.NmtVirtualMemoryData;
import com.oracle.svm.core.nmt.NmtPreImageHeapData;
import com.oracle.svm.core.nmt.NmtPreImageHeapDataAccess;
import com.oracle.svm.core.os.VirtualMemoryProvider;
import com.oracle.svm.core.posix.headers.Unistd;
import com.oracle.svm.core.util.PointerUtils;
Expand Down Expand Up @@ -112,14 +113,8 @@ public UnsignedWord getGranularity() {

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public Pointer reserve(UnsignedWord nbytes, UnsignedWord alignment, boolean executable) {
return reserve0(nbytes, alignment, executable, WordFactory.nullPointer(), NmtCategory.Internal);
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public Pointer reserve(UnsignedWord nbytes, UnsignedWord alignment, boolean executable, NmtVirtualMemoryData nmtData) {
return reserve0(nbytes, alignment, executable, nmtData, NmtCategory.Internal);
public Pointer reserve(UnsignedWord nbytes, UnsignedWord alignment, boolean executable, NmtPreImageHeapData nmtData) {
return reserve0(nbytes, alignment, executable, nmtData, NmtCategory.ImageHeap);
}

@Override
Expand All @@ -129,7 +124,7 @@ public Pointer reserve(UnsignedWord nbytes, UnsignedWord alignment, boolean exec
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
private Pointer reserve0(UnsignedWord nbytes, UnsignedWord alignment, boolean executable, NmtVirtualMemoryData nmtData, NmtCategory category) {
private Pointer reserve0(UnsignedWord nbytes, UnsignedWord alignment, boolean executable, NmtPreImageHeapData nmtData, NmtCategory category) {
if (nbytes.equal(0)) {
return WordFactory.nullPointer();
}
Expand All @@ -151,8 +146,7 @@ private Pointer reserve0(UnsignedWord nbytes, UnsignedWord alignment, boolean ex
if (nmtData.isNull()) {
NativeMemoryTracking.trackReserve(mappingBegin, mappingSize, category);
} else {
nmtData.setReserved(nmtData.getReserved().add(mappingSize));
nmtData.setBaseAddr(mappingBegin);
NmtPreImageHeapDataAccess.enqueueReserve(nmtData, mappingBegin, mappingSize, category);
}
return mappingBegin;
}
Expand All @@ -173,24 +167,16 @@ private Pointer reserve0(UnsignedWord nbytes, UnsignedWord alignment, boolean ex
}
if (nmtData.isNull()) {
NativeMemoryTracking.trackReserve(begin, mappingSize.subtract(unmappedSize), category);
// NativeMemoryTracking.trackReserve(begin, nbytes, category); // *** didn't fix problem.
} else {
nmtData.setReserved(nmtData.getReserved().add(mappingSize.subtract(unmappedSize)));
nmtData.setBaseAddr(begin);
NmtPreImageHeapDataAccess.enqueueReserve(nmtData, begin, mappingSize.subtract(unmappedSize), category);
}
return begin;
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public Pointer mapFile(PointerBase start, UnsignedWord nbytes, WordBase fileHandle, UnsignedWord offset, int access) {
return mapFile0(start, nbytes, fileHandle, offset, access, WordFactory.nullPointer(), NmtCategory.Internal);
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public Pointer mapFile(PointerBase start, UnsignedWord nbytes, WordBase fileHandle, UnsignedWord offset, int access, NmtVirtualMemoryData nmtData) {
return mapFile0(start, nbytes, fileHandle, offset, access, nmtData, NmtCategory.Internal);
public Pointer mapFile(PointerBase start, UnsignedWord nbytes, WordBase fileHandle, UnsignedWord offset, int access, NmtPreImageHeapData nmtData) {
return mapFile0(start, nbytes, fileHandle, offset, access, nmtData, NmtCategory.ImageHeap);
}

@Override
Expand All @@ -200,7 +186,7 @@ public Pointer mapFile(PointerBase start, UnsignedWord nbytes, WordBase fileHand
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
private Pointer mapFile0(PointerBase start, UnsignedWord nbytes, WordBase fileHandle, UnsignedWord offset, int access, NmtVirtualMemoryData nmtData, NmtCategory category) {
private Pointer mapFile0(PointerBase start, UnsignedWord nbytes, WordBase fileHandle, UnsignedWord offset, int access, NmtPreImageHeapData nmtData, NmtCategory category) {
if ((start.isNonNull() && !isAligned(start)) || nbytes.equal(0)) {
return WordFactory.nullPointer();
}
Expand All @@ -212,22 +198,16 @@ private Pointer mapFile0(PointerBase start, UnsignedWord nbytes, WordBase fileHa
int fd = (int) fileHandle.rawValue();
Pointer result = mmap(start, nbytes, accessAsProt(access), flags, fd, offset.rawValue());
if (result.notEqual(MAP_FAILED())) {
commitAndMaybeReserve(result, start, nbytes, nmtData, category);
trackCommitAndMaybeReserve(result, start, nbytes, nmtData, category);
return result;
}
return WordFactory.nullPointer();
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public Pointer commit(PointerBase start, UnsignedWord nbytes, int access) {
return commit0(start, nbytes, access, WordFactory.nullPointer(), NmtCategory.Internal);
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public Pointer commit(PointerBase start, UnsignedWord nbytes, int access, NmtVirtualMemoryData nmtData) {
return commit0(start, nbytes, access, nmtData, NmtCategory.Internal);
public Pointer commit(PointerBase start, UnsignedWord nbytes, int access, NmtPreImageHeapData nmtData) {
return commit0(start, nbytes, access, nmtData, NmtCategory.ImageHeap);
}

@Override
Expand All @@ -237,7 +217,7 @@ public Pointer commit(PointerBase start, UnsignedWord nbytes, int access, NmtCat
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
private Pointer commit0(PointerBase start, UnsignedWord nbytes, int access, NmtVirtualMemoryData nmtData, NmtCategory category) {
private Pointer commit0(PointerBase start, UnsignedWord nbytes, int access, NmtPreImageHeapData nmtData, NmtCategory category) {
if ((start.isNonNull() && !isAligned(start)) || nbytes.equal(0)) {
return WordFactory.nullPointer();
}
Expand All @@ -253,28 +233,26 @@ private Pointer commit0(PointerBase start, UnsignedWord nbytes, int access, NmtV
/* The memory returned by mmap is guaranteed to be zeroed. */
final Pointer result = mmap(start, nbytes, accessAsProt(access), flags, NO_FD, NO_FD_OFFSET);
if (result.notEqual(MAP_FAILED())) {
commitAndMaybeReserve(result, start, nbytes, nmtData, category);
trackCommitAndMaybeReserve(result, start, nbytes, nmtData, category);
return result;
}
return nullPointer();
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
private void commitAndMaybeReserve(PointerBase baseAddr, PointerBase start, UnsignedWord nbytes, NmtVirtualMemoryData nmtData, NmtCategory category) {
private void trackCommitAndMaybeReserve(PointerBase baseAddr, PointerBase start, UnsignedWord nbytes, NmtPreImageHeapData nmtData, NmtCategory category) {
// Account for possible reserve before commit
if (start.isNull()) {
if (nmtData.isNull()) {
NativeMemoryTracking.trackReserve(baseAddr, nbytes, category);
} else {
nmtData.setReserved(nmtData.getReserved().add(nbytes));
nmtData.setBaseAddr(baseAddr);
NmtPreImageHeapDataAccess.enqueueReserve(nmtData, baseAddr, nbytes, category);
}
}
if (nmtData.isNull()) {
NativeMemoryTracking.trackCommit(baseAddr, nbytes, category);
} else {
nmtData.setCommitted(nmtData.getCommitted().add(nbytes));
nmtData.setBaseAddr(baseAddr);
NmtPreImageHeapDataAccess.enqueueCommit(nmtData, baseAddr, nbytes, category);
}
}

Expand Down Expand Up @@ -305,6 +283,12 @@ public int uncommit(PointerBase start, UnsignedWord nbytes) {
@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public int free(PointerBase start, UnsignedWord nbytes) {
return free(start, nbytes, WordFactory.nullPointer());
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public int free(PointerBase start, UnsignedWord nbytes, NmtPreImageHeapData nmtData) {
if (start.isNull() || !isAligned(start) || nbytes.equal(0)) {
return -1;
}
Expand All @@ -313,9 +297,8 @@ public int free(PointerBase start, UnsignedWord nbytes) {
Pointer mappingBegin = PointerUtils.roundDown(start, granularity);
UnsignedWord mappingSize = UnsignedUtils.roundUp(nbytes, granularity);
int ret = munmap(mappingBegin, mappingSize);
if (ret == 0) {
NativeMemoryTracking.trackFree(start, mappingSize); // *** use start here, not
// mappingBegin
if (ret == 0 && nmtData.isNull()) {
NativeMemoryTracking.trackFree(start);
}
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
import com.oracle.svm.core.config.ConfigurationValues;
import com.oracle.svm.core.headers.LibC;
import com.oracle.svm.core.heap.Heap;
import com.oracle.svm.core.nmt.NmtVirtualMemoryData;
import com.oracle.svm.core.nmt.NmtPreImageHeapData;
import com.oracle.svm.core.os.AbstractImageHeapProvider;
import com.oracle.svm.core.os.VirtualMemoryProvider;
import com.oracle.svm.core.os.VirtualMemoryProvider.Access;
Expand Down Expand Up @@ -104,7 +104,7 @@ public boolean guaranteesHeapPreferredAddressSpaceAlignment() {

@Override
@Uninterruptible(reason = "Called during isolate initialization.")
public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, WordPointer basePointer, WordPointer endPointer, NmtVirtualMemoryData nmtData) {
public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, WordPointer basePointer, WordPointer endPointer, NmtPreImageHeapData nmtData) {
Pointer selfReservedMemory = WordFactory.nullPointer();
UnsignedWord requiredSize = getTotalRequiredAddressSpaceSize();
if (reservedAddressSpace.isNull()) {
Expand Down Expand Up @@ -133,13 +133,13 @@ public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, W

int error = DynamicMethodAddressResolutionHeapSupport.get().initialize();
if (error != CEntryPointErrors.NO_ERROR) {
freeImageHeap(selfReservedHeapBase);
freeImageHeap(selfReservedHeapBase, nmtData);
return error;
}

error = DynamicMethodAddressResolutionHeapSupport.get().install(heapBase);
if (error != CEntryPointErrors.NO_ERROR) {
freeImageHeap(selfReservedHeapBase);
freeImageHeap(selfReservedHeapBase, nmtData);
return error;
}
} else {
Expand All @@ -157,15 +157,15 @@ public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, W
IMAGE_HEAP_RELOCATABLE_BEGIN.get(), IMAGE_HEAP_A_RELOCATABLE_POINTER.get(), IMAGE_HEAP_RELOCATABLE_END.get(),
IMAGE_HEAP_WRITABLE_BEGIN.get(), IMAGE_HEAP_WRITABLE_END.get(), nmtData);
if (result != CEntryPointErrors.NO_ERROR) {
freeImageHeap(selfReservedHeapBase);
freeImageHeap(selfReservedHeapBase, nmtData);
}
return result;
}

@Uninterruptible(reason = "Called during isolate initialization.")
private static int initializeImageHeap(Pointer imageHeap, UnsignedWord reservedSize, WordPointer endPointer, WordPointer cachedFd, WordPointer cachedOffsetInFile,
Pointer magicAddress, Word heapBeginSym, Word heapEndSym, Word heapRelocsSym, Pointer heapAnyRelocPointer, Word heapRelocsEndSym, Word heapWritableSym, Word heapWritableEndSym,
NmtVirtualMemoryData nmtData) {
NmtPreImageHeapData nmtData) {
assert heapBeginSym.belowOrEqual(heapWritableSym) && heapWritableSym.belowOrEqual(heapWritableEndSym) && heapWritableEndSym.belowOrEqual(heapEndSym);
assert heapBeginSym.belowOrEqual(heapRelocsSym) && heapRelocsSym.belowOrEqual(heapRelocsEndSym) && heapRelocsEndSym.belowOrEqual(heapEndSym);
assert heapAnyRelocPointer.isNull() || (heapRelocsSym.belowOrEqual(heapAnyRelocPointer) && heapAnyRelocPointer.belowThan(heapRelocsEndSym));
Expand Down Expand Up @@ -257,7 +257,7 @@ private static int initializeImageHeap(Pointer imageHeap, UnsignedWord reservedS

@Uninterruptible(reason = "Called during isolate initialization.")
private static int initializeImageHeapByCopying(Pointer imageHeap, UnsignedWord imageHeapSize, UnsignedWord pageSize, Word heapBeginSym, Word heapWritableSym, Word heapWritableEndSym,
NmtVirtualMemoryData nmtData) {
NmtPreImageHeapData nmtData) {
Pointer committedBegin = VirtualMemoryProvider.get().commit(imageHeap, imageHeapSize, Access.READ | Access.WRITE, nmtData);
if (committedBegin.isNull()) {
return CEntryPointErrors.MAP_HEAP_FAILED;
Expand Down Expand Up @@ -351,6 +351,11 @@ private static boolean checkImageFileMagic(int mapfd, int imagefd, CCharPointer
@Override
@Uninterruptible(reason = "Called during isolate tear-down.")
public int freeImageHeap(PointerBase heapBase) {
return freeImageHeap(heapBase, WordFactory.nullPointer());
}

@Uninterruptible(reason = "Called during isolate tear-down.")
private int freeImageHeap(PointerBase heapBase, NmtPreImageHeapData nmtData) {
if (heapBase.isNull()) { // no memory allocated
return CEntryPointErrors.NO_ERROR;
}
Expand All @@ -360,7 +365,7 @@ public int freeImageHeap(PointerBase heapBase) {
if (DynamicMethodAddressResolutionHeapSupport.isEnabled()) {
addressSpaceStart = addressSpaceStart.subtract(getPreHeapAlignedSizeForDynamicMethodAddressResolver());
}
if (VirtualMemoryProvider.get().free(addressSpaceStart, getTotalRequiredAddressSpaceSize()) != 0) {
if (VirtualMemoryProvider.get().free(addressSpaceStart, getTotalRequiredAddressSpaceSize(), nmtData) != 0) {
return CEntryPointErrors.FREE_IMAGE_HEAP_FAILED;
}
return CEntryPointErrors.NO_ERROR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
import com.oracle.svm.core.c.function.CEntryPointActions;
import com.oracle.svm.core.c.function.CEntryPointErrors;
import com.oracle.svm.core.headers.LibC;
import com.oracle.svm.core.nmt.NmtPreImageHeapData;
import com.oracle.svm.core.os.AbstractCopyingImageHeapProvider;
import com.oracle.svm.core.nmt.NmtVirtualMemoryData;
import com.oracle.svm.core.os.VirtualMemoryProvider;
import com.oracle.svm.core.os.VirtualMemoryProvider.Access;
import com.oracle.svm.core.windows.headers.FileAPI;
Expand All @@ -65,7 +65,7 @@
public class WindowsImageHeapProvider extends AbstractCopyingImageHeapProvider {
@Override
@Uninterruptible(reason = "Called during isolate initialization.")
protected int commitAndCopyMemory(Pointer loadedImageHeap, UnsignedWord imageHeapSize, Pointer newImageHeap, NmtVirtualMemoryData nmtData) {
protected int commitAndCopyMemory(Pointer loadedImageHeap, UnsignedWord imageHeapSize, Pointer newImageHeap, NmtPreImageHeapData nmtData) {
HANDLE imageHeapFileMapping = getImageHeapFileMapping();
if (imageHeapFileMapping.isNull()) {
/* Fall back to copying from memory. */
Expand Down
Loading

0 comments on commit 621569d

Please sign in to comment.