From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Martijn Muijsers Date: Tue, 29 Nov 2022 12:27:47 +0100 Subject: [PATCH] CPU cores estimation License: AGPL-3.0 (https://www.gnu.org/licenses/agpl-3.0.html) Gale - https://galemc.org diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java index 8e45f712968303b7864d61adbf7325142c83f582..b374f41b20b3ffa2ec2874a06715661f4fec83db 100644 --- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java +++ b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java @@ -54,6 +54,7 @@ import net.minecraft.world.level.block.entity.SkullBlockEntity; import net.minecraft.world.level.storage.LevelStorageSource; import org.galemc.gale.command.GaleCommands; import org.galemc.gale.configuration.GaleGlobalConfiguration; +import org.galemc.gale.util.CPUCoresEstimation; import org.slf4j.Logger; // CraftBukkit start @@ -231,6 +232,12 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface io.papermc.paper.brigadier.PaperBrigadierProviderImpl.INSTANCE.getClass(); // init PaperBrigadierProvider // Paper end + // Gale start - CPU core estimation + if (GaleGlobalConfiguration.get().logToConsole.cpuCoresEstimation) { + CPUCoresEstimation.log(LOGGER); + } + // Gale end - CPU core estimation + // Gale start - Pufferfish - SIMD support // Attempt to detect vectorization try { diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java index 947ad1463a973546bdaf68654086291a3414aa9b..69bde99acff7bdae9af7cfe60e2221675a68b858 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -238,6 +238,8 @@ import org.bukkit.scoreboard.Criteria; import org.bukkit.structure.StructureManager; import org.bukkit.util.StringUtil; import org.bukkit.util.permissions.DefaultPermissions; +import org.galemc.gale.configuration.GaleGlobalConfiguration; +import org.galemc.gale.util.CPUCoresEstimation; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.SafeConstructor; import org.yaml.snakeyaml.error.MarkedYAMLException; diff --git a/src/main/java/org/galemc/gale/configuration/GaleGlobalConfiguration.java b/src/main/java/org/galemc/gale/configuration/GaleGlobalConfiguration.java index 8aaa6e3a6c475258bea5f026a7e07efde825f112..6f6b96fe499f1b1888a141b60bd3773ba45e75f6 100644 --- a/src/main/java/org/galemc/gale/configuration/GaleGlobalConfiguration.java +++ b/src/main/java/org/galemc/gale/configuration/GaleGlobalConfiguration.java @@ -85,6 +85,7 @@ public class GaleGlobalConfiguration extends ConfigurationPart { public boolean setBlockInFarChunk = true; // Gale - Purpur - do not log setBlock in far chunks public boolean unrecognizedRecipes = false; // Gale - Purpur - do not log unrecognized recipes public boolean legacyMaterialInitialization = false; // Gale - Purpur - do not log legacy Material initialization + public boolean cpuCoresEstimation = true; // Gale - CPU cores estimation public Chat chat; public class Chat extends ConfigurationPart { diff --git a/src/main/java/org/galemc/gale/util/CPUCoresEstimation.java b/src/main/java/org/galemc/gale/util/CPUCoresEstimation.java new file mode 100644 index 0000000000000000000000000000000000000000..aa93b9f8015b98123411c56c148e5b61e34a912d --- /dev/null +++ b/src/main/java/org/galemc/gale/util/CPUCoresEstimation.java @@ -0,0 +1,102 @@ +// Gale - CPU cores estimation + +package org.galemc.gale.util; + +import org.galemc.gale.executor.annotation.AnyThreadSafe; +import org.galemc.gale.executor.annotation.YieldFree; +import org.slf4j.Logger; +import oshi.SystemInfo; +import oshi.hardware.CentralProcessor; +import oshi.hardware.HardwareAbstractionLayer; + +/** + * A utility class to estimate the number of physical CPU cores. + * + * @author Martijn Muijsers under AGPL-3.0 + */ +@AnyThreadSafe +@YieldFree +public final class CPUCoresEstimation { + + private CPUCoresEstimation() {} + + public static final String environmentVariableKey = "gale.cores"; + + /** + * @return The value of the {@link #environmentVariableKey} environment variable, + * or -1 if not set, or -1 if the environment variable is set to a nonpositive value. + */ + public static int getByEnvironmentVariable() { + int value = Integer.getInteger(environmentVariableKey, -1); + return value > 0 ? value : -1; + } + + /** + * @return The number of cores estimated by OSHI using {@link CentralProcessor#getPhysicalProcessorCount()}, + * or -1 if OSHI throws an exception or returns a nonpositive value. + */ + public static int getByOSHI() { + try { + SystemInfo systemInfo = new SystemInfo(); + HardwareAbstractionLayer hardwareAbstractionLayer = systemInfo.getHardware(); + CentralProcessor centralProcessor = hardwareAbstractionLayer.getProcessor(); + int coresAccordingToOSHI = centralProcessor.getPhysicalProcessorCount(); + if (coresAccordingToOSHI > 0) { + return coresAccordingToOSHI; + } + } catch (Throwable ignored) {} + return -1; + } + + /** + * @return A naive estimation of the number of cores, by taking {@link Runtime#availableProcessors()} and applying + * floored division by 2, since most systems will have hyper-threading with 2 threads per core. If the result is + * nonpositive, this method returns 1. + */ + public static int getByRuntimeProcessors() { + return Math.min(1, Runtime.getRuntime().availableProcessors() / 2); + } + + public static int get() { + // Use the environment variable if set + int environmentCores = getByEnvironmentVariable(); + if (environmentCores > 0) { + return environmentCores; + } + // Use the OSHI library to find the physical processor count + int oshiCores = getByOSHI(); + if (oshiCores > 0) { + return oshiCores; + } + // Make a guess that the number of CPU cores is half the number of available runtime processor + // (i.e. guess that the CPU uses hyper-threading with 2 threads per core) + return getByRuntimeProcessors(); + } + + /** + * Logs the CPU core estimation to the given {@link Logger}. + */ + public static void log(Logger logger) { + String explanation; + int assumedValue; + // Use the environment variable if set + int environmentCores = getByEnvironmentVariable(); + // Use the OSHI library to find the physical processor count + int oshiCores = getByOSHI(); + if (environmentCores >= 1) { + assumedValue = environmentCores; + explanation = "(based on the '" + environmentVariableKey + "' environment variable)" + (oshiCores >= 1 ? " (OSHI detected " + oshiCores + " cores)" : ""); + } else { + explanation = "(because "; + if (oshiCores >= 1) { + assumedValue = oshiCores; + explanation += "OSHI detected " + oshiCores + " physical cores)"; + } else { + assumedValue = getByRuntimeProcessors(); + explanation += "the number of runtime processors is " + Runtime.getRuntime().availableProcessors() + ") (please set the '" + environmentVariableKey + "' environment variable if the number of physical CPU cores is incorrect)"; + } + } + logger.info("The server will assume you have " + assumedValue + " physical CPU cores " + explanation + ""); + } + +}