diff options
author | Luck <git@lucko.me> | 2020-06-23 12:27:34 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2020-06-23 12:27:34 +0100 |
commit | 85561633292550209a1461347f8446b54552b391 (patch) | |
tree | c5bf09a6a0a1bb0324f8e14ecda790184a183c2c /spark-common/src/main/java/me/lucko/spark | |
parent | a2af3f8f7e3693f3e445d2998938bf448d47c35f (diff) | |
download | spark-85561633292550209a1461347f8446b54552b391.tar.gz spark-85561633292550209a1461347f8446b54552b391.tar.bz2 spark-85561633292550209a1461347f8446b54552b391.zip |
Add n-cpus and heap usage to platform info
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark')
-rw-r--r-- | spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java | 1 | ||||
-rw-r--r-- | spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDump.java | 7 | ||||
-rw-r--r-- | spark-common/src/main/java/me/lucko/spark/common/heapdump/HeapDumpSummary.java | 2 | ||||
-rw-r--r-- | spark-common/src/main/java/me/lucko/spark/common/monitor/memory/GarbageCollectionMonitor.java | 9 | ||||
-rw-r--r-- | spark-common/src/main/java/me/lucko/spark/common/platform/AbstractPlatformInfo.java | 17 | ||||
-rw-r--r-- | spark-common/src/main/java/me/lucko/spark/common/platform/PlatformInfo.java (renamed from spark-common/src/main/java/me/lucko/spark/common/PlatformInfo.java) | 91 | ||||
-rw-r--r-- | spark-common/src/main/java/me/lucko/spark/common/sampler/Sampler.java | 2 |
7 files changed, 65 insertions, 64 deletions
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/PlatformInfo.java b/spark-common/src/main/java/me/lucko/spark/common/platform/PlatformInfo.java index 8ff3997..20015dd 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/PlatformInfo.java +++ b/spark-common/src/main/java/me/lucko/spark/common/platform/PlatformInfo.java @@ -18,13 +18,13 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package me.lucko.spark.common; +package me.lucko.spark.common.platform; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; +import me.lucko.spark.proto.SparkProtos.MemoryData; import me.lucko.spark.proto.SparkProtos.PlatformData; +import java.lang.management.MemoryUsage; + public interface PlatformInfo { Type getType(); @@ -35,8 +35,12 @@ public interface PlatformInfo { String getMinecraftVersion(); + int getNCpus(); + + MemoryUsage getHeapUsage(); + default Data toData() { - return new Data(getType(), getName(), getVersion(), getMinecraftVersion()); + return new Data(getType(), getName(), getVersion(), getMinecraftVersion(), getNCpus(), getHeapUsage()); } enum Type { @@ -51,25 +55,7 @@ public interface PlatformInfo { } 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()); + return this.type; } } @@ -78,47 +64,57 @@ public interface PlatformInfo { 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) { + 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 type; + return this.type; } public String getName() { - return name; + return this.name; } public String getVersion() { - return version; + return this.version; } public String getMinecraftVersion() { - return minecraftVersion; + return this.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 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); + .setVersion(this.version) + .setNCpus(this.nCpus) + .setHeapUsage(toProto(this.heapUsage)); if (this.minecraftVersion != null) { proto.setMinecraftVersion(this.minecraftVersion); @@ -126,20 +122,5 @@ public interface PlatformInfo { 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/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; |