Skip to content

Commit

Permalink
Use unattached threads as GC worker threads.
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhaeubl committed Mar 15, 2023
1 parent 35325d6 commit 340a845
Show file tree
Hide file tree
Showing 20 changed files with 292 additions and 198 deletions.
2 changes: 2 additions & 0 deletions substratevm/mx.substratevm/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@
],
"dependencies": [
"com.oracle.svm.core",
# TEMP (chaeubl): must not directly access the posix APIs
"com.oracle.svm.core.posix"
],
"requires" : [
"jdk.management",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,6 @@ public void collect(GCCause cause) {
collect(cause, false);
}

@SuppressWarnings("static-method")
public void initialize() {
if (ParallelGC.isEnabled()) {
ParallelGC.singleton().startWorkerThreads();
}
}

public void maybeCollectOnAllocation() {
boolean outOfMemory = false;
if (hasNeverCollectPolicy()) {
Expand Down Expand Up @@ -208,6 +201,10 @@ private void collectOperation(CollectionVMOperationData data) {
assert VMOperation.isGCInProgress() : "Collection should be a VMOperation.";
assert getCollectionEpoch().equal(data.getRequestingEpoch());

if (SubstrateOptions.UseParallelGC.getValue()) {
ParallelGC.singleton().initialize();
}

timers.mutator.closeAt(data.getRequestingNanoTime());
startCollectionOrExit();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ public void noteCopiedReferent() {
@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public void noteUnmodifiedReference() {
// TEMP (chaeubl): this counter would break
unmodifiedReference += 1L;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,16 @@
import org.graalvm.word.UnsignedWord;

import com.oracle.svm.core.MemoryWalker;
import com.oracle.svm.core.NeverInline;
import com.oracle.svm.core.SubstrateDiagnostics;
import com.oracle.svm.core.SubstrateDiagnostics.DiagnosticThunk;
import com.oracle.svm.core.SubstrateDiagnostics.DiagnosticThunkRegistry;
import com.oracle.svm.core.SubstrateDiagnostics.ErrorContext;
import com.oracle.svm.core.SubstrateOptions;
import com.oracle.svm.core.SubstrateUtil;
import com.oracle.svm.core.NeverInline;
import com.oracle.svm.core.heap.RestrictHeapAccess;
import com.oracle.svm.core.Uninterruptible;
import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.Uninterruptible;
import com.oracle.svm.core.config.ConfigurationValues;
import com.oracle.svm.core.genscavenge.AlignedHeapChunk.AlignedHeader;
import com.oracle.svm.core.genscavenge.ThreadLocalAllocation.Descriptor;
Expand All @@ -70,6 +69,7 @@
import com.oracle.svm.core.heap.ReferenceHandler;
import com.oracle.svm.core.heap.ReferenceHandlerThread;
import com.oracle.svm.core.heap.ReferenceInternals;
import com.oracle.svm.core.heap.RestrictHeapAccess;
import com.oracle.svm.core.heap.RuntimeCodeInfoGCSupport;
import com.oracle.svm.core.jdk.UninterruptibleUtils.AtomicReference;
import com.oracle.svm.core.locks.VMCondition;
Expand Down Expand Up @@ -242,11 +242,6 @@ GCImpl getGCImpl() {
return gcImpl;
}

@Override
public void initGC() {
gcImpl.initialize();
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public boolean isAllocationDisallowed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ Object getForwardedObject(Pointer ptr) {
Object getForwardedObject(Pointer ptr, UnsignedWord header) {
assert isForwardedHeader(header);
if (ReferenceAccess.singleton().haveCompressedReferences()) {
// TEMP (chaeubl):
// TODO (chaeubl): fix this
// if (ReferenceAccess.singleton().getCompressEncoding().hasShift()) {
if (false) {
// References compressed with shift have no bits to spare, so the forwarding
Expand All @@ -435,7 +435,7 @@ void installForwardingPointer(Object original, Object copy) {
assert !isPointerToForwardedObject(Word.objectToUntrackedPointer(original));
UnsignedWord forwardHeader;
if (ReferenceAccess.singleton().haveCompressedReferences()) {
// TEMP (chaeubl):
// TODO (chaeubl): fix this
// if (ReferenceAccess.singleton().getCompressEncoding().hasShift()) {
if (false) {
// Compression with a shift uses all bits of a reference, so store the forwarding
Expand All @@ -460,7 +460,7 @@ Object installForwardingPointerParallel(Object original, UnsignedWord originalHe
UnsignedWord forwardHeader;
boolean hasShift = false;
if (ReferenceAccess.singleton().haveCompressedReferences()) {
// TEMP (chaeubl):
// TODO (chaeubl): fix this
// if (ReferenceAccess.singleton().getCompressEncoding().hasShift()) {
if (false) {
forwardHeader = WordFactory.unsigned(0xe0e0e0e0e0e0e0e0L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private Pointer allocateMemory(UnsignedWord objectSize) {
private Pointer allocateMemoryParallel(UnsignedWord objectSize) {
Pointer result = WordFactory.nullPointer();
/* Fast-path: try allocating in the thread local allocation chunk. */
AlignedHeapChunk.AlignedHeader oldChunk = ParallelGC.getAllocationChunk();
AlignedHeapChunk.AlignedHeader oldChunk = ParallelGC.singleton().getAllocationChunk();
if (oldChunk.isNonNull()) {
result = AlignedHeapChunk.allocateMemory(oldChunk, objectSize);
}
Expand All @@ -214,7 +214,7 @@ private Pointer allocateMemoryParallel(UnsignedWord objectSize) {
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
private static Pointer retractAllocation(UnsignedWord objectSize) {
assert ParallelGC.isEnabled() && ParallelGC.isInParallelPhase();
AlignedHeapChunk.AlignedHeader oldChunk = ParallelGC.getAllocationChunk();
AlignedHeapChunk.AlignedHeader oldChunk = ParallelGC.singleton().getAllocationChunk();
assert oldChunk.isNonNull();
return AlignedHeapChunk.retractAllocation(oldChunk, objectSize);
}
Expand All @@ -239,7 +239,7 @@ private Pointer allocateInNewChunkParallel(AlignedHeapChunk.AlignedHeader oldChu
ParallelGC.mutex.unlock();
}
if (newChunk.isNonNull()) {
ParallelGC.setAllocationChunk(newChunk);
ParallelGC.singleton().setAllocationChunk(newChunk);
return AlignedHeapChunk.allocateMemory(newChunk, objectSize);
}
return WordFactory.nullPointer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,12 @@ private boolean fitsInSurvivors(HeapChunk.Header<?> chunk, boolean isAligned) {
return unalignedChunkFitsInSurvivors((UnalignedHeapChunk.UnalignedHeader) chunk);
}

// TEMP (chaeubl): this is problematic but it isn't called or?
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
private boolean alignedChunkFitsInSurvivors() {
UnsignedWord sum = survivorsToSpacesAccounting.getChunkBytes().add(HeapParameters.getAlignedHeapChunkSize());
return sum.belowOrEqual(GCImpl.getPolicy().getSurvivorSpacesCapacity());
}

// TEMP (chaeubl): this is problematic but it isn't called or?
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
private boolean unalignedChunkFitsInSurvivors(UnalignedHeapChunk.UnalignedHeader chunk) {
UnsignedWord size = UnalignedHeapChunk.getCommittedObjectMemory(chunk);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import org.graalvm.compiler.api.replacements.Fold;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.impl.UnmanagedMemorySupport;
import org.graalvm.word.Pointer;
import org.graalvm.word.WordFactory;
Expand All @@ -50,9 +52,15 @@ static int wordSize() {
return ConfigurationValues.getTarget().wordSize;
}

@Platforms(Platform.HOSTED_ONLY.class)
ChunkBuffer() {
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public void initialize() {
this.size = INITIAL_SIZE;
this.buffer = malloc(this.size);
// TODO (petermz): needs proper error handling
this.buffer = ImageSingletons.lookup(UnmanagedMemorySupport.class).malloc(WordFactory.unsigned(this.size));
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
Expand All @@ -62,7 +70,8 @@ void push(Pointer ptr) {
int oldSize = size;
size *= 2;
assert top < size;
buffer = realloc(buffer, size);
// TODO (petermz): needs proper error handling
buffer = ImageSingletons.lookup(UnmanagedMemorySupport.class).realloc(buffer, WordFactory.unsigned(size));
}
buffer.writeWord(top, ptr);
top += wordSize();
Expand All @@ -89,22 +98,8 @@ boolean isEmpty() {
return top == 0;
}

void release() {
free(buffer);
}

private static Pointer malloc(int bytes) {
// TEMP (chaeubl): needs proper error handling
return ImageSingletons.lookup(UnmanagedMemorySupport.class).malloc(WordFactory.unsigned(bytes));
}

@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
private static Pointer realloc(Pointer orig, int newSize) {
// TEMP (chaeubl): needs proper error handling
return ImageSingletons.lookup(UnmanagedMemorySupport.class).realloc(orig, WordFactory.unsigned(newSize));
}

private static void free(Pointer ptr) {
ImageSingletons.lookup(UnmanagedMemorySupport.class).free(ptr);
void release() {
ImageSingletons.lookup(UnmanagedMemorySupport.class).free(buffer);
}
}
Loading

0 comments on commit 340a845

Please sign in to comment.