2023-09-22 02:40:00 +10:00
|
|
|
From b881f51af7c5f82729a8a0f5feb086c9e34f9525 Mon Sep 17 00:00:00 2001
|
2014-04-12 14:18:37 +10:00
|
|
|
From: md_5 <git@md-5.net>
|
2017-01-28 22:30:38 +00:00
|
|
|
Date: Thu, 26 Jan 2017 21:50:51 +0000
|
2014-04-12 14:18:37 +10:00
|
|
|
Subject: [PATCH] Highly Optimized Tick Loop
|
|
|
|
|
|
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
|
2023-09-22 02:40:00 +10:00
|
|
|
index c1ea8e239b..eb20b62d41 100644
|
2014-04-12 14:18:37 +10:00
|
|
|
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
|
2023-09-22 02:40:00 +10:00
|
|
|
@@ -285,6 +285,12 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
|
2018-07-21 11:24:31 +10:00
|
|
|
public CommandDispatcher vanillaCommandDispatcher;
|
2019-04-25 15:38:41 +10:00
|
|
|
private boolean forceTicks;
|
2014-04-12 14:18:37 +10:00
|
|
|
// CraftBukkit end
|
|
|
|
+ // Spigot start
|
2017-01-28 22:30:38 +00:00
|
|
|
+ public static final int TPS = 20;
|
|
|
|
+ public static final int TICK_TIME = 1000000000 / TPS;
|
2014-04-12 14:18:37 +10:00
|
|
|
+ private static final int SAMPLE_INTERVAL = 100;
|
|
|
|
+ public final double[] recentTps = new double[ 3 ];
|
|
|
|
+ // Spigot end
|
|
|
|
|
2021-11-22 09:00:00 +11:00
|
|
|
public static <S extends MinecraftServer> S spin(Function<Thread, S> function) {
|
2020-06-25 10:00:00 +10:00
|
|
|
AtomicReference<S> atomicreference = new AtomicReference();
|
2023-09-22 02:40:00 +10:00
|
|
|
@@ -930,6 +936,13 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
|
2019-04-23 12:00:00 +10:00
|
|
|
|
2014-04-12 14:18:37 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
+ // Spigot Start
|
|
|
|
+ private static double calcTps(double avg, double exp, double tps)
|
|
|
|
+ {
|
|
|
|
+ return ( avg * exp ) + ( tps * ( 1 - exp ) );
|
|
|
|
+ }
|
|
|
|
+ // Spigot End
|
2017-01-28 22:30:38 +00:00
|
|
|
+
|
2021-11-22 09:00:00 +11:00
|
|
|
protected void runServer() {
|
2014-04-12 14:18:37 +10:00
|
|
|
try {
|
2022-06-08 02:00:00 +10:00
|
|
|
if (!this.initServer()) {
|
2023-09-22 02:40:00 +10:00
|
|
|
@@ -940,8 +953,11 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
|
2023-03-15 03:30:00 +11:00
|
|
|
this.statusIcon = (ServerPing.a) this.loadStatusIcon().orElse(null); // CraftBukkit - decompile error
|
|
|
|
this.status = this.buildServerStatus();
|
2014-04-12 14:18:37 +10:00
|
|
|
|
2022-06-08 02:00:00 +10:00
|
|
|
+ // Spigot start
|
|
|
|
+ Arrays.fill( recentTps, 20 );
|
|
|
|
+ long curTime, tickSection = SystemUtils.getMillis(), tickCount = 1;
|
|
|
|
while (this.running) {
|
|
|
|
- long i = SystemUtils.getMillis() - this.nextTickTime;
|
|
|
|
+ long i = (curTime = SystemUtils.getMillis()) - this.nextTickTime;
|
2019-04-23 12:00:00 +10:00
|
|
|
|
2022-06-08 02:00:00 +10:00
|
|
|
if (i > 5000L && this.nextTickTime - this.lastOverloadWarning >= 30000L) { // CraftBukkit
|
|
|
|
long j = i / 50L;
|
2023-09-22 02:40:00 +10:00
|
|
|
@@ -952,6 +968,16 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
|
2022-06-08 02:00:00 +10:00
|
|
|
this.lastOverloadWarning = this.nextTickTime;
|
|
|
|
}
|
2014-04-12 14:18:37 +10:00
|
|
|
|
2022-06-08 02:00:00 +10:00
|
|
|
+ if ( tickCount++ % SAMPLE_INTERVAL == 0 )
|
|
|
|
+ {
|
|
|
|
+ double currentTps = 1E3 / ( curTime - tickSection ) * SAMPLE_INTERVAL;
|
|
|
|
+ recentTps[0] = calcTps( recentTps[0], 0.92, currentTps ); // 1/exp(5sec/1min)
|
|
|
|
+ recentTps[1] = calcTps( recentTps[1], 0.9835, currentTps ); // 1/exp(5sec/5min)
|
|
|
|
+ recentTps[2] = calcTps( recentTps[2], 0.9945, currentTps ); // 1/exp(5sec/15min)
|
|
|
|
+ tickSection = curTime;
|
|
|
|
+ }
|
|
|
|
+ // Spigot end
|
2019-04-23 12:00:00 +10:00
|
|
|
+
|
2022-06-08 02:00:00 +10:00
|
|
|
if (this.debugCommandProfilerDelayStart) {
|
|
|
|
this.debugCommandProfilerDelayStart = false;
|
|
|
|
this.debugCommandProfiler = new MinecraftServer.TimeProfiler(SystemUtils.getNanos(), this.tickCount);
|
2014-04-12 14:18:37 +10:00
|
|
|
diff --git a/src/main/java/org/spigotmc/SpigotConfig.java b/src/main/java/org/spigotmc/SpigotConfig.java
|
2023-08-26 18:29:15 +10:00
|
|
|
index 0aab647abb..4987040ff2 100644
|
2014-04-12 14:18:37 +10:00
|
|
|
--- a/src/main/java/org/spigotmc/SpigotConfig.java
|
|
|
|
+++ b/src/main/java/org/spigotmc/SpigotConfig.java
|
2019-07-30 20:52:01 +10:00
|
|
|
@@ -266,4 +266,9 @@ public class SpigotConfig
|
2017-06-09 15:58:16 +10:00
|
|
|
}
|
2014-04-12 14:18:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
+
|
|
|
|
+ private static void tpsCommand()
|
|
|
|
+ {
|
|
|
|
+ commands.put( "tps", new TicksPerSecondCommand( "tps" ) );
|
|
|
|
+ }
|
|
|
|
}
|
|
|
|
diff --git a/src/main/java/org/spigotmc/TicksPerSecondCommand.java b/src/main/java/org/spigotmc/TicksPerSecondCommand.java
|
|
|
|
new file mode 100644
|
2023-08-26 18:29:15 +10:00
|
|
|
index 0000000000..f5b6dec1cb
|
2014-04-12 14:18:37 +10:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/src/main/java/org/spigotmc/TicksPerSecondCommand.java
|
2021-01-31 10:08:46 +11:00
|
|
|
@@ -0,0 +1,45 @@
|
2014-04-12 14:18:37 +10:00
|
|
|
+package org.spigotmc;
|
|
|
|
+
|
|
|
|
+import net.minecraft.server.MinecraftServer;
|
|
|
|
+import org.bukkit.ChatColor;
|
|
|
|
+import org.bukkit.command.Command;
|
|
|
|
+import org.bukkit.command.CommandSender;
|
|
|
|
+
|
|
|
|
+public class TicksPerSecondCommand extends Command
|
|
|
|
+{
|
|
|
|
+
|
|
|
|
+ public TicksPerSecondCommand(String name)
|
|
|
|
+ {
|
|
|
|
+ super( name );
|
|
|
|
+ this.description = "Gets the current ticks per second for the server";
|
|
|
|
+ this.usageMessage = "/tps";
|
|
|
|
+ this.setPermission( "bukkit.command.tps" );
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean execute(CommandSender sender, String currentAlias, String[] args)
|
|
|
|
+ {
|
|
|
|
+ if ( !testPermission( sender ) )
|
|
|
|
+ {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ StringBuilder sb = new StringBuilder( ChatColor.GOLD + "TPS from last 1m, 5m, 15m: " );
|
|
|
|
+ for ( double tps : MinecraftServer.getServer().recentTps )
|
|
|
|
+ {
|
|
|
|
+ sb.append( format( tps ) );
|
|
|
|
+ sb.append( ", " );
|
|
|
|
+ }
|
|
|
|
+ sender.sendMessage( sb.substring( 0, sb.length() - 2 ) );
|
2020-06-28 10:29:20 +10:00
|
|
|
+ sender.sendMessage(ChatColor.GOLD + "Current Memory Usage: " + ChatColor.GREEN + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (1024 * 1024)) + "/" + (Runtime.getRuntime().totalMemory() / (1024 * 1024)) + " mb (Max: "
|
|
|
|
+ + (Runtime.getRuntime().maxMemory() / (1024 * 1024)) + " mb)");
|
2014-04-12 14:18:37 +10:00
|
|
|
+
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String format(double tps)
|
|
|
|
+ {
|
|
|
|
+ return ( ( tps > 18.0 ) ? ChatColor.GREEN : ( tps > 16.0 ) ? ChatColor.YELLOW : ChatColor.RED ).toString()
|
|
|
|
+ + ( ( tps > 20.0 ) ? "*" : "" ) + Math.min( Math.round( tps * 100.0 ) / 100.0, 20.0 );
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
--
|
2023-08-26 18:29:15 +10:00
|
|
|
2.42.0
|
2014-04-12 14:18:37 +10:00
|
|
|
|