Skip to content

Commit

Permalink
Print CPU name in dump if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Camotoy committed May 30, 2022
1 parent d74b0e2 commit c83eb7f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/src/main/java/org/geysermc/geyser/dump/DumpInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
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;
Expand All @@ -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;
Expand All @@ -80,6 +82,7 @@ public DumpInfo(boolean addLog) {
this.versionInfo = new VersionInfo();

this.cpuCount = Runtime.getRuntime().availableProcessors();
this.cpuName = CpuUtils.tryGetProcessorName();
this.systemLocale = Locale.getDefault();
this.systemEncoding = System.getProperty("file.encoding");

Expand Down
94 changes: 94 additions & 0 deletions core/src/main/java/org/geysermc/geyser/util/CpuUtils.java
Original file line number Diff line number Diff line change
@@ -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() {
}
}

0 comments on commit c83eb7f

Please sign in to comment.