testserver/patches/server/0284-Improve-Server-Thread-...

110 lines
5.4 KiB
Diff
Raw Normal View History

2021-06-11 12:02:28 +00: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
2021-06-13 08:26:58 +00:00
index 6f29d1fc437764a75d592ccb0c0ddc0593a89967..99d52a9643efe38b449b59b6aac81c5b20ed9477 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/Util.java
+++ b/src/main/java/net/minecraft/Util.java
2021-06-13 08:26:58 +00:00
@@ -56,7 +56,7 @@ import java.util.stream.Stream;
2021-06-11 12:02:28 +00:00
import javax.annotation.Nullable;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.Bootstrap;
-import net.minecraft.util.Mth;
+import net.minecraft.server.ServerWorkerThread;
import net.minecraft.util.datafix.DataFixers;
import net.minecraft.world.level.block.state.properties.Property;
2021-06-13 08:26:58 +00:00
import org.apache.commons.io.IOUtils;
@@ -65,8 +65,8 @@ import org.apache.logging.log4j.Logger;
2021-06-11 12:02:28 +00:00
2021-06-13 08:26:58 +00:00
public class Util {
2021-06-11 12:02:28 +00: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 08:26:58 +00: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 12:02:28 +00:00
private static final ExecutorService IO_POOL = makeIoExecutor();
public static LongSupplier timeSource = System::nanoTime;
2021-06-13 08:26:58 +00:00
public static final UUID NIL_UUID = new UUID(0L, 0L);
@@ -101,14 +101,18 @@ public class Util {
2021-06-11 12:02:28 +00:00
return Instant.now().toEpochMilli();
}
- private static ExecutorService makeExecutor(String name) {
- int i = Mth.clamp(Runtime.getRuntime().availableProcessors() - 1, 1, 7);
2021-06-13 08:26:58 +00:00
+ private static ExecutorService makeExecutor(String s, int priorityModifier) { // Paper - add priority
2021-06-11 12:02:28 +00: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 08:26:58 +00:00
ExecutorService executorService;
+
2021-06-11 12:02:28 +00:00
if (i <= 0) {
2021-06-13 08:26:58 +00:00
executorService = MoreExecutors.newDirectExecutorService();
2021-06-11 12:02:28 +00:00
} else {
2021-06-13 08:26:58 +00:00
- executorService = new ForkJoinPool(i, (forkJoinPool) -> {
- ForkJoinWorkerThread forkJoinWorkerThread = new ForkJoinWorkerThread(forkJoinPool) {
+ executorService = new java.util.concurrent.ThreadPoolExecutor(i, i,0L, TimeUnit.MILLISECONDS, new java.util.concurrent.LinkedBlockingQueue<Runnable>(), target -> new ServerWorkerThread(target, s, priorityModifier));
2021-06-11 12:02:28 +00:00
+ }
+ /*
2021-06-13 08:26:58 +00:00
@Override
2021-06-11 12:02:28 +00:00
protected void onTermination(Throwable throwable) {
if (throwable != null) {
2021-06-13 08:26:58 +00:00
@@ -124,6 +128,7 @@ public class Util {
return forkJoinWorkerThread;
}, Util::onThreadException, true);
2021-06-11 12:02:28 +00:00
}
+ }*/ // Paper end
2021-06-13 08:26:58 +00:00
return executorService;
2021-06-11 12:02:28 +00:00
}
2021-06-13 08:26:58 +00:00
@@ -180,7 +185,7 @@ public class Util {
throw t instanceof RuntimeException ? (RuntimeException)t : new RuntimeException(t);
2021-06-11 12:02:28 +00:00
}
2021-06-13 08:26:58 +00:00
- private static void onThreadException(Thread thread, Throwable t) {
+ public static void onThreadException(Thread thread, Throwable t) { // Paper - make public
pauseInIde(t);
if (t instanceof CompletionException) {
t = t.getCause();
2021-06-11 12:02:28 +00:00
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
2021-06-13 08:26:58 +00:00
index 24fc2eb3ee067a4164db166aa3e07ecbb426bbba..a302d232da3fbaa2cb3e1903cfd096d404847c54 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
2021-06-13 08:26:58 +00:00
@@ -318,6 +318,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
2021-06-11 12:02:28 +00: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 08:26:58 +00:00
index 0000000000000000000000000000000000000000..480129f430da33157342becb9d3b010f9f7c5edd
2021-06-11 12:02:28 +00: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 08:26:58 +00:00
+ this.setUncaughtExceptionHandler(Util::onThreadException);
2021-06-11 12:02:28 +00:00
+ }
+}