aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0260-Improve-Server-Thread-Pool-and-Thread-Priorities.patch
blob: 3d0645bfc41c3297c9577963ec2572577c262cb4 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
and reduce worldgen thread worker count for low core count CPUs.

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

Co-authored-by: Spottedleaf <Spottedleaf@users.noreply.github.com>

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 8cac2075077b1d9c2b01e09c99780ff9e204abb2..bf2833c92eca6491699b4a89410e4e46b5bbe4d1 100644
--- a/src/main/java/net/minecraft/Util.java
+++ b/src/main/java/net/minecraft/Util.java
@@ -92,7 +92,7 @@ public class Util {
     private static final int DEFAULT_MAX_THREADS = 255;
     private static final int DEFAULT_SAFE_FILE_OPERATION_RETRIES = 10;
     private static final String MAX_THREADS_SYSTEM_PROPERTY = "max.bg.threads";
-    private static final TracingExecutor BACKGROUND_EXECUTOR = makeExecutor("Main");
+    private static final TracingExecutor BACKGROUND_EXECUTOR = makeExecutor("Main", -1); // Paper - Perf: add priority
     private static final TracingExecutor IO_POOL = makeIoExecutor("IO-Worker-", false);
     private static final TracingExecutor DOWNLOAD_POOL = makeIoExecutor("Download-", true);
     // Paper start - don't submit BLOCKING PROFILE LOOKUPS to the world gen thread
@@ -163,15 +163,28 @@ public class Util {
         return FILENAME_DATE_TIME_FORMATTER.format(ZonedDateTime.now());
     }
 
-    private static TracingExecutor makeExecutor(String name) {
-        int i = Mth.clamp(Runtime.getRuntime().availableProcessors() - 1, 1, getMaxThreads());
+    private static TracingExecutor makeExecutor(String s, final int priorityModifier) { // Paper - Perf: add priority
+        // Paper start - Perf: use simpler thread pool that allows 1 thread and reduce worldgen thread worker count for low core count CPUs
+        int cpus = Runtime.getRuntime().availableProcessors() / 2;
+        int i;
+        if (cpus <= 4) {
+            i = cpus <= 2 ? 1 : 2;
+        } else if (cpus <= 8) {
+            // [5, 8]
+            i = Math.max(3, cpus - 2);
+        } else {
+            i = cpus * 2 / 3;
+        }
+        i = Math.min(8, i);
+        i = Integer.getInteger("Paper.WorkerThreadCount", i);
+
         ExecutorService executorService;
         if (i <= 0) {
             executorService = MoreExecutors.newDirectExecutorService();
         } else {
-            AtomicInteger atomicInteger = new AtomicInteger(1);
-            executorService = new ForkJoinPool(i, pool -> {
-                final String string2 = "Worker-" + name + "-" + atomicInteger.getAndIncrement();
+            executorService = new java.util.concurrent.ThreadPoolExecutor(i, i,0L, TimeUnit.MILLISECONDS, new java.util.concurrent.LinkedBlockingQueue<>(), target -> new io.papermc.paper.util.ServerWorkerThread(target, s, priorityModifier));
+        }
+        /*      final String string2 = "Worker-" + name + "-" + atomicInteger.getAndIncrement();
                 ForkJoinWorkerThread forkJoinWorkerThread = new ForkJoinWorkerThread(pool) {
                     @Override
                     protected void onStart() {
@@ -193,7 +206,7 @@ public class Util {
                 forkJoinWorkerThread.setName(string2);
                 return forkJoinWorkerThread;
             }, Util::onThreadException, true);
-        }
+        }*/
 
         return new TracingExecutor(executorService);
     }
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index cc15ec47155ee16377a65c9f56a62339dc0a129d..a35e1a741f042d7ac8a21ef24f04fbef96e87825 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -333,6 +333,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
         thread.setUncaughtExceptionHandler((thread1, throwable) -> {
             MinecraftServer.LOGGER.error("Uncaught exception in server thread", throwable);
         });
+        thread.setPriority(Thread.NORM_PRIORITY+2); // Paper - Perf: Boost priority
         if (Runtime.getRuntime().availableProcessors() > 4) {
             thread.setPriority(8);
         }