PaperMC/patches/server/0259-Improve-Server-Thread-Pool-and-Thread-Priorities.patch

116 lines
6 KiB
Diff
Raw Normal View History

2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Tue, 23 Oct 2018 23:14:38 -0400
Subject: [PATCH] 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.
2021-06-11 14:02:28 +02:00
== AT ==
public net.minecraft.Util onThreadException(Ljava/lang/Thread;Ljava/lang/Throwable;)V
Co-authored-by: Spottedleaf <Spottedleaf@users.noreply.github.com>
diff --git a/src/main/java/io/papermc/paper/util/ServerWorkerThread.java b/src/main/java/io/papermc/paper/util/ServerWorkerThread.java
new file mode 100644
index 0000000000000000000000000000000000000000..b60f59cf5cc8eb84a6055b7861857dece7f2501b
--- /dev/null
+++ b/src/main/java/io/papermc/paper/util/ServerWorkerThread.java
@@ -0,0 +1,14 @@
+package io.papermc.paper.util;
+
+import java.util.concurrent.atomic.AtomicInteger;
+import net.minecraft.Util;
+
+public class ServerWorkerThread extends Thread {
+ private static final AtomicInteger threadId = new AtomicInteger(1);
+ public ServerWorkerThread(Runnable target, String poolName, int prioritityModifier) {
+ super(target, "Worker-" + poolName + "-" + threadId.getAndIncrement());
+ setPriority(Thread.NORM_PRIORITY+prioritityModifier); // Deprioritize over main
+ this.setDaemon(true);
+ this.setUncaughtExceptionHandler(Util::onThreadException);
+ }
+}
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/Util.java b/src/main/java/net/minecraft/Util.java
2024-12-03 19:21:12 +01:00
index 3d5bca1b637234df48ca3228c7ffbce21116a0c7..e78f40f7f10a2ed0675b3cb9a2c5730dbc0141db 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/Util.java
+++ b/src/main/java/net/minecraft/Util.java
@@ -92,7 +92,7 @@ public class Util {
2021-11-24 00:56:42 +01:00
private static final int DEFAULT_MAX_THREADS = 255;
2023-12-05 23:21:44 +01:00
private static final int DEFAULT_SAFE_FILE_OPERATION_RETRIES = 10;
2021-11-24 00:56:42 +01:00
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
2024-12-03 19:21:12 +01:00
@@ -163,15 +163,16 @@ public class Util {
2022-07-27 21:49:24 +02:00
return FILENAME_DATE_TIME_FORMATTER.format(ZonedDateTime.now());
2021-06-11 14:02:28 +02:00
}
- private static TracingExecutor makeExecutor(String name) {
2024-12-03 19:21:12 +01:00
+ 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
2024-12-03 19:21:12 +01:00
+ final ExecutorService executorService;
2021-06-11 14:02:28 +02:00
if (i <= 0) {
2021-06-13 10:26:58 +02:00
executorService = MoreExecutors.newDirectExecutorService();
2021-06-11 14:02:28 +02:00
} else {
2023-12-05 23:21:44 +01:00
- AtomicInteger atomicInteger = new AtomicInteger(1);
- executorService = new ForkJoinPool(i, pool -> {
- final String string2 = "Worker-" + name + "-" + atomicInteger.getAndIncrement();
2024-12-03 19:21:12 +01:00
+ executorService = Executors.newFixedThreadPool(i, target -> new io.papermc.paper.util.ServerWorkerThread(target, name, priorityModifier));
2021-06-11 14:02:28 +02:00
+ }
+ /* final String string2 = "Worker-" + name + "-" + atomicInteger.getAndIncrement();
ForkJoinWorkerThread forkJoinWorkerThread = new ForkJoinWorkerThread(pool) {
2021-06-13 10:26:58 +02:00
@Override
protected void onStart() {
2024-12-03 19:21:12 +01:00
@@ -193,13 +194,26 @@ public class Util {
forkJoinWorkerThread.setName(string2);
2021-06-13 10:26:58 +02:00
return forkJoinWorkerThread;
}, Util::onThreadException, true);
- }
+ }*/
2021-06-11 14:02:28 +02:00
return new TracingExecutor(executorService);
2021-06-11 14:02:28 +02:00
}
2024-12-03 19:21:12 +01:00
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() {
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
2024-12-03 19:21:12 +01:00
index 65dee566937104e301b917d037f2db456751801c..848f0faa50a5c4d6fbfe5fe1e168d1946fd9c28c 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
2024-10-27 18:11:15 +01:00
@@ -332,6 +332,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
thread.setUncaughtExceptionHandler((thread1, throwable) -> {
MinecraftServer.LOGGER.error("Uncaught exception in server thread", throwable);
});
+ thread.setPriority(Thread.NORM_PRIORITY+2); // Paper - Perf: Boost priority
if (Runtime.getRuntime().availableProcessors() > 4) {
thread.setPriority(8);
}