Back to index

munin  2.0.1
GarbageCollectionInfo.java
Go to the documentation of this file.
00001 package org.munin.plugin.jmx;
00002 
00003 import java.io.IOException;
00004 import java.io.PrintWriter;
00005 import java.lang.management.GarbageCollectorMXBean;
00006 import java.lang.management.ManagementFactory;
00007 import java.util.ArrayList;
00008 import java.util.Collection;
00009 import java.util.Set;
00010 import java.util.TreeSet;
00011 
00012 import javax.management.MalformedObjectNameException;
00013 import javax.management.ObjectName;
00014 
00015 public abstract class GarbageCollectionInfo extends
00016               AbstractAnnotationGraphsProvider {
00017 
00018        protected GarbageCollectionInfo(Config config) {
00019               super(config);
00020        }
00021 
00022        private Collection<GarbageCollectorMXBean> collectors;
00023 
00024        private Collection<GarbageCollectorMXBean> getCollectors() {
00025               if (collectors == null) {
00026                      try {
00027                             ObjectName gcName = new ObjectName(
00028                                           ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE
00029                                                         + ",*");
00030 
00031                             // ensure that the MBeans are sorted consistently
00032                             Set<ObjectName> mbeans = new TreeSet<ObjectName>(getConnection()
00033                                           .queryNames(gcName, null));
00034                             collectors = new ArrayList<GarbageCollectorMXBean>(mbeans
00035                                           .size());
00036                             for (ObjectName objectName : mbeans) {
00037                                    GarbageCollectorMXBean collector = ManagementFactory
00038                                                  .newPlatformMXBeanProxy(getConnection(), objectName
00039                                                                .getCanonicalName(),
00040                                                                GarbageCollectorMXBean.class);
00041                                    collectors.add(collector);
00042                             }
00043                      } catch (IOException e) {
00044                             e.printStackTrace();
00045                      } catch (MalformedObjectNameException e) {
00046                             e.printStackTrace();
00047                      }
00048               }
00049               return collectors;
00050        }
00051 
00052        @Override
00053        protected void printFieldsConfig(PrintWriter out) {
00054               for (GarbageCollectorMXBean collector : getCollectors()) {
00055                      String name = getFieldName(collector);
00056                      String label = getFieldLabel(collector);
00057                      String info = getFieldInfo(collector);
00058                      printFieldConfig(out, name, label, info, "DERIVE", null, null, 0,
00059                                    Double.NaN);
00060               }
00061        }
00062 
00063        @Override
00064        public void printValues(PrintWriter out) {
00065                      for (GarbageCollectorMXBean collector : getCollectors()) {
00066                             String name = getFieldName(collector);
00067                             printFieldAttribute(out, name, "value", getValue(collector));
00068                      }
00069        }
00070 
00071        protected abstract String getFieldName(GarbageCollectorMXBean collector);
00072 
00073        protected abstract String getFieldLabel(GarbageCollectorMXBean collector);
00074 
00075        protected abstract String getFieldInfo(GarbageCollectorMXBean collector);
00076 
00077        protected abstract Object getValue(GarbageCollectorMXBean collector);
00078 
00079        protected String getCollectorInfo(GarbageCollectorMXBean collector) {
00080               StringBuilder builder = new StringBuilder(collector.getName());
00081               builder.append(" collector (handles these memory pools: ");
00082               boolean first = true;
00083               for (String poolName : collector.getMemoryPoolNames()) {
00084                      builder.append(poolName);
00085                      if (first) {
00086                             first = false;
00087                      } else {
00088                             builder.append(", ");
00089                      }
00090               }
00091               builder.append(")");
00092               return builder.toString();
00093        }
00094 
00095        @Graph(title = "Garbage Collection Count", vlabel = "count", info = "Shows the number of garbage collections for all existing garbage collectors in the target JVM.")
00096        public static class Count extends GarbageCollectionInfo {
00097               public Count(Config config) {
00098                      super(config);
00099               }
00100 
00101               @Override
00102               protected String getFieldName(GarbageCollectorMXBean collector) {
00103                      return "count_" + toFieldName(collector.getName());
00104               }
00105 
00106               @Override
00107               public String getFieldLabel(GarbageCollectorMXBean collector) {
00108                      return "Collection count of " + collector.getName();
00109               }
00110 
00111               @Override
00112               protected String getFieldInfo(GarbageCollectorMXBean collector) {
00113                      return "Number of collections that have occurred of the "
00114                                    + getCollectorInfo(collector);
00115               }
00116 
00117               @Override
00118               public Object getValue(GarbageCollectorMXBean collector) {
00119                      return collector.getCollectionCount();
00120               }
00121 
00122               public static void main(String args[]) {
00123                      runGraph(args);
00124               }
00125 
00126        }
00127 
00128        @Graph(title = "Garbage Collection Time", vlabel = "ms", info = "Shows the time spent in garbage collections for all existing garbage collectors in the target JVM.")
00129        public static class Time extends GarbageCollectionInfo {
00130               public Time(Config config) {
00131                      super(config);
00132                      // TODO Auto-generated constructor stub
00133               }
00134 
00135               @Override
00136               protected String getFieldName(GarbageCollectorMXBean collector) {
00137                      return "time_" + toFieldName(collector.getName());
00138               }
00139 
00140               @Override
00141               public String getFieldLabel(GarbageCollectorMXBean collector) {
00142                      return "Collection time of " + collector.getName();
00143               }
00144 
00145               @Override
00146               protected String getFieldInfo(GarbageCollectorMXBean collector) {
00147                      return "Approximate collection elapsed time in milliseconds of the "
00148                                    + getCollectorInfo(collector);
00149               }
00150 
00151               @Override
00152               public Object getValue(GarbageCollectorMXBean collector) {
00153                      return collector.getCollectionTime();
00154               }
00155 
00156               public static void main(String args[]) {
00157                      runGraph(args);
00158               }
00159 
00160        }
00161 }