File size: 9,293 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package net.minecraft;

import com.google.common.collect.Maps;
import com.mojang.logging.LogUtils;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.hardware.GraphicsCard;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.hardware.PhysicalMemory;
import oshi.hardware.VirtualMemory;
import oshi.hardware.CentralProcessor.ProcessorIdentifier;

public class SystemReport {
    public static final long BYTES_PER_MEBIBYTE = 1048576L;
    private static final long ONE_GIGA = 1000000000L;
    private static final Logger LOGGER = LogUtils.getLogger();
    private static final String OPERATING_SYSTEM = System.getProperty("os.name")
        + " ("
        + System.getProperty("os.arch")
        + ") version "
        + System.getProperty("os.version");
    private static final String JAVA_VERSION = System.getProperty("java.version") + ", " + System.getProperty("java.vendor");
    private static final String JAVA_VM_VERSION = System.getProperty("java.vm.name")
        + " ("
        + System.getProperty("java.vm.info")
        + "), "
        + System.getProperty("java.vm.vendor");
    private final Map<String, String> entries = Maps.newLinkedHashMap();

    public SystemReport() {
        this.setDetail("Minecraft Version", SharedConstants.getCurrentVersion().getName());
        this.setDetail("Minecraft Version ID", SharedConstants.getCurrentVersion().getId());
        this.setDetail("Operating System", OPERATING_SYSTEM);
        this.setDetail("Java Version", JAVA_VERSION);
        this.setDetail("Java VM Version", JAVA_VM_VERSION);
        this.setDetail("Memory", () -> {
            Runtime runtime = Runtime.getRuntime();
            long i = runtime.maxMemory();
            long j = runtime.totalMemory();
            long k = runtime.freeMemory();
            long l = i / 1048576L;
            long i1 = j / 1048576L;
            long j1 = k / 1048576L;
            return k + " bytes (" + j1 + " MiB) / " + j + " bytes (" + i1 + " MiB) up to " + i + " bytes (" + l + " MiB)";
        });
        this.setDetail("CPUs", () -> String.valueOf(Runtime.getRuntime().availableProcessors()));
        this.ignoreErrors("hardware", () -> this.putHardware(new SystemInfo()));
        this.setDetail("JVM Flags", () -> {
            List<String> list = Util.getVmArguments().collect(Collectors.toList());
            return String.format(Locale.ROOT, "%d total; %s", list.size(), String.join(" ", list));
        });
    }

    public void setDetail(String p_143520_, String p_143521_) {
        this.entries.put(p_143520_, p_143521_);
    }

    public void setDetail(String p_143523_, Supplier<String> p_143524_) {
        try {
            this.setDetail(p_143523_, p_143524_.get());
        } catch (Exception exception) {
            LOGGER.warn("Failed to get system info for {}", p_143523_, exception);
            this.setDetail(p_143523_, "ERR");
        }
    }

    private void putHardware(SystemInfo p_143536_) {
        HardwareAbstractionLayer hardwareabstractionlayer = p_143536_.getHardware();
        this.ignoreErrors("processor", () -> this.putProcessor(hardwareabstractionlayer.getProcessor()));
        this.ignoreErrors("graphics", () -> this.putGraphics(hardwareabstractionlayer.getGraphicsCards()));
        this.ignoreErrors("memory", () -> this.putMemory(hardwareabstractionlayer.getMemory()));
        this.ignoreErrors("storage", this::putStorage);
    }

    private void ignoreErrors(String p_143517_, Runnable p_143518_) {
        try {
            p_143518_.run();
        } catch (Throwable throwable) {
            LOGGER.warn("Failed retrieving info for group {}", p_143517_, throwable);
        }
    }

    public static float sizeInMiB(long p_342777_) {
        return (float)p_342777_ / 1048576.0F;
    }

    private void putPhysicalMemory(List<PhysicalMemory> p_143532_) {
        int i = 0;

        for (PhysicalMemory physicalmemory : p_143532_) {
            String s = String.format(Locale.ROOT, "Memory slot #%d ", i++);
            this.setDetail(s + "capacity (MiB)", () -> String.format(Locale.ROOT, "%.2f", sizeInMiB(physicalmemory.getCapacity())));
            this.setDetail(s + "clockSpeed (GHz)", () -> String.format(Locale.ROOT, "%.2f", (float)physicalmemory.getClockSpeed() / 1.0E9F));
            this.setDetail(s + "type", physicalmemory::getMemoryType);
        }
    }

    private void putVirtualMemory(VirtualMemory p_143550_) {
        this.setDetail("Virtual memory max (MiB)", () -> String.format(Locale.ROOT, "%.2f", sizeInMiB(p_143550_.getVirtualMax())));
        this.setDetail("Virtual memory used (MiB)", () -> String.format(Locale.ROOT, "%.2f", sizeInMiB(p_143550_.getVirtualInUse())));
        this.setDetail("Swap memory total (MiB)", () -> String.format(Locale.ROOT, "%.2f", sizeInMiB(p_143550_.getSwapTotal())));
        this.setDetail("Swap memory used (MiB)", () -> String.format(Locale.ROOT, "%.2f", sizeInMiB(p_143550_.getSwapUsed())));
    }

    private void putMemory(GlobalMemory p_143542_) {
        this.ignoreErrors("physical memory", () -> this.putPhysicalMemory(p_143542_.getPhysicalMemory()));
        this.ignoreErrors("virtual memory", () -> this.putVirtualMemory(p_143542_.getVirtualMemory()));
    }

    private void putGraphics(List<GraphicsCard> p_143553_) {
        int i = 0;

        for (GraphicsCard graphicscard : p_143553_) {
            String s = String.format(Locale.ROOT, "Graphics card #%d ", i++);
            this.setDetail(s + "name", graphicscard::getName);
            this.setDetail(s + "vendor", graphicscard::getVendor);
            this.setDetail(s + "VRAM (MiB)", () -> String.format(Locale.ROOT, "%.2f", sizeInMiB(graphicscard.getVRam())));
            this.setDetail(s + "deviceId", graphicscard::getDeviceId);
            this.setDetail(s + "versionInfo", graphicscard::getVersionInfo);
        }
    }

    private void putProcessor(CentralProcessor p_143540_) {
        ProcessorIdentifier processoridentifier = p_143540_.getProcessorIdentifier();
        this.setDetail("Processor Vendor", processoridentifier::getVendor);
        this.setDetail("Processor Name", processoridentifier::getName);
        this.setDetail("Identifier", processoridentifier::getIdentifier);
        this.setDetail("Microarchitecture", processoridentifier::getMicroarchitecture);
        this.setDetail("Frequency (GHz)", () -> String.format(Locale.ROOT, "%.2f", (float)processoridentifier.getVendorFreq() / 1.0E9F));
        this.setDetail("Number of physical packages", () -> String.valueOf(p_143540_.getPhysicalPackageCount()));
        this.setDetail("Number of physical CPUs", () -> String.valueOf(p_143540_.getPhysicalProcessorCount()));
        this.setDetail("Number of logical CPUs", () -> String.valueOf(p_143540_.getLogicalProcessorCount()));
    }

    private void putStorage() {
        this.putSpaceForProperty("jna.tmpdir");
        this.putSpaceForProperty("org.lwjgl.system.SharedLibraryExtractPath");
        this.putSpaceForProperty("io.netty.native.workdir");
        this.putSpaceForProperty("java.io.tmpdir");
        this.putSpaceForPath("workdir", () -> "");
    }

    private void putSpaceForProperty(String p_345490_) {
        this.putSpaceForPath(p_345490_, () -> System.getProperty(p_345490_));
    }

    private void putSpaceForPath(String p_344137_, Supplier<String> p_344490_) {
        String s = "Space in storage for " + p_344137_ + " (MiB)";

        try {
            String s1 = p_344490_.get();
            if (s1 == null) {
                this.setDetail(s, "<path not set>");
                return;
            }

            FileStore filestore = Files.getFileStore(Path.of(s1));
            this.setDetail(
                s, String.format(Locale.ROOT, "available: %.2f, total: %.2f", sizeInMiB(filestore.getUsableSpace()), sizeInMiB(filestore.getTotalSpace()))
            );
        } catch (InvalidPathException invalidpathexception) {
            LOGGER.warn("{} is not a path", p_344137_, invalidpathexception);
            this.setDetail(s, "<invalid path>");
        } catch (Exception exception) {
            LOGGER.warn("Failed retrieving storage space for {}", p_344137_, exception);
            this.setDetail(s, "ERR");
        }
    }

    public void appendToCrashReportString(StringBuilder p_143526_) {
        p_143526_.append("-- ").append("System Details").append(" --\n");
        p_143526_.append("Details:");
        this.entries.forEach((p_143529_, p_143530_) -> {
            p_143526_.append("\n\t");
            p_143526_.append(p_143529_);
            p_143526_.append(": ");
            p_143526_.append(p_143530_);
        });
    }

    public String toLineSeparatedString() {
        return this.entries
            .entrySet()
            .stream()
            .map(p_143534_ -> p_143534_.getKey() + ": " + p_143534_.getValue())
            .collect(Collectors.joining(System.lineSeparator()));
    }
}