aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src/main/java/me/lucko/spark
diff options
context:
space:
mode:
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark')
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/monitor/cpu/CpuInfo.java69
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/platform/PlatformStatisticsProvider.java2
2 files changed, 71 insertions, 0 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/monitor/cpu/CpuInfo.java b/spark-common/src/main/java/me/lucko/spark/common/monitor/cpu/CpuInfo.java
new file mode 100644
index 0000000..a179904
--- /dev/null
+++ b/spark-common/src/main/java/me/lucko/spark/common/monitor/cpu/CpuInfo.java
@@ -0,0 +1,69 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package me.lucko.spark.common.monitor.cpu;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Pattern;
+
+/**
+ * Small utility to query the CPU model on Linux systems.
+ */
+public enum CpuInfo {
+ ;
+
+ private static final Pattern SPACE_COLON_SPACE_PATTERN = Pattern.compile("\\s+:\\s");
+
+ /**
+ * Queries the CPU model.
+ *
+ * @return the cpu model
+ */
+ public static String queryCpuModel() {
+ List<String> cpuInfo = readFile("/proc/cpuinfo");
+ for (String line : cpuInfo) {
+ String[] splitLine = SPACE_COLON_SPACE_PATTERN.split(line);
+
+ if (splitLine[0].equals("model name") || splitLine[0].equals("Processor")) {
+ return splitLine[1];
+ }
+ }
+ return "";
+ }
+
+ private static List<String> readFile(String file) {
+ Path path = Paths.get(file);
+ if (Files.isReadable(path)) {
+ try {
+ return Files.readAllLines(path, StandardCharsets.UTF_8);
+ } catch (IOException e) {
+ // ignore
+ }
+ }
+ return new ArrayList<>();
+ }
+
+}
diff --git a/spark-common/src/main/java/me/lucko/spark/common/platform/PlatformStatisticsProvider.java b/spark-common/src/main/java/me/lucko/spark/common/platform/PlatformStatisticsProvider.java
index a16c643..fce45ec 100644
--- a/spark-common/src/main/java/me/lucko/spark/common/platform/PlatformStatisticsProvider.java
+++ b/spark-common/src/main/java/me/lucko/spark/common/platform/PlatformStatisticsProvider.java
@@ -21,6 +21,7 @@
package me.lucko.spark.common.platform;
import me.lucko.spark.common.SparkPlatform;
+import me.lucko.spark.common.monitor.cpu.CpuInfo;
import me.lucko.spark.common.monitor.cpu.CpuMonitor;
import me.lucko.spark.common.monitor.disk.DiskUsage;
import me.lucko.spark.common.monitor.memory.GarbageCollectorStatistics;
@@ -58,6 +59,7 @@ public class PlatformStatisticsProvider {
.setLast15M(CpuMonitor.systemLoad15MinAvg())
.build()
)
+ .setModelName(CpuInfo.queryCpuModel())
.build()
)
.setMemory(SystemStatistics.Memory.newBuilder()