From 56b7c6ebb56b3870f95c59c7f2bb87e7e7803a73 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Sun, 15 Sep 2024 16:16:43 +0100 Subject: [PATCH] Add config options --- .../impl/fawe/v1_20_R2/PaperweightGetBlocks.java | 2 +- .../impl/fawe/v1_20_R3/PaperweightGetBlocks.java | 2 +- .../impl/fawe/v1_20_R4/PaperweightGetBlocks.java | 2 +- .../impl/fawe/v1_21_R1/PaperweightGetBlocks.java | 2 +- .../main/java/com/fastasyncworldedit/core/Fawe.java | 8 ++------ .../core/configuration/Settings.java | 13 +++++++++++++ .../com/fastasyncworldedit/core/util/MemUtil.java | 6 +++++- 7 files changed, 24 insertions(+), 11 deletions(-) diff --git a/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightGetBlocks.java index dc14cf2d7..54c06a9f8 100644 --- a/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_20_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R2/PaperweightGetBlocks.java @@ -438,7 +438,7 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc final ServerLevel nmsWorld = serverLevel; CompletableFuture 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) { diff --git a/worldedit-bukkit/adapters/adapter-1_20_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R3/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_20_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R3/PaperweightGetBlocks.java index 00fdb5f51..23821a16e 100644 --- a/worldedit-bukkit/adapters/adapter-1_20_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R3/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_20_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R3/PaperweightGetBlocks.java @@ -438,7 +438,7 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc final ServerLevel nmsWorld = serverLevel; CompletableFuture 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) { diff --git a/worldedit-bukkit/adapters/adapter-1_20_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R4/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_20_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R4/PaperweightGetBlocks.java index 867b8976c..981e8a7b8 100644 --- a/worldedit-bukkit/adapters/adapter-1_20_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R4/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_20_5/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_20_R4/PaperweightGetBlocks.java @@ -439,7 +439,7 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc final ServerLevel nmsWorld = serverLevel; CompletableFuture 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) { diff --git a/worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightGetBlocks.java index f73962f8e..f5f921c8b 100644 --- a/worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_21/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_R1/PaperweightGetBlocks.java @@ -440,7 +440,7 @@ public class PaperweightGetBlocks extends CharGetBlocks implements BukkitGetBloc final ServerLevel nmsWorld = serverLevel; CompletableFuture 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) { diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/Fawe.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/Fawe.java index 74e1bf945..54abd43c0 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/Fawe.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/Fawe.java @@ -138,7 +138,7 @@ public class Fawe { } catch (Throwable ignored) { } }, 0); - TaskManager.taskManager().repeatAsync(() -> MemUtil.checkAndSetApproachingLimit(), 1); + TaskManager.taskManager().repeatAsync(MemUtil::checkAndSetApproachingLimit, 1); TaskManager.taskManager().repeat(timer, 1); @@ -418,16 +418,12 @@ public class Fawe { 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); diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/configuration/Settings.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/configuration/Settings.java index 60ec9311a..d9a9d2487 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/configuration/Settings.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/configuration/Settings.java @@ -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 @@ -599,6 +605,13 @@ public class Settings extends Config { }) 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", diff --git a/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/MemUtil.java b/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/MemUtil.java index f72fe92f0..5f01db884 100644 --- a/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/MemUtil.java +++ b/worldedit-core/src/main/java/com/fastasyncworldedit/core/util/MemUtil.java @@ -1,6 +1,8 @@ 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; @@ -8,6 +10,7 @@ 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); @@ -60,7 +63,8 @@ public class MemUtil { 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 memoryLimitedTasks = new ConcurrentLinkedQueue<>();