Print CPU name in dump if possible

This commit is contained in:
Camotoy 2022-05-30 14:51:03 -04:00
parent d74b0e236d
commit c83eb7f142
No known key found for this signature in database
GPG Key ID: 7EEFB66FE798081F
2 changed files with 97 additions and 0 deletions

View File

@ -40,6 +40,7 @@ import org.geysermc.geyser.text.AsteriskSerializer;
import org.geysermc.geyser.configuration.GeyserConfiguration;
import org.geysermc.geyser.network.MinecraftProtocol;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.util.CpuUtils;
import org.geysermc.geyser.util.FileUtils;
import org.geysermc.geyser.util.WebUtils;
import org.geysermc.floodgate.util.DeviceOs;
@ -64,6 +65,7 @@ public class DumpInfo {
private final DumpInfo.VersionInfo versionInfo;
private final int cpuCount;
private final String cpuName;
private final Locale systemLocale;
private final String systemEncoding;
private Properties gitInfo;
@ -80,6 +82,7 @@ public class DumpInfo {
this.versionInfo = new VersionInfo();
this.cpuCount = Runtime.getRuntime().availableProcessors();
this.cpuName = CpuUtils.tryGetProcessorName();
this.systemLocale = Locale.getDefault();
this.systemEncoding = System.getProperty("file.encoding");

View File

@ -0,0 +1,94 @@
/*
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.util;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.regex.Pattern;
public final class CpuUtils {
public static String tryGetProcessorName() {
try {
if (new File("/proc/cpuinfo").canRead()) {
return getLinuxProcessorName();
} else {
return getWindowsProcessorName();
}
} catch (Exception e) {
return e.getMessage();
}
}
/**
* Much of the code here was copied from the OSHI project. This is simply stripped down to only get the CPU model.
* https://github.com/oshi/oshi/
*/
private static String getLinuxProcessorName() throws Exception {
List<String> lines = Files.readAllLines(Paths.get("/proc/cpuinfo"), StandardCharsets.UTF_8);
Pattern whitespaceColonWhitespace = Pattern.compile("\\s+:\\s"); // From ParseUtil
for (String line : lines) {
String[] splitLine = whitespaceColonWhitespace.split(line);
if ("model name".equals(splitLine[0]) || "Processor".equals(splitLine[0])) {
return splitLine[1];
}
}
return "unknown";
}
/**
* https://stackoverflow.com/a/6327663
*/
private static String getWindowsProcessorName() throws Exception {
final String cpuNameCmd = "reg query \"HKLM\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\" /v ProcessorNameString";
final String regstrToken = "REG_SZ";
Process process = Runtime.getRuntime().exec(cpuNameCmd);
process.waitFor();
InputStream is = process.getInputStream();
StringBuilder sb = new StringBuilder();
while (is.available() != 0) {
sb.append((char) is.read());
}
String result = sb.toString();
int p = result.indexOf(regstrToken);
if (p == -1) {
return null;
}
return result.substring(p + regstrToken.length()).trim();
}
private CpuUtils() {
}
}