
com.purej.vminspect.data.SystemData Maven / Gradle / Ivy
// Copyright (c), 2013, adopus consulting GmbH Switzerland, all rights reserved.
package com.purej.vminspect.data;
import java.lang.management.ClassLoadingMXBean;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadMXBean;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* Provides information about the virtual machine currently running in.
*
* @author Stefan Mueller
*/
public class SystemData {
// Host name and IP:
private static final String HOST_IP;
// If sun-classes exists in classpath:
private static final boolean SUN_CLASSES_EXIST;
static {
// Calc localhost:
String hostIp;
try {
InetAddress localHost = InetAddress.getLocalHost();
hostIp = localHost.getHostName() + " (" + localHost.getHostAddress() + ")";
} catch (Exception e) {
hostIp = "Unknown";
}
HOST_IP = hostIp;
// Calc sun-classes:
boolean sunexists = false;
try {
Class.forName("com.sun.management.OperatingSystemMXBean");
Class.forName("com.sun.management.UnixOperatingSystemMXBean");
sunexists = true;
} catch (Throwable t) {
// Ignore...
}
SUN_CLASSES_EXIST = sunexists;
}
private final String _rtInfo;
private final RuntimeMXBean _rtb;
protected final OperatingSystemMXBean _osb;
private final int _threadCurrentCount;
private final int _clLoadedClassCount;
private final long _clTotalLoadedClassCount;
private final long _gcCollectionCount;
private final long _gcCollectionTimeMillis;
private final MemoryData _memoryHeap;
private final MemoryData _memoryNonHeap;
// Those values might be set by subclasses:
protected MemoryData _memoryPhysical;
protected MemoryData _memorySwap;
protected long _processCpuTimeMillis;
protected double _processCpuLoadPct;
protected double _systemCpuLoadPct;
protected long _openFileDescriptorCount;
protected long _maxFileDescriptorCount;
/**
* Creates a new instanceof of the correct SystemData instance.
*/
public static SystemData create() {
return SUN_CLASSES_EXIST ? new SunSystemData() : new SystemData();
}
/**
* Creates a new instance of this class.
*/
protected SystemData() {
// Store runtime-infos:
_rtInfo = System.getProperty("java.runtime.name") + ", " + System.getProperty("java.runtime.version");
_rtb = ManagementFactory.getRuntimeMXBean();
// Store thread-infos:
ThreadMXBean tb = ManagementFactory.getThreadMXBean();
_threadCurrentCount = tb.getThreadCount();
// Get class-loader stuff:
ClassLoadingMXBean clb = ManagementFactory.getClassLoadingMXBean();
_clLoadedClassCount = clb.getLoadedClassCount();
_clTotalLoadedClassCount = clb.getTotalLoadedClassCount();
// Build gc collection time:
long tmpGcCollectionCount = 0;
long tmpGcCollectionTimeMillis = 0;
for (GarbageCollectorMXBean garbageCollector : ManagementFactory.getGarbageCollectorMXBeans()) {
tmpGcCollectionCount += garbageCollector.getCollectionCount();
tmpGcCollectionTimeMillis += garbageCollector.getCollectionTime();
}
_gcCollectionCount = tmpGcCollectionCount;
_gcCollectionTimeMillis = tmpGcCollectionTimeMillis;
// Get heap / none heap memory:
MemoryMXBean mb = ManagementFactory.getMemoryMXBean();
_memoryHeap = new MemoryData(mb.getHeapMemoryUsage());
_memoryNonHeap = new MemoryData(mb.getNonHeapMemoryUsage());
// Physical / swap memory - Note: Most info is hidden in sun-classes, cannot check instance-of here
// to prevent class-not-found-exception for VMs without sun-classes! See dedicated sub-class SunSystemData.
_osb = ManagementFactory.getOperatingSystemMXBean();
_memoryPhysical = MemoryData.UNKNOWN;
_memorySwap = MemoryData.UNKNOWN;
_processCpuTimeMillis = -1;
_processCpuLoadPct = -1;
_systemCpuLoadPct = -1;
_openFileDescriptorCount = -1;
_maxFileDescriptorCount = -1;
}
/**
* Returns the java name and version.
*/
public String getRtInfo() {
return _rtInfo;
}
/**
* The name of the runtime process.
*/
public String getRtProcessName() {
return _rtb.getName();
}
/**
* Returns the startup time of this runtime process.
*/
public Date getRtProcessStartup() {
return new Date(_rtb.getStartTime());
}
/**
* Returns the input arguments passed to the Java virtual machine.
*/
public String getRtProcessArguments() {
StringBuilder result = new StringBuilder();
for (String jvmArg : _rtb.getInputArguments()) {
if (result.length() > 0) {
result.append('\n');
}
result.append(jvmArg);
}
return result.toString();
}
/**
* Returns the current system properties.
*/
public String getRtSystemProperties() {
List lines = new ArrayList();
for (Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy