9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2026-01-04 15:41:40 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0142-Track-each-world-MSPT.patch
Altiami 2d6e6b28f3 SparklyPaper: Parallel world ticking (#246)
* SparklyPaper: Parallel world ticking

* per world mspt (/leaf mspt)

* fix chunk loading off-main violations

* rebase and make tpsbar per world

* temp fix for async chunk sending crash

* add /leaf mspt compact and more cleanup

* TCRF SparklyPaper (Pathothingi): Fix Nether and End portals for non-player entities

* fix Potothingi's name

* change thread name

* fix plugin related async ticks (hopefully)

* Revert "fix plugin related async ticks (hopefully)"

This reverts commit 7a9b79adc538989ecbec162dd377245706522a87.

* Add more config guards

* rebase on upstream

* actually add the paper patches

* fix villagers failing to release poi

* rebase

* make async chunk send work with parallel world ticking again

---------

Co-authored-by: Taiyou06 <kaandindar21@gmail.com>
2025-03-17 17:54:12 +03:00

138 lines
6.2 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Taiyou06 <kaandindar21@gmail.com>
Date: Thu, 6 Mar 2025 00:26:19 +0100
Subject: [PATCH] Track each world MSPT
diff --git a/net/minecraft/server/MinecraftServer.java b/net/minecraft/server/MinecraftServer.java
index dd1c55e983bc1ddc9a77a0b825b78eba62c201ec..f7a3cdb3953a959fb258fcb0eeea81ea2c8b217a 100644
--- a/net/minecraft/server/MinecraftServer.java
+++ b/net/minecraft/server/MinecraftServer.java
@@ -1687,7 +1687,16 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
// Leaf - SparklyPaper parallel world ticking mod (move level ticking logic out for branch convergence)
private void tickLevel(ServerLevel serverLevel, BooleanSupplier hasTimeLeft) {
try {
+ long i = Util.getNanos(); // SparklyPaper - track world's MSPT
serverLevel.tick(hasTimeLeft);
+ // SparklyPaper start - track world's MSPT
+ long j = Util.getNanos() - i;
+
+ // These are from the "tickServer" function
+ serverLevel.tickTimes5s.add(this.tickCount, j);
+ serverLevel.tickTimes10s.add(this.tickCount, j);
+ serverLevel.tickTimes60s.add(this.tickCount, j);
+ // SparklyPaper end
} catch (Throwable levelTickingException) {
CrashReport crashReport = CrashReport.forThrowable(levelTickingException, "Exception ticking world");
serverLevel.fillReportDetails(crashReport);
diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java
index 3c0975e419b8ce58371e0cfae07abdad498bf5df..a37547b6cd228ab0e157a44b66d4a25b3a88751e 100644
--- a/net/minecraft/server/level/ServerLevel.java
+++ b/net/minecraft/server/level/ServerLevel.java
@@ -574,6 +574,12 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe
}
// Paper end - chunk tick iteration
+ // SparklyPaper start - track world's MSPT
+ public final MinecraftServer.TickTimes tickTimes5s = new MinecraftServer.TickTimes(100);
+ public final MinecraftServer.TickTimes tickTimes10s = new MinecraftServer.TickTimes(200);
+ public final MinecraftServer.TickTimes tickTimes60s = new MinecraftServer.TickTimes(1200);
+ // SparklyPaper end
+
public ServerLevel(
MinecraftServer server,
Executor dispatcher,
diff --git a/org/purpurmc/purpur/task/TPSBarTask.java b/org/purpurmc/purpur/task/TPSBarTask.java
index 8769993e7ca59da309087051a3cd38fc562c15d1..b05f30f973c771641cda031332d033f14ea0572a 100644
--- a/org/purpurmc/purpur/task/TPSBarTask.java
+++ b/org/purpurmc/purpur/task/TPSBarTask.java
@@ -7,6 +7,9 @@ import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.purpurmc.purpur.PurpurConfig;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
+import org.bukkit.craftbukkit.CraftWorld;
+import net.minecraft.server.level.ServerLevel;
+import org.dreeam.leaf.config.modules.async.SparklyPaperParallelWorldTicking;
public class TPSBarTask extends BossBarTask {
private static TPSBarTask instance;
@@ -28,13 +31,51 @@ public class TPSBarTask extends BossBarTask {
@Override
void updateBossBar(BossBar bossbar, Player player) {
- bossbar.progress(getBossBarProgress());
- bossbar.color(getBossBarColor());
- bossbar.name(MiniMessage.miniMessage().deserialize(PurpurConfig.commandTPSBarTitle,
+ if (SparklyPaperParallelWorldTicking.enabled) {
+ // Get player's current world
+ ServerLevel serverLevel = ((CraftWorld)player.getWorld()).getHandle();
+
+ // Calculate world-specific MSPT and TPS
+ double worldMspt = calculateWorldMSPT(serverLevel);
+ double worldTps = Math.min(20.0, 1000.0 / Math.max(worldMspt, 0.001)); // Avoid division by zero
+
+ // Store original values
+ double originalTps = this.tps;
+ double originalMspt = this.mspt;
+
+ try {
+ // Temporarily set to world values
+ this.tps = worldTps;
+ this.mspt = worldMspt;
+
+ // Create MSPT component with world name
+ Component msptWithWorld = Component.empty()
+ .append(getMSPTColor())
+ .append(Component.text(" [" + player.getWorld().getName() + "]").color(net.kyori.adventure.text.format.NamedTextColor.GRAY));
+
+ // Update the boss bar using the methods that depend on the fields
+ bossbar.progress(getBossBarProgress());
+ bossbar.color(getBossBarColor());
+ bossbar.name(MiniMessage.miniMessage().deserialize(PurpurConfig.commandTPSBarTitle,
+ Placeholder.component("tps", getTPSColor()),
+ Placeholder.component("mspt", msptWithWorld),
+ Placeholder.component("ping", getPingColor(player.getPing()))
+ ));
+ } finally {
+ // Restore original values
+ this.tps = originalTps;
+ this.mspt = originalMspt;
+ }
+ } else {
+ // Default behavior
+ bossbar.progress(getBossBarProgress());
+ bossbar.color(getBossBarColor());
+ bossbar.name(MiniMessage.miniMessage().deserialize(PurpurConfig.commandTPSBarTitle,
Placeholder.component("tps", getTPSColor()),
Placeholder.component("mspt", getMSPTColor()),
Placeholder.component("ping", getPingColor(player.getPing()))
- ));
+ ));
+ }
}
@Override
@@ -136,6 +177,22 @@ public class TPSBarTask extends BossBarTask {
return MiniMessage.miniMessage().deserialize(color, Placeholder.parsed("text", String.format("%s", ping)));
}
+ private double calculateWorldMSPT(ServerLevel serverLevel) {
+ long[] times = serverLevel.tickTimes5s.getTimes();
+ long total = 0L;
+ int count = 0;
+
+ for (long value : times) {
+ if (value > 0L) {
+ total += value;
+ count++;
+ }
+ }
+
+ if (count == 0) return 0.0;
+ return (double) total / (double) count * 1.0E-6D;
+ }
+
public enum FillMode {
TPS, MSPT, PING
}