| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.mojang.brigadier.CommandDispatcher;
|
| 4 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 5 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 6 | import com.mojang.logging.LogUtils;
|
| 7 | import java.io.IOException;
|
| 8 | import java.nio.file.Path;
|
| 9 | import java.nio.file.Paths;
|
| 10 | import java.util.Locale;
|
| 11 | import java.util.function.Consumer;
|
| 12 | import net.minecraft.SharedConstants;
|
| 13 | import net.minecraft.SystemReport;
|
| 14 | import net.minecraft.commands.CommandSourceStack;
|
| 15 | import net.minecraft.commands.Commands;
|
| 16 | import net.minecraft.network.chat.Component;
|
| 17 | import net.minecraft.server.MinecraftServer;
|
| 18 | import net.minecraft.util.FileUtil;
|
| 19 | import net.minecraft.util.FileZipper;
|
| 20 | import net.minecraft.util.TimeUtil;
|
| 21 | import net.minecraft.util.Util;
|
| 22 | import net.minecraft.util.profiling.EmptyProfileResults;
|
| 23 | import net.minecraft.util.profiling.ProfileResults;
|
| 24 | import net.minecraft.util.profiling.metrics.storage.MetricsPersister;
|
| 25 | import org.apache.commons.io.FileUtils;
|
| 26 | import org.slf4j.Logger;
|
| 27 |
|
| 28 | public class PerfCommand {
|
| 29 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 30 | private static final SimpleCommandExceptionType ERROR_NOT_RUNNING = new SimpleCommandExceptionType(Component.translatable("commands.perf.notRunning"));
|
| 31 | private static final SimpleCommandExceptionType ERROR_ALREADY_RUNNING = new SimpleCommandExceptionType(Component.translatable("commands.perf.alreadyRunning"));
|
| 32 |
|
| 33 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 34 | dispatcher.register(
|
| 35 | Commands.literal("perf")
|
| 36 | .requires(Commands.hasPermission(Commands.LEVEL_OWNERS))
|
| 37 | .then(Commands.literal("start").executes(c -> startProfilingDedicatedServer(c.getSource())))
|
| 38 | .then(Commands.literal("stop").executes(c -> stopProfilingDedicatedServer(c.getSource())))
|
| 39 | );
|
| 40 | }
|
| 41 |
|
| 42 | private static int startProfilingDedicatedServer(final CommandSourceStack source) throws CommandSyntaxException {
|
| 43 | MinecraftServer server = source.getServer();
|
| 44 | if (server.isRecordingMetrics()) {
|
| 45 | throw ERROR_ALREADY_RUNNING.create();
|
| 46 | }
|
| 47 |
|
| 48 | Consumer<ProfileResults> onStopped = results -> whenStopped(source, results);
|
| 49 | Consumer<Path> onReportFinished = profilingLogs -> saveResults(source, profilingLogs, server);
|
| 50 | server.startRecordingMetrics(onStopped, onReportFinished);
|
| 51 | source.sendSuccess(() -> Component.translatable("commands.perf.started"), false);
|
| 52 | return 0;
|
| 53 | }
|
| 54 |
|
| 55 | private static int stopProfilingDedicatedServer(final CommandSourceStack source) throws CommandSyntaxException {
|
| 56 | MinecraftServer server = source.getServer();
|
| 57 | if (!server.isRecordingMetrics()) {
|
| 58 | throw ERROR_NOT_RUNNING.create();
|
| 59 | }
|
| 60 |
|
| 61 | server.finishRecordingMetrics();
|
| 62 | return 0;
|
| 63 | }
|
| 64 |
|
| 65 | private static void saveResults(final CommandSourceStack source, final Path report, final MinecraftServer server) {
|
| 66 | String profilingName = String.format(
|
| 67 | Locale.ROOT, "%s-%s-%s", Util.getFilenameFormattedDateTime(), server.getWorldData().getLevelName(), SharedConstants.getCurrentVersion().id()
|
| 68 | );
|
| 69 |
|
| 70 | String zipFile;
|
| 71 | try {
|
| 72 | zipFile = FileUtil.findAvailableName(MetricsPersister.PROFILING_RESULTS_DIR, profilingName, ".zip");
|
| 73 | } catch (IOException e) {
|
| 74 | source.sendFailure(Component.translatable("commands.perf.reportFailed"));
|
| 75 | LOGGER.error("Failed to create report name", e);
|
| 76 | return;
|
| 77 | }
|
| 78 |
|
| 79 | try (FileZipper fileZipper = new FileZipper(MetricsPersister.PROFILING_RESULTS_DIR.resolve(zipFile))) {
|
| 80 | fileZipper.add(Paths.get("system.txt"), server.fillSystemReport(new SystemReport()).toLineSeparatedString());
|
| 81 | fileZipper.add(report);
|
| 82 | }
|
| 83 |
|
| 84 | try {
|
| 85 | FileUtils.forceDelete(report.toFile());
|
| 86 | } catch (IOException e) {
|
| 87 | LOGGER.warn("Failed to delete temporary profiling file {}", report, e);
|
| 88 | }
|
| 89 |
|
| 90 | source.sendSuccess(() -> Component.translatable("commands.perf.reportSaved", zipFile), false);
|
| 91 | }
|
| 92 |
|
| 93 | private static void whenStopped(final CommandSourceStack source, final ProfileResults results) {
|
| 94 | if (results != EmptyProfileResults.EMPTY) {
|
| 95 | int ticks = results.getTickDuration();
|
| 96 | double durationInSeconds = (double)results.getNanoDuration() / TimeUtil.NANOSECONDS_PER_SECOND;
|
| 97 | source.sendSuccess(
|
| 98 | () -> Component.translatable(
|
| 99 | "commands.perf.stopped", String.format(Locale.ROOT, "%.2f", durationInSeconds), ticks, String.format(Locale.ROOT, "%.2f", ticks / durationInSeconds)
|
| 100 | ),
|
| 101 | false
|
| 102 | );
|
| 103 | }
|
| 104 | }
|
| 105 | }
|
| 106 |
|