From 85561633292550209a1461347f8446b54552b391 Mon Sep 17 00:00:00 2001 From: Luck Date: Tue, 23 Jun 2020 12:27:34 +0100 Subject: Add n-cpus and heap usage to platform info --- .../java/me/lucko/spark/common/PlatformInfo.java | 145 --------------------- .../java/me/lucko/spark/common/SparkPlugin.java | 1 + .../me/lucko/spark/common/heapdump/HeapDump.java | 7 +- .../spark/common/heapdump/HeapDumpSummary.java | 2 +- .../monitor/memory/GarbageCollectionMonitor.java | 9 +- .../common/platform/AbstractPlatformInfo.java | 17 +++ .../lucko/spark/common/platform/PlatformInfo.java | 126 ++++++++++++++++++ .../me/lucko/spark/common/sampler/Sampler.java | 2 +- spark-common/src/main/proto/spark/spark.proto | 9 ++ 9 files changed, 164 insertions(+), 154 deletions(-) delete mode 100644 spark-common/src/main/java/me/lucko/spark/common/PlatformInfo.java create mode 100644 spark-common/src/main/java/me/lucko/spark/common/platform/AbstractPlatformInfo.java create mode 100644 spark-common/src/main/java/me/lucko/spark/common/platform/PlatformInfo.java (limited to 'spark-common/src') diff --git a/spark-common/src/main/java/me/lucko/spark/common/PlatformInfo.java b/spark-common/src/main/java/me/lucko/spark/common/PlatformInfo.java deleted file mode 100644 index 8ff3997..0000000 --- a/spark-common/src/main/java/me/lucko/spark/common/PlatformInfo.java +++ /dev/null @@ -1,145 +0,0 @@ -/* - * This file is part of spark. - * - * Copyright (c) lucko (Luck) - * 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 . - */ - -package me.lucko.spark.common; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; -import me.lucko.spark.proto.SparkProtos.PlatformData; - -public interface PlatformInfo { - - Type getType(); - - String getName(); - - String getVersion(); - - String getMinecraftVersion(); - - default Data toData() { - return new Data(getType(), getName(), getVersion(), getMinecraftVersion()); - } - - enum Type { - SERVER(PlatformData.Type.SERVER), - CLIENT(PlatformData.Type.CLIENT), - PROXY(PlatformData.Type.PROXY); - - private final PlatformData.Type type; - - Type(PlatformData.Type type) { - this.type = type; - } - - public PlatformData.Type toProto() { - return type; - } - - public static Type fromProto(PlatformData.Type proto) { - for (Type type : values()) { - if (type.toProto() == proto) { - return type; - } - } - - return null; - } - - public String getName() { - return super.name().toLowerCase(); - } - - public static Type fromName(String name) { - return valueOf(name.toUpperCase()); - } - } - - final class Data { - private final Type type; - private final String name; - private final String version; - private final String minecraftVersion; - - public Data(Type type, String name, String version, String minecraftVersion) { - this.type = type; - this.name = name; - this.version = version; - this.minecraftVersion = minecraftVersion; - } - - public Type getType() { - return type; - } - - public String getName() { - return name; - } - - public String getVersion() { - return version; - } - - public String getMinecraftVersion() { - return minecraftVersion; - } - - // TODO: decide if necessary - public JsonObject serialize() { - JsonObject server = new JsonObject(); - server.add("type", new JsonPrimitive(this.type.toString().toLowerCase())); - server.add("name", new JsonPrimitive(this.name)); - server.add("version", new JsonPrimitive(this.version)); - if (this.minecraftVersion != null) { - server.add("minecraftVersion", new JsonPrimitive(this.minecraftVersion)); - } - return server; - } - - public PlatformData toProto() { - PlatformData.Builder proto = PlatformData.newBuilder() - .setType(this.type.toProto()) - .setName(this.name) - .setVersion(this.version); - - if (this.minecraftVersion != null) { - proto.setMinecraftVersion(this.minecraftVersion); - } - - return proto.build(); - } - - // TODO: decide if necessary - public static PlatformInfo.Data deserialize(JsonElement element) { - JsonObject serverObject = element.getAsJsonObject(); - Type type = Type.fromName(serverObject.get("type").getAsJsonPrimitive().getAsString()); - String name = serverObject.get("name").getAsJsonPrimitive().getAsString(); - String version = serverObject.get("version").getAsJsonPrimitive().getAsString(); - String minecraftVersion; - if (serverObject.has("minecraftVersion")) { - minecraftVersion = serverObject.get("minecraftVersion").getAsJsonPrimitive().getAsString(); - } else { - minecraftVersion = null; - } - return new PlatformInfo.Data(type, name, version, minecraftVersion); - } - } -} diff --git a/spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java b/spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java index 359e9a7..92f4cf7 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java +++ b/spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java @@ -21,6 +21,7 @@ package me.lucko.spark.common; import me.lucko.spark.common.command.sender.CommandSender; +import me.lucko.spark.common.platform.PlatformInfo; import me.lucko.spark.common.sampler.ThreadDumper; import me.lucko.spark.common.sampler.tick.TickHook; import me.lucko.spark.common.sampler.tick.TickReporter; diff --git a/spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDump.java b/spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDump.java index f2c1c72..975dbb3 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDump.java +++ b/spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDump.java @@ -20,14 +20,15 @@ package me.lucko.spark.common.heapdump; -import javax.management.JMX; -import javax.management.MBeanServer; -import javax.management.ObjectName; import java.io.IOException; import java.lang.management.ManagementFactory; import java.lang.reflect.Method; import java.nio.file.Path; +import javax.management.JMX; +import javax.management.MBeanServer; +import javax.management.ObjectName; + /** * Utility for creating .hprof memory heap snapshots. */ diff --git a/spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDumpSummary.java b/spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDumpSummary.java index f7a562c..61ffd71 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDumpSummary.java +++ b/spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDumpSummary.java @@ -21,7 +21,7 @@ package me.lucko.spark.common.heapdump; import me.lucko.spark.common.command.sender.CommandSender; -import me.lucko.spark.common.PlatformInfo; +import me.lucko.spark.common.platform.PlatformInfo; import me.lucko.spark.proto.SparkProtos; import me.lucko.spark.proto.SparkProtos.HeapData; import me.lucko.spark.proto.SparkProtos.HeapEntry; diff --git a/spark-common/src/main/java/me/lucko/spark/common/monitor/memory/GarbageCollectionMonitor.java b/spark-common/src/main/java/me/lucko/spark/common/monitor/memory/GarbageCollectionMonitor.java index d750b1a..f66d43b 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/monitor/memory/GarbageCollectionMonitor.java +++ b/spark-common/src/main/java/me/lucko/spark/common/monitor/memory/GarbageCollectionMonitor.java @@ -22,15 +22,16 @@ package me.lucko.spark.common.monitor.memory; import com.sun.management.GarbageCollectionNotificationInfo; +import java.lang.management.GarbageCollectorMXBean; +import java.lang.management.ManagementFactory; +import java.util.ArrayList; +import java.util.List; + import javax.management.ListenerNotFoundException; import javax.management.Notification; import javax.management.NotificationEmitter; import javax.management.NotificationListener; import javax.management.openmbean.CompositeData; -import java.lang.management.GarbageCollectorMXBean; -import java.lang.management.ManagementFactory; -import java.util.ArrayList; -import java.util.List; public class GarbageCollectionMonitor implements NotificationListener, AutoCloseable { diff --git a/spark-common/src/main/java/me/lucko/spark/common/platform/AbstractPlatformInfo.java b/spark-common/src/main/java/me/lucko/spark/common/platform/AbstractPlatformInfo.java new file mode 100644 index 0000000..645d5b2 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/platform/AbstractPlatformInfo.java @@ -0,0 +1,17 @@ +package me.lucko.spark.common.platform; + +import java.lang.management.ManagementFactory; +import java.lang.management.MemoryUsage; + +public abstract class AbstractPlatformInfo implements PlatformInfo { + + @Override + public int getNCpus() { + return Runtime.getRuntime().availableProcessors(); + } + + @Override + public MemoryUsage getHeapUsage() { + return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage(); + } +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/platform/PlatformInfo.java b/spark-common/src/main/java/me/lucko/spark/common/platform/PlatformInfo.java new file mode 100644 index 0000000..20015dd --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/platform/PlatformInfo.java @@ -0,0 +1,126 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * 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 . + */ + +package me.lucko.spark.common.platform; + +import me.lucko.spark.proto.SparkProtos.MemoryData; +import me.lucko.spark.proto.SparkProtos.PlatformData; + +import java.lang.management.MemoryUsage; + +public interface PlatformInfo { + + Type getType(); + + String getName(); + + String getVersion(); + + String getMinecraftVersion(); + + int getNCpus(); + + MemoryUsage getHeapUsage(); + + default Data toData() { + return new Data(getType(), getName(), getVersion(), getMinecraftVersion(), getNCpus(), getHeapUsage()); + } + + enum Type { + SERVER(PlatformData.Type.SERVER), + CLIENT(PlatformData.Type.CLIENT), + PROXY(PlatformData.Type.PROXY); + + private final PlatformData.Type type; + + Type(PlatformData.Type type) { + this.type = type; + } + + public PlatformData.Type toProto() { + return this.type; + } + } + + final class Data { + private final Type type; + private final String name; + private final String version; + private final String minecraftVersion; + private final int nCpus; + private final MemoryUsage heapUsage; + + public Data(Type type, String name, String version, String minecraftVersion, int nCpus, MemoryUsage heapUsage) { + this.type = type; + this.name = name; + this.version = version; + this.minecraftVersion = minecraftVersion; + this.nCpus = nCpus; + this.heapUsage = heapUsage; + } + + public Type getType() { + return this.type; + } + + public String getName() { + return this.name; + } + + public String getVersion() { + return this.version; + } + + public String getMinecraftVersion() { + return this.minecraftVersion; + } + + public int getNCpus() { + return this.nCpus; + } + + public MemoryUsage getHeapUsage() { + return this.heapUsage; + } + + private static MemoryData toProto(MemoryUsage usage) { + return MemoryData.newBuilder() + .setUsed(usage.getUsed()) + .setCommitted(usage.getCommitted()) + .setMax(usage.getMax()) + .build(); + } + + public PlatformData toProto() { + PlatformData.Builder proto = PlatformData.newBuilder() + .setType(this.type.toProto()) + .setName(this.name) + .setVersion(this.version) + .setNCpus(this.nCpus) + .setHeapUsage(toProto(this.heapUsage)); + + if (this.minecraftVersion != null) { + proto.setMinecraftVersion(this.minecraftVersion); + } + + return proto.build(); + } + } +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/sampler/Sampler.java b/spark-common/src/main/java/me/lucko/spark/common/sampler/Sampler.java index cc72ca7..e772cb3 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/sampler/Sampler.java +++ b/spark-common/src/main/java/me/lucko/spark/common/sampler/Sampler.java @@ -23,7 +23,7 @@ package me.lucko.spark.common.sampler; import com.google.common.util.concurrent.ThreadFactoryBuilder; import me.lucko.spark.common.command.sender.CommandSender; -import me.lucko.spark.common.PlatformInfo; +import me.lucko.spark.common.platform.PlatformInfo; import me.lucko.spark.common.sampler.aggregator.DataAggregator; import me.lucko.spark.common.sampler.aggregator.SimpleDataAggregator; import me.lucko.spark.common.sampler.aggregator.TickedDataAggregator; diff --git a/spark-common/src/main/proto/spark/spark.proto b/spark-common/src/main/proto/spark/spark.proto index fbb6250..f06c097 100644 --- a/spark-common/src/main/proto/spark/spark.proto +++ b/spark-common/src/main/proto/spark/spark.proto @@ -16,12 +16,21 @@ message CommandSenderData { } } +message MemoryData { + int64 used = 1; + int64 committed = 2; + int64 max = 3; +} + message PlatformData { Type type = 1; string name = 2; string version = 3; string minecraft_version = 4; // optional + int32 n_cpus = 5; + MemoryData heapUsage = 6; + enum Type { SERVER = 0; CLIENT = 1; -- cgit