From 87b644834fda685bad44a1aaf5a9e7e786045571 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Fri, 6 Jan 2023 14:31:09 -0500 Subject: [PATCH] Adjust thread priorities --- .../core/config/ModernFixConfig.java | 6 +++- .../core/config/ModernFixEarlyConfig.java | 7 +++++ .../IntegratedServerMixin.java | 29 +++++++++++++++++++ .../perf/thread_priorities/UtilMixin.java | 25 ++++++++++++++++ src/main/resources/modernfix.mixins.json | 6 ++-- 5 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/embeddedt/modernfix/mixin/perf/thread_priorities/IntegratedServerMixin.java create mode 100644 src/main/java/org/embeddedt/modernfix/mixin/perf/thread_priorities/UtilMixin.java diff --git a/src/main/java/org/embeddedt/modernfix/core/config/ModernFixConfig.java b/src/main/java/org/embeddedt/modernfix/core/config/ModernFixConfig.java index fd9052fc..4849072d 100644 --- a/src/main/java/org/embeddedt/modernfix/core/config/ModernFixConfig.java +++ b/src/main/java/org/embeddedt/modernfix/core/config/ModernFixConfig.java @@ -22,16 +22,20 @@ public class ModernFixConfig { public static ForgeConfigSpec.ConfigValue> BLACKLIST_ASYNC_JEI_PLUGINS; + public static ForgeConfigSpec.IntValue INTEGRATED_SERVER_PRIORITY; + public static ForgeConfigSpec.IntValue BACKGROUND_WORKER_PRIORITY; + public static Set jeiPluginBlacklist; static { - List empty = Collections.emptyList(); Predicate locationValidator = o -> o instanceof String && ((String)o).contains(":"); BLACKLIST_ASYNC_JEI_PLUGINS = COMMON_BUILDER .comment("These JEI plugins will be loaded on the main thread") .defineList("blacklist_async_jei_plugins", ImmutableList.of( "jepb:jei_plugin" ), locationValidator); + INTEGRATED_SERVER_PRIORITY = COMMON_BUILDER.comment("Thread priority to use for the integrated server. By default this is one less than the client thread, to help prevent the server from lowering FPS.").defineInRange("integratedServerPriority", 4, 1, 10); + BACKGROUND_WORKER_PRIORITY = COMMON_BUILDER.comment("Priority to use for the background workers that complete various tasks. By default this is one less than the client thread.").defineInRange("backgroundWorkerPriority", 4, 1, 10); } static { diff --git a/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java b/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java index 787c9be9..a651bf24 100644 --- a/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java +++ b/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java @@ -1,5 +1,6 @@ package org.embeddedt.modernfix.core.config; +import net.minecraftforge.fml.loading.FMLLoader; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -27,6 +28,12 @@ public class ModernFixEarlyConfig { this.addMixinRule("bugfix.concurrency", true); this.addMixinRule("bugfix.edge_chunk_not_saved", true); this.addMixinRule("perf.async_jei", true); + this.addMixinRule("perf.thread_priorities", true); + + /* Mod compat */ + if(FMLLoader.getLoadingModList().getModFileById("smoothboot") != null) { + this.options.get("mixin.perf.thread_priorities").addModOverride(false, "smoothboot"); + } } /** diff --git a/src/main/java/org/embeddedt/modernfix/mixin/perf/thread_priorities/IntegratedServerMixin.java b/src/main/java/org/embeddedt/modernfix/mixin/perf/thread_priorities/IntegratedServerMixin.java new file mode 100644 index 00000000..19126ed5 --- /dev/null +++ b/src/main/java/org/embeddedt/modernfix/mixin/perf/thread_priorities/IntegratedServerMixin.java @@ -0,0 +1,29 @@ +package org.embeddedt.modernfix.mixin.perf.thread_priorities; + +import com.mojang.authlib.GameProfileRepository; +import com.mojang.authlib.minecraft.MinecraftSessionService; +import net.minecraft.client.Minecraft; +import net.minecraft.resources.DataPackRegistries; +import net.minecraft.resources.ResourcePackList; +import net.minecraft.server.integrated.IntegratedServer; +import net.minecraft.server.management.PlayerProfileCache; +import net.minecraft.util.registry.DynamicRegistries; +import net.minecraft.world.chunk.listener.IChunkStatusListenerFactory; +import net.minecraft.world.storage.IServerConfiguration; +import net.minecraft.world.storage.SaveFormat; +import org.embeddedt.modernfix.ModernFix; +import org.embeddedt.modernfix.core.config.ModernFixConfig; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(IntegratedServer.class) +public class IntegratedServerMixin { + @Inject(method = "", at = @At("RETURN")) + private void adjustServerPriority(Thread pServerThread, Minecraft pMinecraft, DynamicRegistries.Impl pRegistryHolder, SaveFormat.LevelSave pStorageSource, ResourcePackList pPackRepository, DataPackRegistries pResources, IServerConfiguration pWorldData, MinecraftSessionService pSessionService, GameProfileRepository pProfileRepository, PlayerProfileCache pProfileCache, IChunkStatusListenerFactory pProgressListenerfactory, CallbackInfo ci) { + int pri = ModernFixConfig.INTEGRATED_SERVER_PRIORITY.get(); + ModernFix.LOGGER.info("Changing server thread priority to " + pri); + pServerThread.setPriority(pri); + } +} diff --git a/src/main/java/org/embeddedt/modernfix/mixin/perf/thread_priorities/UtilMixin.java b/src/main/java/org/embeddedt/modernfix/mixin/perf/thread_priorities/UtilMixin.java new file mode 100644 index 00000000..491eb080 --- /dev/null +++ b/src/main/java/org/embeddedt/modernfix/mixin/perf/thread_priorities/UtilMixin.java @@ -0,0 +1,25 @@ +package org.embeddedt.modernfix.mixin.perf.thread_priorities; + +import net.minecraft.util.Util; +import org.embeddedt.modernfix.ModernFix; +import org.embeddedt.modernfix.core.config.ModernFixConfig; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.ModifyArg; + +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.ForkJoinWorkerThread; + +@Mixin(Util.class) +public class UtilMixin { + @ModifyArg(method = "makeExecutor", at = @At(value = "INVOKE", target = "Ljava/util/concurrent/ForkJoinPool;(ILjava/util/concurrent/ForkJoinPool$ForkJoinWorkerThreadFactory;Ljava/lang/Thread$UncaughtExceptionHandler;Z)V"), index = 1) + private static ForkJoinPool.ForkJoinWorkerThreadFactory adjustPriorityOfThreadFactory(ForkJoinPool.ForkJoinWorkerThreadFactory factory) { + return pool -> { + ForkJoinWorkerThread thread = factory.newThread(pool); + int pri = ModernFixConfig.BACKGROUND_WORKER_PRIORITY.get(); + ModernFix.LOGGER.info("Changing priority of " + thread.getName() + " to " + pri); + thread.setPriority(pri); + return thread; + }; + } +} diff --git a/src/main/resources/modernfix.mixins.json b/src/main/resources/modernfix.mixins.json index 9f4721bf..8b50312f 100644 --- a/src/main/resources/modernfix.mixins.json +++ b/src/main/resources/modernfix.mixins.json @@ -16,7 +16,8 @@ "perf.reduce_blockstate_cache_rebuilds.AbstractBlockStateMixin", "perf.reduce_blockstate_cache_rebuilds.GameDataMixin", "perf.reduce_blockstate_cache_rebuilds.BlockCallbacksMixin", - "perf.boost_worker_count.UtilMixin" + "perf.boost_worker_count.UtilMixin", + "perf.thread_priorities.UtilMixin" ], "client": [ "perf.skip_first_datapack_reload.MinecraftMixin", @@ -29,7 +30,8 @@ "perf.async_jei.ClientLifecycleHandlerMixin", "perf.async_jei.JeiStarterMixin", "perf.async_jei.PluginCallerMixin", - "perf.async_jei.RecipeManagerInternalMixin" + "perf.async_jei.RecipeManagerInternalMixin", + "perf.thread_priorities.IntegratedServerMixin" ], "injectors": { "defaultRequire": 1