aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0276-Improve-Server-Thread-Pool-and-Thread-Priorities.patch
blob: d3cd6525a9bbbbe2168c66da67090fb583cf1cf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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.

== AT ==
public net.minecraft.Util onThreadException(Ljava/lang/Thread;Ljava/lang/Throwable;)V

diff --git a/src/main/java/io/papermc/paper/util/ServerWorkerThread.java b/src/main/java/io/papermc/paper/util/ServerWorkerThread.java
new file mode 100644
index 0000000000000000000000000000000000000000..b60f59cf5cc8eb84a6055b7861857dece7f2501b
--- /dev/null
+++ b/src/main/java/io/papermc/paper/util/ServerWorkerThread.java
@@ -0,0 +1,14 @@
+package io.papermc.paper.util;
+
+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);
+    }
+}
diff --git a/src/main/java/net/minecraft/Util.java b/src/main/java/net/minecraft/Util.java
index 0f05d26248d8c999048a88796df227a6a1e3755f..7354711e194ab58b11b68f447c1fc795fe611a65 100644
--- a/src/main/java/net/minecraft/Util.java
+++ b/src/main/java/net/minecraft/Util.java
@@ -81,7 +81,7 @@ 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 BACKGROUND_EXECUTOR = makeExecutor("Main");
+    private static final ExecutorService BACKGROUND_EXECUTOR = makeExecutor("Main", -1); // Paper - add -1 priority
     // Paper start - don't submit BLOCKING PROFILE LOOKUPS to the world gen thread
     public static final ExecutorService PROFILE_EXECUTOR = Executors.newFixedThreadPool(2, new java.util.concurrent.ThreadFactory() {
 
@@ -144,14 +144,18 @@ public class Util {
         return FILENAME_DATE_TIME_FORMATTER.format(ZonedDateTime.now());
     }
 
-    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 io.papermc.paper.util.ServerWorkerThread(target, s, priorityModifier));
+        }
+        /*
                     @Override
                     protected void onTermination(Throwable throwable) {
                         if (throwable != null) {
@@ -167,6 +171,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 c7f665cd6879603ee58c85f04c7ae85737e3fc35..fb9bc736bddcc992677b8dff5295a4df843f50c4 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -321,6 +321,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;
     }