mirror of
https://github.com/PaperMC/Paper.git
synced 2025-08-06 15:12:13 -07:00
Improve Server Thread Pool and Thread Priorities
Use a simple executor since Fork join is a much more complex pool type and we are not using its capabilities. Set thread priorities so main thread has above normal priority over server threads Allow usage of a single thread executor by not using ForkJoin so single core CPU's and reduce worldgen thread worker count for low core count CPUs. == AT == public net.minecraft.Util onThreadException(Ljava/lang/Thread;Ljava/lang/Throwable;)V Co-authored-by: Spottedleaf <Spottedleaf@users.noreply.github.com>
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
--- a/net/minecraft/Util.java
|
||||
+++ b/net/minecraft/Util.java
|
||||
@@ -95,6 +95,22 @@
|
||||
private static final TracingExecutor BACKGROUND_EXECUTOR = makeExecutor("Main");
|
||||
@@ -92,9 +92,25 @@
|
||||
private static final int DEFAULT_MAX_THREADS = 255;
|
||||
private static final int DEFAULT_SAFE_FILE_OPERATION_RETRIES = 10;
|
||||
private static final String MAX_THREADS_SYSTEM_PROPERTY = "max.bg.threads";
|
||||
- private static final TracingExecutor BACKGROUND_EXECUTOR = makeExecutor("Main");
|
||||
+ private static final TracingExecutor BACKGROUND_EXECUTOR = makeExecutor("Main", -1); // Paper - Perf: add priority
|
||||
private static final TracingExecutor IO_POOL = makeIoExecutor("IO-Worker-", false);
|
||||
private static final TracingExecutor DOWNLOAD_POOL = makeIoExecutor("Download-", true);
|
||||
+ // Paper start - don't submit BLOCKING PROFILE LOOKUPS to the world gen thread
|
||||
@@ -32,7 +36,58 @@
|
||||
}
|
||||
|
||||
public static long getEpochMillis() {
|
||||
@@ -537,7 +553,7 @@
|
||||
@@ -147,15 +163,16 @@
|
||||
return FILENAME_DATE_TIME_FORMATTER.format(ZonedDateTime.now());
|
||||
}
|
||||
|
||||
- private static TracingExecutor makeExecutor(String name) {
|
||||
+ private static TracingExecutor makeExecutor(String name, final int priorityModifier) { // Paper - Perf: add priority
|
||||
int i = maxAllowedExecutorThreads();
|
||||
- ExecutorService executorService;
|
||||
+ // Paper start - Perf: use simpler thread pool that allows 1 thread and reduce worldgen thread worker count for low core count CPUs
|
||||
+ final ExecutorService executorService;
|
||||
if (i <= 0) {
|
||||
executorService = MoreExecutors.newDirectExecutorService();
|
||||
} else {
|
||||
- AtomicInteger atomicInteger = new AtomicInteger(1);
|
||||
- executorService = new ForkJoinPool(i, pool -> {
|
||||
- final String string2 = "Worker-" + name + "-" + atomicInteger.getAndIncrement();
|
||||
+ executorService = Executors.newFixedThreadPool(i, target -> new io.papermc.paper.util.ServerWorkerThread(target, name, priorityModifier));
|
||||
+ }
|
||||
+ /* final String string2 = "Worker-" + name + "-" + atomicInteger.getAndIncrement();
|
||||
ForkJoinWorkerThread forkJoinWorkerThread = new ForkJoinWorkerThread(pool) {
|
||||
@Override
|
||||
protected void onStart() {
|
||||
@@ -177,13 +194,26 @@
|
||||
forkJoinWorkerThread.setName(string2);
|
||||
return forkJoinWorkerThread;
|
||||
}, Util::onThreadException, true);
|
||||
- }
|
||||
+ }*/
|
||||
|
||||
return new TracingExecutor(executorService);
|
||||
}
|
||||
|
||||
public static int maxAllowedExecutorThreads() {
|
||||
- return Mth.clamp(Runtime.getRuntime().availableProcessors() - 1, 1, getMaxThreads());
|
||||
+ // Paper start - Perf: use simpler thread pool that allows 1 thread and reduce worldgen thread worker count for low core count CPUs
|
||||
+ final int cpus = Runtime.getRuntime().availableProcessors() / 2;
|
||||
+ int maxExecutorThreads;
|
||||
+ if (cpus <= 4) {
|
||||
+ maxExecutorThreads = cpus <= 2 ? 1 : 2;
|
||||
+ } else if (cpus <= 8) {
|
||||
+ // [5, 8]
|
||||
+ maxExecutorThreads = Math.max(3, cpus - 2);
|
||||
+ } else {
|
||||
+ maxExecutorThreads = cpus * 2 / 3;
|
||||
+ }
|
||||
+ maxExecutorThreads = Math.min(8, maxExecutorThreads);
|
||||
+ return Integer.getInteger("Paper.WorkerThreadCount", maxExecutorThreads);
|
||||
+ // Paper end - Perf: use simpler thread pool that allows 1 thread and reduce worldgen thread worker count for low core count CPUs
|
||||
}
|
||||
|
||||
private static int getMaxThreads() {
|
||||
@@ -537,7 +567,7 @@
|
||||
public static <K extends Enum<K>, V> EnumMap<K, V> makeEnumMap(Class<K> enumClass, Function<K, V> mapper) {
|
||||
EnumMap<K, V> enumMap = new EnumMap<>(enumClass);
|
||||
|
||||
|
Reference in New Issue
Block a user