From a7a54598b29dcc035f6dd214c62df43370d967b9 Mon Sep 17 00:00:00 2001 From: James Seibel Date: Sun, 16 Jul 2023 17:41:32 -0500 Subject: [PATCH] Add Thread runTime to the thread presets --- .../distanthorizons/core/config/Config.java | 19 ++-- .../ThreadPresetConfigEventHandler.java | 90 ++++++++++++++++--- 2 files changed, 88 insertions(+), 21 deletions(-) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/config/Config.java b/core/src/main/java/com/seibel/distanthorizons/core/config/Config.java index 942063ace..c9c98cb98 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/config/Config.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/config/Config.java @@ -739,8 +739,11 @@ public class Config public static final String THREAD_RUN_TIME_RATIO_NOTE = "" + "If this value is less than 1.0, it will be treated as a percentage \n" - + "of time a each thread can run before going idle. \n" - + ""; + + "of time each thread can run before going idle. \n" + + "\n" + + "This can be used to reduce CPU usage if the thread count \n" + + "is already set to 1 for the given option, or more finely \n" + + "tune CPU performance."; public static final ConfigEntry numberOfWorldGenerationThreads = new ConfigEntry.Builder() @@ -759,7 +762,7 @@ public class Config + THREAD_NOTE) .build(); public static final ConfigEntry runTimeRatioForWorldGenerationThreads = new ConfigEntry.Builder() - .setMinDefaultMax(0.01, 1.0, 1.0) + .setMinDefaultMax(0.01, ThreadPresetConfigEventHandler.getWorldGenDefaultRunTimeRatio(), 1.0) .comment(THREAD_RUN_TIME_RATIO_NOTE) .build(); @@ -777,7 +780,7 @@ public class Config + THREAD_NOTE) .build(); public static final ConfigEntry runTimeRatioForBufferBuilderThreads = new ConfigEntry.Builder() - .setMinDefaultMax(0.01, 1.0, 1.0) + .setMinDefaultMax(0.01, ThreadPresetConfigEventHandler.getBufferBuilderDefaultRunTimeRatio(), 1.0) .comment(THREAD_RUN_TIME_RATIO_NOTE) .build(); @@ -795,7 +798,7 @@ public class Config + THREAD_NOTE) .build(); public static final ConfigEntry runTimeRatioForFileHandlerThreads = new ConfigEntry.Builder() - .setMinDefaultMax(0.01, 1.0, 1.0) + .setMinDefaultMax(0.01, ThreadPresetConfigEventHandler.getFileHandlerDefaultRunTimeRatio(), 1.0) .comment(THREAD_RUN_TIME_RATIO_NOTE) .build(); @@ -816,13 +819,13 @@ public class Config + THREAD_NOTE) .build(); public static final ConfigEntry runTimeRatioForDataConverterThreads = new ConfigEntry.Builder() - .setMinDefaultMax(0.01, 1.0, 1.0) + .setMinDefaultMax(0.01, ThreadPresetConfigEventHandler.getDataConverterDefaultRunTimeRatio(), 1.0) .comment(THREAD_RUN_TIME_RATIO_NOTE) .build(); public static final ConfigEntry numberOfChunkLodConverterThreads = new ConfigEntry.Builder() .setMinDefaultMax(1, - ThreadPresetConfigEventHandler.getChunkLodConvertersDefaultThreadCount(), + ThreadPresetConfigEventHandler.getChunkLodConverterDefaultThreadCount(), Runtime.getRuntime().availableProcessors()) .comment("" + "How many threads should be used to convert Minecraft chunks into LOD data? \n" @@ -833,7 +836,7 @@ public class Config + THREAD_NOTE) .build(); public static final ConfigEntry runTimeRatioForChunkLodConverterThreads = new ConfigEntry.Builder() - .setMinDefaultMax(0.01, 1.0, 1.0) + .setMinDefaultMax(0.01, ThreadPresetConfigEventHandler.getChunkLodConverterDefaultRunTimeRatio(), 1.0) .comment(THREAD_RUN_TIME_RATIO_NOTE) .build(); diff --git a/core/src/main/java/com/seibel/distanthorizons/core/config/eventHandlers/presets/ThreadPresetConfigEventHandler.java b/core/src/main/java/com/seibel/distanthorizons/core/config/eventHandlers/presets/ThreadPresetConfigEventHandler.java index 2c60d3ea9..5bdbc3722 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/config/eventHandlers/presets/ThreadPresetConfigEventHandler.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/config/eventHandlers/presets/ThreadPresetConfigEventHandler.java @@ -20,8 +20,9 @@ public class ThreadPresetConfigEventHandler extends AbstractPresetConfigEventHan private static final Logger LOGGER = LogManager.getLogger(); + public static int getWorldGenDefaultThreadCount() { return getThreadCountByPercent(0.1); } - private final ConfigEntryWithPresetOptions worldGen = new ConfigEntryWithPresetOptions<>(Config.Client.Advanced.MultiThreading.numberOfWorldGenerationThreads, + private final ConfigEntryWithPresetOptions worldGenThreadCount = new ConfigEntryWithPresetOptions<>(Config.Client.Advanced.MultiThreading.numberOfWorldGenerationThreads, new HashMap() {{ this.put(EThreadPreset.MINIMAL_IMPACT, 1); @@ -30,9 +31,20 @@ public class ThreadPresetConfigEventHandler extends AbstractPresetConfigEventHan this.put(EThreadPreset.AGGRESSIVE, getThreadCountByPercent(0.4)); this.put(EThreadPreset.I_PAID_FOR_THE_WHOLE_CPU, getThreadCountByPercent(1.0)); }}); + public static double getWorldGenDefaultRunTimeRatio() { return 0.25; } + private final ConfigEntryWithPresetOptions worldGenRunTime = new ConfigEntryWithPresetOptions<>(Config.Client.Advanced.MultiThreading.runTimeRatioForWorldGenerationThreads, + new HashMap() + {{ + this.put(EThreadPreset.MINIMAL_IMPACT, 0.1); + this.put(EThreadPreset.LOW_IMPACT, getWorldGenDefaultRunTimeRatio()); + this.put(EThreadPreset.BALANCED, 0.5); + this.put(EThreadPreset.AGGRESSIVE, 0.75); + this.put(EThreadPreset.I_PAID_FOR_THE_WHOLE_CPU, 1.0); + }}); + public static int getBufferBuilderDefaultThreadCount() { return getThreadCountByPercent(0.1); } - private final ConfigEntryWithPresetOptions bufferBuilders = new ConfigEntryWithPresetOptions<>(Config.Client.Advanced.MultiThreading.numberOfBufferBuilderThreads, + private final ConfigEntryWithPresetOptions bufferBuilderThreadCount = new ConfigEntryWithPresetOptions<>(Config.Client.Advanced.MultiThreading.numberOfBufferBuilderThreads, new HashMap() {{ this.put(EThreadPreset.MINIMAL_IMPACT, 1); @@ -41,9 +53,20 @@ public class ThreadPresetConfigEventHandler extends AbstractPresetConfigEventHan this.put(EThreadPreset.AGGRESSIVE, getThreadCountByPercent(0.4)); this.put(EThreadPreset.I_PAID_FOR_THE_WHOLE_CPU, getThreadCountByPercent(1.0)); }}); + public static double getBufferBuilderDefaultRunTimeRatio() { return 0.5; } + private final ConfigEntryWithPresetOptions bufferBuilderRunTime = new ConfigEntryWithPresetOptions<>(Config.Client.Advanced.MultiThreading.runTimeRatioForBufferBuilderThreads, + new HashMap() + {{ + this.put(EThreadPreset.MINIMAL_IMPACT, 0.25); + this.put(EThreadPreset.LOW_IMPACT, getBufferBuilderDefaultRunTimeRatio()); + this.put(EThreadPreset.BALANCED, 0.75); + this.put(EThreadPreset.AGGRESSIVE, 1.0); + this.put(EThreadPreset.I_PAID_FOR_THE_WHOLE_CPU, 1.0); + }}); + public static int getFileHandlerDefaultThreadCount() { return getThreadCountByPercent(0.1); } - private final ConfigEntryWithPresetOptions fileHandlers = new ConfigEntryWithPresetOptions<>(Config.Client.Advanced.MultiThreading.numberOfFileHandlerThreads, + private final ConfigEntryWithPresetOptions fileHandlerThreadCount = new ConfigEntryWithPresetOptions<>(Config.Client.Advanced.MultiThreading.numberOfFileHandlerThreads, new HashMap() {{ this.put(EThreadPreset.MINIMAL_IMPACT, 1); @@ -52,9 +75,20 @@ public class ThreadPresetConfigEventHandler extends AbstractPresetConfigEventHan this.put(EThreadPreset.AGGRESSIVE, getThreadCountByPercent(0.2)); this.put(EThreadPreset.I_PAID_FOR_THE_WHOLE_CPU, getThreadCountByPercent(1.0)); }}); + public static double getFileHandlerDefaultRunTimeRatio() { return 0.5; } + private final ConfigEntryWithPresetOptions fileHandlerRunTime = new ConfigEntryWithPresetOptions<>(Config.Client.Advanced.MultiThreading.runTimeRatioForFileHandlerThreads, + new HashMap() + {{ + this.put(EThreadPreset.MINIMAL_IMPACT, 0.25); + this.put(EThreadPreset.LOW_IMPACT, getFileHandlerDefaultRunTimeRatio()); + this.put(EThreadPreset.BALANCED, 0.75); + this.put(EThreadPreset.AGGRESSIVE, 1.0); + this.put(EThreadPreset.I_PAID_FOR_THE_WHOLE_CPU, 1.0); + }}); + public static int getDataConverterDefaultThreadCount() { return getThreadCountByPercent(0.1); } - private final ConfigEntryWithPresetOptions dataConverters = new ConfigEntryWithPresetOptions<>(Config.Client.Advanced.MultiThreading.numberOfDataConverterThreads, + private final ConfigEntryWithPresetOptions dataConverterThreadCount = new ConfigEntryWithPresetOptions<>(Config.Client.Advanced.MultiThreading.numberOfDataConverterThreads, new HashMap() {{ this.put(EThreadPreset.MINIMAL_IMPACT, 1); @@ -63,17 +97,38 @@ public class ThreadPresetConfigEventHandler extends AbstractPresetConfigEventHan this.put(EThreadPreset.AGGRESSIVE, getThreadCountByPercent(0.2)); this.put(EThreadPreset.I_PAID_FOR_THE_WHOLE_CPU, getThreadCountByPercent(1.0)); }}); + public static double getDataConverterDefaultRunTimeRatio() { return 0.25; } + private final ConfigEntryWithPresetOptions dataConverterRunTime = new ConfigEntryWithPresetOptions<>(Config.Client.Advanced.MultiThreading.runTimeRatioForDataConverterThreads, + new HashMap() + {{ + this.put(EThreadPreset.MINIMAL_IMPACT, 0.1); + this.put(EThreadPreset.LOW_IMPACT, getDataConverterDefaultRunTimeRatio()); + this.put(EThreadPreset.BALANCED, 0.75); + this.put(EThreadPreset.AGGRESSIVE, 1.0); + this.put(EThreadPreset.I_PAID_FOR_THE_WHOLE_CPU, 1.0); + }}); - public static int getChunkLodConvertersDefaultThreadCount() { return getThreadCountByPercent(0.1); } - private final ConfigEntryWithPresetOptions chunkLodConverters = new ConfigEntryWithPresetOptions<>(Config.Client.Advanced.MultiThreading.numberOfChunkLodConverterThreads, + + public static int getChunkLodConverterDefaultThreadCount() { return getThreadCountByPercent(0.1); } + private final ConfigEntryWithPresetOptions chunkLodConverterThreadCount = new ConfigEntryWithPresetOptions<>(Config.Client.Advanced.MultiThreading.numberOfChunkLodConverterThreads, new HashMap() {{ this.put(EThreadPreset.MINIMAL_IMPACT, 1); - this.put(EThreadPreset.LOW_IMPACT, getChunkLodConvertersDefaultThreadCount()); + this.put(EThreadPreset.LOW_IMPACT, getChunkLodConverterDefaultThreadCount()); this.put(EThreadPreset.BALANCED, getThreadCountByPercent(0.2)); this.put(EThreadPreset.AGGRESSIVE, getThreadCountByPercent(0.4)); this.put(EThreadPreset.I_PAID_FOR_THE_WHOLE_CPU, getThreadCountByPercent(1.0)); }}); + public static double getChunkLodConverterDefaultRunTimeRatio() { return 0.5; } + private final ConfigEntryWithPresetOptions chunkLodConverterRunTime = new ConfigEntryWithPresetOptions<>(Config.Client.Advanced.MultiThreading.runTimeRatioForChunkLodConverterThreads, + new HashMap() + {{ + this.put(EThreadPreset.MINIMAL_IMPACT, 0.25); + this.put(EThreadPreset.LOW_IMPACT, getDataConverterDefaultRunTimeRatio()); + this.put(EThreadPreset.BALANCED, 0.75); + this.put(EThreadPreset.AGGRESSIVE, 1.0); + this.put(EThreadPreset.I_PAID_FOR_THE_WHOLE_CPU, 1.0); + }}); @@ -85,16 +140,25 @@ public class ThreadPresetConfigEventHandler extends AbstractPresetConfigEventHan private ThreadPresetConfigEventHandler() { // add each config used by this preset - this.configList.add(this.worldGen); - this.configList.add(this.bufferBuilders); - this.configList.add(this.fileHandlers); - this.configList.add(this.dataConverters); - this.configList.add(this.chunkLodConverters); + this.configList.add(this.worldGenThreadCount); + this.configList.add(this.worldGenRunTime); + + this.configList.add(this.bufferBuilderThreadCount); + this.configList.add(this.bufferBuilderRunTime); + + this.configList.add(this.fileHandlerThreadCount); + this.configList.add(this.fileHandlerRunTime); + + this.configList.add(this.dataConverterThreadCount); + this.configList.add(this.dataConverterRunTime); + + this.configList.add(this.chunkLodConverterThreadCount); + this.configList.add(this.chunkLodConverterRunTime); for (ConfigEntryWithPresetOptions config : this.configList) { - // ignore try-using, the listener should only ever be added once and should never be removed + // ignore try-using, the listeners should only ever be added once and should never be removed new ConfigChangeListener<>(config.configEntry, (val) -> { this.onConfigValueChanged(); }); } }