Skip to content

Commit

Permalink
Add config options
Browse files Browse the repository at this point in the history
  • Loading branch information
dordsor21 committed Oct 27, 2024
1 parent 3588cbc commit 7fe7ec3
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ 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()) {
if ((chunk == null && MemUtil.shouldBeginSlow()) || Settings.settings().QUEUE.ASYNC_CHUNK_LOAD_WRITE) {
try {
chunk = nmsChunkFuture.get(); // "Artificially" slow FAWE down if memory low as performing the
} catch (InterruptedException | ExecutionException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ 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()) {
if ((chunk == null && MemUtil.shouldBeginSlow()) || Settings.settings().QUEUE.ASYNC_CHUNK_LOAD_WRITE) {
try {
chunk = nmsChunkFuture.get(); // "Artificially" slow FAWE down if memory low as performing the
} catch (InterruptedException | ExecutionException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ 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()) {
if ((chunk == null && MemUtil.shouldBeginSlow()) || Settings.settings().QUEUE.ASYNC_CHUNK_LOAD_WRITE) {
try {
chunk = nmsChunkFuture.get(); // "Artificially" slow FAWE down if memory low as performing the
} catch (InterruptedException | ExecutionException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ 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()) {
if ((chunk == null && MemUtil.shouldBeginSlow()) || Settings.settings().QUEUE.ASYNC_CHUNK_LOAD_WRITE) {
try {
chunk = nmsChunkFuture.get(); // "Artificially" slow FAWE down if memory low as performing the
} catch (InterruptedException | ExecutionException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private Fawe(final IFawe implementation) {
} catch (Throwable ignored) {
}
}, 0);
TaskManager.taskManager().repeatAsync(() -> MemUtil.checkAndSetApproachingLimit(), 1);
TaskManager.taskManager().repeatAsync(MemUtil::checkAndSetApproachingLimit, 1);

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

Expand Down Expand Up @@ -418,16 +418,12 @@ 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");
LOGGER.warn("High memory usage detected, FAWE will attempt to slow operations to prevent a crash.");
MemUtil.memoryLimitedTask();
}, null, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public class Settings extends Config {
" - Disable with 100 or -1."
})
public int MAX_MEMORY_PERCENT = 95;
@Comment({
"When percent memory usage reaches this threshold some aspects of editing will be slowed down:",
" - FAWE-Asynchronous chunk loading when writing changes (see queue.async-chunk-load-write)"
})
public int SLOWER_MEMORY_PERCENT = 80;

@Create
public ENABLED_COMPONENTS ENABLED_COMPONENTS;
@Create
Expand Down Expand Up @@ -599,6 +605,13 @@ public static class QUEUE {
})
public boolean POOL = true;

@Comment({
"If chunk loading for writing edits to the world should be performed asynchronously to to FAWE",
" - Enable to improve performance at the expense of memory",
" - If experience out of memory crashed, disable this or reduce slower-memory-percent"
})
public boolean ASYNC_CHUNK_LOAD_WRITE = true;

public static class PROGRESS {

@Comment({"Display constant titles about the progress of a user's edit",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.fastasyncworldedit.core.util;

import com.fastasyncworldedit.core.configuration.Settings;
import com.sk89q.worldedit.internal.util.LogManagerCompat;
import org.apache.logging.log4j.Logger;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;

public class MemUtil {

private static final Logger LOGGER = LogManagerCompat.getLogger();
private static final AtomicBoolean memory = new AtomicBoolean(false);
private static final AtomicBoolean slower = new AtomicBoolean(false);

Expand Down Expand Up @@ -60,7 +63,8 @@ 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);
boolean limited = size >= Settings.settings().SLOWER_MEMORY_PERCENT;
slower.set(limited);
}

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

0 comments on commit 7fe7ec3

Please sign in to comment.