diff options
author | Luck <git@lucko.me> | 2021-12-30 12:54:30 +0000 |
---|---|---|
committer | Luck <git@lucko.me> | 2021-12-30 12:54:30 +0000 |
commit | 732110084a44e5d18dee5b6744cc44c9cdd41fb9 (patch) | |
tree | 902ae5da91d13241ccef133b8c07ef1f8599450c /spark-common/src | |
parent | 2003a5c8dffa52d1bd8b923a0f899141ae816fbd (diff) | |
download | spark-732110084a44e5d18dee5b6744cc44c9cdd41fb9.tar.gz spark-732110084a44e5d18dee5b6744cc44c9cdd41fb9.tar.bz2 spark-732110084a44e5d18dee5b6744cc44c9cdd41fb9.zip |
Include the name of the CPU model in stats for Linux systems
Diffstat (limited to 'spark-common/src')
3 files changed, 72 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() diff --git a/spark-common/src/main/proto/spark/spark.proto b/spark-common/src/main/proto/spark/spark.proto index 678df3a..520f19e 100644 --- a/spark-common/src/main/proto/spark/spark.proto +++ b/spark-common/src/main/proto/spark/spark.proto @@ -36,6 +36,7 @@ message SystemStatistics { int32 threads = 1; Usage process_usage = 2; Usage system_usage = 3; + string modelName = 4; // optional message Usage { double last1m = 1; |