/* * SSSync, a Simple and Stupid Synchronizer for data with multi-valued attributes * Copyright (C) 2014 Ludovic Pouzenc * * This file is part of SSSync. * * SSSync 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. * * SSSync 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 SSSync. If not, see */ package utils; import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; import java.lang.management.MemoryPoolMXBean; import java.lang.management.MemoryUsage; import java.lang.management.RuntimeMXBean; import org.apache.log4j.Level; import org.apache.log4j.Logger; /** * TODO javadoc * * @author lpouzenc */ public class JVMStatsDumper { private static final Logger logger = Logger.getLogger(JVMStatsDumper.class.getName()); public static void logGCStats() { // Skip all string construction if will not print this stuff if ( logger.getLevel().isGreaterOrEqual(Level.INFO) ) { return; } long totalGarbageCollections = 0; long garbageCollectionTime = 0; final String gcDumpHeader="Dumping Garbage Collector statistics\n" + "+--------------------+-----------------------------+\n" + "+ GC Name + Count + Time (ms) +\n" + "+--------------------+--------------+--------------+\n"; StringBuilder sb = new StringBuilder(1024); sb.append(gcDumpHeader); for(GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) { long count = gc.getCollectionCount(); long time = gc.getCollectionTime(); sb.append(String.format("+ %18s + %,12d + %,12d +%n", gc.getName(), count, time)); if(count >= 0) totalGarbageCollections += count; if(time >= 0) garbageCollectionTime += time; } sb.append("+ + + +\n"); sb.append(String.format("+ %18s + %,12d + %,12d +%n", "Total", totalGarbageCollections, garbageCollectionTime )); sb.append("+--------------------+--------------+--------------+\n"); sb.append("JVM arguments : "); RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean(); for ( String arg : runtimeMxBean.getInputArguments() ) { sb.append(arg + " "); } logger.debug(sb); } /** * Helper function to log the current memory usage */ public static void logMemoryUsage() { // Skip all string construction if will not print this stuff if ( logger.getLevel().isGreaterOrEqual(Level.INFO) ) { return; } final String memDumpHeader="Dumping memory statistics\n" + "+--------------------------------------------------------------------------------+\n" + "+ + Current (kio) + Peak (kio) +\n" + "+ Pool +-----------------------------------------------------------+\n" + "+ + Used + Committed + Used + Committed +\n" + "+--------------------+--------------+--------------+--------------+--------------+\n"; StringBuilder sb = new StringBuilder(1024); sb.append(memDumpHeader); for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) { MemoryUsage peak = pool.getPeakUsage(); MemoryUsage curr = pool.getUsage(); sb.append(String.format("+ %18s + %,12d + %,12d + %,12d + %,12d +%n", pool.getName(),curr.getUsed()/1024, curr.getCommitted()/1024, peak.getUsed()/1024, peak.getCommitted()/1024 )); pool.resetPeakUsage(); //XXX Maybe this is not a global action and is useless on a temporary object used once } sb.append("+--------------------+--------------+--------------+--------------+--------------+\n"); logger.debug(sb); } }