2020-06-02 08:19:07 +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.
2021-03-16 08:19:45 +01:00
diff --git a/src/main/java/net/minecraft/SystemUtils.java b/src/main/java/net/minecraft/SystemUtils.java
2021-03-16 14:04:28 +01:00
index 68ce7605bd63ea280b96db8230463d2afb0a6cb1..46d82c1548088b8305f758699388edf0d5d4d050 100644
2021-03-16 08:19:45 +01:00
--- a/src/main/java/net/minecraft/SystemUtils.java
+++ b/src/main/java/net/minecraft/SystemUtils.java
2021-03-16 14:04:28 +01:00
@@ -45,6 +45,7 @@ import java.util.stream.Stream;
import javax.annotation.Nullable;
import net.minecraft.resources.MinecraftKey;
import net.minecraft.server.DispenserRegistry;
+import net.minecraft.server.ServerWorkerThread;
import net.minecraft.util.MathHelper;
import net.minecraft.util.datafix.DataConverterRegistry;
import net.minecraft.world.level.block.state.properties.IBlockState;
@@ -54,8 +55,8 @@ import org.apache.logging.log4j.Logger;
2020-09-12 17:23:34 +02:00
public class SystemUtils {
private static final AtomicInteger c = new AtomicInteger(1);
- private static final ExecutorService d = a("Bootstrap");
- private static final ExecutorService e = a("Main");
+ private static final ExecutorService d = a("Bootstrap", -2); // Paper - add -2 priority
+ private static final ExecutorService e = a("Main", -1); // Paper - add -1 priority
private static final ExecutorService f = n();
public static LongSupplier a = System::nanoTime;
public static final UUID b = new UUID(0L, 0L); public static final UUID getNullUUID() {return b;} // Paper OBFHELPER
2021-03-16 14:04:28 +01:00
@@ -85,15 +86,18 @@ public class SystemUtils {
2020-09-12 17:23:34 +02:00
return Instant.now().toEpochMilli();
2020-06-02 08:19:07 +02:00
}
2020-09-12 17:23:34 +02:00
- private static ExecutorService a(String s) {
2020-06-02 08:19:07 +02:00
- int i = MathHelper.clamp(Runtime.getRuntime().availableProcessors() - 1, 1, 7);
- Object object;
2020-09-12 17:23:34 +02:00
+ private static ExecutorService a(String s, int priorityModifier) { // Paper - add priority
2020-06-02 08:19:07 +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);
+ ExecutorService object;
if (i <= 0) {
object = MoreExecutors.newDirectExecutorService();
} else {
- object = new ForkJoinPool(i, (forkjoinpool) -> {
- ForkJoinWorkerThread forkjoinworkerthread = new ForkJoinWorkerThread(forkjoinpool) {
2020-09-12 17:23:34 +02:00
+ object = new java.util.concurrent.ThreadPoolExecutor(i, i,0L, TimeUnit.MILLISECONDS, new java.util.concurrent.LinkedBlockingQueue<Runnable>(), target -> new ServerWorkerThread(target, s, priorityModifier));
2020-06-02 08:19:07 +02:00
+ }
+ /*
protected void onTermination(Throwable throwable) {
if (throwable != null) {
SystemUtils.LOGGER.warn("{} died", this.getName(), throwable);
2021-03-16 14:04:28 +01:00
@@ -109,6 +113,7 @@ public class SystemUtils {
2020-06-25 16:09:55 +02:00
return forkjoinworkerthread;
}, SystemUtils::a, true);
}
2020-06-02 08:19:07 +02:00
+ }*/ // Paper end
return (ExecutorService) object;
}
2021-03-16 14:04:28 +01:00
@@ -157,6 +162,7 @@ public class SystemUtils {
2020-09-12 17:23:34 +02:00
});
}
+ public static void onThreadError(Thread thread, Throwable throwable) { a(thread, throwable); } // Paper - OBFHELPER
private static void a(Thread thread, Throwable throwable) {
c(throwable);
if (throwable instanceof CompletionException) {
2021-03-16 08:19:45 +01:00
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
2021-06-11 08:29:15 +02:00
index 52c0dd4f2779125116d9dcccc2aef7a11af92945..7d1ed99a919194296ee08a67cc543bedcf48395a 100644
2021-03-16 08:19:45 +01:00
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
2021-05-11 05:47:51 +02:00
@@ -286,6 +286,7 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
2021-03-16 08:19:45 +01:00
S s0 = function.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
index 0000000000000000000000000000000000000000..4ff28bd2ab2e85a45d79b46c82ebba6a489579ca
--- /dev/null
+++ b/src/main/java/net/minecraft/server/ServerWorkerThread.java
@@ -0,0 +1,14 @@
+package net.minecraft.server;
+
+import net.minecraft.SystemUtils;
+import java.util.concurrent.atomic.AtomicInteger;
+
+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(SystemUtils::onThreadError);
+ }
+}