| 1 | package net.minecraft;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.io.FileNotFoundException;
|
| 5 | import java.lang.management.ManagementFactory;
|
| 6 | import java.lang.management.MemoryUsage;
|
| 7 | import java.nio.file.FileStore;
|
| 8 | import java.nio.file.Files;
|
| 9 | import java.nio.file.InvalidPathException;
|
| 10 | import java.nio.file.NoSuchFileException;
|
| 11 | import java.nio.file.Path;
|
| 12 | import java.util.ArrayList;
|
| 13 | import java.util.List;
|
| 14 | import java.util.Locale;
|
| 15 | import java.util.function.Predicate;
|
| 16 | import java.util.function.Supplier;
|
| 17 | import java.util.stream.Collectors;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 | import org.slf4j.Logger;
|
| 20 | import oshi.SystemInfo;
|
| 21 | import oshi.hardware.CentralProcessor;
|
| 22 | import oshi.hardware.GlobalMemory;
|
| 23 | import oshi.hardware.GraphicsCard;
|
| 24 | import oshi.hardware.HardwareAbstractionLayer;
|
| 25 | import oshi.hardware.PhysicalMemory;
|
| 26 | import oshi.hardware.VirtualMemory;
|
| 27 | import oshi.hardware.CentralProcessor.ProcessorIdentifier;
|
| 28 | import oshi.software.os.OSProcess;
|
| 29 | import oshi.software.os.OperatingSystem;
|
| 30 |
|
| 31 | public class SystemReport {
|
| 32 | public static final long BYTES_PER_MEBIBYTE = 1048576L;
|
| 33 | private static final long ONE_GIGA = 1000000000L;
|
| 34 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 35 | private static final String OPERATING_SYSTEM = System.getProperty("os.name")
|
| 36 | + " ("
|
| 37 | + System.getProperty("os.arch")
|
| 38 | + ") version "
|
| 39 | + System.getProperty("os.version");
|
| 40 | private static final String JAVA_VERSION = System.getProperty("java.version") + ", " + System.getProperty("java.vendor");
|
| 41 | private static final String JAVA_VM_VERSION = System.getProperty("java.vm.name")
|
| 42 | + " ("
|
| 43 | + System.getProperty("java.vm.info")
|
| 44 | + "), "
|
| 45 | + System.getProperty("java.vm.vendor");
|
| 46 | private final List<CrashReportCategory.Entry> entries = new ArrayList<>();
|
| 47 |
|
| 48 | public SystemReport() {
|
| 49 | this.setDetail("Minecraft Version", () -> SharedConstants.getCurrentVersion().name());
|
| 50 | this.setDetail("Minecraft Version ID", () -> SharedConstants.getCurrentVersion().id());
|
| 51 | this.setDetail("Operating System", OPERATING_SYSTEM);
|
| 52 | this.setDetail("Java Version", JAVA_VERSION);
|
| 53 | this.setDetail("Java VM Version", JAVA_VM_VERSION);
|
| 54 | this.setDetail("Memory", () -> {
|
| 55 | Runtime runtime = Runtime.getRuntime();
|
| 56 | long max = runtime.maxMemory();
|
| 57 | long total = runtime.totalMemory();
|
| 58 | long free = runtime.freeMemory();
|
| 59 | long maxMb = max / 1048576L;
|
| 60 | long totalMb = total / 1048576L;
|
| 61 | long freeMb = free / 1048576L;
|
| 62 | return free + " bytes (" + freeMb + " MiB) / " + total + " bytes (" + totalMb + " MiB) up to " + max + " bytes (" + maxMb + " MiB)";
|
| 63 | });
|
| 64 | this.setDetail("Memory (heap)", () -> printMemoryUsage(ManagementFactory.getMemoryMXBean().getHeapMemoryUsage()));
|
| 65 | this.setDetail("Memory (non-heap)", () -> printMemoryUsage(ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage()));
|
| 66 | this.setDetail("CPUs", () -> String.valueOf(Runtime.getRuntime().availableProcessors()));
|
| 67 | this.ignoreErrors("hardware", () -> this.putHardware(new SystemInfo()));
|
| 68 | this.ignoreErrors("software", () -> this.putSoftware(new SystemInfo()));
|
| 69 | this.setDetail("JVM Flags", () -> printJvmFlags(arg -> arg.startsWith("-X")));
|
| 70 | this.setDetail("Debug Flags", () -> printJvmFlags(arg -> arg.startsWith("-DMC_DEBUG_")));
|
| 71 | }
|
| 72 |
|
| 73 | private static String printMemoryUsage(final MemoryUsage memoryUsage) {
|
| 74 | return String.format(
|
| 75 | Locale.ROOT,
|
| 76 | "init: %03dMiB, used: %03dMiB, committed: %03dMiB, max: %03dMiB",
|
| 77 | memoryUsage.getInit() / 1048576L,
|
| 78 | memoryUsage.getUsed() / 1048576L,
|
| 79 | memoryUsage.getCommitted() / 1048576L,
|
| 80 | memoryUsage.getMax() / 1048576L
|
| 81 | );
|
| 82 | }
|
| 83 |
|
| 84 | private static String printJvmFlags(final Predicate<String> selector) {
|
| 85 | List<String> allArguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
|
| 86 | List<String> selectedArguments = allArguments.stream().filter(selector).toList();
|
| 87 | return String.format(Locale.ROOT, "%d total; %s", selectedArguments.size(), String.join(" ", selectedArguments));
|
| 88 | }
|
| 89 |
|
| 90 | public void setDetail(final String key, final String value) {
|
| 91 | this.entries.add(new CrashReportCategory.Entry(key, value));
|
| 92 | }
|
| 93 |
|
| 94 | public void setDetail(final String key, final CrashReportDetail<Object> valueSupplier) {
|
| 95 | try {
|
| 96 | this.entries.add(new CrashReportCategory.Entry(key, valueSupplier.call()));
|
| 97 | } catch (Throwable t) {
|
| 98 | LOGGER.warn("Failed to get system info for {}", key, t);
|
| 99 | this.entries.add(new CrashReportCategory.Entry(key, t));
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | private void putHardware(final SystemInfo systemInfo) {
|
| 104 | HardwareAbstractionLayer hardware = systemInfo.getHardware();
|
| 105 | this.ignoreErrors("processor", () -> this.putProcessor(hardware.getProcessor()));
|
| 106 | this.ignoreErrors("graphics", () -> this.putGraphics(hardware.getGraphicsCards()));
|
| 107 | this.ignoreErrors("memory", () -> this.putMemory(hardware.getMemory()));
|
| 108 | this.ignoreErrors("storage", this::putStorage);
|
| 109 | }
|
| 110 |
|
| 111 | private void putSoftware(final SystemInfo systemInfo) {
|
| 112 | OperatingSystem os = systemInfo.getOperatingSystem();
|
| 113 | this.setDetail("Operating System Version", os::toString);
|
| 114 | this.setDetail("Process Elevated", os::isElevated);
|
| 115 | this.ignoreErrors("process", () -> this.putProcessDetails(os.getCurrentProcess()));
|
| 116 | }
|
| 117 |
|
| 118 | private void ignoreErrors(final String group, final Runnable action) {
|
| 119 | try {
|
| 120 | action.run();
|
| 121 | } catch (Throwable t) {
|
| 122 | LOGGER.warn("Failed retrieving info for group {}", group, t);
|
| 123 | this.entries.add(new CrashReportCategory.Entry(group, t));
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | public static float sizeInMiB(final long bytes) {
|
| 128 | return (float)bytes / 1048576.0F;
|
| 129 | }
|
| 130 |
|
| 131 | private void putPhysicalMemory(final List<PhysicalMemory> memoryPackages) {
|
| 132 | int memorySlot = 0;
|
| 133 |
|
| 134 | for (PhysicalMemory physicalMemory : memoryPackages) {
|
| 135 | String prefix = String.format(Locale.ROOT, "Memory slot #%d ", memorySlot++);
|
| 136 | this.setDetail(prefix + "capacity (MiB)", () -> String.format(Locale.ROOT, "%.2f", sizeInMiB(physicalMemory.getCapacity())));
|
| 137 | this.setDetail(prefix + "clockSpeed (GHz)", () -> String.format(Locale.ROOT, "%.2f", (float)physicalMemory.getClockSpeed() / 1.0E9F));
|
| 138 | this.setDetail(prefix + "type", physicalMemory::getMemoryType);
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | private void putVirtualMemory(final VirtualMemory virtualMemory) {
|
| 143 | this.setDetail("Virtual memory max (MiB)", () -> String.format(Locale.ROOT, "%.2f", sizeInMiB(virtualMemory.getVirtualMax())));
|
| 144 | this.setDetail("Virtual memory used (MiB)", () -> String.format(Locale.ROOT, "%.2f", sizeInMiB(virtualMemory.getVirtualInUse())));
|
| 145 | this.setDetail("Swap memory total (MiB)", () -> String.format(Locale.ROOT, "%.2f", sizeInMiB(virtualMemory.getSwapTotal())));
|
| 146 | this.setDetail("Swap memory used (MiB)", () -> String.format(Locale.ROOT, "%.2f", sizeInMiB(virtualMemory.getSwapUsed())));
|
| 147 | }
|
| 148 |
|
| 149 | private void putMemory(final GlobalMemory memory) {
|
| 150 | this.ignoreErrors("physical memory", () -> this.putPhysicalMemory(memory.getPhysicalMemory()));
|
| 151 | this.ignoreErrors("virtual memory", () -> this.putVirtualMemory(memory.getVirtualMemory()));
|
| 152 | }
|
| 153 |
|
| 154 | private void putGraphics(final List<GraphicsCard> graphicsCards) {
|
| 155 | int gpuIndex = 0;
|
| 156 |
|
| 157 | for (GraphicsCard graphicsCard : graphicsCards) {
|
| 158 | String prefix = String.format(Locale.ROOT, "Graphics card #%d ", gpuIndex++);
|
| 159 | this.setDetail(prefix + "name", graphicsCard::getName);
|
| 160 | this.setDetail(prefix + "vendor", graphicsCard::getVendor);
|
| 161 | this.setDetail(prefix + "VRAM (MiB)", () -> String.format(Locale.ROOT, "%.2f", sizeInMiB(graphicsCard.getVRam())));
|
| 162 | this.setDetail(prefix + "deviceId", graphicsCard::getDeviceId);
|
| 163 | this.setDetail(prefix + "versionInfo", graphicsCard::getVersionInfo);
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | private void putProcessor(final CentralProcessor processor) {
|
| 168 | ProcessorIdentifier processorIdentifier = processor.getProcessorIdentifier();
|
| 169 | this.setDetail("Processor Vendor", processorIdentifier::getVendor);
|
| 170 | this.setDetail("Processor Name", processorIdentifier::getName);
|
| 171 | this.setDetail("Identifier", processorIdentifier::getIdentifier);
|
| 172 | this.setDetail("Microarchitecture", processorIdentifier::getMicroarchitecture);
|
| 173 | this.setDetail("Frequency (GHz)", () -> String.format(Locale.ROOT, "%.2f", (float)processorIdentifier.getVendorFreq() / 1.0E9F));
|
| 174 | this.setDetail("Number of physical packages", () -> String.valueOf(processor.getPhysicalPackageCount()));
|
| 175 | this.setDetail("Number of physical CPUs", () -> String.valueOf(processor.getPhysicalProcessorCount()));
|
| 176 | this.setDetail("Number of logical CPUs", () -> String.valueOf(processor.getLogicalProcessorCount()));
|
| 177 | }
|
| 178 |
|
| 179 | private void putStorage() {
|
| 180 | this.putSpaceForProperty("jna.tmpdir");
|
| 181 | this.putSpaceForProperty("org.lwjgl.system.SharedLibraryExtractPath");
|
| 182 | this.putSpaceForProperty("io.netty.native.workdir");
|
| 183 | this.putSpaceForProperty("java.io.tmpdir");
|
| 184 | this.putSpaceForPath("workdir", () -> "");
|
| 185 | }
|
| 186 |
|
| 187 | private void putProcessDetails(final OSProcess process) {
|
| 188 | this.setDetail(
|
| 189 | "Process Loads",
|
| 190 | () -> {
|
| 191 | double userTimeSeconds = process.getUserTime() / 1000.0;
|
| 192 | double kernelTimeSeconds = process.getKernelTime() / 1000.0;
|
| 193 | double upTimeSeconds = process.getUpTime() / 1000.0;
|
| 194 | double totalTimeSeconds = kernelTimeSeconds + userTimeSeconds;
|
| 195 | return String.format(
|
| 196 | Locale.ROOT,
|
| 197 | "Uptime: %.0fs, user: %.0fs (%.2f%%), kernel: %.0fs (%.2f%%), total: %.0fs (%.2f%%)",
|
| 198 | upTimeSeconds,
|
| 199 | userTimeSeconds,
|
| 200 | userTimeSeconds / upTimeSeconds * 100.0,
|
| 201 | kernelTimeSeconds,
|
| 202 | kernelTimeSeconds / upTimeSeconds * 100.0,
|
| 203 | totalTimeSeconds,
|
| 204 | totalTimeSeconds / upTimeSeconds * 100.0
|
| 205 | );
|
| 206 | }
|
| 207 | );
|
| 208 | this.setDetail("Process Virtual Size (MiB)", () -> String.format(Locale.ROOT, "%.2f", sizeInMiB(process.getVirtualSize())));
|
| 209 | this.setDetail("Process Resident Size (MiB)", () -> String.format(Locale.ROOT, "%.2f", sizeInMiB(process.getResidentSetSize())));
|
| 210 | }
|
| 211 |
|
| 212 | private void putSpaceForProperty(final String env) {
|
| 213 | this.putSpaceForPath(env, () -> System.getProperty(env));
|
| 214 | }
|
| 215 |
|
| 216 | private void putSpaceForPath(final String id, final Supplier<@Nullable String> pathSupplier) {
|
| 217 | String key = "Space in storage for " + id + " (MiB)";
|
| 218 |
|
| 219 | try {
|
| 220 | String path = pathSupplier.get();
|
| 221 | if (path == null) {
|
| 222 | this.setDetail(key, "<path not set>");
|
| 223 | return;
|
| 224 | }
|
| 225 |
|
| 226 | FileStore store = Files.getFileStore(Path.of(path));
|
| 227 | this.setDetail(key, () -> String.format(Locale.ROOT, "available: %.2f, total: %.2f", sizeInMiB(store.getUsableSpace()), sizeInMiB(store.getTotalSpace())));
|
| 228 | } catch (InvalidPathException e) {
|
| 229 | LOGGER.warn("{} is not a path", id, e);
|
| 230 | this.setDetail(key, "<invalid path>");
|
| 231 | } catch (NoSuchFileException | FileNotFoundException e) {
|
| 232 | this.setDetail(key, "<no such file>");
|
| 233 | } catch (Exception e) {
|
| 234 | LOGGER.warn("Failed retrieving storage space for {}", id, e);
|
| 235 | this.setDetail(key, "ERR");
|
| 236 | }
|
| 237 | }
|
| 238 |
|
| 239 | public void appendToCrashReportString(final StringBuilder sb) {
|
| 240 | sb.append("-- ").append("System Details").append(" --\n");
|
| 241 | sb.append("Details:");
|
| 242 | this.entries.forEach(entry -> {
|
| 243 | sb.append("\n\t");
|
| 244 | sb.append(entry.key());
|
| 245 | sb.append(": ");
|
| 246 | sb.append(entry.value());
|
| 247 | });
|
| 248 | }
|
| 249 |
|
| 250 | public String toLineSeparatedString() {
|
| 251 | return this.entries.stream().map(e -> e.key() + ": " + e.value()).collect(Collectors.joining(System.lineSeparator()));
|
| 252 | }
|
| 253 | }
|
| 254 |
|