3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-12-21 05:50:05 +01:00
Paper/patches/server-remapped/0307-Improve-Server-Thread-Pool-and-Thread-Priorities.patch
Mariell Hoversholm a207d14a0e
Rename all patches to correct numbering scheme
Signed-off-by: Mariell Hoversholm <proximyst@proximyst.com>
2021-06-11 15:46:44 +02:00

122 Zeilen
6.0 KiB
Diff

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
index 7b36274718b7cce24ac00530697f145648d52590..cec5ad5052c8cf6059e9b117117846bdb217748f 100644
--- a/src/main/java/net/minecraft/Util.java
+++ b/src/main/java/net/minecraft/Util.java
@@ -45,7 +45,7 @@ import java.util.stream.Stream;
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;
import org.apache.logging.log4j.LogManager;
@@ -54,8 +54,8 @@ import org.apache.logging.log4j.Logger;
public class Util {
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");
+ private static final ExecutorService BOOTSTRAP_EXECUTOR = a("Bootstrap", -2); // Paper - add -2 priority
+ private static final ExecutorService BACKGROUND_EXECUTOR = a("Main", -1); // Paper - add -1 priority
private static final ExecutorService IO_POOL = makeIoExecutor();
public static LongSupplier timeSource = System::nanoTime;
public static final UUID NIL_UUID = new UUID(0L, 0L); public static final UUID getNullUUID() {return NIL_UUID;} // Paper OBFHELPER
@@ -85,30 +85,34 @@ public class Util {
return Instant.now().toEpochMilli();
}
- private static ExecutorService makeExecutor(String name) {
- int i = Mth.clamp(Runtime.getRuntime().availableProcessors() - 1, 1, 7);
- Object object;
+ private static ExecutorService a(String s, int priorityModifier) { // Paper - add priority
+ // 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) {
+ object = new java.util.concurrent.ThreadPoolExecutor(i, i,0L, TimeUnit.MILLISECONDS, new java.util.concurrent.LinkedBlockingQueue<Runnable>(), target -> new ServerWorkerThread(target, s, priorityModifier));
+ }
+ /*
protected void onTermination(Throwable throwable) {
if (throwable != null) {
- Util.LOGGER.warn("{} died", this.getName(), throwable);
+ SystemUtils.LOGGER.warn("{} died", this.getName(), throwable);
} else {
- Util.LOGGER.debug("{} shutdown", this.getName());
+ SystemUtils.LOGGER.debug("{} shutdown", this.getName());
}
super.onTermination(throwable);
}
};
- forkjoinworkerthread.setName("Worker-" + name + "-" + Util.WORKER_COUNT.getAndIncrement());
+ forkjoinworkerthread.setName("Worker-" + s + "-" + SystemUtils.c.getAndIncrement());
return forkjoinworkerthread;
- }, Util::onThreadException, true);
+ }, SystemUtils::a, true);
}
+ }*/ // Paper end
return (ExecutorService) object;
}
@@ -157,6 +161,7 @@ public class Util {
});
}
+ public static void onThreadError(Thread thread, Throwable throwable) { onThreadException(thread, throwable); } // Paper - OBFHELPER
private static void onThreadException(Thread thread, Throwable throwable) {
pauseInIde(throwable);
if (throwable instanceof CompletionException) {
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 5a76ca77b974ff6fe862c9e05a88b507a34b44be..5faa8f3cc251b6687e33e40009db98d2aee48f2c 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -284,6 +284,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
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
index 0000000000000000000000000000000000000000..4313e21463c16ea9a3edc81763d40702f9d3930b
--- /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);
+ this.setUncaughtExceptionHandler(Util::onThreadError);
+ }
+}