Back to index

munin  2.0.1
AbstractMemoryPoolProvider.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.ManagementFactory;
00006 import java.lang.management.MemoryPoolMXBean;
00007 import java.lang.reflect.AccessibleObject;
00008 import java.util.Set;
00009 
00010 import javax.management.MalformedObjectNameException;
00011 import javax.management.ObjectName;
00012 
00013 public abstract class AbstractMemoryPoolProvider extends
00014               AbstractMemoryUsageProvider {
00015 
00016        protected String threshold;
00017 
00018        private final LegacyPool pool;
00019        private final UsageType usage;
00020 
00021        protected AbstractMemoryPoolProvider(Config config, final LegacyPool pool,
00022                      final UsageType usage) {
00023               super(config);
00024 
00025               this.usage = usage;
00026               this.pool = pool;
00027        }
00028 
00029        @Field(info = "The usage threshold value of this memory pool in bytes. The default value is zero.")
00030        public String threshold() {
00031               return threshold;
00032        }
00033 
00034        @Override
00035        protected void printGraphConfig(PrintWriter out, String title,
00036                      String vlabel, String info, String args, boolean update,
00037                      boolean graph) {
00038               String poolName;
00039               try {
00040                      poolName = getMemoryPoolMXBean(pool).getName();
00041               } catch (Exception e) {
00042                      poolName = "<unknown pool>";
00043               }
00044               super.printGraphConfig(out, title, vlabel, info + " (" + poolName
00045                             + ").", args, update, graph);
00046        }
00047 
00048        @Override
00049        protected void printFieldValue(PrintWriter out, AccessibleObject accessible) {
00050               super.printFieldValue(out, accessible);
00051        }
00052 
00064        protected void prepareValues() throws Exception {
00065               MemoryPoolMXBean poolMXBean = getMemoryPoolMXBean(pool);
00066               prepareMemoryUsage(poolMXBean, usage);
00067               long thresholdValue;
00068               try {
00069                      switch (usage) {
00070                      case USAGE:
00071                             thresholdValue = poolMXBean.getUsageThreshold();
00072                             break;
00073                      // XXX does it make sense to return this threshold value for
00074                      // peak usage graphs?
00075                      case PEAK:
00076                      case POST_GC:
00077                             thresholdValue = poolMXBean.getCollectionUsageThreshold();
00078                             break;
00079                      default:
00080                             throw new IllegalArgumentException("Unknown UsageType: "
00081                                           + usage);
00082                      }
00083                      threshold = String.valueOf(thresholdValue);
00084               } catch (UnsupportedOperationException e) {
00085                      threshold = "U";
00086               }
00087        }
00088 
00089        protected MemoryPoolMXBean getMemoryPoolMXBean(LegacyPool pool)
00090                      throws MalformedObjectNameException, IOException {
00091               ObjectName gcName = new ObjectName(
00092                             ManagementFactory.MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",*");
00093 
00094               Set<ObjectName> mbeans = getConnection().queryNames(gcName, null);
00095               for (ObjectName objName : mbeans) {
00096                      MemoryPoolMXBean gc = ManagementFactory.newPlatformMXBeanProxy(
00097                                    getConnection(), objName.getCanonicalName(),
00098                                    MemoryPoolMXBean.class);
00099                      LegacyPool gcPool = LegacyPool.getLegacyPool(gc.getName());
00100                      if (gcPool == pool) {
00101                             return gc;
00102                      }
00103               }
00104               return null;
00105        }
00106 }