View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.beans;
12  
13  import java.lang.management.ManagementFactory;
14  import java.util.LinkedList;
15  import java.util.List;
16  import java.util.Set;
17  
18  import javax.management.MBeanServer;
19  import javax.management.MalformedObjectNameException;
20  import javax.management.ObjectInstance;
21  import javax.management.ObjectName;
22  import javax.management.openmbean.CompositeDataSupport;
23  
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import psiprobe.model.jmx.MemoryPool;
28  import psiprobe.tools.JmxTools;
29  
30  /**
31   * The Class JvmMemoryInfoAccessorBean.
32   */
33  public class JvmMemoryInfoAccessorBean {
34  
35    /** The Constant logger. */
36    private static final Logger logger = LoggerFactory.getLogger(JvmMemoryInfoAccessorBean.class);
37  
38    /**
39     * Gets the pools.
40     *
41     * @return the pools
42     *
43     * @throws MalformedObjectNameException the malformed object name exception
44     */
45    public List<MemoryPool> getPools() throws MalformedObjectNameException {
46  
47      MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
48      Set<ObjectInstance> objectInstanceMemoryPools =
49          mbeanServer.queryMBeans(new ObjectName("java.lang:type=MemoryPool,*"), null);
50      // totals
51      long totalInit = 0;
52      long totalMax = 0;
53      long totalUsed = 0;
54      long totalCommitted = 0;
55  
56      List<MemoryPool> memoryPools = new LinkedList<>();
57      for (ObjectInstance objectInstance : objectInstanceMemoryPools) {
58        ObjectName objName = objectInstance.getObjectName();
59        MemoryPool memoryPool = new MemoryPool();
60        memoryPool.setName(JmxTools.getStringAttr(mbeanServer, objName, "Name"));
61        memoryPool.setType(JmxTools.getStringAttr(mbeanServer, objName, "Type"));
62  
63        CompositeDataSupport cd =
64            (CompositeDataSupport) JmxTools.getAttribute(mbeanServer, objName, "Usage");
65        /*
66         * It seems that "Usage" attribute of one of the pools may turn into null intermittently. We
67         * better have a dip in the graph then an NPE though.
68         */
69        if (cd != null) {
70          memoryPool.setMax(JmxTools.getLongAttr(cd, "max"));
71          memoryPool.setUsed(JmxTools.getLongAttr(cd, "used"));
72          memoryPool.setInit(JmxTools.getLongAttr(cd, "init"));
73          memoryPool.setCommitted(JmxTools.getLongAttr(cd, "committed"));
74        } else {
75          logger.error("Oops, JVM problem? {} 'Usage' attribute is NULL!", objName);
76        }
77  
78        totalInit += memoryPool.getInit();
79        totalMax += memoryPool.getMax();
80        totalUsed += memoryPool.getUsed();
81        totalCommitted += memoryPool.getCommitted();
82  
83        memoryPools.add(memoryPool);
84      }
85  
86      if (!memoryPools.isEmpty()) {
87        MemoryPool pool = new MemoryPool();
88        pool.setName("Total");
89        pool.setType("TOTAL");
90        pool.setInit(totalInit);
91        pool.setUsed(totalUsed);
92        pool.setMax(totalMax);
93        pool.setCommitted(totalCommitted);
94        memoryPools.add(pool);
95      }
96  
97      return memoryPools;
98  
99    }
100 }