feat: 完善能耗监控系统

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-07-13 19:38:10 +08:00
parent ffeccab654
commit f8a987606a
14 changed files with 164 additions and 67 deletions

View File

@ -90,7 +90,7 @@
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<type>jar</type>
<version>1.9-R0.1-SNAPSHOT</version>
<version>1.10.2-R0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.citycraft</groupId>

View File

@ -45,9 +45,7 @@ public class Yum extends JavaPlugin {
@Override
public void onDisable() {
NetworkManager.unregister();
// for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
// YumAPI.uninject(plugin);
// }
}
@Override
@ -60,8 +58,9 @@ public class Yum extends JavaPlugin {
initListeners();
initRunnable();
new VersionChecker(this);
YumAPI.updaterepo(Bukkit.getConsoleSender());
YumAPI.updatecheck(Bukkit.getConsoleSender());
YumAPI.updateInject();
YumAPI.updateRepo(Bukkit.getConsoleSender());
YumAPI.updateCheck(Bukkit.getConsoleSender());
}
@Override

View File

@ -12,7 +12,9 @@ import org.bukkit.plugin.Plugin;
import cn.citycraft.CommonData.UpdatePlugin;
import cn.citycraft.PluginHelper.kit.PKit;
import cn.citycraft.PluginHelper.kit.PluginKit;
import pw.yumc.Yum.inject.CommandInjector;
import pw.yumc.Yum.inject.ListenerInjector;
import pw.yumc.Yum.inject.TaskInjector;
import pw.yumc.Yum.managers.ConfigManager;
import pw.yumc.Yum.managers.DownloadManager;
@ -104,10 +106,12 @@ public class YumAPI {
* 插件
*/
public static void inject(final Plugin plugin) {
if (plugin.isEnabled()) {
CommandInjector.inject(plugin);
// ListenerInjector.inject(plugin);
ListenerInjector.inject(plugin);
TaskInjector.inject(plugin);
}
}
/**
* 安装新插件
@ -150,8 +154,8 @@ public class YumAPI {
* 插件名称
* @return 是否安装成功
*/
public static boolean installfromyum(final CommandSender sender, final String pluginname) {
return installfromyum(sender, pluginname, null);
public static boolean installFromYum(final CommandSender sender, final String pluginname) {
return installFromYum(sender, pluginname, null);
}
/**
@ -165,7 +169,7 @@ public class YumAPI {
* 插件版本
* @return 是否安装成功
*/
public static boolean installfromyum(final CommandSender sender, final String pluginname, final String version) {
public static boolean installFromYum(final CommandSender sender, final String pluginname, final String version) {
final PluginInfo pi = repo.getPlugin(pluginname);
if (pi != null) {
return install(sender, pi.name, pi.getUrl(sender, version));
@ -203,6 +207,18 @@ public class YumAPI {
plugman.reload(plugin);
}
/**
* 取消注入
*
* @param plugin
* 插件
*/
public static void uninject() {
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
YumAPI.uninject(plugin);
}
}
/**
* 取消注入
*
@ -264,7 +280,7 @@ public class YumAPI {
* @param sender
* 命令发送者
*/
public static void updateall(final CommandSender sender) {
public static void updateAll(final CommandSender sender) {
main.getServer().getScheduler().runTaskAsynchronously(main, new Runnable() {
@Override
public void run() {
@ -279,7 +295,7 @@ public class YumAPI {
sender.sendMessage("§d开始更新服务器可更新插件");
for (final Plugin updateplugin : ulist) {
sender.sendMessage("§d一键更新: §a开始更新" + updateplugin.getName() + "!");
if (!updatefromyum(sender, updateplugin, null, true)) {
if (!updateFromYum(sender, updateplugin, null, true)) {
failed++;
}
}
@ -288,7 +304,7 @@ public class YumAPI {
}
sender.sendMessage("§d一键更新: §e已下载所有需要升级的插件到 服务器更新 文件夹");
sender.sendMessage("§d一键更新: §e插件将在重启后自动更新(或使用§b/yum upgrade§e直接升级)!");
updatecheck(sender);
updateCheck(sender);
} else {
sender.sendMessage("§6更新: §e未找到需要更新且可以用Yum处理的插件!");
}
@ -303,8 +319,8 @@ public class YumAPI {
* @param sender
* 命令发送者
*/
public static void updatecheck(final CommandSender sender) {
main.getServer().getScheduler().runTaskLaterAsynchronously(main, new Runnable() {
public static void updateCheck(final CommandSender sender) {
PluginKit.runTaskLaterAsync(new Runnable() {
@Override
public void run() {
final List<Plugin> ulist = getUpdateList(sender);
@ -324,8 +340,8 @@ public class YumAPI {
* 插件实体
* @return 是否更新成功
*/
public static boolean updatefromyum(final CommandSender sender, final Plugin plugin) {
return updatefromyum(sender, plugin, null);
public static boolean updateFromYum(final CommandSender sender, final Plugin plugin) {
return updateFromYum(sender, plugin, null);
}
/**
@ -339,8 +355,8 @@ public class YumAPI {
* 插件版本(null则自动获取)
* @return
*/
public static boolean updatefromyum(final CommandSender sender, final Plugin plugin, final String version) {
return updatefromyum(sender, plugin, version, false);
public static boolean updateFromYum(final CommandSender sender, final Plugin plugin, final String version) {
return updateFromYum(sender, plugin, version, false);
}
/**
@ -356,7 +372,7 @@ public class YumAPI {
* 是否一键更新
* @return
*/
public static boolean updatefromyum(final CommandSender sender, final Plugin plugin, final String version, final boolean oneKeyUpdate) {
public static boolean updateFromYum(final CommandSender sender, final Plugin plugin, final String version, final boolean oneKeyUpdate) {
final PluginInfo pi = repo.getPlugin(plugin.getName());
if (pi != null) {
final File pFile = new File(Bukkit.getUpdateFolderFile(), plugman.getPluginFile(plugin).getName());
@ -374,11 +390,25 @@ public class YumAPI {
return false;
}
/**
* 更新注入
*/
public static void updateInject() {
PluginKit.runTaskLater(new Runnable() {
@Override
public void run() {
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
YumAPI.inject(plugin);
}
}
}, 60);
}
/**
* 更新Yum源数据
*/
public static void updaterepo(final CommandSender sender) {
main.getServer().getScheduler().runTaskAsynchronously(main, new Runnable() {
public static void updateRepo(final CommandSender sender) {
PluginKit.runTaskAsync(new Runnable() {
@Override
public void run() {
repo.updateRepositories(sender);

View File

@ -23,6 +23,7 @@ import cn.citycraft.PluginHelper.commands.HandlerCommands;
import cn.citycraft.PluginHelper.commands.InvokeCommandEvent;
import cn.citycraft.PluginHelper.commands.InvokeSubCommand;
import cn.citycraft.PluginHelper.ext.kit.Reflect;
import cn.citycraft.PluginHelper.kit.StrKit;
import pw.yumc.Yum.Yum;
import pw.yumc.Yum.inject.CommandInjector;
import pw.yumc.Yum.inject.ListenerInjector;
@ -34,10 +35,13 @@ import pw.yumc.Yum.inject.TaskInjector;
* @author
*/
public class MonitorCommand implements HandlerCommands {
Yum main;
private final String prefix = "§6[§bYum §a能耗监控§6] ";
private final String total = "§6总耗时: §a%.2f秒 ";
private final String count = "§6执行次数: §b%s次 ";
private final String avg = "§6平均耗时: §d%.5f秒!";
private final String p_n_f = prefix + "§c插件不存在!";
public MonitorCommand(final Yum yum) {
main = yum;
final InvokeSubCommand cmdhandler = new InvokeSubCommand(yum, "monitor");
cmdhandler.setAllCommandOnlyConsole(yum.getConfig().getBoolean("onlyFileCommandConsole", true));
cmdhandler.registerCommands(this);
@ -54,12 +58,12 @@ public class MonitorCommand implements HandlerCommands {
final String pname = e.getArgs()[0];
final CommandSender sender = e.getSender();
if (Bukkit.getPluginManager().getPlugin(pname) == null) {
sender.sendMessage("§c插件不存在!");
sender.sendMessage(p_n_f);
return;
}
final PluginManager pluginManager = Bukkit.getPluginManager();
final SimpleCommandMap commandMap = Reflect.on(pluginManager).get("commandMap");
sender.sendMessage("§6插件 §b" + pname + " §6的命令能耗如下!");
sender.sendMessage(prefix + "§6插件 §b" + pname + " §6的命令能耗如下!");
final Map<String, Command> temp = new HashMap<>();
for (final Command command : commandMap.getCommands()) {
if (command instanceof PluginCommand) {
@ -76,10 +80,10 @@ public class MonitorCommand implements HandlerCommands {
final CommandInjector injected = (CommandInjector) executor;
final StringBuffer str = new StringBuffer();
str.append("§6- §e" + command.getValue().getName() + " ");
str.append(String.format("§6总耗时: §a%.2f秒 ", injected.totalTime / 1000000.0));
str.append("§6执行次数: §b" + injected.count + "");
str.append(String.format(total, injected.totalTime / 1000000.0));
str.append(String.format(count, injected.count));
if (injected.count != 0) {
str.append(String.format("§6平均耗时: §d%.2f秒!", injected.totalTime / 1000000.0 / injected.count));
str.append(String.format(avg, injected.totalTime / 1000000.0 / injected.count));
}
e.getSender().sendMessage(str.toString());
}
@ -92,26 +96,39 @@ public class MonitorCommand implements HandlerCommands {
final CommandSender sender = e.getSender();
final Plugin plugin = Bukkit.getPluginManager().getPlugin(pname);
if (plugin == null) {
sender.sendMessage("§c插件不存在!");
sender.sendMessage(p_n_f);
return;
}
sender.sendMessage("§6插件 §b" + pname + " §6的事件能耗如下!");
sender.sendMessage(prefix + "§6插件 §b" + pname + " §6的事件能耗如下!");
final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin);
final Map<String, Long> eventTotalTime = new HashMap<>();
final Map<String, Long> eventCount = new HashMap<>();
for (final RegisteredListener listener : listeners) {
final EventExecutor executor = Reflect.on(listener).get("executor");
if (executor instanceof ListenerInjector) {
final ListenerInjector injected = (ListenerInjector) executor;
for (final String entry : injected.eventTotalTime.keySet()) {
if (eventTotalTime.containsKey(entry)) {
eventTotalTime.put(entry, eventTotalTime.get(entry) + injected.eventTotalTime.get(entry));
eventCount.put(entry, eventCount.get(entry) + injected.eventCount.get(entry));
} else {
eventTotalTime.put(entry, injected.eventTotalTime.get(entry));
eventCount.put(entry, injected.eventCount.get(entry));
}
}
}
}
for (final String event : eventTotalTime.keySet()) {
final StringBuffer str = new StringBuffer();
str.append("§6- §e" + injected.getOriginalExecutor().getClass().getSimpleName() + " ");
str.append(String.format("§6总耗时: §a%.2f秒 ", injected.totalTime / 1000000.0));
str.append("§6执行次数: §b" + injected.count + "");
if (injected.count != 0) {
str.append(String.format("§6平均耗时: §d%.2f秒!", injected.totalTime / 1000000.0 / injected.count));
str.append("§6- §e" + event + " ");
str.append(String.format(total, eventTotalTime.get(event) / 1000000.0));
str.append(String.format(count, eventCount.get(event)));
if (eventCount.get(event) != 0) {
str.append(String.format(avg, eventTotalTime.get(event) / 1000000.0 / eventCount.get(event)));
}
e.getSender().sendMessage(str.toString());
}
}
}
@HandlerCommand(name = "task", minimumArguments = 1, possibleArguments = "插件名称")
public void task(final InvokeCommandEvent e) {
@ -119,24 +136,27 @@ public class MonitorCommand implements HandlerCommands {
final CommandSender sender = e.getSender();
final Plugin plugin = Bukkit.getPluginManager().getPlugin(pname);
if (plugin == null) {
sender.sendMessage("§c插件不存在!");
sender.sendMessage(p_n_f);
return;
}
final List<BukkitTask> pendingTasks = Bukkit.getScheduler().getPendingTasks();
sender.sendMessage("§6插件 §b" + pname + " §6的任务能耗如下!");
sender.sendMessage(prefix + "§6插件 §b" + pname + " §6的任务能耗如下!");
for (final BukkitTask pendingTask : pendingTasks) {
if (pendingTask.getOwner().getName().equalsIgnoreCase(pname)) {
final Runnable task = Reflect.on(pendingTask).get("task");
if (task instanceof TaskInjector) {
final TaskInjector executor = (TaskInjector) task;
final StringBuffer str = new StringBuffer();
str.append("§6- §e" + executor.getOriginalTask().getClass().getSimpleName() + " ");
str.append(String.format("§6总耗时: §a%.2f秒 ", executor.totalTime / 1000000.0));
str.append("§6执行次数: §b" + executor.count + "");
final Class<? extends Runnable> taskName = executor.getOriginalTask().getClass();
str.append("§6- §e" + (StrKit.isBlank(taskName.getSimpleName()) ? taskName.getName() : taskName.getSimpleName()) + " ");
str.append(String.format(total, executor.totalTime / 1000000.0));
str.append(String.format(count, executor.count));
if (executor.count != 0) {
str.append(String.format("§6平均耗时: §d%.2f秒!", executor.totalTime / 1000000.0 / executor.count));
str.append(String.format(avg, executor.totalTime / 1000000.0 / executor.count));
}
e.getSender().sendMessage(str.toString());
}
}
}
}
}

View File

@ -143,9 +143,9 @@ public class YumCommand implements HandlerCommands, Listener {
@Override
public void run() {
if (args.length < 2) {
YumAPI.installfromyum(sender, pluginname);
YumAPI.installFromYum(sender, pluginname);
} else {
YumAPI.installfromyum(sender, pluginname, args[1]);
YumAPI.installFromYum(sender, pluginname, args[1]);
}
}
});
@ -178,7 +178,7 @@ public class YumCommand implements HandlerCommands, Listener {
@EventHandler
public void onAdminJoin(final PlayerJoinEvent e) {
if (e.getPlayer().isOp()) {
YumAPI.updatecheck(e.getPlayer());
YumAPI.updateCheck(e.getPlayer());
}
}
@ -334,9 +334,9 @@ public class YumCommand implements HandlerCommands, Listener {
@Override
public void run() {
if (args.length < 2) {
YumAPI.updatefromyum(sender, plugin);
YumAPI.updateFromYum(sender, plugin);
} else {
YumAPI.updatefromyum(sender, plugin, args[1]);
YumAPI.updateFromYum(sender, plugin, args[1]);
}
}
});
@ -354,7 +354,7 @@ public class YumCommand implements HandlerCommands, Listener {
Bukkit.getScheduler().runTaskAsynchronously(main, new Runnable() {
@Override
public void run() {
YumAPI.updateall(e.getSender());
YumAPI.updateAll(e.getSender());
}
});
}

View File

@ -83,7 +83,7 @@ public class CommandInjector implements TabExecutor {
@Override
public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) {
final long start = System.nanoTime();
// todo add a more aggressive 10 ms cpu sample
// TODO add a more aggressive 10 ms cpu sample
final boolean result = originalExecutor.onCommand(sender, command, label, args);
final long end = System.nanoTime();
@ -95,7 +95,7 @@ public class CommandInjector implements TabExecutor {
@Override
public List<String> onTabComplete(final CommandSender sender, final Command command, final String alias, final String[] args) {
final long start = System.nanoTime();
// todo add a more aggressive 10 ms cpu sample
// TODO add a more aggressive 10 ms cpu sample
final List<String> result = originalCompleter.onTabComplete(sender, command, alias, args);
final long end = System.nanoTime();

View File

@ -1,6 +1,8 @@
package pw.yumc.Yum.inject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.event.Event;
import org.bukkit.event.EventException;
@ -9,6 +11,7 @@ import org.bukkit.event.Listener;
import org.bukkit.plugin.EventExecutor;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredListener;
import org.bukkit.plugin.TimedRegisteredListener;
import cn.citycraft.PluginHelper.ext.kit.Reflect;
@ -16,17 +19,25 @@ public class ListenerInjector implements EventExecutor {
private final EventExecutor originalExecutor;
public long totalTime;
public long count;
public Map<String, Long> eventTotalTime;
public Map<String, Long> eventCount;
public ListenerInjector(final EventExecutor originalExecutor) {
this.originalExecutor = originalExecutor;
eventTotalTime = new HashMap<>();
eventCount = new HashMap<>();
}
public static void inject(final Plugin plugin) {
final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin);
for (final RegisteredListener listener : listeners) {
if (listener instanceof TimedRegisteredListener) {
return;
}
final EventExecutor originalExecutor = Reflect.on(listener).get("executor");
if (originalExecutor instanceof ListenerInjector) {
return;
}
final ListenerInjector listenerInjector = new ListenerInjector(originalExecutor);
Reflect.on(listener).set("executor", listenerInjector);
}
@ -35,6 +46,9 @@ public class ListenerInjector implements EventExecutor {
public static void uninject(final Plugin plugin) {
final List<RegisteredListener> listeners = HandlerList.getRegisteredListeners(plugin);
for (final RegisteredListener listener : listeners) {
if (listener instanceof TimedRegisteredListener) {
return;
}
final EventExecutor executor = Reflect.on(listener).get("executor");
if (executor instanceof ListenerInjector) {
Reflect.on(listener).set("executor", ((ListenerInjector) executor).getOriginalExecutor());
@ -46,12 +60,17 @@ public class ListenerInjector implements EventExecutor {
public void execute(final Listener listener, final Event event) throws EventException {
if (!event.isAsynchronous()) {
final long start = System.nanoTime();
// todo add a more aggressive 10 ms cpu sample
// TODO add a more aggressive 10 ms cpu sample
originalExecutor.execute(listener, event);
final long end = System.nanoTime();
totalTime += end - start;
count++;
final String en = event.getEventName();
if (eventTotalTime.containsKey(en)) {
eventTotalTime.put(en, eventTotalTime.get(en) + end - start);
eventCount.put(en, eventCount.get(en) + 1);
} else {
eventTotalTime.put(en, end - start);
eventCount.put(en, 1L);
}
} else {
originalExecutor.execute(listener, event);
}

View File

@ -29,6 +29,9 @@ public class TaskInjector implements Runnable {
// we could ignore async tasks for now
if (pendingTask.isSync() && pendingTask.getOwner().equals(plugin)) {
final Runnable originalTask = Reflect.on(pendingTask).get("task");
if (originalTask instanceof TaskInjector) {
return;
}
final TaskInjector taskInjector = new TaskInjector(originalTask);
Reflect.on(pendingTask).set("task", taskInjector);
}
@ -56,7 +59,7 @@ public class TaskInjector implements Runnable {
@Override
public void run() {
final long start = System.nanoTime();
// todo add a more aggressive 10 ms cpu sample
// TODO add a more aggressive 10 ms cpu sample
originalTask.run();
final long end = System.nanoTime();

View File

@ -109,6 +109,7 @@ public class PluginInfo implements Serializable {
} else if (pom != null && !pom.isEmpty()) {
pom = pom.replace("[name]", name).replace("[branch]", branch);
sender.sendMessage("§6版本: §b尝试从在线POM文件获取最新版本...");
System.out.println(pom);
ver = IOUtil.getXMLTag(pom, "version", null);
if (ver != null) {
sender.sendMessage("§6版本: §a成功获取到最新版本 §e" + ver + " §a...");

View File

@ -1,3 +1,7 @@
############################
### Yum插件主配置文件
############################
#配置版本号 请勿修改!!!
Version: 2.2

View File

@ -0,0 +1,9 @@
############################
### 能耗监控系统配置文件
############################
#配置版本号 请勿修改!!!
Version: 1.0
#是否开启
Enable: true

View File

@ -1,3 +1,7 @@
############################
### 网络管理系统配置文件
############################
#配置版本号 请勿修改!!!
Version: 1.2

View File

@ -1,3 +1,7 @@
############################
### 安全管理系统配置文件
############################
#配置版本号 请勿修改!!!
Version: 1.0

View File

@ -1,3 +1,7 @@
############################
### 线程管理系统配置文件
############################
#配置版本号 请勿修改!!!
Version: 1.0