2021-06-11 14:02:28 +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
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-01-19 12:30:04 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								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.
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 14:02:28 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-11-19 15:53:20 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								== AT ==
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								public net.minecraft.Util onThreadException(Ljava/lang/Thread;Ljava/lang/Throwable;)V
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-01-19 12:30:04 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Co-authored-by: Spottedleaf <Spottedleaf@users.noreply.github.com>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-10-24 12:43:46 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								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..0000000000000000000000000000000000000000
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								--- /dev/null
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+++ b/src/main/java/io/papermc/paper/util/ServerWorkerThread.java
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								@@ -0,0 +0,0 @@
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+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);
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+    }
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+}
 
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 14:02:28 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								diff --git a/src/main/java/net/minecraft/Util.java b/src/main/java/net/minecraft/Util.java
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								--- a/src/main/java/net/minecraft/Util.java
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+++ b/src/main/java/net/minecraft/Util.java
 
							 
						 
					
						
							
								
									
										
										
										
											2021-11-23 15:56:42 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								@@ -0,0 +0,0 @@ public class Util {
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								     private static final int DEFAULT_MAX_THREADS = 255;
							 
						 
					
						
							
								
									
										
										
										
											2023-12-05 15:21:44 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								     private static final int DEFAULT_SAFE_FILE_OPERATION_RETRIES = 10;
							 
						 
					
						
							
								
									
										
										
										
											2021-11-23 15:56:42 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								     private static final String MAX_THREADS_SYSTEM_PROPERTY = "max.bg.threads";
							 
						 
					
						
							
								
									
										
										
										
											2024-10-23 14:12:17 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								-    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);
							 
						 
					
						
							
								
									
										
										
										
											2022-07-03 14:55:56 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								     // Paper start - don't submit BLOCKING PROFILE LOOKUPS to the world gen thread
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 14:02:28 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								@@ -0,0 +0,0 @@ public class Util {
 
							 
						 
					
						
							
								
									
										
										
										
											2022-07-27 21:49:24 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								         return FILENAME_DATE_TIME_FORMATTER.format(ZonedDateTime.now());
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 14:02:28 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								     }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								 
							 
						 
					
						
							
								
									
										
										
										
											2024-10-23 14:12:17 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								-    private static TracingExecutor makeExecutor(String name) {
 
							 
						 
					
						
							
								
									
										
										
										
											2021-11-23 15:56:42 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								-        int i = Mth.clamp(Runtime.getRuntime().availableProcessors() - 1, 1, getMaxThreads());
 
							 
						 
					
						
							
								
									
										
										
										
											2024-10-23 14:12:17 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								+    private static TracingExecutor makeExecutor(String s, final int priorityModifier) { // Paper - Perf: add priority
 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-21 13:56:22 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								+        // Paper start - Perf: use simpler thread pool that allows 1 thread and reduce worldgen thread worker count for low core count CPUs
 
							 
						 
					
						
							
								
									
										
										
										
											2024-01-19 12:30:04 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								+        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);
 
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 14:02:28 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								+        i = Integer.getInteger("Paper.WorkerThreadCount", i);
 
							 
						 
					
						
							
								
									
										
										
										
											2024-10-23 14:12:17 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								+
 
							 
						 
					
						
							
								
									
										
										
										
											2021-06-13 01:26:58 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								         ExecutorService executorService;
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 14:02:28 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								         if (i <= 0) {
							 
						 
					
						
							
								
									
										
										
										
											2021-06-13 01:26:58 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								             executorService = MoreExecutors.newDirectExecutorService();
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 14:02:28 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								         } else {
							 
						 
					
						
							
								
									
										
										
										
											2023-12-05 15:21:44 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								-            AtomicInteger atomicInteger = new AtomicInteger(1);
 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-12 12:14:06 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								-            executorService = new ForkJoinPool(i, pool -> {
 
							 
						 
					
						
							
								
									
										
										
										
											2024-10-23 14:12:17 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								-                final String string2 = "Worker-" + name + "-" + atomicInteger.getAndIncrement();
 
							 
						 
					
						
							
								
									
										
										
										
											2023-12-05 15:21:44 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								+            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));
 
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 14:02:28 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								+        }
 
							 
						 
					
						
							
								
									
										
										
										
											2024-10-23 14:12:17 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								+        /*      final String string2 = "Worker-" + name + "-" + atomicInteger.getAndIncrement();
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								                 ForkJoinWorkerThread forkJoinWorkerThread = new ForkJoinWorkerThread(pool) {
							 
						 
					
						
							
								
									
										
										
										
											2021-06-13 01:26:58 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								                     @Override
							 
						 
					
						
							
								
									
										
										
										
											2024-10-23 14:12:17 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								                     protected void onStart() {
							 
						 
					
						
							
								
									
										
										
										
											2021-06-13 01:26:58 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								@@ -0,0 +0,0 @@ public class Util {
 
							 
						 
					
						
							
								
									
										
										
										
											2024-10-23 14:12:17 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								                 forkJoinWorkerThread.setName(string2);
							 
						 
					
						
							
								
									
										
										
										
											2021-06-13 01:26:58 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								                 return forkJoinWorkerThread;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								             }, Util::onThreadException, true);
							 
						 
					
						
							
								
									
										
										
										
											2024-10-23 14:12:17 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								-        }
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+        }*/
 
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 14:02:28 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								 
							 
						 
					
						
							
								
									
										
										
										
											2024-10-23 14:12:17 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								         return new TracingExecutor(executorService);
							 
						 
					
						
							
								
									
										
										
										
											2021-06-11 14:02:28 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								     }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								--- a/src/main/java/net/minecraft/server/MinecraftServer.java
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								@@ -0,0 +0,0 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
 
							 
						 
					
						
							
								
									
										
										
										
											2023-08-15 19:37:17 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								         thread.setUncaughtExceptionHandler((thread1, throwable) -> {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								             MinecraftServer.LOGGER.error("Uncaught exception in server thread", throwable);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								         });
							 
						 
					
						
							
								
									
										
										
										
											2024-01-21 13:56:22 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								+        thread.setPriority(Thread.NORM_PRIORITY+2); // Paper - Perf: Boost priority
 
							 
						 
					
						
							
								
									
										
										
										
											2023-08-15 19:37:17 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								         if (Runtime.getRuntime().availableProcessors() > 4) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								             thread.setPriority(8);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								         }