Search in sources :

Example 1 with CommandContext

use of com.sk89q.minecraft.util.commands.CommandContext in project WorldGuard by EngineHub.

the class WorldGuardCommands method profile.

@Command(aliases = { "profile" }, usage = "[-p] [-i <interval>] [-t <thread filter>] [<minutes>]", desc = "Profile the CPU usage of the server", min = 0, max = 1, flags = "t:i:p")
@CommandPermissions("worldguard.profile")
public void profile(final CommandContext args, final Actor sender) throws CommandException, AuthorizationException {
    Predicate<ThreadInfo> threadFilter;
    String threadName = args.getFlag('t');
    final boolean pastebin;
    if (args.hasFlag('p')) {
        sender.checkPermission("worldguard.report.pastebin");
        pastebin = true;
    } else {
        pastebin = false;
    }
    if (threadName == null) {
        threadFilter = new ThreadIdFilter(Thread.currentThread().getId());
    } else if (threadName.equals("*")) {
        threadFilter = thread -> true;
    } else {
        threadFilter = new ThreadNameFilter(threadName);
    }
    int minutes;
    if (args.argsLength() == 0) {
        minutes = 5;
    } else {
        minutes = args.getInteger(0);
        if (minutes < 1) {
            throw new CommandException("You must run the profile for at least 1 minute.");
        } else if (minutes > 10) {
            throw new CommandException("You can profile for, at maximum, 10 minutes.");
        }
    }
    int interval = 20;
    if (args.hasFlag('i')) {
        interval = args.getFlagInteger('i');
        if (interval < 1 || interval > 100) {
            throw new CommandException("Interval must be between 1 and 100 (in milliseconds)");
        }
        if (interval < 10) {
            sender.printDebug("Note: A low interval may cause additional slowdown during profiling.");
        }
    }
    Sampler sampler;
    synchronized (this) {
        if (activeSampler != null) {
            throw new CommandException("A profile is currently in progress! Please use /wg stopprofile to cancel the current profile.");
        }
        SamplerBuilder builder = new SamplerBuilder();
        builder.setThreadFilter(threadFilter);
        builder.setRunTime(minutes, TimeUnit.MINUTES);
        builder.setInterval(interval);
        sampler = activeSampler = builder.start();
    }
    sender.print(TextComponent.of("Starting CPU profiling. Results will be available in " + minutes + " minutes.", TextColor.LIGHT_PURPLE).append(TextComponent.newline()).append(TextComponent.of("Use ", TextColor.GRAY)).append(TextComponent.of("/wg stopprofile", TextColor.AQUA).clickEvent(ClickEvent.of(ClickEvent.Action.SUGGEST_COMMAND, "/wg stopprofile"))).append(TextComponent.of(" at any time to cancel CPU profiling.", TextColor.GRAY)));
    worldGuard.getSupervisor().monitor(FutureForwardingTask.create(sampler.getFuture(), "CPU profiling for " + minutes + " minutes", sender));
    sampler.getFuture().addListener(() -> {
        synchronized (WorldGuardCommands.this) {
            activeSampler = null;
        }
    }, MoreExecutors.directExecutor());
    Futures.addCallback(sampler.getFuture(), new FutureCallback<>() {

        @Override
        public void onSuccess(Sampler result) {
            String output = result.toString();
            try {
                File dest = new File(worldGuard.getPlatform().getConfigDir().toFile(), "profile.txt");
                Files.write(output, dest, StandardCharsets.UTF_8);
                sender.print("CPU profiling data written to " + dest.getAbsolutePath());
            } catch (IOException e) {
                sender.printError("Failed to write CPU profiling data: " + e.getMessage());
            }
            if (pastebin) {
                ActorCallbackPaste.pastebin(worldGuard.getSupervisor(), sender, output, "Profile result: %s.profile");
            }
        }

        @Override
        public void onFailure(Throwable throwable) {
        }
    }, MoreExecutors.directExecutor());
}
Also used : Sampler(com.sk89q.worldguard.util.profiler.SamplerBuilder.Sampler) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) CommandPermissions(com.sk89q.minecraft.util.commands.CommandPermissions) FutureForwardingTask(com.sk89q.worldedit.util.task.FutureForwardingTask) SystemInfoReport(com.sk89q.worldedit.util.report.SystemInfoReport) World(com.sk89q.worldedit.world.World) ActorCallbackPaste(com.sk89q.worldedit.util.paste.ActorCallbackPaste) SamplerBuilder(com.sk89q.worldguard.util.profiler.SamplerBuilder) Level(java.util.logging.Level) CommandException(com.sk89q.minecraft.util.commands.CommandException) Task(com.sk89q.worldedit.util.task.Task) ApplicableRegionsReport(com.sk89q.worldguard.util.report.ApplicableRegionsReport) ThreadInfo(java.lang.management.ThreadInfo) AuthorizationException(com.sk89q.worldedit.util.auth.AuthorizationException) Files(com.google.common.io.Files) TextColor(com.sk89q.worldedit.util.formatting.text.format.TextColor) ClickEvent(com.sk89q.worldedit.util.formatting.text.event.ClickEvent) WorldGuard(com.sk89q.worldguard.WorldGuard) NestedCommand(com.sk89q.minecraft.util.commands.NestedCommand) ConfigReport(com.sk89q.worldguard.util.report.ConfigReport) WorldEdit(com.sk89q.worldedit.WorldEdit) ReportList(com.sk89q.worldedit.util.report.ReportList) Nullable(javax.annotation.Nullable) TaskStateComparator(com.sk89q.worldedit.util.task.TaskStateComparator) ThreadNameFilter(com.sk89q.worldguard.util.profiler.ThreadNameFilter) TextComponent(com.sk89q.worldedit.util.formatting.text.TextComponent) Predicate(java.util.function.Predicate) LoggerToChatHandler(com.sk89q.worldguard.util.logging.LoggerToChatHandler) ThreadIdFilter(com.sk89q.worldguard.util.profiler.ThreadIdFilter) LocalPlayer(com.sk89q.worldguard.LocalPlayer) IOException(java.io.IOException) MessageBox(com.sk89q.worldedit.util.formatting.component.MessageBox) Logger(java.util.logging.Logger) FutureCallback(com.google.common.util.concurrent.FutureCallback) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Actor(com.sk89q.worldedit.extension.platform.Actor) TimeUnit(java.util.concurrent.TimeUnit) TextComponentProducer(com.sk89q.worldedit.util.formatting.component.TextComponentProducer) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) ConfigurationManager(com.sk89q.worldguard.config.ConfigurationManager) Capability(com.sk89q.worldedit.extension.platform.Capability) CommandContext(com.sk89q.minecraft.util.commands.CommandContext) Command(com.sk89q.minecraft.util.commands.Command) ThreadNameFilter(com.sk89q.worldguard.util.profiler.ThreadNameFilter) ThreadIdFilter(com.sk89q.worldguard.util.profiler.ThreadIdFilter) CommandException(com.sk89q.minecraft.util.commands.CommandException) IOException(java.io.IOException) SamplerBuilder(com.sk89q.worldguard.util.profiler.SamplerBuilder) ThreadInfo(java.lang.management.ThreadInfo) Sampler(com.sk89q.worldguard.util.profiler.SamplerBuilder.Sampler) File(java.io.File) NestedCommand(com.sk89q.minecraft.util.commands.NestedCommand) Command(com.sk89q.minecraft.util.commands.Command) CommandPermissions(com.sk89q.minecraft.util.commands.CommandPermissions)

Aggregations

Files (com.google.common.io.Files)1 FutureCallback (com.google.common.util.concurrent.FutureCallback)1 Futures (com.google.common.util.concurrent.Futures)1 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)1 Command (com.sk89q.minecraft.util.commands.Command)1 CommandContext (com.sk89q.minecraft.util.commands.CommandContext)1 CommandException (com.sk89q.minecraft.util.commands.CommandException)1 CommandPermissions (com.sk89q.minecraft.util.commands.CommandPermissions)1 NestedCommand (com.sk89q.minecraft.util.commands.NestedCommand)1 WorldEdit (com.sk89q.worldedit.WorldEdit)1 Actor (com.sk89q.worldedit.extension.platform.Actor)1 Capability (com.sk89q.worldedit.extension.platform.Capability)1 AuthorizationException (com.sk89q.worldedit.util.auth.AuthorizationException)1 MessageBox (com.sk89q.worldedit.util.formatting.component.MessageBox)1 TextComponentProducer (com.sk89q.worldedit.util.formatting.component.TextComponentProducer)1 TextComponent (com.sk89q.worldedit.util.formatting.text.TextComponent)1 ClickEvent (com.sk89q.worldedit.util.formatting.text.event.ClickEvent)1 TextColor (com.sk89q.worldedit.util.formatting.text.format.TextColor)1 ActorCallbackPaste (com.sk89q.worldedit.util.paste.ActorCallbackPaste)1 ReportList (com.sk89q.worldedit.util.report.ReportList)1