Timings v2

TODO: Add #isStopping to FullServerTickHandler#stopTiming in patch 191
expose isRunning
This commit is contained in:
Aikar
2016-02-29 18:48:17 -06:00
parent 30e04bfa2f
commit d4d8262f6e
32 changed files with 3030 additions and 400 deletions

View File

@@ -870,7 +870,6 @@ public final class Bukkit {
*/
public static void reload() {
server.reload();
org.spigotmc.CustomTimingsHandler.reload(); // Spigot
}
/**

View File

@@ -1978,6 +1978,26 @@ public interface Server extends PluginMessageRecipient, net.kyori.adventure.audi
throw new UnsupportedOperationException("Not supported yet.");
}
// Paper start
@NotNull
public org.bukkit.configuration.file.YamlConfiguration getBukkitConfig()
{
throw new UnsupportedOperationException( "Not supported yet." );
}
@NotNull
public org.bukkit.configuration.file.YamlConfiguration getSpigotConfig()
{
throw new UnsupportedOperationException("Not supported yet.");
}
@NotNull
public org.bukkit.configuration.file.YamlConfiguration getPaperConfig()
{
throw new UnsupportedOperationException("Not supported yet.");
}
// Paper end
/**
* Sends the component to the player
*

View File

@@ -0,0 +1,25 @@
package org.bukkit.command;
import org.jetbrains.annotations.NotNull;
/**
* @deprecated Timings will be removed in the future
*/
@Deprecated(forRemoval = true)
public class BufferedCommandSender implements MessageCommandSender {
private final StringBuffer buffer = new StringBuffer();
@Override
public void sendMessage(@NotNull String message) {
buffer.append(message);
buffer.append("\n");
}
@NotNull
public String getBuffer() {
return buffer.toString();
}
public void reset() {
this.buffer.setLength(0);
}
}

View File

@@ -33,7 +33,16 @@ public abstract class Command {
protected String usageMessage;
private String permission;
private net.kyori.adventure.text.Component permissionMessage; // Paper
public org.spigotmc.CustomTimingsHandler timings; // Spigot
/**
* @deprecated Timings will be removed in the future
*/
@Deprecated(forRemoval = true)
public co.aikar.timings.Timing timings; // Paper
/**
* @deprecated Timings will be removed in the future
*/
@Deprecated(forRemoval = true)
@NotNull public String getTimingName() {return getName();} // Paper
protected Command(@NotNull String name) {
this(name, "", "/" + name, new ArrayList<String>());
@@ -47,7 +56,6 @@ public abstract class Command {
this.usageMessage = (usageMessage == null) ? "/" + name : usageMessage;
this.aliases = aliases;
this.activeAliases = new ArrayList<String>(aliases);
this.timings = new org.spigotmc.CustomTimingsHandler("** Command: " + name); // Spigot
}
/**
@@ -245,7 +253,6 @@ public abstract class Command {
}
this.nextLabel = name;
if (!isRegistered()) {
this.timings = new org.spigotmc.CustomTimingsHandler("** Command: " + name); // Spigot
this.label = name;
return true;
}

View File

@@ -9,6 +9,7 @@ public class FormattedCommandAlias extends Command {
public FormattedCommandAlias(@NotNull String alias, @NotNull String[] formatStrings) {
super(alias);
timings = co.aikar.timings.TimingsManager.getCommandTiming("minecraft", this); // Spigot
this.formatStrings = formatStrings;
}
@@ -113,6 +114,10 @@ public class FormattedCommandAlias extends Command {
return formatString;
}
@NotNull
@Override // Paper
public String getTimingName() {return "Command Forwarder - " + super.getTimingName();} // Paper
private static boolean inRange(int i, int j, int k) {
return i >= j && i <= k;
}

View File

@@ -0,0 +1,138 @@
package org.bukkit.command;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.Plugin;
import java.util.Set;
import java.util.UUID;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* For when all you care about is just messaging
*
* @deprecated Timings will be removed in the future
*/
@Deprecated(forRemoval = true)
public interface MessageCommandSender extends CommandSender {
@Override
default void sendMessage(@NotNull String[] messages) {
for (String message : messages) {
sendMessage(message);
}
}
@Override
default void sendMessage(@Nullable UUID sender, @NotNull String message) {
sendMessage(message);
}
@Override
default void sendMessage(@Nullable UUID sender, @NotNull String[] messages) {
for (String message : messages) {
sendMessage(message);
}
}
@NotNull
@Override
default Server getServer() {
return Bukkit.getServer();
}
// Paper start
@Override
default net.kyori.adventure.text.@org.jetbrains.annotations.NotNull Component name() {
throw new UnsupportedOperationException();
}
// Paper end
@NotNull
@Override
default String getName() {
throw new UnsupportedOperationException();
}
@Override
default boolean isOp() {
throw new UnsupportedOperationException();
}
@Override
default void setOp(boolean value) {
throw new UnsupportedOperationException();
}
@Override
default boolean isPermissionSet(@NotNull String name) {
throw new UnsupportedOperationException();
}
@Override
default boolean isPermissionSet(@NotNull Permission perm) {
throw new UnsupportedOperationException();
}
@Override
default boolean hasPermission(@NotNull String name) {
throw new UnsupportedOperationException();
}
@Override
default boolean hasPermission(@NotNull Permission perm) {
throw new UnsupportedOperationException();
}
@NotNull
@Override
default PermissionAttachment addAttachment(@NotNull Plugin plugin, @NotNull String name, boolean value) {
throw new UnsupportedOperationException();
}
@NotNull
@Override
default PermissionAttachment addAttachment(@NotNull Plugin plugin) {
throw new UnsupportedOperationException();
}
@NotNull
@Override
default PermissionAttachment addAttachment(@NotNull Plugin plugin, @NotNull String name, boolean value, int ticks) {
throw new UnsupportedOperationException();
}
@NotNull
@Override
default PermissionAttachment addAttachment(@NotNull Plugin plugin, int ticks) {
throw new UnsupportedOperationException();
}
@Override
default void removeAttachment(@NotNull PermissionAttachment attachment) {
throw new UnsupportedOperationException();
}
@Override
default void recalculatePermissions() {
throw new UnsupportedOperationException();
}
@NotNull
@Override
default Set<PermissionAttachmentInfo> getEffectivePermissions() {
throw new UnsupportedOperationException();
}
@NotNull
@Override
default Spigot spigot() {
throw new UnsupportedOperationException();
}
}

View File

@@ -16,7 +16,6 @@ import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.command.defaults.HelpCommand;
import org.bukkit.command.defaults.PluginsCommand;
import org.bukkit.command.defaults.ReloadCommand;
import org.bukkit.command.defaults.TimingsCommand;
import org.bukkit.command.defaults.VersionCommand;
import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;
@@ -36,7 +35,7 @@ public class SimpleCommandMap implements CommandMap {
register("bukkit", new VersionCommand("version"));
register("bukkit", new ReloadCommand("reload"));
//register("bukkit", new PluginsCommand("plugins")); // Paper
register("bukkit", new TimingsCommand("timings"));
register("bukkit", new co.aikar.timings.TimingsCommand("timings")); // Paper
}
public void setFallbackCommands() {
@@ -68,6 +67,7 @@ public class SimpleCommandMap implements CommandMap {
*/
@Override
public boolean register(@NotNull String label, @NotNull String fallbackPrefix, @NotNull Command command) {
command.timings = co.aikar.timings.TimingsManager.getCommandTiming(fallbackPrefix, command); // Paper
label = label.toLowerCase(Locale.ROOT).trim();
fallbackPrefix = fallbackPrefix.toLowerCase(Locale.ROOT).trim();
boolean registered = register(label, command, false, fallbackPrefix);
@@ -144,16 +144,22 @@ public class SimpleCommandMap implements CommandMap {
return false;
}
// Paper start - Plugins do weird things to workaround normal registration
if (target.timings == null) {
target.timings = co.aikar.timings.TimingsManager.getCommandTiming(null, target);
}
// Paper end
try {
target.timings.startTiming(); // Spigot
try (co.aikar.timings.Timing ignored = target.timings.startTiming()) { // Paper - use try with resources
// Note: we don't return the result of target.execute as thats success / failure, we return handled (true) or not handled (false)
target.execute(sender, sentCommandLabel, Arrays.copyOfRange(args, 1, args.length));
target.timings.stopTiming(); // Spigot
} // target.timings.stopTiming(); // Spigot // Paper
} catch (CommandException ex) {
target.timings.stopTiming(); // Spigot
//target.timings.stopTiming(); // Spigot // Paper
throw ex;
} catch (Throwable ex) {
target.timings.stopTiming(); // Spigot
//target.timings.stopTiming(); // Spigot // Paper
throw new CommandException("Unhandled exception executing '" + commandLine + "' in " + target, ex);
}

View File

@@ -1,250 +0,0 @@
package org.bukkit.command.defaults;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredListener;
import org.bukkit.plugin.TimedRegisteredListener;
import org.bukkit.util.StringUtil;
import org.jetbrains.annotations.NotNull;
// Spigot start
// CHECKSTYLE:OFF
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import org.bukkit.command.RemoteConsoleCommandSender;
import org.bukkit.plugin.SimplePluginManager;
import org.spigotmc.CustomTimingsHandler;
// CHECKSTYLE:ON
// Spigot end
public class TimingsCommand extends BukkitCommand {
private static final List<String> TIMINGS_SUBCOMMANDS = ImmutableList.of("report", "reset", "on", "off", "paste"); // Spigot
public static long timingStart = 0; // Spigot
public TimingsCommand(@NotNull String name) {
super(name);
this.description = "Manages Spigot Timings data to see performance of the server."; // Spigot
this.usageMessage = "/timings <reset|report|on|off|paste>"; // Spigot
this.setPermission("bukkit.command.timings");
}
// Spigot start - redesigned Timings Command
public void executeSpigotTimings(@NotNull CommandSender sender, @NotNull String[] args) {
if ("on".equals(args[0])) {
((SimplePluginManager) Bukkit.getPluginManager()).useTimings(true);
CustomTimingsHandler.reload();
sender.sendMessage("Enabled Timings & Reset");
return;
} else if ("off".equals(args[0])) {
((SimplePluginManager) Bukkit.getPluginManager()).useTimings(false);
sender.sendMessage("Disabled Timings");
return;
}
if (!Bukkit.getPluginManager().useTimings()) {
sender.sendMessage("Please enable timings by typing /timings on");
return;
}
boolean paste = "paste".equals(args[0]);
if ("reset".equals(args[0])) {
CustomTimingsHandler.reload();
sender.sendMessage("Timings reset");
} else if ("merged".equals(args[0]) || "report".equals(args[0]) || paste) {
long sampleTime = System.nanoTime() - timingStart;
int index = 0;
File timingFolder = new File("timings");
timingFolder.mkdirs();
File timings = new File(timingFolder, "timings.txt");
ByteArrayOutputStream bout = (paste) ? new ByteArrayOutputStream() : null;
while (timings.exists()) timings = new File(timingFolder, "timings" + (++index) + ".txt");
PrintStream fileTimings = null;
try {
fileTimings = (paste) ? new PrintStream(bout) : new PrintStream(timings);
CustomTimingsHandler.printTimings(fileTimings);
fileTimings.println("Sample time " + sampleTime + " (" + sampleTime / 1E9 + "s)");
fileTimings.println("<spigotConfig>");
fileTimings.println(Bukkit.spigot().getConfig().saveToString());
fileTimings.println("</spigotConfig>");
if (paste) {
new PasteThread(sender, bout).start();
return;
}
sender.sendMessage("Timings written to " + timings.getPath());
sender.sendMessage("Paste contents of file into form at http://www.spigotmc.org/go/timings to read results.");
} catch (IOException e) {
} finally {
if (fileTimings != null) {
fileTimings.close();
}
}
}
}
// Spigot end
@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String currentAlias, @NotNull String[] args) {
if (!testPermission(sender)) return true;
if (args.length < 1) { // Spigot
sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
return false;
}
// Spigot start
if (true) {
executeSpigotTimings(sender, args);
return true;
}
// Spigot end
if (!sender.getServer().getPluginManager().useTimings()) {
sender.sendMessage("Please enable timings by setting \"settings.plugin-profiling\" to true in bukkit.yml");
return true;
}
boolean separate = "separate".equalsIgnoreCase(args[0]);
if ("reset".equalsIgnoreCase(args[0])) {
for (HandlerList handlerList : HandlerList.getHandlerLists()) {
for (RegisteredListener listener : handlerList.getRegisteredListeners()) {
if (listener instanceof TimedRegisteredListener) {
((TimedRegisteredListener) listener).reset();
}
}
}
sender.sendMessage("Timings reset");
} else if ("merged".equalsIgnoreCase(args[0]) || separate) {
int index = 0;
int pluginIdx = 0;
File timingFolder = new File("timings");
timingFolder.mkdirs();
File timings = new File(timingFolder, "timings.txt");
File names = null;
while (timings.exists()) timings = new File(timingFolder, "timings" + (++index) + ".txt");
PrintStream fileTimings = null;
PrintStream fileNames = null;
try {
fileTimings = new PrintStream(timings);
if (separate) {
names = new File(timingFolder, "names" + index + ".txt");
fileNames = new PrintStream(names);
}
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
pluginIdx++;
long totalTime = 0;
if (separate) {
fileNames.println(pluginIdx + " " + plugin.getDescription().getFullName());
fileTimings.println("Plugin " + pluginIdx);
} else {
fileTimings.println(plugin.getDescription().getFullName());
}
for (RegisteredListener listener : HandlerList.getRegisteredListeners(plugin)) {
if (listener instanceof TimedRegisteredListener) {
TimedRegisteredListener trl = (TimedRegisteredListener) listener;
long time = trl.getTotalTime();
int count = trl.getCount();
if (count == 0) continue;
long avg = time / count;
totalTime += time;
Class<? extends Event> eventClass = trl.getEventClass();
if (count > 0 && eventClass != null) {
fileTimings.println(" " + eventClass.getSimpleName() + (trl.hasMultiple() ? " (and sub-classes)" : "") + " Time: " + time + " Count: " + count + " Avg: " + avg);
}
}
}
fileTimings.println(" Total time " + totalTime + " (" + totalTime / 1000000000 + "s)");
}
sender.sendMessage("Timings written to " + timings.getPath());
if (separate) sender.sendMessage("Names written to " + names.getPath());
} catch (IOException e) {
} finally {
if (fileTimings != null) {
fileTimings.close();
}
if (fileNames != null) {
fileNames.close();
}
}
} else {
sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
return false;
}
return true;
}
@NotNull
@Override
public List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) {
Preconditions.checkArgument(sender != null, "Sender cannot be null");
Preconditions.checkArgument(args != null, "Arguments cannot be null");
Preconditions.checkArgument(alias != null, "Alias cannot be null");
if (args.length == 1) {
return StringUtil.copyPartialMatches(args[0], TIMINGS_SUBCOMMANDS, new ArrayList<String>(TIMINGS_SUBCOMMANDS.size()));
}
return ImmutableList.of();
}
// Spigot start
private static class PasteThread extends Thread {
private final CommandSender sender;
private final ByteArrayOutputStream bout;
public PasteThread(@NotNull CommandSender sender, @NotNull ByteArrayOutputStream bout) {
super("Timings paste thread");
this.sender = sender;
this.bout = bout;
}
@Override
public synchronized void start() {
if (sender instanceof RemoteConsoleCommandSender) {
run();
} else {
super.start();
}
}
@Override
public void run() {
try {
HttpURLConnection con = (HttpURLConnection) new URL("https://timings.spigotmc.org/paste").openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setInstanceFollowRedirects(false);
OutputStream out = con.getOutputStream();
out.write(bout.toByteArray());
out.close();
com.google.gson.JsonObject location = new com.google.gson.Gson().fromJson(new java.io.InputStreamReader(con.getInputStream()), com.google.gson.JsonObject.class);
con.getInputStream().close();
String pasteID = location.get("key").getAsString();
sender.sendMessage(ChatColor.GREEN + "Timings results can be viewed at https://www.spigotmc.org/go/timings?url=" + pasteID);
} catch (IOException ex) {
sender.sendMessage(ChatColor.RED + "Error pasting timings, check your console for more information");
Bukkit.getServer().getLogger().log(Level.WARNING, "Could not paste timings", ex);
}
}
}
// Spigot end
}

View File

@@ -2771,7 +2771,19 @@ public interface Player extends HumanEntity, Conversable, OfflinePlayer, PluginM
@Deprecated // Paper
public void sendMessage(@NotNull net.md_5.bungee.api.ChatMessageType position, @Nullable java.util.UUID sender, @NotNull net.md_5.bungee.api.chat.BaseComponent... components) {
throw new UnsupportedOperationException("Not supported yet.");
}
// Paper start
/**
* @return the player's ping
* @deprecated use {@link Player#getPing()}
*/
@Deprecated
public int getPing() {
throw new UnsupportedOperationException( "Not supported yet." );
}
// Paper end
}
@NotNull

View File

@@ -381,7 +381,6 @@ public final class SimplePluginManager implements PluginManager {
}
}
org.bukkit.command.defaults.TimingsCommand.timingStart = System.nanoTime(); // Spigot
return result.toArray(new Plugin[result.size()]);
}
@@ -429,9 +428,9 @@ public final class SimplePluginManager implements PluginManager {
if (result != null) {
plugins.add(result);
lookupNames.put(result.getDescription().getName(), result);
lookupNames.put(result.getDescription().getName().toLowerCase(java.util.Locale.ENGLISH), result); // Paper
for (String provided : result.getDescription().getProvides()) {
lookupNames.putIfAbsent(provided, result);
lookupNames.putIfAbsent(provided.toLowerCase(java.util.Locale.ENGLISH), result); // Paper
}
}
@@ -461,7 +460,7 @@ public final class SimplePluginManager implements PluginManager {
@Nullable
public synchronized Plugin getPlugin(@NotNull String name) {
if (true) {return this.paperPluginManager.getPlugin(name);} // Paper
return lookupNames.get(name.replace(' ', '_'));
return lookupNames.get(name.replace(' ', '_').toLowerCase(java.util.Locale.ENGLISH)); // Paper
}
@Override
@@ -689,7 +688,8 @@ public final class SimplePluginManager implements PluginManager {
throw new IllegalPluginAccessException("Plugin attempted to register " + event + " while not enabled");
}
if (useTimings) {
executor = new co.aikar.timings.TimedEventExecutor(executor, plugin, null, event); // Paper
if (false) { // Spigot - RL handles useTimings check now // Paper
getEventListeners(event).register(new TimedRegisteredListener(listener, executor, priority, plugin, ignoreCancelled));
} else {
getEventListeners(event).register(new RegisteredListener(listener, executor, priority, plugin, ignoreCancelled));
@@ -924,7 +924,7 @@ public final class SimplePluginManager implements PluginManager {
@Override
public boolean useTimings() {
if (true) {return this.paperPluginManager.useTimings();} // Paper
return useTimings;
return co.aikar.timings.Timings.isTimingsEnabled(); // Spigot
}
/**
@@ -932,8 +932,9 @@ public final class SimplePluginManager implements PluginManager {
*
* @param use True if per event timing code should be used
*/
@Deprecated(forRemoval = true)
public void useTimings(boolean use) {
useTimings = use;
co.aikar.timings.Timings.setTimingsEnabled(use); // Paper
}
// Paper start

View File

@@ -55,7 +55,6 @@ public final class JavaPluginLoader implements PluginLoader {
private final Pattern[] fileFilters = new Pattern[]{Pattern.compile("\\.jar$")};
private final List<PluginClassLoader> loaders = new CopyOnWriteArrayList<PluginClassLoader>();
private final LibraryLoader libraryLoader;
public static final CustomTimingsHandler pluginParentTimer = new CustomTimingsHandler("** Plugins"); // Spigot
/**
* This class was not meant to be constructed explicitly
@@ -294,27 +293,21 @@ public final class JavaPluginLoader implements PluginLoader {
}
}
final CustomTimingsHandler timings = new CustomTimingsHandler("Plugin: " + plugin.getDescription().getFullName() + " Event: " + listener.getClass().getName() + "::" + method.getName() + "(" + eventClass.getSimpleName() + ")", pluginParentTimer); // Spigot
EventExecutor executor = new EventExecutor() {
EventExecutor executor = new co.aikar.timings.TimedEventExecutor(new EventExecutor() { // Paper
@Override
public void execute(@NotNull Listener listener, @NotNull Event event) throws EventException {
public void execute(@NotNull Listener listener, @NotNull Event event) throws EventException { // Paper
try {
if (!eventClass.isAssignableFrom(event.getClass())) {
return;
}
// Spigot start
boolean isAsync = event.isAsynchronous();
if (!isAsync) timings.startTiming();
method.invoke(listener, event);
if (!isAsync) timings.stopTiming();
// Spigot end
} catch (InvocationTargetException ex) {
throw new EventException(ex.getCause());
} catch (Throwable t) {
throw new EventException(t);
}
}
};
}, plugin, method, eventClass); // Paper
if (false) { // Spigot - RL handles useTimings check now
eventSet.add(new TimedRegisteredListener(listener, executor, eh.priority(), plugin, eh.ignoreCancelled()));
} else {

View File

@@ -2,6 +2,7 @@ package org.bukkit.util;
import org.bukkit.Server;
import org.bukkit.event.server.ServerListPingEvent;
import org.jetbrains.annotations.Nullable;
/**
* This is a cached version of a server-icon. It's internal representation
@@ -12,4 +13,9 @@ import org.bukkit.event.server.ServerListPingEvent;
* @see Server#loadServerIcon(java.io.File)
* @see ServerListPingEvent#setServerIcon(CachedServerIcon)
*/
public interface CachedServerIcon {}
public interface CachedServerIcon {
@Nullable
public String getData(); // Paper
}

View File

@@ -1,137 +1,67 @@
/*
* This file is licensed under the MIT License (MIT).
*
* Copyright (c) 2014 Daniel Ennis <http://aikar.co>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spigotmc;
import java.io.PrintStream;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.defaults.TimingsCommand;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.bukkit.plugin.AuthorNagException;
import co.aikar.timings.Timing;
import co.aikar.timings.Timings;
import co.aikar.timings.TimingsManager;
import java.lang.reflect.Method;
import java.util.logging.Level;
/**
* Provides custom timing sections for /timings merged.
* This is here for legacy purposes incase any plugin used it.
*
* If you use this, migrate ASAP as this will be removed in the future!
*
* @deprecated
* @see co.aikar.timings.Timings#of
*/
public class CustomTimingsHandler {
private static Queue<CustomTimingsHandler> HANDLERS = new ConcurrentLinkedQueue<CustomTimingsHandler>();
/*========================================================================*/
private final String name;
private final CustomTimingsHandler parent;
private long count = 0;
private long start = 0;
private long timingDepth = 0;
private long totalTime = 0;
private long curTickTotal = 0;
private long violations = 0;
@Deprecated(forRemoval = true)
public final class CustomTimingsHandler {
private final Timing handler;
public CustomTimingsHandler(@NotNull String name) {
this(name, null);
}
Timing timing;
public CustomTimingsHandler(@NotNull String name, @Nullable CustomTimingsHandler parent) {
this.name = name;
this.parent = parent;
HANDLERS.add(this);
}
/**
* Prints the timings and extra data to the given stream.
*
* @param printStream output stream
*/
public static void printTimings(@NotNull PrintStream printStream) {
printStream.println("Minecraft");
for (CustomTimingsHandler timings : HANDLERS) {
long time = timings.totalTime;
long count = timings.count;
if (count == 0) {
continue;
}
long avg = time / count;
printStream.println(" " + timings.name + " Time: " + time + " Count: " + count + " Avg: " + avg + " Violations: " + timings.violations);
new AuthorNagException("Deprecated use of CustomTimingsHandler. Please Switch to Timings.of ASAP").printStackTrace();
try {
final Method ofSafe = TimingsManager.class.getDeclaredMethod("getHandler", String.class, String.class, Timing.class);
ofSafe.setAccessible(true);
timing = (Timing) ofSafe.invoke(null,"Minecraft", "(Deprecated API) " + name, null);
} catch (Exception e) {
e.printStackTrace();
Bukkit.getLogger().log(Level.SEVERE, "This handler could not be registered");
timing = Timings.NULL_HANDLER;
}
printStream.println("# Version " + Bukkit.getVersion());
int entities = 0;
int livingEntities = 0;
for (World world : Bukkit.getWorlds()) {
entities += world.getEntities().size();
livingEntities += world.getLivingEntities().size();
}
printStream.println("# Entities " + entities);
printStream.println("# LivingEntities " + livingEntities);
handler = timing;
}
/**
* Resets all timings.
*/
public static void reload() {
if (Bukkit.getPluginManager().useTimings()) {
for (CustomTimingsHandler timings : HANDLERS) {
timings.reset();
}
}
TimingsCommand.timingStart = System.nanoTime();
}
public void startTiming() { handler.startTiming(); }
public void stopTiming() { handler.stopTiming(); }
/**
* Ticked every tick by CraftBukkit to count the number of times a timer
* caused TPS loss.
*/
public static void tick() {
if (Bukkit.getPluginManager().useTimings()) {
for (CustomTimingsHandler timings : HANDLERS) {
if (timings.curTickTotal > 50000000) {
timings.violations += Math.ceil(timings.curTickTotal / 50000000);
}
timings.curTickTotal = 0;
timings.timingDepth = 0; // incase reset messes this up
}
}
}
/**
* Starts timing to track a section of code.
*/
public void startTiming() {
// If second condtion fails we are already timing
if (Bukkit.getPluginManager().useTimings() && ++timingDepth == 1) {
start = System.nanoTime();
if (parent != null && ++parent.timingDepth == 1) {
parent.start = start;
}
}
}
/**
* Stops timing a section of code.
*/
public void stopTiming() {
if (Bukkit.getPluginManager().useTimings()) {
if (--timingDepth != 0 || start == 0) {
return;
}
long diff = System.nanoTime() - start;
totalTime += diff;
curTickTotal += diff;
count++;
start = 0;
if (parent != null) {
parent.stopTiming();
}
}
}
/**
* Reset this timer, setting all values to zero.
*/
public void reset() {
count = 0;
violations = 0;
curTickTotal = 0;
totalTime = 0;
start = 0;
timingDepth = 0;
}
}