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

92 Zeilen
4.7 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.
diff --git a/src/main/java/net/minecraft/Util.java b/src/main/java/net/minecraft/Util.java
2022-06-08 02:42:07 +02:00
index d7e22ddf89619b58516ccef1d75a4c33df61b73c..70b8b86ed827217e536e35a690160adf9e974bb5 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
2022-06-08 02:42:07 +02:00
@@ -78,8 +78,8 @@ public class Util {
2021-11-24 00:56:42 +01:00
private static final int DEFAULT_MAX_THREADS = 255;
private static final String MAX_THREADS_SYSTEM_PROPERTY = "max.bg.threads";
2021-06-11 14:02:28 +02:00
private static final AtomicInteger WORKER_COUNT = new AtomicInteger(1);
- private static final ExecutorService BOOTSTRAP_EXECUTOR = makeExecutor("Bootstrap");
- private static final ExecutorService BACKGROUND_EXECUTOR = makeExecutor("Main");
2021-06-13 10:26:58 +02:00
+ private static final ExecutorService BOOTSTRAP_EXECUTOR = makeExecutor("Bootstrap", -2); // Paper - add -2 priority
+ private static final ExecutorService BACKGROUND_EXECUTOR = makeExecutor("Main", -1); // Paper - add -1 priority
2021-06-11 14:02:28 +02:00
private static final ExecutorService IO_POOL = makeIoExecutor();
public static LongSupplier timeSource = System::nanoTime;
2022-03-01 06:43:03 +01:00
public static final Ticker TICKER = new Ticker() {
2022-06-08 02:42:07 +02:00
@@ -121,14 +121,18 @@ public class Util {
2021-06-11 14:02:28 +02:00
return Instant.now().toEpochMilli();
}
- 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 {
2021-06-13 10:26:58 +02:00
- executorService = new ForkJoinPool(i, (forkJoinPool) -> {
- ForkJoinWorkerThread forkJoinWorkerThread = new ForkJoinWorkerThread(forkJoinPool) {
2021-11-24 00:56:42 +01:00
+ executorService = new java.util.concurrent.ThreadPoolExecutor(i, i,0L, TimeUnit.MILLISECONDS, new java.util.concurrent.LinkedBlockingQueue<Runnable>(), target -> new net.minecraft.server.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) {
2022-06-08 02:42:07 +02:00
@@ -144,6 +148,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
2022-06-10 16:15:38 +02:00
index 7bec6d7b376e382c6266456314eda3ea1cc885ec..3bd5f3249290a3e844b7e88625c2585df398af71 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
2022-06-08 02:42:07 +02:00
@@ -303,6 +303,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
2021-06-11 14:02:28 +02:00
S s0 = serverFactory.apply(thread); // CraftBukkit - decompile error
atomicreference.set(s0);
+ thread.setPriority(Thread.NORM_PRIORITY+2); // Paper - boost priority
thread.start();
return s0;
}
diff --git a/src/main/java/net/minecraft/server/ServerWorkerThread.java b/src/main/java/net/minecraft/server/ServerWorkerThread.java
new file mode 100644
2021-06-13 10:26:58 +02:00
index 0000000000000000000000000000000000000000..480129f430da33157342becb9d3b010f9f7c5edd
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/net/minecraft/server/ServerWorkerThread.java
@@ -0,0 +1,14 @@
+package net.minecraft.server;
+
+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);
2021-06-13 10:26:58 +02:00
+ this.setUncaughtExceptionHandler(Util::onThreadException);
2021-06-11 14:02:28 +02:00
+ }
+}