diff options
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark')
29 files changed, 798 insertions, 320 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java b/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java index ef21d1c..8eb4565 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java +++ b/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java @@ -21,19 +21,20 @@ package me.lucko.spark.common; import com.google.common.collect.ImmutableList; - import me.lucko.spark.common.command.Arguments; import me.lucko.spark.common.command.Command; +import me.lucko.spark.common.command.CommandResponseHandler; import me.lucko.spark.common.command.modules.MemoryModule; +import me.lucko.spark.common.command.modules.MonitoringModule; import me.lucko.spark.common.command.modules.SamplerModule; import me.lucko.spark.common.command.modules.TickMonitoringModule; import me.lucko.spark.common.command.tabcomplete.CompletionSupplier; import me.lucko.spark.common.command.tabcomplete.TabCompleter; -import me.lucko.spark.sampler.ThreadDumper; -import me.lucko.spark.sampler.TickCounter; -import me.lucko.spark.util.BytebinClient; +import me.lucko.spark.common.monitor.tick.TpsCalculator; +import me.lucko.spark.common.sampler.TickCounter; +import me.lucko.spark.common.util.BytebinClient; +import okhttp3.OkHttpClient; -import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -41,52 +42,68 @@ import java.util.List; import java.util.stream.Collectors; /** - * Abstract command handling class used by all platforms. + * Abstract spark implementation used by all platforms. * * @param <S> the sender (e.g. CommandSender) type used by the platform */ -public abstract class SparkPlatform<S> { +public class SparkPlatform<S> { /** The URL of the viewer frontend */ public static final String VIEWER_URL = "https://sparkprofiler.github.io/#"; + /** The shared okhttp client */ + private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient(); /** The bytebin instance used by the platform */ - public static final BytebinClient BYTEBIN_CLIENT = new BytebinClient("https://bytebin.lucko.me/", "spark-plugin"); - - /** The prefix used in all messages */ - private static final String PREFIX = "&8[&fspark&8] &7"; - - private static <T> List<Command<T>> prepareCommands() { - ImmutableList.Builder<Command<T>> builder = ImmutableList.builder(); - new SamplerModule<T>().registerCommands(builder::add); - new TickMonitoringModule<T>().registerCommands(builder::add); - new MemoryModule<T>().registerCommands(builder::add); - return builder.build(); + public static final BytebinClient BYTEBIN_CLIENT = new BytebinClient(OK_HTTP_CLIENT, "https://bytebin.lucko.me/", "spark-plugin"); + + private final List<Command<S>> commands; + private final SparkPlugin<S> plugin; + + private final TickCounter tickCounter; + private final TpsCalculator tpsCalculator; + + public SparkPlatform(SparkPlugin<S> plugin) { + this.plugin = plugin; + + ImmutableList.Builder<Command<S>> commandsBuilder = ImmutableList.builder(); + new SamplerModule<S>().registerCommands(commandsBuilder::add); + new MonitoringModule<S>().registerCommands(commandsBuilder::add); + new TickMonitoringModule<S>().registerCommands(commandsBuilder::add); + new MemoryModule<S>().registerCommands(commandsBuilder::add); + this.commands = commandsBuilder.build(); + + this.tickCounter = plugin.createTickCounter(); + this.tpsCalculator = this.tickCounter != null ? new TpsCalculator() : null; + } + + public void enable() { + if (this.tickCounter != null) { + this.tickCounter.addTickTask(this.tpsCalculator); + this.tickCounter.start(); + } + } + + public void disable() { + if (this.tickCounter != null) { + this.tickCounter.close(); + } + } + + public SparkPlugin<S> getPlugin() { + return this.plugin; } - private final List<Command<S>> commands = prepareCommands(); - - // abstract methods implemented by each platform - public abstract String getVersion(); - public abstract Path getPluginFolder(); - public abstract String getLabel(); - public abstract void sendMessage(S sender, String message); - public abstract void sendMessage(String message); - public abstract void sendLink(String url); - public abstract void runAsync(Runnable r); - public abstract ThreadDumper getDefaultThreadDumper(); - public abstract TickCounter newTickCounter(); - - public void sendPrefixedMessage(S sender, String message) { - sendMessage(sender, PREFIX + message); + public TickCounter getTickCounter() { + return this.tickCounter; } - public void sendPrefixedMessage(String message) { - sendMessage(PREFIX + message); + public TpsCalculator getTpsCalculator() { + return this.tpsCalculator; } public void executeCommand(S sender, String[] args) { + CommandResponseHandler<S> resp = new CommandResponseHandler<>(this, sender); if (args.length == 0) { - sendUsage(sender); + sendUsage(resp); return; } @@ -96,15 +113,15 @@ public abstract class SparkPlatform<S> { for (Command<S> command : this.commands) { if (command.aliases().contains(alias)) { try { - command.executor().execute(this, sender, new Arguments(rawArgs)); + command.executor().execute(this, sender, resp, new Arguments(rawArgs)); } catch (IllegalArgumentException e) { - sendMessage(sender, "&c" + e.getMessage()); + resp.replyPrefixed("&c" + e.getMessage()); } return; } } - sendUsage(sender); + sendUsage(resp); } public List<String> tabCompleteCommand(S sender, String[] args) { @@ -127,15 +144,15 @@ public abstract class SparkPlatform<S> { return Collections.emptyList(); } - private void sendUsage(S sender) { - sendPrefixedMessage(sender, "&fspark &7v" + getVersion()); + private void sendUsage(CommandResponseHandler<S> sender) { + sender.replyPrefixed("&fspark &7v" + getPlugin().getVersion()); for (Command<S> command : this.commands) { - sendMessage(sender, "&b&l> &7/" + getLabel() + " " + command.aliases().get(0)); + sender.reply("&b&l> &7/" + getPlugin().getLabel() + " " + command.aliases().get(0)); for (Command.ArgumentInfo arg : command.arguments()) { if (arg.requiresParameter()) { - sendMessage(sender, " &8[&7--" + arg.argumentName() + "&8 <" + arg.parameterDescription() + ">]"); + sender.reply(" &8[&7--" + arg.argumentName() + "&8 <" + arg.parameterDescription() + ">]"); } else { - sendMessage(sender, " &8[&7--" + arg.argumentName() + "]"); + sender.reply(" &8[&7--" + arg.argumentName() + "]"); } } } diff --git a/spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java b/spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java new file mode 100644 index 0000000..7a3a353 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java @@ -0,0 +1,49 @@ +/* + * 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.common; + +import me.lucko.spark.common.sampler.ThreadDumper; +import me.lucko.spark.common.sampler.TickCounter; + +import java.nio.file.Path; +import java.util.Set; + +public interface SparkPlugin<S> { + + String getVersion(); + + Path getPluginFolder(); + + String getLabel(); + + Set<S> getSenders(); + + void sendMessage(S sender, String message); + + void sendLink(S sender, String url); + + void runAsync(Runnable r); + + ThreadDumper getDefaultThreadDumper(); + + TickCounter createTickCounter(); + +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/Command.java b/spark-common/src/main/java/me/lucko/spark/common/command/Command.java index fb440b1..c9f6551 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/Command.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/Command.java @@ -21,7 +21,6 @@ package me.lucko.spark.common.command; import com.google.common.collect.ImmutableList; - import me.lucko.spark.common.SparkPlatform; import java.util.Collections; @@ -109,7 +108,7 @@ public class Command<S> { @FunctionalInterface public interface Executor<S> { - void execute(SparkPlatform<S> platform, S sender, Arguments arguments); + void execute(SparkPlatform<S> platform, S sender, CommandResponseHandler resp, Arguments arguments); } @FunctionalInterface diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/CommandResponseHandler.java b/spark-common/src/main/java/me/lucko/spark/common/command/CommandResponseHandler.java new file mode 100644 index 0000000..a5a7391 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/command/CommandResponseHandler.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.common.command; + +import me.lucko.spark.common.SparkPlatform; + +import java.util.Set; +import java.util.function.Consumer; + +public class CommandResponseHandler<S> { + + /** The prefix used in all messages */ + private static final String PREFIX = "&8[&fspark&8] &7"; + + private final SparkPlatform<S> platform; + private final S sender; + + public CommandResponseHandler(SparkPlatform<S> platform, S sender) { + this.platform = platform; + this.sender = sender; + } + + public S sender() { + return this.sender; + } + + public void allSenders(Consumer<? super S> action) { + Set<S> senders = this.platform.getPlugin().getSenders(); + senders.add(this.sender); + senders.forEach(action); + } + + public void reply(String message) { + this.platform.getPlugin().sendMessage(this.sender, message); + } + + public void broadcast(String message) { + allSenders(sender -> this.platform.getPlugin().sendMessage(sender, message)); + } + + public void replyPrefixed(String message) { + this.platform.getPlugin().sendMessage(this.sender, PREFIX + message); + } + + public void broadcastPrefixed(String message) { + allSenders(sender -> this.platform.getPlugin().sendMessage(sender, PREFIX + message)); + } + + public void replyLink(String link) { + this.platform.getPlugin().sendLink(this.sender, link); + } + + public void broadcastLink(String link) { + allSenders(sender -> this.platform.getPlugin().sendLink(sender, link)); + } + +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/MemoryModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/MemoryModule.java index 5f17d54..2cb2e07 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/MemoryModule.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/MemoryModule.java @@ -24,9 +24,8 @@ import me.lucko.spark.common.SparkPlatform; import me.lucko.spark.common.command.Command; import me.lucko.spark.common.command.CommandModule; import me.lucko.spark.common.command.tabcomplete.TabCompleter; -import me.lucko.spark.memory.HeapDump; -import me.lucko.spark.memory.HeapDumpSummary; - +import me.lucko.spark.common.memory.HeapDump; +import me.lucko.spark.common.memory.HeapDumpSummary; import okhttp3.MediaType; import java.io.IOException; @@ -44,34 +43,34 @@ public class MemoryModule<S> implements CommandModule<S> { consumer.accept(Command.<S>builder() .aliases("heapsummary") .argumentUsage("run-gc-before", null) - .executor((platform, sender, arguments) -> { - platform.runAsync(() -> { - if (arguments.boolFlag("run-gc-before")) { - platform.sendPrefixedMessage("&7Running garbage collector..."); - System.gc(); - } - - platform.sendPrefixedMessage("&7Creating a new heap dump summary, please wait..."); - - HeapDumpSummary heapDump; - try { - heapDump = HeapDumpSummary.createNew(); - } catch (Exception e) { - platform.sendPrefixedMessage("&cAn error occurred whilst inspecting the heap."); - e.printStackTrace(); - return; - } - - byte[] output = heapDump.formCompressedDataPayload(); - try { - String key = SparkPlatform.BYTEBIN_CLIENT.postGzippedContent(output, JSON_TYPE); - platform.sendPrefixedMessage("&bHeap dump summmary output:"); - platform.sendLink(SparkPlatform.VIEWER_URL + key); - } catch (IOException e) { - platform.sendPrefixedMessage("&cAn error occurred whilst uploading the data."); - e.printStackTrace(); - } - }); + .executor((platform, sender, resp, arguments) -> { + platform.getPlugin().runAsync(() -> { + if (arguments.boolFlag("run-gc-before")) { + resp.broadcastPrefixed("&7Running garbage collector..."); + System.gc(); + } + + resp.broadcastPrefixed("&7Creating a new heap dump summary, please wait..."); + + HeapDumpSummary heapDump; + try { + heapDump = HeapDumpSummary.createNew(); + } catch (Exception e) { + resp.broadcastPrefixed("&cAn error occurred whilst inspecting the heap."); + e.printStackTrace(); + return; + } + + byte[] output = heapDump.formCompressedDataPayload(); + try { + String key = SparkPlatform.BYTEBIN_CLIENT.postContent(output, JSON_TYPE, false).key(); + resp.broadcastPrefixed("&bHeap dump summmary output:"); + resp.broadcastLink(SparkPlatform.VIEWER_URL + key); + } catch (IOException e) { + resp.broadcastPrefixed("&cAn error occurred whilst uploading the data."); + e.printStackTrace(); + } + }); }) .tabCompleter((platform, sender, arguments) -> TabCompleter.completeForOpts(arguments, "--run-gc-before")) .build() @@ -81,35 +80,36 @@ public class MemoryModule<S> implements CommandModule<S> { .aliases("heapdump") .argumentUsage("run-gc-before", null) .argumentUsage("include-non-live", null) - .executor((platform, sender, arguments) -> { - platform.runAsync(() -> { - Path pluginFolder = platform.getPluginFolder(); - try { - Files.createDirectories(pluginFolder); - } catch (IOException e) { - // ignore - } - - Path file = pluginFolder.resolve("heap-" + DateTimeFormatter.ofPattern("yyyy-MM-dd_HH.mm.ss").format(LocalDateTime.now()) + (HeapDump.isOpenJ9() ? ".phd" : ".hprof")); - boolean liveOnly = !arguments.boolFlag("include-non-live"); - - if (arguments.boolFlag("run-gc-before")) { - platform.sendPrefixedMessage("&7Running garbage collector..."); - System.gc(); - } - - platform.sendPrefixedMessage("&7Creating a new heap dump, please wait..."); - - try { - HeapDump.dumpHeap(file, liveOnly); - } catch (Exception e) { - platform.sendPrefixedMessage("&cAn error occurred whilst creating a heap dump."); - e.printStackTrace(); - return; - } - - platform.sendPrefixedMessage("&bHeap dump written to: " + file.toString()); - }); + .executor((platform, sender, resp, arguments) -> { + // ignore + platform.getPlugin().runAsync(() -> { + Path pluginFolder = platform.getPlugin().getPluginFolder(); + try { + Files.createDirectories(pluginFolder); + } catch (IOException e) { + // ignore + } + + Path file = pluginFolder.resolve("heap-" + DateTimeFormatter.ofPattern("yyyy-MM-dd_HH.mm.ss").format(LocalDateTime.now()) + (HeapDump.isOpenJ9() ? ".phd" : ".hprof")); + boolean liveOnly = !arguments.boolFlag("include-non-live"); + + if (arguments.boolFlag("run-gc-before")) { + resp.broadcastPrefixed("&7Running garbage collector..."); + System.gc(); + } + + resp.broadcastPrefixed("&7Creating a new heap dump, please wait..."); + + try { + HeapDump.dumpHeap(file, liveOnly); + } catch (Exception e) { + resp.broadcastPrefixed("&cAn error occurred whilst creating a heap dump."); + e.printStackTrace(); + return; + } + + resp.broadcastPrefixed("&bHeap dump written to: " + file.toString()); + }); }) .tabCompleter((platform, sender, arguments) -> TabCompleter.completeForOpts(arguments, "--run-gc-before", "--include-non-live")) .build() diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/MonitoringModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/MonitoringModule.java new file mode 100644 index 0000000..b543e1d --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/MonitoringModule.java @@ -0,0 +1,51 @@ +/* + * 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.common.command.modules; + +import me.lucko.spark.common.command.Command; +import me.lucko.spark.common.command.CommandModule; +import me.lucko.spark.common.monitor.tick.TpsCalculator; + +import java.util.function.Consumer; + +public class MonitoringModule<S> implements CommandModule<S> { + + @Override + public void registerCommands(Consumer<Command<S>> consumer) { + consumer.accept(Command.<S>builder() + .aliases("tps") + .executor((platform, sender, resp, arguments) -> { + TpsCalculator tpsCalculator = platform.getTpsCalculator(); + if (tpsCalculator == null) { + resp.replyPrefixed("TPS data is not available."); + return; + } + + String formattedTpsString = tpsCalculator.toFormattedString(); + resp.replyPrefixed("TPS from last 5s, 10s, 1m, 5m, 15m"); + resp.replyPrefixed(formattedTpsString); + }) + .tabCompleter(Command.TabCompleter.empty()) + .build() + ); + } + +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java index 9d00a96..a0f171c 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java @@ -23,14 +23,14 @@ package me.lucko.spark.common.command.modules; import me.lucko.spark.common.SparkPlatform; import me.lucko.spark.common.command.Command; import me.lucko.spark.common.command.CommandModule; +import me.lucko.spark.common.command.CommandResponseHandler; import me.lucko.spark.common.command.tabcomplete.CompletionSupplier; import me.lucko.spark.common.command.tabcomplete.TabCompleter; -import me.lucko.spark.sampler.Sampler; -import me.lucko.spark.sampler.SamplerBuilder; -import me.lucko.spark.sampler.ThreadDumper; -import me.lucko.spark.sampler.ThreadGrouper; -import me.lucko.spark.sampler.TickCounter; - +import me.lucko.spark.common.sampler.Sampler; +import me.lucko.spark.common.sampler.SamplerBuilder; +import me.lucko.spark.common.sampler.ThreadDumper; +import me.lucko.spark.common.sampler.ThreadGrouper; +import me.lucko.spark.common.sampler.TickCounter; import okhttp3.MediaType; import java.io.IOException; @@ -62,15 +62,15 @@ public class SamplerModule<S> implements CommandModule<S> { .argumentUsage("interval", "interval millis") .argumentUsage("only-ticks-over", "tick length millis") .argumentUsage("include-line-numbers", null) - .executor((platform, sender, arguments) -> { + .executor((platform, sender, resp, arguments) -> { int timeoutSeconds = arguments.intFlag("timeout"); if (timeoutSeconds != -1 && timeoutSeconds <= 10) { - platform.sendPrefixedMessage(sender, "&cThe specified timeout is not long enough for accurate results to be formed. Please choose a value greater than 10."); + resp.replyPrefixed("&cThe specified timeout is not long enough for accurate results to be formed. Please choose a value greater than 10."); return; } if (timeoutSeconds != -1 && timeoutSeconds < 30) { - platform.sendPrefixedMessage(sender, "&7The accuracy of the output will significantly improve when sampling is able to run for longer periods. Consider setting a timeout value over 30 seconds."); + resp.replyPrefixed("&7The accuracy of the output will significantly improve when sampling is able to run for longer periods. Consider setting a timeout value over 30 seconds."); } double intervalMillis = arguments.doubleFlag("interval"); @@ -84,7 +84,7 @@ public class SamplerModule<S> implements CommandModule<S> { ThreadDumper threadDumper; if (threads.isEmpty()) { // use the server thread - threadDumper = platform.getDefaultThreadDumper(); + threadDumper = platform.getPlugin().getDefaultThreadDumper(); } else if (threads.contains("*")) { threadDumper = ThreadDumper.ALL; } else { @@ -108,10 +108,9 @@ public class SamplerModule<S> implements CommandModule<S> { int ticksOver = arguments.intFlag("only-ticks-over"); TickCounter tickCounter = null; if (ticksOver != -1) { - try { - tickCounter = platform.newTickCounter(); - } catch (UnsupportedOperationException e) { - platform.sendPrefixedMessage(sender, "&cTick counting is not supported!"); + tickCounter = platform.getTickCounter(); + if (tickCounter == null) { + resp.replyPrefixed("&cTick counting is not supported!"); return; } } @@ -119,11 +118,11 @@ public class SamplerModule<S> implements CommandModule<S> { Sampler sampler; synchronized (this.activeSamplerMutex) { if (this.activeSampler != null) { - platform.sendPrefixedMessage(sender, "&7An active sampler is already running."); + resp.replyPrefixed("&7An active sampler is already running."); return; } - platform.sendPrefixedMessage("&7Initializing a new profiler, please wait..."); + |
