Paper/patches/server/0275-Improve-Server-Thread-Pool-and-Thread-Priorities.patch

94 Zeilen
4.8 KiB
Diff

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.
== AT ==
public net.minecraft.Util onThreadException(Ljava/lang/Thread;Ljava/lang/Throwable;)V
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
2023-12-05 23:21:44 +01:00
index aa52b271bd556a29f774fde375b713d0d187521b..8188febd6f1039a31619b42af23df18afd2e985c 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
2023-12-05 23:21:44 +01:00
@@ -86,7 +86,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";
2021-06-11 14:02:28 +02:00
- private static final ExecutorService BACKGROUND_EXECUTOR = makeExecutor("Main");
2021-06-13 10:26:58 +02:00
+ private static final ExecutorService BACKGROUND_EXECUTOR = makeExecutor("Main", -1); // Paper - add -1 priority
2023-12-05 23:21:44 +01:00
private static final ExecutorService IO_POOL = makeIoExecutor("IO-Worker-", false);
private static final ExecutorService DOWNLOAD_POOL = makeIoExecutor("Download-", true);
// Paper start - don't submit BLOCKING PROFILE LOOKUPS to the world gen thread
2023-12-05 23:21:44 +01:00
@@ -152,15 +152,18 @@ 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 ExecutorService makeExecutor(String name) {
2021-11-24 00:56:42 +01:00
- int i = Mth.clamp(Runtime.getRuntime().availableProcessors() - 1, 1, getMaxThreads());
2021-06-13 10:26:58 +02:00
+ private static ExecutorService makeExecutor(String s, int priorityModifier) { // Paper - add priority
2021-06-11 14:02:28 +02:00
+ // Paper start - use simpler thread pool that allows 1 thread
+ int i = Math.min(8, Math.max(Runtime.getRuntime().availableProcessors() - 2, 1));
+ i = Integer.getInteger("Paper.WorkerThreadCount", i);
2021-06-13 10:26:58 +02:00
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) -> {
- ForkJoinWorkerThread forkJoinWorkerThread = new ForkJoinWorkerThread(pool) {
+ executorService = new java.util.concurrent.ThreadPoolExecutor(i, i,0L, TimeUnit.MILLISECONDS, new java.util.concurrent.LinkedBlockingQueue<>(), target -> new io.papermc.paper.util.ServerWorkerThread(target, s, priorityModifier));
2021-06-11 14:02:28 +02:00
+ }
+ /*
2021-06-13 10:26:58 +02:00
@Override
2021-06-11 14:02:28 +02:00
protected void onTermination(Throwable throwable) {
if (throwable != null) {
2023-12-05 23:21:44 +01:00
@@ -176,6 +179,7 @@ public class Util {
2021-06-13 10:26:58 +02:00
return forkJoinWorkerThread;
}, Util::onThreadException, true);
2021-06-11 14:02:28 +02:00
}
+ }*/ // Paper end
2021-06-13 10:26:58 +02:00
return executorService;
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
2023-12-05 23:21:44 +01:00
index a2daac80fcbe51b7b68ab01416f052d01267f3fe..0090f920f0e0460c5880f0661d29fdc95530abf2 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
2023-12-05 23:21:44 +01:00
@@ -312,6 +312,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
thread.setUncaughtExceptionHandler((thread1, throwable) -> {
MinecraftServer.LOGGER.error("Uncaught exception in server thread", throwable);
});
2021-06-11 14:02:28 +02:00
+ thread.setPriority(Thread.NORM_PRIORITY+2); // Paper - boost priority
if (Runtime.getRuntime().availableProcessors() > 4) {
thread.setPriority(8);
}