From 9ad7e2fe1547cbc2dcff91831447aca9ca0d4898 Mon Sep 17 00:00:00 2001 From: Aikar Date: Tue, 2 Jun 2020 02:19:07 -0400 Subject: [PATCH] Improve Thread Pool usage to allow single threads for single cpu servers Switch to a standard fixed size ThreadPoolExecutor as we don't use the advanced capabilities of a ForkJoinPool. ForkJoinPool does not allow single threads, and really rather not use 2 different executor types based on core count. Also, change thread priorities so that main thread is prioritized by the OS at a higher priority than the other threads. May not help too much but it at least signals the OS the information to know main is more important. --- .../Asynchronous-chunk-IO-and-loading.patch | 13 --- ...er-Thread-Pool-and-Thread-Priorities.patch | 90 +++++++++++++++++++ ...ble-thread-count-default-for-bootstr.patch | 20 ----- 3 files changed, 90 insertions(+), 33 deletions(-) create mode 100644 Spigot-Server-Patches/Improve-Server-Thread-Pool-and-Thread-Priorities.patch delete mode 100644 Spigot-Server-Patches/Use-more-reasonable-thread-count-default-for-bootstr.patch diff --git a/Spigot-Server-Patches/Asynchronous-chunk-IO-and-loading.patch b/Spigot-Server-Patches/Asynchronous-chunk-IO-and-loading.patch index 27be20fd00..432a202126 100644 --- a/Spigot-Server-Patches/Asynchronous-chunk-IO-and-loading.patch +++ b/Spigot-Server-Patches/Asynchronous-chunk-IO-and-loading.patch @@ -3830,19 +3830,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 } + // Paper end } -diff --git a/src/main/java/net/minecraft/server/SystemUtils.java b/src/main/java/net/minecraft/server/SystemUtils.java -index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 ---- a/src/main/java/net/minecraft/server/SystemUtils.java -+++ b/src/main/java/net/minecraft/server/SystemUtils.java -@@ -0,0 +0,0 @@ public class SystemUtils { - i = Integer.getInteger("Paper.WorkerThreadCount", i); // Paper - allow overriding - Object object; - -- if (i <= 0) { -+ if (i <= 0 || (Runtime.getRuntime().availableProcessors() == 1 && !Boolean.getBoolean("Paper.allowAsyncChunksSingleCore"))) { // Paper - disable server worker queue if single core system - object = MoreExecutors.newDirectExecutorService(); - } else { - object = new ForkJoinPool(i, (forkjoinpool) -> { diff --git a/src/main/java/net/minecraft/server/TicketType.java b/src/main/java/net/minecraft/server/TicketType.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/net/minecraft/server/TicketType.java diff --git a/Spigot-Server-Patches/Improve-Server-Thread-Pool-and-Thread-Priorities.patch b/Spigot-Server-Patches/Improve-Server-Thread-Pool-and-Thread-Priorities.patch new file mode 100644 index 0000000000..29d53e3fb5 --- /dev/null +++ b/Spigot-Server-Patches/Improve-Server-Thread-Pool-and-Thread-Priorities.patch @@ -0,0 +1,90 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Aikar +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/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 IAsyncTaskHandlerReentrant { ++ if (throwable instanceof CompletionException) { ++ throwable = throwable.getCause(); ++ } ++ ++ if (throwable instanceof ReportedException) { ++ DispenserRegistry.a(((ReportedException) throwable).a().e()); ++ System.exit(-1); ++ } ++ ++ MinecraftServer.LOGGER.error(String.format("Caught exception in thread %s", thread), throwable); ++ }); ++ } ++} +diff --git a/src/main/java/net/minecraft/server/SystemUtils.java b/src/main/java/net/minecraft/server/SystemUtils.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/net/minecraft/server/SystemUtils.java ++++ b/src/main/java/net/minecraft/server/SystemUtils.java +@@ -0,0 +0,0 @@ public class SystemUtils { + } + + private static ExecutorService k() { +- int i = MathHelper.clamp(Runtime.getRuntime().availableProcessors() - 1, 1, 7); +- Object object; ++ // 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(), ServerWorkerThread::new); ++ } ++ /* + protected void onTermination(Throwable throwable) { + if (throwable != null) { + SystemUtils.LOGGER.warn("{} died", this.getName(), throwable); +@@ -0,0 +0,0 @@ public class SystemUtils { + + SystemUtils.LOGGER.error(String.format("Caught exception in thread %s", thread), throwable); + }, true); +- } ++ }*/ // Paper end + + return (ExecutorService) object; + } diff --git a/Spigot-Server-Patches/Use-more-reasonable-thread-count-default-for-bootstr.patch b/Spigot-Server-Patches/Use-more-reasonable-thread-count-default-for-bootstr.patch deleted file mode 100644 index 0d55658d41..0000000000 --- a/Spigot-Server-Patches/Use-more-reasonable-thread-count-default-for-bootstr.patch +++ /dev/null @@ -1,20 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: Aikar -Date: Tue, 23 Oct 2018 23:14:38 -0400 -Subject: [PATCH] Use more reasonable thread count default for bootstrap - - -diff --git a/src/main/java/net/minecraft/server/SystemUtils.java b/src/main/java/net/minecraft/server/SystemUtils.java -index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 ---- a/src/main/java/net/minecraft/server/SystemUtils.java -+++ b/src/main/java/net/minecraft/server/SystemUtils.java -@@ -0,0 +0,0 @@ public class SystemUtils { - } - - private static ExecutorService k() { -- int i = MathHelper.clamp(Runtime.getRuntime().availableProcessors() - 1, 1, 7); -+ int i = Math.min(8, Math.max(Runtime.getRuntime().availableProcessors() - 2, 2)); // Paper - use more reasonable default - 2 is hard minimum to avoid using unlimited threads -+ i = Integer.getInteger("Paper.WorkerThreadCount", i); // Paper - allow overriding - Object object; - - if (i <= 0) {