| 1 | package net.minecraft.server.dedicated;
|
| 2 |
|
| 3 | import com.google.common.collect.Streams;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import java.lang.management.ThreadInfo;
|
| 6 | import java.nio.file.Path;
|
| 7 | import java.util.Arrays;
|
| 8 | import java.util.Comparator;
|
| 9 | import java.util.Locale;
|
| 10 | import java.util.Timer;
|
| 11 | import java.util.TimerTask;
|
| 12 | import java.util.stream.Collectors;
|
| 13 | import net.minecraft.CrashReport;
|
| 14 | import net.minecraft.CrashReportCategory;
|
| 15 | import net.minecraft.ReportType;
|
| 16 | import net.minecraft.server.Bootstrap;
|
| 17 | import net.minecraft.util.TimeUtil;
|
| 18 | import net.minecraft.util.Util;
|
| 19 | import net.minecraft.world.level.gamerules.GameRules;
|
| 20 | import org.slf4j.Logger;
|
| 21 |
|
| 22 | public class ServerWatchdog implements Runnable {
|
| 23 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 24 | private static final long MAX_SHUTDOWN_TIME = 10000L;
|
| 25 | private static final int SHUTDOWN_STATUS = 1;
|
| 26 | private static final Comparator<ThreadInfo> THREAD_INFO_COMPARATOR = Comparator.comparing(ThreadInfo::isDaemon)
|
| 27 | .thenComparing(ThreadInfo::getThreadState)
|
| 28 | .thenComparing(ThreadInfo::getThreadName);
|
| 29 | private final DedicatedServer server;
|
| 30 | private final long maxTickTimeNanos;
|
| 31 |
|
| 32 | public ServerWatchdog(final DedicatedServer server) {
|
| 33 | this.server = server;
|
| 34 | this.maxTickTimeNanos = server.getMaxTickLength() * TimeUtil.NANOSECONDS_PER_MILLISECOND;
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public void run() {
|
| 39 | while (this.server.isRunning()) {
|
| 40 | long nextTickTimeNanos = this.server.getNextTickTime();
|
| 41 | long currentTimeNanos = Util.getNanos();
|
| 42 | long deltaNanos = currentTimeNanos - nextTickTimeNanos;
|
| 43 | if (deltaNanos > this.maxTickTimeNanos) {
|
| 44 | LOGGER.error(
|
| 45 | LogUtils.FATAL_MARKER,
|
| 46 | "A single server tick took {} seconds (should be max {})",
|
| 47 | String.format(Locale.ROOT, "%.2f", (float)deltaNanos / (float)TimeUtil.NANOSECONDS_PER_SECOND),
|
| 48 | String.format(Locale.ROOT, "%.2f", this.server.tickRateManager().millisecondsPerTick() / (float)TimeUtil.MILLISECONDS_PER_SECOND)
|
| 49 | );
|
| 50 | LOGGER.error(LogUtils.FATAL_MARKER, "Considering it to be crashed, server will forcibly shutdown.");
|
| 51 | CrashReport report = createWatchdogCrashReport("Watching Server", this.server.getRunningThread().threadId());
|
| 52 | this.server.fillSystemReport(report.getSystemReport());
|
| 53 | CrashReportCategory serverStats = report.addCategory("Performance stats");
|
| 54 | serverStats.setDetail("Random tick rate", () -> this.server.getGameRules().getAsString(GameRules.RANDOM_TICK_SPEED));
|
| 55 | serverStats.setDetail(
|
| 56 | "Level stats",
|
| 57 | () -> Streams.stream(this.server.getAllLevels())
|
| 58 | .map(level -> level.dimension().identifier() + ": " + level.getWatchdogStats())
|
| 59 | .collect(Collectors.joining(",\n"))
|
| 60 | );
|
| 61 | Bootstrap.realStdoutPrintln("Crash report:\n" + report.getFriendlyReport(ReportType.CRASH));
|
| 62 | Path file = this.server.getServerDirectory().resolve("crash-reports").resolve("crash-" + Util.getFilenameFormattedDateTime() + "-server.txt");
|
| 63 | if (report.saveToFile(file, ReportType.CRASH)) {
|
| 64 | LOGGER.error("This crash report has been saved to: {}", file.toAbsolutePath());
|
| 65 | } else {
|
| 66 | LOGGER.error("We were unable to save this crash report to disk.");
|
| 67 | }
|
| 68 |
|
| 69 | this.exit();
|
| 70 | }
|
| 71 |
|
| 72 | try {
|
| 73 | Thread.sleep((nextTickTimeNanos + this.maxTickTimeNanos - currentTimeNanos) / TimeUtil.NANOSECONDS_PER_MILLISECOND);
|
| 74 | } catch (InterruptedException var10) {
|
| 75 | }
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | public static CrashReport createWatchdogCrashReport(final String message, final long mainThreadId) {
|
| 80 | ThreadInfo[] threadInfos = Util.dumpThreadInfo();
|
| 81 | Arrays.sort(threadInfos, THREAD_INFO_COMPARATOR);
|
| 82 | StringBuilder builder = new StringBuilder();
|
| 83 | Error exception = new Error("Watchdog (" + message + ")");
|
| 84 |
|
| 85 | for (ThreadInfo threadInfo : threadInfos) {
|
| 86 | if (threadInfo.getThreadId() == mainThreadId) {
|
| 87 | exception.setStackTrace(threadInfo.getStackTrace());
|
| 88 | }
|
| 89 |
|
| 90 | builder.append("\n");
|
| 91 | builder.append(threadInfo);
|
| 92 | }
|
| 93 |
|
| 94 | CrashReport report = new CrashReport(message, exception);
|
| 95 | CrashReportCategory threadDump = report.addCategory("Thread Dump");
|
| 96 | threadDump.setDetail("Threads", builder);
|
| 97 | return report;
|
| 98 | }
|
| 99 |
|
| 100 | private void exit() {
|
| 101 | try {
|
| 102 | Timer timer = new Timer();
|
| 103 | timer.schedule(new TimerTask() {
|
| 104 | @Override
|
| 105 | public void run() {
|
| 106 | Runtime.getRuntime().halt(1);
|
| 107 | }
|
| 108 | }, 10000L);
|
| 109 | System.exit(1);
|
| 110 | } catch (Throwable ignored) {
|
| 111 | Runtime.getRuntime().halt(1);
|
| 112 | }
|
| 113 | }
|
| 114 | }
|
| 115 |
|