From 1f0b85d4e48a1b69c1c883ab2ee32082bd113404 Mon Sep 17 00:00:00 2001 From: Euphyllia Bierque Date: Tue, 28 Jan 2025 16:36:06 -0800 Subject: [PATCH] Add API to retrieve TPS from a specified location Resolves https://github.com/PaperMC/Folia/pull/310 --- .../features/0004-Add-TPS-From-Region.patch | 92 +++++++++++++++++++ .../features/0006-Add-TPS-From-Region.patch | 79 ++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 folia-api/paper-patches/features/0004-Add-TPS-From-Region.patch create mode 100644 folia-server/paper-patches/features/0006-Add-TPS-From-Region.patch diff --git a/folia-api/paper-patches/features/0004-Add-TPS-From-Region.patch b/folia-api/paper-patches/features/0004-Add-TPS-From-Region.patch new file mode 100644 index 0000000000..7d1cbb85db --- /dev/null +++ b/folia-api/paper-patches/features/0004-Add-TPS-From-Region.patch @@ -0,0 +1,92 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Euphyllia Bierque +Date: Tue, 28 Jan 2025 16:34:17 -0800 +Subject: [PATCH] Add TPS From Region + + +diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java +index 9196b1e62b328b1e9790b966600aba9681dd0ddc..86192033979d78f6308451b024995eac963af9ab 100644 +--- a/src/main/java/org/bukkit/Bukkit.java ++++ b/src/main/java/org/bukkit/Bukkit.java +@@ -2975,6 +2975,42 @@ public final class Bukkit { + return server.isGlobalTickThread(); + } + // Paper end - Folia region threading API ++ // Folia start - region TPS API ++ /** ++ * Gets the TPS from the region which owns the specified location, or {@code null} if no region owns ++ * the specified location. ++ * ++ * @param location The location for which to get the TPS ++ * @return TPS (5s, 15s, 1m, 5m, 15m), or null if the region doesn't exist ++ */ ++ public static double @Nullable [] getRegionTPS(@NotNull Location location) { ++ return server.getRegionTPS(location); ++ } ++ ++ /** ++ * Gets the TPS from the region which owns the specified chunk, or {@code null} if no region owns ++ * the specified location. ++ * ++ * @param chunk - The specified chunk ++ * @return TPS (5s, 15s, 1m, 5m, 15m), or null if the region doesn't exist ++ */ ++ public static double @Nullable [] getRegionTPS(@NotNull Chunk chunk) { ++ return server.getRegionTPS(chunk); ++ } ++ ++ /** ++ * Gets the TPS from the region which owns the specified chunk, or {@code null} if no region owns ++ * the specified location. ++ * ++ * @param world - World containing the chunk ++ * @param chunkX - X-coordinate of the chunk ++ * @param chunkZ - Z-coordinate of the chunk ++ * @return TPS (5s, 15s, 1m, 5m, 15m), or null if the region doesn't exist ++ */ ++ public static double @Nullable [] getRegionTPS(@NotNull World world, int chunkX, int chunkZ) { ++ return server.getRegionTPS(world, chunkX, chunkZ); ++ } ++ // Folia end - region TPS API + + @NotNull + public static Server.Spigot spigot() { +diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java +index 11923ef0ea75f702273ba5481ac6d46cc0f17697..4cad7f842418b0702575db29cc79dcfcc207d589 100644 +--- a/src/main/java/org/bukkit/Server.java ++++ b/src/main/java/org/bukkit/Server.java +@@ -2654,4 +2654,34 @@ public interface Server extends PluginMessageRecipient, net.kyori.adventure.audi + */ + void allowPausing(@NotNull org.bukkit.plugin.Plugin plugin, boolean value); + // Paper end - API to check if the server is sleeping ++ // Folia start - region TPS API ++ /** ++ * Gets the TPS from the region which owns the specified location, or {@code null} if no region owns ++ * the specified location. ++ * ++ * @param location The location for which to get the TPS ++ * @return TPS (5s, 15s, 1m, 5m, 15m), or null if the region doesn't exist ++ */ ++ double @Nullable [] getRegionTPS(@NotNull Location location); ++ ++ /** ++ * Gets the TPS from the region which owns the specified chunk, or {@code null} if no region owns ++ * the specified location. ++ * ++ * @param chunk - The specified chunk ++ * @return TPS (5s, 15s, 1m, 5m, 15m), or null if the region doesn't exist ++ */ ++ double @Nullable [] getRegionTPS(@NotNull Chunk chunk); ++ ++ /** ++ * Gets the TPS from the region which owns the specified chunk, or {@code null} if no region owns ++ * the specified location. ++ * ++ * @param world - World containing the chunk ++ * @param chunkX - X-coordinate of the chunk ++ * @param chunkZ - Z-coordinate of the chunk ++ * @return TPS (5s, 15s, 1m, 5m, 15m), or null if the region doesn't exist ++ */ ++ double @Nullable [] getRegionTPS(@NotNull World world, int chunkX, int chunkZ); ++ // Folia end - region TPS API + } diff --git a/folia-server/paper-patches/features/0006-Add-TPS-From-Region.patch b/folia-server/paper-patches/features/0006-Add-TPS-From-Region.patch new file mode 100644 index 0000000000..cd2165b910 --- /dev/null +++ b/folia-server/paper-patches/features/0006-Add-TPS-From-Region.patch @@ -0,0 +1,79 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Euphyllia Bierque +Date: Tue, 28 Jan 2025 16:35:13 -0800 +Subject: [PATCH] Add TPS From Region + + +diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java +index 20bcfeef39746d547ef17ccf2b66d44ce1e6e354..755527ed59b50c95aeca71d0fc5fea224731ff51 100644 +--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java ++++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java +@@ -3333,4 +3333,68 @@ public final class CraftServer implements Server { + this.console.addPluginAllowingSleep(plugin.getName(), value); + } + // Paper end - API to check if the server is sleeping ++ // Folia start - region TPS API ++ /** ++ * Gets the TPS from the region which owns the specified location, or {@code null} if no region owns ++ * the specified location. ++ * ++ * @param location The location for which to get the TPS ++ * @return TPS (5s, 15s, 1m, 5m, 15m), or null if the region doesn't exist ++ */ ++ @Override ++ public double[] getRegionTPS(Location location) { ++ Preconditions.checkArgument(location != null, "Location cannot be null"); ++ ++ return this.getRegionTPS(location.getWorld(), location.getBlockX() >> 4, location.getBlockZ() >> 4); ++ } ++ ++ /** ++ * Gets the TPS from the region which owns the specified chunk, or {@code null} if no region owns ++ * the specified location. ++ * ++ * @param chunk - The specified chunk ++ * @return TPS (5s, 15s, 1m, 5m, 15m), or null if the region doesn't exist ++ */ ++ @Override ++ public double[] getRegionTPS(org.bukkit.Chunk chunk) { ++ Preconditions.checkArgument(chunk != null, "Chunk cannot be null"); ++ ++ return this.getRegionTPS(chunk.getWorld(), chunk.getX(), chunk.getZ()); ++ } ++ ++ /** ++ * Gets the TPS from the region which owns the specified chunk, or {@code null} if no region owns ++ * the specified location. ++ * ++ * @param world - World containing the chunk ++ * @param chunkX - X-coordinate of the chunk ++ * @param chunkZ - Z-coordinate of the chunk ++ * @return TPS (5s, 15s, 1m, 5m, 15m), or null if the region doesn't exist ++ */ ++ @Override ++ public double[] getRegionTPS(World world, int chunkX, int chunkZ) { ++ Preconditions.checkArgument(world != null, "World cannot be null"); ++ ++ return getTPSFromRegion(((CraftWorld)world).getHandle(), chunkX, chunkZ); ++ } ++ ++ private static double[] getTPSFromRegion(ServerLevel world, int chunkX, int chunkZ) { ++ io.papermc.paper.threadedregions.ThreadedRegionizer.ThreadedRegion ++ region = world.regioniser.getRegionAtSynchronised(chunkX, chunkZ); ++ if (region == null) { ++ return null; ++ } else { ++ io.papermc.paper.threadedregions.TickRegions.TickRegionData regionData = region.getData(); ++ final long currTime = System.nanoTime(); ++ final io.papermc.paper.threadedregions.TickRegionScheduler.RegionScheduleHandle regionScheduleHandle = regionData.getRegionSchedulingHandle(); ++ return new double[] { ++ regionScheduleHandle.getTickReport5s(currTime).tpsData().segmentAll().average(), ++ regionScheduleHandle.getTickReport15s(currTime).tpsData().segmentAll().average(), ++ regionScheduleHandle.getTickReport1m(currTime).tpsData().segmentAll().average(), ++ regionScheduleHandle.getTickReport5m(currTime).tpsData().segmentAll().average(), ++ regionScheduleHandle.getTickReport15m(currTime).tpsData().segmentAll().average(), ++ }; ++ } ++ } ++ // Folia end - region TPS API + }