org.graylog2.shared.system.stats.jvm.JvmStats Maven / Gradle / Ivy
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* 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
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* .
*/
package org.graylog2.shared.system.stats.jvm;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.auto.value.AutoValue;
import org.graylog.autovalue.WithBeanGetter;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.RuntimeMXBean;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@JsonAutoDetect
@AutoValue
@WithBeanGetter
public abstract class JvmStats {
public static final JvmStats INSTANCE;
static {
final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
final long heapInit = memoryMXBean.getHeapMemoryUsage().getInit();
final long heapMax = memoryMXBean.getHeapMemoryUsage().getMax();
final long nonHeapInit = memoryMXBean.getNonHeapMemoryUsage().getInit();
final long nonHeapMax = memoryMXBean.getNonHeapMemoryUsage().getMax();
long directMemoryMax;
try {
Class> vmClass = Class.forName("sun.misc.VM");
directMemoryMax = (long) vmClass.getMethod("maxDirectMemory").invoke(null);
} catch (Throwable t) {
directMemoryMax = -1;
}
final Memory memory = Memory.create(heapInit, heapMax, nonHeapInit, nonHeapMax, directMemoryMax);
final RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
final long startTime = runtimeMXBean.getStartTime();
final String version = runtimeMXBean.getSystemProperties().get("java.version");
final String vmName = runtimeMXBean.getVmName();
final String vmVendor = runtimeMXBean.getVmVendor();
final String vmVersion = runtimeMXBean.getVmVersion();
final String specName = runtimeMXBean.getSpecName();
final String specVendor = runtimeMXBean.getSpecVendor();
final String specVersion = runtimeMXBean.getSpecVersion();
final List inputArguments = runtimeMXBean.getInputArguments();
String bootClassPath;
try {
bootClassPath = runtimeMXBean.getBootClassPath();
} catch (UnsupportedOperationException e) {
bootClassPath = "";
}
final String classPath = runtimeMXBean.getClassPath();
// TODO Remove some sensitive values or don't output at all?
final Map systemProperties = runtimeMXBean.getSystemProperties();
final List gcMxBeans = ManagementFactory.getGarbageCollectorMXBeans();
final List garbageCollectors = new ArrayList<>(gcMxBeans.size());
for (GarbageCollectorMXBean gcMxBean : gcMxBeans) {
garbageCollectors.add(gcMxBean.getName());
}
final List memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
final List memoryPools = new ArrayList<>(memoryPoolMXBeans.size());
for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
memoryPools.add(memoryPoolMXBean.getName());
}
INSTANCE = JvmStats.create(version, vmName, vmVersion, vmVendor, specName, specVersion, specVendor, startTime, memory, inputArguments, bootClassPath, classPath, systemProperties, garbageCollectors, memoryPools);
}
@JsonProperty
public abstract String version();
@JsonProperty
public abstract String vmName();
@JsonProperty
public abstract String vmVersion();
@JsonProperty
public abstract String vmVendor();
@JsonProperty
public abstract String specName();
@JsonProperty
public abstract String specVersion();
@JsonProperty
public abstract String specVendor();
@JsonProperty
public abstract long startTime();
@JsonProperty
public abstract Memory mem();
@JsonProperty
public abstract List inputArguments();
@JsonProperty
public abstract String bootClassPath();
@JsonProperty
public abstract String classPath();
@JsonProperty
public abstract Map systemProperties();
@JsonProperty
public abstract List garbageCollectors();
@JsonProperty
public abstract List memoryPools();
@JsonCreator
public static JvmStats create(@JsonProperty("version") String version,
@JsonProperty("vm_name") String vmName,
@JsonProperty("vm_version") String vmVersion,
@JsonProperty("vm_vendor") String vmVendor,
@JsonProperty("spec_name") String specName,
@JsonProperty("spec_version") String specVersion,
@JsonProperty("spec_vendor") String specVendor,
@JsonProperty("start_time") long startTime,
@JsonProperty("mem") JvmStats.Memory mem,
@JsonProperty("input_arguments") List inputArguments,
@JsonProperty("boot_class_path") String bootClassPath,
@JsonProperty("class_path") String classPath,
@JsonProperty("system_properties") Map systemProperties,
@JsonProperty("garbage_collectors") List garbageCollectors,
@JsonProperty("memory_pools") List memoryPools) {
return new AutoValue_JvmStats(
version, vmName, vmVersion, vmVendor, specName, specVersion, specVendor,
startTime, mem, inputArguments, bootClassPath, classPath, systemProperties,
garbageCollectors, memoryPools);
}
@JsonAutoDetect
@AutoValue
@WithBeanGetter
public abstract static class Memory {
@JsonProperty
public abstract long heapInit();
@JsonProperty
public abstract long heapMax();
@JsonProperty
public abstract long nonHeapInit();
@JsonProperty
public abstract long nonHeapMax();
@JsonProperty
public abstract long directMemoryMax();
@JsonCreator
public static Memory create(@JsonProperty("heap_init") long heapInit,
@JsonProperty("heap_max") long heapMax,
@JsonProperty("non_heap_init") long nonHeapInit,
@JsonProperty("non_heap_max") long nonHeapMax,
@JsonProperty("direct_memory_max") long directMemoryMax) {
return new AutoValue_JvmStats_Memory(heapInit, heapMax, nonHeapInit, nonHeapMax, directMemoryMax);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy