mirror of
https://github.com/PaperMC/Paper.git
synced 2024-11-23 15:47:44 +01:00
91 lines
4.7 KiB
Diff
91 lines
4.7 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 90d22b75fd33d8355e59b6bab79757c5b436c9d7..a8772417eb54e1a52c490b75b470a502a480f925 100644
|
|
--- a/src/main/java/net/minecraft/Util.java
|
|
+++ b/src/main/java/net/minecraft/Util.java
|
|
@@ -69,8 +69,8 @@ public class Util {
|
|
private static final int DEFAULT_MAX_THREADS = 255;
|
|
private static final String MAX_THREADS_SYSTEM_PROPERTY = "max.bg.threads";
|
|
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 = makeExecutor("Bootstrap", -2); // Paper - add -2 priority
|
|
+ private static final ExecutorService BACKGROUND_EXECUTOR = makeExecutor("Main", -1); // Paper - add -1 priority
|
|
private static final ExecutorService IO_POOL = makeIoExecutor();
|
|
public static LongSupplier timeSource = System::nanoTime;
|
|
public static final Ticker TICKER = new Ticker() {
|
|
@@ -112,14 +112,18 @@ public class Util {
|
|
return Instant.now().toEpochMilli();
|
|
}
|
|
|
|
- private static ExecutorService makeExecutor(String name) {
|
|
- int i = Mth.clamp(Runtime.getRuntime().availableProcessors() - 1, 1, getMaxThreads());
|
|
+ private static ExecutorService makeExecutor(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 executorService;
|
|
+
|
|
if (i <= 0) {
|
|
executorService = MoreExecutors.newDirectExecutorService();
|
|
} else {
|
|
- 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 net.minecraft.server.ServerWorkerThread(target, s, priorityModifier));
|
|
+ }
|
|
+ /*
|
|
@Override
|
|
protected void onTermination(Throwable throwable) {
|
|
if (throwable != null) {
|
|
@@ -135,6 +139,7 @@ public class Util {
|
|
return forkJoinWorkerThread;
|
|
}, Util::onThreadException, true);
|
|
}
|
|
+ }*/ // Paper end
|
|
|
|
return executorService;
|
|
}
|
|
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
index 785df5ec8a5a4e307149edd5ccf7db75f7edb1fc..de10fc8a9d08dbab245292a4291590b54b0aa609 100644
|
|
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
@@ -318,6 +318,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..480129f430da33157342becb9d3b010f9f7c5edd
|
|
--- /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::onThreadException);
|
|
+ }
|
|
+}
|