Skip to content

Commit

Permalink
It's ugly but it stops OOM
Browse files Browse the repository at this point in the history
  • Loading branch information
dordsor21 committed Sep 14, 2024
1 parent dfdb92e commit 63606d7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.fastasyncworldedit.core.queue.implementation.QueueHandler;
import com.fastasyncworldedit.core.queue.implementation.blocks.CharGetBlocks;
import com.fastasyncworldedit.core.util.MathMan;
import com.fastasyncworldedit.core.util.MemUtil;
import com.fastasyncworldedit.core.util.NbtUtils;
import com.fastasyncworldedit.core.util.collection.AdaptedMap;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
Expand Down Expand Up @@ -430,7 +431,7 @@ public CompletableFuture<LevelChunk> ensureLoaded(ServerLevel nmsWorld, int chun
}

@Override
@SuppressWarnings("rawtypes")
@SuppressWarnings({"rawtypes", "unchecked"})
public synchronized <T extends Future<T>> T call(IQueueExtent<? extends IChunk> owner, IChunkSet set, Runnable finalizer) {
if (!callLock.isHeldByCurrentThread()) {
throw new IllegalStateException("Attempted to call chunk GET but chunk was not call-locked.");
Expand All @@ -439,6 +440,15 @@ public synchronized <T extends Future<T>> T call(IQueueExtent<? extends IChunk>
final ServerLevel nmsWorld = serverLevel;
CompletableFuture<LevelChunk> nmsChunkFuture = ensureLoaded(nmsWorld, chunkX, chunkZ);
LevelChunk chunk = nmsChunkFuture.getNow(null);
if (chunk == null && MemUtil.shouldBeginSlow()) {
try {
chunk = nmsChunkFuture.get(); // "Artificially" slow FAWE down if memory low as performing the
} catch (InterruptedException | ExecutionException e) {
LOGGER.error("Could not get chunk at {},{} whilst low memory", chunkX, chunkZ, e);
throw new FaweException(
TextComponent.of("Could not get chunk at " + chunkX + "," + chunkZ + " whilst low memory: " + e.getMessage()));
}
}
// Run immediately if possible
if (chunk != null) {
return internalCall(set, finalizer, chunk, nmsWorld);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ private Fawe(final IFawe implementation) {
} catch (Throwable ignored) {
}
}, 0);
TaskManager.taskManager().repeatAsync(() -> MemUtil.checkAndSetApproachingLimit(), 1);

TaskManager.taskManager().repeat(timer, 1);

Expand Down Expand Up @@ -417,11 +418,16 @@ private void setupMemoryListener() {
final NotificationEmitter ne = (NotificationEmitter) memBean;

ne.addNotificationListener((notification, handback) -> {
LOGGER.info("Notification");
final long heapSize = Runtime.getRuntime().totalMemory();
final long heapMaxSize = Runtime.getRuntime().maxMemory();
if (heapSize < heapMaxSize) {
return;
}
LOGGER.info("NNNNNNNNNNNNNNNNNNNNNNNNNNNN");
LOGGER.info("NNNNNNNNNNNNNNNNNNNNNNNNNNNN");
LOGGER.info("NNNNNNNNNNNNNNNNNNNNNNNNNNNN");
LOGGER.info("JJJJJJJJJJJJJJJJJJJJJJJJJJJJ");
MemUtil.memoryLimitedTask();
}, null, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import com.fastasyncworldedit.core.extent.filter.block.ChunkFilterBlock;
import com.fastasyncworldedit.core.extent.processor.IBatchProcessorHolder;
import com.fastasyncworldedit.core.internal.simd.SimdSupport;
import com.fastasyncworldedit.core.internal.simd.VectorizedCharFilterBlock;
import com.fastasyncworldedit.core.internal.simd.VectorizedFilter;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.math.BlockVector2;
Expand Down Expand Up @@ -157,11 +154,7 @@ default ChunkFilterBlock apply(
if (newChunk != null) {
chunk = newChunk;
if (block == null) {
if (SimdSupport.useVectorApi() && filter instanceof VectorizedFilter) {
block = new VectorizedCharFilterBlock(this);
} else {
block = this.createFilterBlock();
}
block = this.createFilterBlock();
}
block.initChunk(chunkX, chunkZ);
chunk.filterBlocks(filter, block, region, full);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class MemUtil {

private static final AtomicBoolean memory = new AtomicBoolean(false);
private static final AtomicBoolean slower = new AtomicBoolean(false);

public static boolean isMemoryFree() {
return !memory.get();
Expand All @@ -28,6 +29,10 @@ public static boolean isMemoryLimitedSlow() {
return false;
}

public static boolean shouldBeginSlow() {
return slower.get();
}

public static long getUsedBytes() {
return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
}
Expand All @@ -51,6 +56,13 @@ public static int calculateMemory() {
return size;
}

public static void checkAndSetApproachingLimit() {
final long heapFreeSize = Runtime.getRuntime().freeMemory();
final long heapMaxSize = Runtime.getRuntime().maxMemory();
final int size = (int) ((heapFreeSize * 100) / heapMaxSize);
slower.set(size > 80);
}

private static final Queue<Runnable> memoryLimitedTasks = new ConcurrentLinkedQueue<>();
private static final Queue<Runnable> memoryPlentifulTasks = new ConcurrentLinkedQueue<>();

Expand Down

0 comments on commit 63606d7

Please sign in to comment.