use of java.lang.management.GarbageCollectorMXBean in project opentsdb by OpenTSDB.
the class StatsRpc method printJVMStats.
/**
* Yield (chiefly memory-related) stats about this OpenTSDB instance's JVM.
* @param tsdb The TSDB from which to fetch stats.
* @param query The query to which to respond.
*/
private void printJVMStats(final TSDB tsdb, final HttpQuery query) {
final Map<String, Map<String, Object>> map = new HashMap<String, Map<String, Object>>();
final RuntimeMXBean runtime_bean = ManagementFactory.getRuntimeMXBean();
final Map<String, Object> runtime = new HashMap<String, Object>();
map.put("runtime", runtime);
runtime.put("startTime", runtime_bean.getStartTime());
runtime.put("uptime", runtime_bean.getUptime());
runtime.put("vmName", runtime_bean.getVmName());
runtime.put("vmVendor", runtime_bean.getVmVendor());
runtime.put("vmVersion", runtime_bean.getVmVersion());
final MemoryMXBean mem_bean = ManagementFactory.getMemoryMXBean();
final Map<String, Object> memory = new HashMap<String, Object>();
map.put("memory", memory);
memory.put("heapMemoryUsage", mem_bean.getHeapMemoryUsage());
memory.put("nonHeapMemoryUsage", mem_bean.getNonHeapMemoryUsage());
memory.put("objectsPendingFinalization", mem_bean.getObjectPendingFinalizationCount());
final List<GarbageCollectorMXBean> gc_beans = ManagementFactory.getGarbageCollectorMXBeans();
final Map<String, Object> gc = new HashMap<String, Object>();
map.put("gc", gc);
for (final GarbageCollectorMXBean gc_bean : gc_beans) {
final Map<String, Object> stats = new HashMap<String, Object>();
final String name = formatStatName(gc_bean.getName());
if (name == null) {
LOG.warn("Null name for bean: " + gc_bean);
continue;
}
gc.put(name, stats);
stats.put("collectionCount", gc_bean.getCollectionCount());
stats.put("collectionTime", gc_bean.getCollectionTime());
}
final List<MemoryPoolMXBean> pool_beans = ManagementFactory.getMemoryPoolMXBeans();
final Map<String, Object> pools = new HashMap<String, Object>();
map.put("pools", pools);
for (final MemoryPoolMXBean pool_bean : pool_beans) {
final Map<String, Object> stats = new HashMap<String, Object>();
final String name = formatStatName(pool_bean.getName());
if (name == null) {
LOG.warn("Null name for bean: " + pool_bean);
continue;
}
pools.put(name, stats);
stats.put("collectionUsage", pool_bean.getCollectionUsage());
stats.put("usage", pool_bean.getUsage());
stats.put("peakUsage", pool_bean.getPeakUsage());
stats.put("type", pool_bean.getType());
}
final OperatingSystemMXBean os_bean = ManagementFactory.getOperatingSystemMXBean();
final Map<String, Object> os = new HashMap<String, Object>();
map.put("os", os);
os.put("systemLoadAverage", os_bean.getSystemLoadAverage());
query.sendReply(query.serializer().formatJVMStatsV1(map));
}
use of java.lang.management.GarbageCollectorMXBean in project neo4j by neo4j.
the class GCMetrics method start.
@Override
public void start() {
for (final GarbageCollectorMXBean gcBean : ManagementFactory.getGarbageCollectorMXBeans()) {
registry.register(name(GC_TIME, prettifyName(gcBean.getName())), (Gauge<Long>) gcBean::getCollectionTime);
registry.register(name(GC_COUNT, prettifyName(gcBean.getName())), (Gauge<Long>) gcBean::getCollectionCount);
}
}
use of java.lang.management.GarbageCollectorMXBean in project graphdb by neo4j-attic.
the class GraphDbInstance method start.
/**
* Starts Neo4j with default configuration
* @param graphDb The graph database service.
*
* @param storeDir path to directory where Neo4j store is located
* @param create if true a new Neo4j store will be created if no store exist
* at <CODE>storeDir</CODE>
* @param configuration parameters
* @throws StartupFailedException if unable to start
*/
public synchronized Map<Object, Object> start(GraphDatabaseService graphDb, KernelExtensionLoader kernelExtensionLoader) {
if (started) {
throw new IllegalStateException("Neo4j instance already started");
}
Map<Object, Object> params = config.getParams();
boolean useMemoryMapped = Boolean.parseBoolean((String) config.getInputParams().get(Config.USE_MEMORY_MAPPED_BUFFERS));
boolean dumpToConsole = Boolean.parseBoolean((String) config.getInputParams().get(Config.DUMP_CONFIGURATION));
storeDir = FileUtils.fixSeparatorsInPath(storeDir);
StringLogger logger = StringLogger.getLogger(storeDir);
AutoConfigurator autoConfigurator = new AutoConfigurator(storeDir, useMemoryMapped, dumpToConsole);
autoConfigurator.configure(subset(config.getInputParams(), Config.USE_MEMORY_MAPPED_BUFFERS));
// params.putAll( config.getInputParams() );
String separator = System.getProperty("file.separator");
String store = storeDir + separator + "neostore";
params.put("store_dir", storeDir);
params.put("neo_store", store);
params.put("create", String.valueOf(create));
String logicalLog = storeDir + separator + "nioneo_logical.log";
params.put("logical_log", logicalLog);
byte[] resourceId = UTF8.encode("414141");
params.put(LockManager.class, config.getLockManager());
params.put(LockReleaser.class, config.getLockReleaser());
kernelExtensionLoader.configureKernelExtensions();
config.getTxModule().registerDataSource(Config.DEFAULT_DATA_SOURCE_NAME, Config.NIO_NEO_DB_CLASS, resourceId, params);
// hack for lucene index recovery if in path
if (!config.isReadOnly() || config.isBackupSlave()) {
try {
Class clazz = Class.forName(Config.LUCENE_DS_CLASS);
cleanWriteLocksInLuceneDirectory(storeDir + File.separator + "lucene");
byte[] luceneId = UTF8.encode("162373");
registerLuceneDataSource("lucene", clazz.getName(), config.getTxModule(), storeDir + File.separator + "lucene", config.getLockManager(), luceneId, params);
} catch (ClassNotFoundException e) {
// ok index util not on class path
} catch (NoClassDefFoundError err) {
// ok index util not on class path
}
try {
Class clazz = Class.forName(Config.LUCENE_FULLTEXT_DS_CLASS);
cleanWriteLocksInLuceneDirectory(storeDir + File.separator + "lucene-fulltext");
byte[] luceneId = UTF8.encode("262374");
registerLuceneDataSource("lucene-fulltext", clazz.getName(), config.getTxModule(), storeDir + File.separator + "lucene-fulltext", config.getLockManager(), luceneId, params);
} catch (ClassNotFoundException e) {
// ok index util not on class path
} catch (NoClassDefFoundError err) {
// ok index util not on class path
}
}
persistenceSource = new NioNeoDbPersistenceSource();
config.setPersistenceSource(Config.DEFAULT_DATA_SOURCE_NAME, create);
config.getIdGeneratorModule().setPersistenceSourceInstance(persistenceSource);
config.getTxModule().init();
config.getPersistenceModule().init();
persistenceSource.init();
config.getIdGeneratorModule().init();
config.getGraphDbModule().init();
kernelExtensionLoader.initializeIndexProviders();
config.getTxModule().start();
config.getPersistenceModule().start(config.getTxModule().getTxManager(), persistenceSource, config.getSyncHookFactory());
persistenceSource.start(config.getTxModule().getXaDataSourceManager());
config.getIdGeneratorModule().start();
config.getGraphDbModule().start(config.getLockReleaser(), config.getPersistenceModule().getPersistenceManager(), config.getRelationshipTypeCreator(), params);
logger.logMessage("--- CONFIGURATION START ---");
logger.logMessage(autoConfigurator.getNiceMemoryInformation());
logger.logMessage("Kernel version: " + Version.getKernel());
for (Version componentVersion : Service.load(Version.class)) {
logger.logMessage(componentVersion.toString());
}
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
logger.logMessage(String.format("Operating System: %s; version: %s; arch: %s; cpus: %s", os.getName(), os.getVersion(), os.getArch(), os.getAvailableProcessors()));
logger.logMessage("VM Name: " + runtime.getVmName());
logger.logMessage("VM Vendor: " + runtime.getVmVendor());
logger.logMessage("VM Version: " + runtime.getVmVersion());
if (runtime.isBootClassPathSupported()) {
logger.logMessage("Boot Class Path: " + runtime.getBootClassPath());
}
logger.logMessage("Class Path: " + runtime.getClassPath());
logger.logMessage("Library Path: " + runtime.getLibraryPath());
for (GarbageCollectorMXBean gcBean : ManagementFactory.getGarbageCollectorMXBeans()) {
logger.logMessage("Garbage Collector: " + gcBean.getName() + ": " + Arrays.toString(gcBean.getMemoryPoolNames()));
}
logger.logMessage("VM Arguments: " + runtime.getInputArguments());
logger.logMessage("");
logConfiguration(params, logger, dumpToConsole);
logger.logMessage("--- CONFIGURATION END ---");
logger.flush();
started = true;
return Collections.unmodifiableMap(params);
}
use of java.lang.management.GarbageCollectorMXBean in project commons by twitter.
the class JvmStats method register.
/**
* Add a series of system and jvm-level stats to the given registry.
*/
public static void register(MetricRegistry registry) {
final MetricRegistry stats = registry.scope("jvm");
final MemoryMXBean mem = ManagementFactory.getMemoryMXBean();
// memory stats
final MetricRegistry heapRegistry = stats.scope("heap");
registerMemoryStats(heapRegistry, new MemoryReporter() {
@Override
public MemoryUsage getUsage() {
return mem.getHeapMemoryUsage();
}
});
final MetricRegistry nonHeapRegistry = stats.scope("nonheap");
registerMemoryStats(nonHeapRegistry, new MemoryReporter() {
@Override
public MemoryUsage getUsage() {
return mem.getNonHeapMemoryUsage();
}
});
// threads
final ThreadMXBean threads = ManagementFactory.getThreadMXBean();
final MetricRegistry threadRegistry = stats.scope("thread");
threadRegistry.register(new AbstractGauge<Integer>("daemon_count") {
@Override
public Integer read() {
return threads.getDaemonThreadCount();
}
});
threadRegistry.register(new AbstractGauge<Integer>("count") {
@Override
public Integer read() {
return threads.getThreadCount();
}
});
threadRegistry.register(new AbstractGauge<Integer>("peak_count") {
@Override
public Integer read() {
return threads.getPeakThreadCount();
}
});
// class loading bean
final ClassLoadingMXBean classLoadingBean = ManagementFactory.getClassLoadingMXBean();
stats.register(new AbstractGauge<Integer>("classes_loaded") {
@Override
public Integer read() {
return classLoadingBean.getLoadedClassCount();
}
});
stats.register(new AbstractGauge<Long>("total_classes_loaded") {
@Override
public Long read() {
return classLoadingBean.getTotalLoadedClassCount();
}
});
stats.register(new AbstractGauge<Long>("classes_unloaded") {
@Override
public Long read() {
return classLoadingBean.getUnloadedClassCount();
}
});
// runtime
final RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
stats.register(new AbstractGauge<Long>("start_time") {
@Override
public Long read() {
return runtime.getStartTime();
}
});
stats.register(new AbstractGauge<Long>("uptime") {
@Override
public Long read() {
return runtime.getUptime();
}
});
//stats.register(new AbstractGauge<String>)
// os
final OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
stats.register(new AbstractGauge<Integer>("num_cpus") {
@Override
public Integer read() {
return os.getAvailableProcessors();
}
});
if (os instanceof com.sun.management.OperatingSystemMXBean) {
final com.sun.management.OperatingSystemMXBean sunOsMbean = (com.sun.management.OperatingSystemMXBean) os;
// if this is indeed an operating system
stats.register(new AbstractGauge<Long>("free_physical_memory") {
@Override
public Long read() {
return sunOsMbean.getFreePhysicalMemorySize();
}
});
stats.register(new AbstractGauge<Long>("free_swap") {
@Override
public Long read() {
return sunOsMbean.getFreeSwapSpaceSize();
}
});
stats.register(new AbstractGauge<Long>("process_cpu_time") {
@Override
public Long read() {
return sunOsMbean.getProcessCpuTime();
}
});
}
if (os instanceof com.sun.management.UnixOperatingSystemMXBean) {
// it's a unix system... I know this!
final UnixOperatingSystemMXBean unix = (UnixOperatingSystemMXBean) os;
stats.register(new AbstractGauge<Long>("fd_count") {
@Override
public Long read() {
return unix.getOpenFileDescriptorCount();
}
});
stats.register(new AbstractGauge<Long>("fd_limit") {
@Override
public Long read() {
return unix.getMaxFileDescriptorCount();
}
});
}
// mem
final List<MemoryPoolMXBean> memPool = ManagementFactory.getMemoryPoolMXBeans();
final MetricRegistry memRegistry = stats.scope("mem");
final MetricRegistry currentMem = memRegistry.scope("current");
final MetricRegistry postGCRegistry = memRegistry.scope("postGC");
for (final MemoryPoolMXBean pool : memPool) {
String name = normalizeName(pool.getName());
registerMemoryStats(currentMem.scope(name), new MemoryReporter() {
@Override
public MemoryUsage getUsage() {
return pool.getUsage();
}
});
registerMemoryStats(postGCRegistry.scope(name), new MemoryReporter() {
@Override
public MemoryUsage getUsage() {
return pool.getCollectionUsage();
}
});
}
currentMem.register(new AbstractGauge<Long>("used") {
@Override
public Long read() {
long sum = 0;
for (MemoryPoolMXBean pool : memPool) {
MemoryUsage usage = pool.getUsage();
if (usage != null) {
sum += usage.getUsed();
}
}
return sum;
}
});
AbstractGauge<Long> totalPostGCGauge = new AbstractGauge<Long>("used") {
@Override
public Long read() {
long sum = 0;
for (MemoryPoolMXBean pool : memPool) {
MemoryUsage usage = pool.getCollectionUsage();
if (usage != null) {
sum += usage.getUsed();
}
}
return sum;
}
};
postGCRegistry.register(totalPostGCGauge);
// java 1.7 specific buffer pool gauges
Multimap<String, AbstractGauge<Long>> java17gauges = buildJava17Gauges();
if (!java17gauges.isEmpty()) {
MetricRegistry bufferRegistry = stats.scope("buffer");
for (String scope : java17gauges.keySet()) {
MetricRegistry pool = bufferRegistry.scope(scope);
for (AbstractGauge<Long> gauge : java17gauges.get(scope)) {
pool.register(gauge);
}
}
}
// gc
final List<GarbageCollectorMXBean> gcPool = ManagementFactory.getGarbageCollectorMXBeans();
MetricRegistry gcRegistry = stats.scope("gc");
for (final GarbageCollectorMXBean gc : gcPool) {
String name = normalizeName(gc.getName());
MetricRegistry scoped = memRegistry.scope(name);
scoped.register(new AbstractGauge<Long>("cycles") {
@Override
public Long read() {
return gc.getCollectionCount();
}
});
scoped.register(new AbstractGauge<Long>("msec") {
@Override
public Long read() {
return gc.getCollectionTime();
}
});
}
gcRegistry.register(new AbstractGauge<Long>("cycles") {
@Override
public Long read() {
long sum = 0;
for (GarbageCollectorMXBean pool : gcPool) {
long count = pool.getCollectionCount();
if (count > 0) {
sum += count;
}
}
return sum;
}
});
gcRegistry.register(new AbstractGauge<Long>("msec") {
@Override
public Long read() {
long sum = 0;
for (GarbageCollectorMXBean pool : gcPool) {
long msec = pool.getCollectionTime();
if (msec > 0) {
sum += msec;
}
}
return sum;
}
});
}
use of java.lang.management.GarbageCollectorMXBean in project jvm-tools by aragozin.
the class YoungGCPauseBenchmark method benchmark.
public TestResult benchmark() throws IOException {
STRING_TEMPLATE = new char[stringLen];
if (mode == DataMode.CONST) {
overrideRate = 1.1d;
}
System.out.println("Java: " + System.getProperty("java.version") + " VM: " + System.getProperty("java.vm.version"));
System.out.println("Data model: " + Integer.getInteger("sun.arch.data.model"));
long tenuredSize = Runtime.getRuntime().maxMemory();
concurentMode = false;
// getting more accurate data
for (MemoryPoolMXBean bean : ManagementFactory.getMemoryPoolMXBeans()) {
if (CONC_MODE.contains(bean.getName())) {
concurentMode = true;
}
if (OLD_POOLS.contains(bean.getName())) {
tenuredSize = bean.getUsage().getMax();
if (tenuredSize < 0) {
tenuredSize = bean.getUsage().getCommitted();
}
System.out.println("Exact old space size is " + (tenuredSize) + " bytes");
oldMemPool = bean;
break;
}
}
beans: for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
for (String pool : bean.getMemoryPoolNames()) {
if (OLD_POOLS.contains(pool)) {
oldGcMBean = (com.sun.management.GarbageCollectorMXBean) bean;
continue beans;
} else {
}
}
youngGcMBean = bean;
}
boolean entrySizeAdjust = false;
if (entrySize <= 0) {
entrySizeAdjust = true;
System.out.println("Estimating entry memory footprint ...");
System.gc();
long used = getOldSpaceUsed();
int testSize = 100000;
initMaps(testSize);
while (size() < testSize) {
populateMap(concurentMode, testSize);
}
System.gc();
long footPrint = getOldSpaceUsed() - used;
entrySize = align((int) (footPrint / testSize), 32);
System.out.println("Entry footprint: " + entrySize);
maps = null;
}
System.gc();
long oldSpaceUsed = getOldSpaceUsed();
long freeTenured = tenuredSize - getOldSpaceUsed();
calculateCount(freeTenured);
if (concurentMode) {
System.out.println("Concurent mode is enabled");
System.out.println("Available old space: " + (freeTenured >> 20) + "MiB");
} else {
System.out.println("Available old space: " + (freeTenured >> 20) + "MiB (-" + headRoom + " MiB)");
}
if (count < 0) {
System.out.println("Heap size is too small, increase heap size or reduce headroom");
return null;
}
System.out.println("Young space collector: " + youngGcMBean.getName());
System.out.println("Old space collector: " + oldGcMBean.getName());
System.out.println("Populating - " + count);
initMaps(count);
int n = 0;
if (entrySizeAdjust) {
int targetSize = 4 * count / 5;
while (size() < targetSize) {
populateMap(concurentMode, count);
n++;
}
System.gc();
System.gc();
long footPrint = getOldSpaceUsed() - oldSpaceUsed;
entrySize = align((int) (footPrint / size()), 32);
System.out.println("Adjusted entry footprint: " + entrySize);
calculateCount(freeTenured);
}
while (size() < count) {
populateMap(concurentMode, count);
n++;
}
if (concurentMode) {
while (--n > 0) {
processMap(false);
}
}
// Let's wait for at least one major collection to complete
if (!oldGcMBean.getName().startsWith("G1")) {
// so we have to ignore this
if (oldGcMBean != null) {
long c = oldGcMBean.getCollectionCount();
while (c == oldGcMBean.getCollectionCount()) {
processMap(false);
}
}
}
System.out.println("Size: " + size());
if (!dryMode) {
System.out.println("Processing ... ");
} else {
System.out.println("Processing ... (DRY MODE ENABLED)");
}
StringBuffer sb = new StringBuffer();
sb.append("Limits:");
if (maxTime > 0) {
sb.append(" ").append(maxTime + " sec");
}
if (maxOld > 0) {
sb.append(" ").append(maxOld + " old collections");
}
if (maxYoung > 0) {
sb.append(" ").append(maxYoung + " young collections");
}
System.out.println(sb.toString());
activeOverrideRate = overrideRate;
YoungGcTimeTracker tracker = new YoungGcTimeTracker();
tracker.init();
// start count down here
long startTime = System.currentTimeMillis();
long oldC = oldGcMBean.getCollectionCount();
long youngC = youngGcMBean.getCollectionCount();
while (true) {
processMap(dryMode);
tracker.probe();
if (maxOld > 0) {
if (oldGcMBean.getCollectionCount() - oldC > maxOld) {
break;
}
}
if (maxYoung > 0) {
if (youngGcMBean.getCollectionCount() - youngC > maxYoung) {
break;
}
}
if (maxTime > 0 && (System.currentTimeMillis() > (startTime + TimeUnit.SECONDS.toMillis(maxTime)))) {
break;
}
}
System.out.println("Benchmark complete");
return tracker.result();
}
Aggregations