diff options
author | Luck <git@lucko.me> | 2020-01-11 19:24:30 +0000 |
---|---|---|
committer | Luck <git@lucko.me> | 2020-01-11 19:24:30 +0000 |
commit | 9e066d1052643b2be270b4039a102260abb41ebb (patch) | |
tree | 31bddf86d03d723f4ccdcdcccb67ca9711bb8b5c | |
parent | 47c65e9c64ec387f845eac5784d86d9685d8ea71 (diff) | |
download | spark-9e066d1052643b2be270b4039a102260abb41ebb.tar.gz spark-9e066d1052643b2be270b4039a102260abb41ebb.tar.bz2 spark-9e066d1052643b2be270b4039a102260abb41ebb.zip |
Implement PaperTickCounter using server tick event instead of scheduler
5 files changed, 100 insertions, 10 deletions
diff --git a/build.gradle b/build.gradle index d3b2366..29422f3 100644 --- a/build.gradle +++ b/build.gradle @@ -29,7 +29,7 @@ subprojects { maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } maven { url "https://repo.lucko.me/" } - maven { url "https://hub.spigotmc.org/nexus/content/repositories/snapshots/" } + maven { url "https://papermc.io/repo/repository/maven-public/" } maven { url "https://repo.spongepowered.org/maven" } maven { url "https://repo.velocitypowered.com/releases/" } } diff --git a/spark-bukkit/build.gradle b/spark-bukkit/build.gradle index 56ba20f..c4b9d8c 100644 --- a/spark-bukkit/build.gradle +++ b/spark-bukkit/build.gradle @@ -4,7 +4,7 @@ dependencies { exclude(module: 'text-api') exclude(module: 'text-serializer-gson') } - compileOnly 'org.spigotmc:spigot-api:1.14.4-R0.1-SNAPSHOT' + compileOnly 'com.destroystokyo.paper:paper-api:1.14.4-R0.1-SNAPSHOT' // placeholders compileOnly 'me.clip:placeholderapi:2.10.3' diff --git a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitSparkPlugin.java b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitSparkPlugin.java index edeeb50..0eca719 100644 --- a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitSparkPlugin.java +++ b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitSparkPlugin.java @@ -130,6 +130,21 @@ public class BukkitSparkPlugin extends JavaPlugin implements SparkPlugin { @Override public TickCounter createTickCounter() { - return new BukkitTickCounter(this); + if (classExists("com.destroystokyo.paper.event.server.ServerTickStartEvent")) { + getLogger().info("Using Paper ServerTickStartEvent for tick monitoring"); + return new PaperTickCounter(this); + } else { + getLogger().info("Using Bukkit scheduler for tick monitoring"); + return new BukkitTickCounter(this); + } + } + + private static boolean classExists(String className) { + try { + Class.forName(className); + return true; + } catch (ClassNotFoundException e) { + return false; + } } } diff --git a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/PaperTickCounter.java b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/PaperTickCounter.java new file mode 100644 index 0000000..e545687 --- /dev/null +++ b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/PaperTickCounter.java @@ -0,0 +1,75 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.bukkit; + +import com.destroystokyo.paper.event.server.ServerTickStartEvent; +import me.lucko.spark.common.sampler.TickCounter; +import org.bukkit.event.EventHandler; +import org.bukkit.event.HandlerList; +import org.bukkit.event.Listener; +import org.bukkit.plugin.Plugin; + +import java.util.HashSet; +import java.util.Set; + +public class PaperTickCounter implements TickCounter, Listener { + private final Plugin plugin; + + private final Set<TickTask> tasks = new HashSet<>(); + private int tick = 0; + + public PaperTickCounter(Plugin plugin) { + this.plugin = plugin; + } + + @EventHandler + public void onServerTickEvent(ServerTickStartEvent e) { + for (TickTask r : this.tasks) { + r.onTick(this); + } + this.tick++; + } + + @Override + public void start() { + this.plugin.getServer().getPluginManager().registerEvents(this, this.plugin); + } + + @Override + public void close() { + HandlerList.unregisterAll(this); + } + + @Override + public int getCurrentTick() { + return this.tick; + } + + @Override + public void addTickTask(TickTask runnable) { + this.tasks.add(runnable); + } + + @Override + public void removeTickTask(TickTask runnable) { + this.tasks.remove(runnable); + } +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/monitor/tick/TpsCalculator.java b/spark-common/src/main/java/me/lucko/spark/common/monitor/tick/TpsCalculator.java index 3000523..6b4f2d1 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/monitor/tick/TpsCalculator.java +++ b/spark-common/src/main/java/me/lucko/spark/common/monitor/tick/TpsCalculator.java @@ -42,7 +42,7 @@ public class TpsCalculator implements TickCounter.TickTask { private static final long SEC_IN_NANO = TimeUnit.SECONDS.toNanos(1); private static final int TPS = 20; private static final int SAMPLE_INTERVAL = 20; - private static final BigDecimal TPS_BASE = new BigDecimal(SEC_IN_NANO).multiply(new BigDecimal((long) SAMPLE_INTERVAL)); + private static final BigDecimal TPS_BASE = new BigDecimal(SEC_IN_NANO).multiply(new BigDecimal(SAMPLE_INTERVAL)); private final TpsRollingAverage avg5Sec = new TpsRollingAverage(5); private final TpsRollingAverage avg10Sec = new TpsRollingAverage(10); @@ -56,7 +56,6 @@ public class TpsCalculator implements TickCounter.TickTask { private long last = 0; - // called every tick @Override public void onTick(TickCounter counter) { if (counter.getCurrentTick() % SAMPLE_INTERVAL != 0) { @@ -72,9 +71,10 @@ public class TpsCalculator implements TickCounter.TickTask { long diff = now - this.last; BigDecimal currentTps = TPS_BASE.divide(new BigDecimal(diff), 30, RoundingMode.HALF_UP); + BigDecimal total = currentTps.multiply(new BigDecimal(diff)); for (TpsRollingAverage rollingAverage : this.averages) { - rollingAverage.add(currentTps, diff); + rollingAverage.add(currentTps, diff, total); } this.last = now; @@ -120,22 +120,22 @@ public class TpsCalculator implements TickCounter.TickTask { TpsRollingAverage(int size) { this.size = size; this.time = size * SEC_IN_NANO; - this.total = new BigDecimal((long) TPS).multiply(new BigDecimal(SEC_IN_NANO)).multiply(new BigDecimal((long) size)); + this.total = new BigDecimal(TPS).multiply(new BigDecimal(SEC_IN_NANO)).multiply(new BigDecimal(size)); this.samples = new BigDecimal[size]; this.times = new long[size]; for (int i = 0; i < size; i++) { - this.samples[i] = new BigDecimal((long) TPS); + this.samples[i] = new BigDecimal(TPS); this.times[i] = SEC_IN_NANO; } } - public void add(BigDecimal x, long t) { + public void add(BigDecimal x, long t, BigDecimal total) { this.time -= this.times[this.index]; this.total = this.total.subtract(this.samples[this.index].multiply(new BigDecimal(this.times[this.index]))); this.samples[this.index] = x; this.times[this.index] = t; this.time += t; - this.total = this.total.add(x.multiply(new BigDecimal(t))); + this.total = this.total.add(total); if (++this.index == this.size) { this.index = 0; } |