2013-04-10 06:08:33 +02:00
|
|
|
From 64a476e80485ab2a1a28f1b1809071f261eed141 Mon Sep 17 00:00:00 2001
|
2013-02-03 02:28:39 +01:00
|
|
|
From: md_5 <md_5@live.com.au>
|
|
|
|
Date: Sun, 3 Feb 2013 12:28:17 +1100
|
|
|
|
Subject: [PATCH] Tick loop optimization - sleep for as long as possible.
|
|
|
|
|
|
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
|
2013-04-10 04:36:11 +02:00
|
|
|
index 64d5882..8de8570 100644
|
2013-02-03 02:28:39 +01:00
|
|
|
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
|
2013-03-25 08:57:00 +01:00
|
|
|
@@ -83,6 +83,12 @@ public abstract class MinecraftServer implements ICommandListener, Runnable, IMo
|
2013-02-03 02:28:39 +01:00
|
|
|
public java.util.Queue<Runnable> processQueue = new java.util.concurrent.ConcurrentLinkedQueue<Runnable>();
|
|
|
|
public int autosavePeriod;
|
|
|
|
// CraftBukkit end
|
|
|
|
+ // Spigot start
|
|
|
|
+ private static final int TPS = 20;
|
|
|
|
+ private static final int TICK_TIME = 1000000000 / TPS;
|
|
|
|
+ public static double currentTPS = 0;
|
|
|
|
+ private static long catchupTime = 0;
|
|
|
|
+ // Spigot end
|
|
|
|
|
|
|
|
public MinecraftServer(OptionSet options) { // CraftBukkit - signature file -> OptionSet
|
2013-03-15 22:35:56 +01:00
|
|
|
k = this;
|
2013-04-10 04:36:11 +02:00
|
|
|
@@ -379,39 +385,23 @@ public abstract class MinecraftServer implements ICommandListener, Runnable, IMo
|
2013-02-03 02:28:39 +01:00
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
if (this.init()) {
|
|
|
|
- long i = System.currentTimeMillis();
|
|
|
|
-
|
2013-03-15 22:35:56 +01:00
|
|
|
- for (long j = 0L; this.isRunning; this.P = true) {
|
2013-02-03 02:28:39 +01:00
|
|
|
- long k = System.currentTimeMillis();
|
|
|
|
- long l = k - i;
|
|
|
|
-
|
2013-03-15 22:35:56 +01:00
|
|
|
- if (l > 2000L && i - this.Q >= 15000L) {
|
2013-02-03 02:28:39 +01:00
|
|
|
- if (this.server.getWarnOnOverload()) // CraftBukkit - Added option to suppress warning messages
|
2013-03-15 22:35:56 +01:00
|
|
|
- this.getLogger().warning("Can\'t keep up! Did the system time change, or is the server overloaded?");
|
2013-02-03 02:28:39 +01:00
|
|
|
- l = 2000L;
|
2013-03-15 22:35:56 +01:00
|
|
|
- this.Q = i;
|
2013-02-03 02:28:39 +01:00
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (l < 0L) {
|
2013-03-15 22:35:56 +01:00
|
|
|
- this.getLogger().warning("Time ran backwards! Did the system time change?");
|
2013-02-03 02:28:39 +01:00
|
|
|
- l = 0L;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- j += l;
|
|
|
|
- i = k;
|
|
|
|
- if (this.worlds.get(0).everyoneDeeplySleeping()) { // CraftBukkit
|
|
|
|
- this.q();
|
|
|
|
- j = 0L;
|
|
|
|
+ // Spigot start
|
2013-03-15 23:08:53 +01:00
|
|
|
+ for (long lastTick = 0L; this.isRunning; this.P = true) {
|
2013-02-03 02:28:39 +01:00
|
|
|
+ long curTime = System.nanoTime();
|
|
|
|
+ long wait = TICK_TIME - (curTime - lastTick) - catchupTime;
|
|
|
|
+ if (wait > 0) {
|
|
|
|
+ Thread.sleep(wait / 1000000);
|
|
|
|
+ catchupTime = 0;
|
|
|
|
+ continue;
|
|
|
|
} else {
|
|
|
|
- while (j > 50L) {
|
|
|
|
- MinecraftServer.currentTick = (int) (System.currentTimeMillis() / 50); // CraftBukkit
|
|
|
|
- j -= 50L;
|
|
|
|
- this.q();
|
|
|
|
- }
|
|
|
|
+ catchupTime = Math.min(TICK_TIME * TPS, Math.abs(wait));
|
|
|
|
}
|
|
|
|
-
|
|
|
|
- Thread.sleep(1L);
|
|
|
|
+ currentTPS = (currentTPS * 0.95) + (1E9 / (curTime - lastTick) * 0.05);
|
|
|
|
+ lastTick = curTime;
|
|
|
|
+ MinecraftServer.currentTick++;
|
|
|
|
+ this.q();
|
|
|
|
}
|
|
|
|
+ // Spigot end
|
|
|
|
} else {
|
|
|
|
this.a((CrashReport) null);
|
|
|
|
}
|
2013-02-08 07:09:51 +01:00
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/Spigot.java b/src/main/java/org/bukkit/craftbukkit/Spigot.java
|
2013-02-26 18:21:40 +01:00
|
|
|
index 4a4f949..db46037 100644
|
2013-02-08 07:09:51 +01:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/Spigot.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/Spigot.java
|
2013-02-23 01:50:29 +01:00
|
|
|
@@ -6,6 +6,8 @@ import org.bukkit.configuration.file.YamlConfiguration;
|
2013-02-08 07:09:51 +01:00
|
|
|
public class Spigot {
|
2013-02-23 01:50:29 +01:00
|
|
|
|
2013-02-08 07:09:51 +01:00
|
|
|
public static void initialize(CraftServer server, SimpleCommandMap commandMap, YamlConfiguration configuration) {
|
|
|
|
+ commandMap.register("bukkit", new org.bukkit.craftbukkit.command.TicksPerSecondCommand("tps"));
|
2013-02-23 01:50:29 +01:00
|
|
|
+
|
|
|
|
server.whitelistMessage = configuration.getString("settings.whitelist-message", server.whitelistMessage);
|
|
|
|
server.stopMessage = configuration.getString("settings.stop-message", server.stopMessage);
|
|
|
|
server.logCommands = configuration.getBoolean("settings.log-commands", true);
|
2013-02-03 02:28:39 +01:00
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/command/TicksPerSecondCommand.java b/src/main/java/org/bukkit/craftbukkit/command/TicksPerSecondCommand.java
|
|
|
|
new file mode 100644
|
|
|
|
index 0000000..f114a31
|
|
|
|
--- /dev/null
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/command/TicksPerSecondCommand.java
|
|
|
|
@@ -0,0 +1,35 @@
|
|
|
|
+package org.bukkit.craftbukkit.command;
|
|
|
|
+
|
|
|
|
+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;
|
|
|
|
+
|
|
|
|
+ double tps = Math.min(20, Math.round(MinecraftServer.currentTPS * 10) / 10.0);
|
|
|
|
+ ChatColor color;
|
|
|
|
+ if (tps > 19.2D) {
|
|
|
|
+ color = ChatColor.GREEN;
|
|
|
|
+ } else if (tps > 17.4D) {
|
|
|
|
+ color = ChatColor.YELLOW;
|
|
|
|
+ } else {
|
|
|
|
+ color = ChatColor.RED;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ sender.sendMessage(ChatColor.GOLD + "[TPS] " + color + tps);
|
|
|
|
+
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
--
|
2013-04-10 04:36:11 +02:00
|
|
|
1.8.2.1
|
2013-02-03 02:28:39 +01:00
|
|
|
|