jdk/src/share/classes/sun/management/snmp/jvminstr/JvmMemPoolEntryImpl.java
changeset 2 90ce3da70b43
child 35 935f747e70d3
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2003-2006 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 package sun.management.snmp.jvminstr;
       
    26 
       
    27 // java imports
       
    28 //
       
    29 import java.io.Serializable;
       
    30 import java.util.Map;
       
    31 
       
    32 // jmx imports
       
    33 //
       
    34 import com.sun.jmx.snmp.SnmpStatusException;
       
    35 import com.sun.jmx.snmp.SnmpDefinitions;
       
    36 
       
    37 // jdmk imports
       
    38 //
       
    39 import com.sun.jmx.snmp.agent.SnmpMib;
       
    40 
       
    41 import java.lang.management.ManagementFactory;
       
    42 import java.lang.management.MemoryUsage;
       
    43 import java.lang.management.MemoryType;
       
    44 import java.lang.management.MemoryPoolMXBean;
       
    45 
       
    46 import sun.management.snmp.jvmmib.JvmMemPoolEntryMBean;
       
    47 import sun.management.snmp.jvmmib.EnumJvmMemPoolState;
       
    48 import sun.management.snmp.jvmmib.EnumJvmMemPoolType;
       
    49 import sun.management.snmp.jvmmib.EnumJvmMemPoolThreshdSupport;
       
    50 import sun.management.snmp.jvmmib.EnumJvmMemPoolCollectThreshdSupport;
       
    51 import sun.management.snmp.util.MibLogger;
       
    52 import sun.management.snmp.util.JvmContextFactory;
       
    53 
       
    54 /**
       
    55  * The class is used for implementing the "JvmMemPoolEntry" group.
       
    56  */
       
    57 public class JvmMemPoolEntryImpl implements JvmMemPoolEntryMBean {
       
    58 
       
    59     /**
       
    60      * Variable for storing the value of "JvmMemPoolIndex".
       
    61      *
       
    62      * "An index value opaquely computed by the agent which uniquely
       
    63      * identifies a row in the jvmMemPoolTable.
       
    64      * "
       
    65      *
       
    66      */
       
    67     final protected int jvmMemPoolIndex;
       
    68 
       
    69 
       
    70     final static String memoryTag = "jvmMemPoolEntry.getUsage";
       
    71     final static String peakMemoryTag = "jvmMemPoolEntry.getPeakUsage";
       
    72     final static String collectMemoryTag =
       
    73         "jvmMemPoolEntry.getCollectionUsage";
       
    74     final static MemoryUsage ZEROS = new MemoryUsage(0,0,0,0);
       
    75 
       
    76 
       
    77 
       
    78     MemoryUsage getMemoryUsage() {
       
    79         try {
       
    80             final Map<Object, Object> m = JvmContextFactory.getUserData();
       
    81 
       
    82             if (m != null) {
       
    83                 final MemoryUsage cached = (MemoryUsage)
       
    84                     m.get(memoryTag);
       
    85                 if (cached != null) {
       
    86                     log.debug("getMemoryUsage",
       
    87                           "jvmMemPoolEntry.getUsage found in cache.");
       
    88                     return cached;
       
    89                 }
       
    90 
       
    91                 MemoryUsage u = pool.getUsage();
       
    92                 if (u == null) u = ZEROS;
       
    93 
       
    94                 m.put(memoryTag,u);
       
    95                 return u;
       
    96             }
       
    97             // Should never come here.
       
    98             // Log error!
       
    99             log.trace("getMemoryUsage", "ERROR: should never come here!");
       
   100             return pool.getUsage();
       
   101         } catch (RuntimeException x) {
       
   102             log.trace("getMemoryUsage",
       
   103                   "Failed to get MemoryUsage: " + x);
       
   104             log.debug("getMemoryUsage",x);
       
   105             throw x;
       
   106         }
       
   107 
       
   108     }
       
   109 
       
   110     MemoryUsage getPeakMemoryUsage() {
       
   111         try {
       
   112             final Map<Object, Object> m = JvmContextFactory.getUserData();
       
   113 
       
   114             if (m != null) {
       
   115                 final MemoryUsage cached = (MemoryUsage)
       
   116                     m.get(peakMemoryTag);
       
   117                 if (cached != null) {
       
   118                     if (log.isDebugOn())
       
   119                         log.debug("getPeakMemoryUsage",
       
   120                               peakMemoryTag + " found in cache.");
       
   121                     return cached;
       
   122                 }
       
   123 
       
   124                 MemoryUsage u = pool.getPeakUsage();
       
   125                 if (u == null) u = ZEROS;
       
   126 
       
   127                 m.put(peakMemoryTag,u);
       
   128                 return u;
       
   129             }
       
   130             // Should never come here.
       
   131             // Log error!
       
   132             log.trace("getPeakMemoryUsage", "ERROR: should never come here!");
       
   133             return ZEROS;
       
   134         } catch (RuntimeException x) {
       
   135             log.trace("getPeakMemoryUsage",
       
   136                   "Failed to get MemoryUsage: " + x);
       
   137             log.debug("getPeakMemoryUsage",x);
       
   138             throw x;
       
   139         }
       
   140 
       
   141     }
       
   142 
       
   143     MemoryUsage getCollectMemoryUsage() {
       
   144         try {
       
   145             final Map<Object, Object> m = JvmContextFactory.getUserData();
       
   146 
       
   147             if (m != null) {
       
   148                 final MemoryUsage cached = (MemoryUsage)
       
   149                     m.get(collectMemoryTag);
       
   150                 if (cached != null) {
       
   151                     if (log.isDebugOn())
       
   152                         log.debug("getCollectMemoryUsage",
       
   153                                   collectMemoryTag + " found in cache.");
       
   154                     return cached;
       
   155                 }
       
   156 
       
   157                 MemoryUsage u = pool.getCollectionUsage();
       
   158                 if (u == null) u = ZEROS;
       
   159 
       
   160                 m.put(collectMemoryTag,u);
       
   161                 return u;
       
   162             }
       
   163             // Should never come here.
       
   164             // Log error!
       
   165             log.trace("getCollectMemoryUsage",
       
   166                       "ERROR: should never come here!");
       
   167             return ZEROS;
       
   168         } catch (RuntimeException x) {
       
   169             log.trace("getPeakMemoryUsage",
       
   170                   "Failed to get MemoryUsage: " + x);
       
   171             log.debug("getPeakMemoryUsage",x);
       
   172             throw x;
       
   173         }
       
   174 
       
   175     }
       
   176 
       
   177     final MemoryPoolMXBean pool;
       
   178 
       
   179     /**
       
   180      * Constructor for the "JvmMemPoolEntry" group.
       
   181      */
       
   182     public JvmMemPoolEntryImpl(MemoryPoolMXBean mp, int index) {
       
   183         this.pool=mp;
       
   184         this.jvmMemPoolIndex = index;
       
   185     }
       
   186 
       
   187     /**
       
   188      * Getter for the "JvmMemPoolMaxSize" variable.
       
   189      */
       
   190     public Long getJvmMemPoolMaxSize() throws SnmpStatusException {
       
   191         final long val = getMemoryUsage().getMax();
       
   192         if (val > -1) return  new Long(val);
       
   193         else return JvmMemoryImpl.Long0;
       
   194     }
       
   195 
       
   196     /**
       
   197      * Getter for the "JvmMemPoolUsed" variable.
       
   198      */
       
   199     public Long getJvmMemPoolUsed() throws SnmpStatusException {
       
   200         final long val = getMemoryUsage().getUsed();
       
   201         if (val > -1) return  new Long(val);
       
   202         else return JvmMemoryImpl.Long0;
       
   203     }
       
   204 
       
   205     /**
       
   206      * Getter for the "JvmMemPoolInitSize" variable.
       
   207      */
       
   208     public Long getJvmMemPoolInitSize() throws SnmpStatusException {
       
   209         final long val = getMemoryUsage().getInit();
       
   210         if (val > -1) return  new Long(val);
       
   211         else return JvmMemoryImpl.Long0;
       
   212     }
       
   213 
       
   214     /**
       
   215      * Getter for the "JvmMemPoolCommitted" variable.
       
   216      */
       
   217     public Long getJvmMemPoolCommitted() throws SnmpStatusException {
       
   218         final long val = getMemoryUsage().getCommitted();
       
   219         if (val > -1) return  new Long(val);
       
   220         else return JvmMemoryImpl.Long0;
       
   221     }
       
   222 
       
   223     /**
       
   224      * Getter for the "JvmMemPoolPeakMaxSize" variable.
       
   225      */
       
   226     public Long getJvmMemPoolPeakMaxSize() throws SnmpStatusException {
       
   227         final long val = getPeakMemoryUsage().getMax();
       
   228         if (val > -1) return  new Long(val);
       
   229         else return JvmMemoryImpl.Long0;
       
   230     }
       
   231 
       
   232     /**
       
   233      * Getter for the "JvmMemPoolPeakUsed" variable.
       
   234      */
       
   235     public Long getJvmMemPoolPeakUsed() throws SnmpStatusException {
       
   236         final long val = getPeakMemoryUsage().getUsed();
       
   237         if (val > -1) return  new Long(val);
       
   238         else return JvmMemoryImpl.Long0;
       
   239     }
       
   240 
       
   241     /**
       
   242      * Getter for the "JvmMemPoolPeakCommitted" variable.
       
   243      */
       
   244     public Long getJvmMemPoolPeakCommitted() throws SnmpStatusException {
       
   245         final long val = getPeakMemoryUsage().getCommitted();
       
   246         if (val > -1) return  new Long(val);
       
   247         else return JvmMemoryImpl.Long0;
       
   248     }
       
   249 
       
   250     /**
       
   251      * Getter for the "JvmMemPoolCollectMaxSize" variable.
       
   252      */
       
   253     public Long getJvmMemPoolCollectMaxSize() throws SnmpStatusException {
       
   254         final long val = getCollectMemoryUsage().getMax();
       
   255         if (val > -1) return  new Long(val);
       
   256         else return JvmMemoryImpl.Long0;
       
   257     }
       
   258 
       
   259     /**
       
   260      * Getter for the "JvmMemPoolCollectUsed" variable.
       
   261      */
       
   262     public Long getJvmMemPoolCollectUsed() throws SnmpStatusException {
       
   263         final long val = getCollectMemoryUsage().getUsed();
       
   264         if (val > -1) return  new Long(val);
       
   265         else return JvmMemoryImpl.Long0;
       
   266     }
       
   267 
       
   268     /**
       
   269      * Getter for the "JvmMemPoolCollectCommitted" variable.
       
   270      */
       
   271     public Long getJvmMemPoolCollectCommitted() throws SnmpStatusException {
       
   272         final long val = getCollectMemoryUsage().getCommitted();
       
   273         if (val > -1) return  new Long(val);
       
   274         else return JvmMemoryImpl.Long0;
       
   275     }
       
   276 
       
   277     /**
       
   278      * Getter for the "JvmMemPoolThreshold" variable.
       
   279      */
       
   280     public Long getJvmMemPoolThreshold() throws SnmpStatusException {
       
   281         if (!pool.isUsageThresholdSupported())
       
   282             return JvmMemoryImpl.Long0;
       
   283         final long val = pool.getUsageThreshold();
       
   284         if (val > -1) return  new Long(val);
       
   285         else return JvmMemoryImpl.Long0;
       
   286     }
       
   287 
       
   288     /**
       
   289      * Setter for the "JvmMemPoolThreshold" variable.
       
   290      */
       
   291     public void setJvmMemPoolThreshold(Long x) throws SnmpStatusException {
       
   292         final long val = x.longValue();
       
   293         if (val < 0 )
       
   294             throw new SnmpStatusException(SnmpDefinitions.snmpRspWrongValue);
       
   295         // This should never throw an exception has the checks have
       
   296         // already been performed in checkJvmMemPoolThreshold().
       
   297         //
       
   298         pool.setUsageThreshold(val);
       
   299     }
       
   300 
       
   301     /**
       
   302      * Checker for the "JvmMemPoolThreshold" variable.
       
   303      */
       
   304     public void checkJvmMemPoolThreshold(Long x) throws SnmpStatusException {
       
   305         // if threshold is -1, it means that low memory detection is not
       
   306         // supported.
       
   307 
       
   308         if (!pool.isUsageThresholdSupported())
       
   309             throw new
       
   310                 SnmpStatusException(SnmpDefinitions.snmpRspInconsistentValue);
       
   311         final long val = x.longValue();
       
   312         if (val < 0 )
       
   313             throw new SnmpStatusException(SnmpDefinitions.snmpRspWrongValue);
       
   314     }
       
   315 
       
   316     /**
       
   317      * Getter for the "JvmMemPoolThreshdSupport" variable.
       
   318      */
       
   319     public EnumJvmMemPoolThreshdSupport getJvmMemPoolThreshdSupport()
       
   320         throws SnmpStatusException {
       
   321         if (pool.isUsageThresholdSupported())
       
   322             return EnumJvmMemPoolThreshdSupported;
       
   323         else
       
   324             return EnumJvmMemPoolThreshdUnsupported;
       
   325     }
       
   326 
       
   327     /**
       
   328      * Getter for the "JvmMemPoolThreshdCount" variable.
       
   329      */
       
   330     public Long getJvmMemPoolThreshdCount()
       
   331         throws SnmpStatusException {
       
   332         if (!pool.isUsageThresholdSupported())
       
   333             return JvmMemoryImpl.Long0;
       
   334         final long val = pool.getUsageThresholdCount();
       
   335         if (val > -1) return  new Long(val);
       
   336         else return JvmMemoryImpl.Long0;
       
   337     }
       
   338 
       
   339     /**
       
   340      * Getter for the "JvmMemPoolCollectThreshold" variable.
       
   341      */
       
   342     public Long getJvmMemPoolCollectThreshold() throws SnmpStatusException {
       
   343         if (!pool.isCollectionUsageThresholdSupported())
       
   344             return JvmMemoryImpl.Long0;
       
   345         final long val = pool.getCollectionUsageThreshold();
       
   346         if (val > -1) return  new Long(val);
       
   347         else return JvmMemoryImpl.Long0;
       
   348     }
       
   349 
       
   350     /**
       
   351      * Setter for the "JvmMemPoolCollectThreshold" variable.
       
   352      */
       
   353     public void setJvmMemPoolCollectThreshold(Long x)
       
   354         throws SnmpStatusException {
       
   355         final long val = x.longValue();
       
   356         if (val < 0 )
       
   357             throw new SnmpStatusException(SnmpDefinitions.snmpRspWrongValue);
       
   358         // This should never throw an exception has the checks have
       
   359         // already been performed in checkJvmMemPoolCollectThreshold().
       
   360         //
       
   361         pool.setCollectionUsageThreshold(val);
       
   362     }
       
   363 
       
   364     /**
       
   365      * Checker for the "JvmMemPoolCollectThreshold" variable.
       
   366      */
       
   367     public void checkJvmMemPoolCollectThreshold(Long x)
       
   368         throws SnmpStatusException {
       
   369         // if threshold is -1, it means that low memory detection is not
       
   370         // supported.
       
   371 
       
   372         if (!pool.isCollectionUsageThresholdSupported())
       
   373             throw new
       
   374                 SnmpStatusException(SnmpDefinitions.snmpRspInconsistentValue);
       
   375         final long val = x.longValue();
       
   376         if (val < 0 )
       
   377             throw new SnmpStatusException(SnmpDefinitions.snmpRspWrongValue);
       
   378     }
       
   379 
       
   380     /**
       
   381      * Getter for the "JvmMemPoolThreshdSupport" variable.
       
   382      */
       
   383     public EnumJvmMemPoolCollectThreshdSupport
       
   384         getJvmMemPoolCollectThreshdSupport()
       
   385         throws SnmpStatusException {
       
   386         if (pool.isCollectionUsageThresholdSupported())
       
   387             return EnumJvmMemPoolCollectThreshdSupported;
       
   388         else
       
   389             return EnumJvmMemPoolCollectThreshdUnsupported;
       
   390     }
       
   391 
       
   392     /**
       
   393      * Getter for the "JvmMemPoolCollectThreshdCount" variable.
       
   394      */
       
   395     public Long getJvmMemPoolCollectThreshdCount()
       
   396         throws SnmpStatusException {
       
   397         if (!pool.isCollectionUsageThresholdSupported())
       
   398             return JvmMemoryImpl.Long0;
       
   399         final long val = pool.getCollectionUsageThresholdCount();
       
   400         if (val > -1) return  new Long(val);
       
   401         else return JvmMemoryImpl.Long0;
       
   402     }
       
   403 
       
   404     public static EnumJvmMemPoolType jvmMemPoolType(MemoryType type)
       
   405         throws SnmpStatusException {
       
   406         if (type.equals(MemoryType.HEAP))
       
   407             return  EnumJvmMemPoolTypeHeap;
       
   408         else if (type.equals(MemoryType.NON_HEAP))
       
   409             return EnumJvmMemPoolTypeNonHeap;
       
   410         throw new SnmpStatusException(SnmpStatusException.snmpRspWrongValue);
       
   411     }
       
   412 
       
   413     /**
       
   414      * Getter for the "JvmMemPoolType" variable.
       
   415      */
       
   416     public EnumJvmMemPoolType getJvmMemPoolType() throws SnmpStatusException {
       
   417         return jvmMemPoolType(pool.getType());
       
   418     }
       
   419 
       
   420     /**
       
   421      * Getter for the "JvmMemPoolName" variable.
       
   422      */
       
   423     public String getJvmMemPoolName() throws SnmpStatusException {
       
   424         return JVM_MANAGEMENT_MIB_IMPL.validJavaObjectNameTC(pool.getName());
       
   425     }
       
   426 
       
   427     /**
       
   428      * Getter for the "JvmMemPoolIndex" variable.
       
   429      */
       
   430     public Integer getJvmMemPoolIndex() throws SnmpStatusException {
       
   431         return new Integer(jvmMemPoolIndex);
       
   432     }
       
   433 
       
   434 
       
   435     /**
       
   436      * Getter for the "JvmMemPoolState" variable.
       
   437      */
       
   438     public EnumJvmMemPoolState getJvmMemPoolState()
       
   439         throws SnmpStatusException {
       
   440         if (pool.isValid())
       
   441             return JvmMemPoolStateValid;
       
   442         else
       
   443             return JvmMemPoolStateInvalid;
       
   444     }
       
   445 
       
   446     /**
       
   447      * Getter for the "JvmMemPoolPeakReset" variable.
       
   448      */
       
   449     public synchronized Long getJvmMemPoolPeakReset()
       
   450         throws SnmpStatusException {
       
   451         return new Long(jvmMemPoolPeakReset);
       
   452     }
       
   453 
       
   454     /**
       
   455      * Setter for the "JvmMemPoolPeakReset" variable.
       
   456      */
       
   457     public synchronized void setJvmMemPoolPeakReset(Long x)
       
   458         throws SnmpStatusException {
       
   459         final long l = x.longValue();
       
   460         if (l > jvmMemPoolPeakReset) {
       
   461             final long stamp = System.currentTimeMillis();
       
   462             pool.resetPeakUsage();
       
   463             jvmMemPoolPeakReset = stamp;
       
   464             log.debug("setJvmMemPoolPeakReset",
       
   465                       "jvmMemPoolPeakReset="+stamp);
       
   466         }
       
   467     }
       
   468 
       
   469     /**
       
   470      * Checker for the "JvmMemPoolPeakReset" variable.
       
   471      */
       
   472     public void checkJvmMemPoolPeakReset(Long x) throws SnmpStatusException {
       
   473     }
       
   474 
       
   475     /* Last time peak usage was reset */
       
   476     private long jvmMemPoolPeakReset = 0;
       
   477 
       
   478     private final static EnumJvmMemPoolState JvmMemPoolStateValid =
       
   479         new EnumJvmMemPoolState("valid");
       
   480     private final static EnumJvmMemPoolState JvmMemPoolStateInvalid =
       
   481         new EnumJvmMemPoolState("invalid");
       
   482 
       
   483     private static final EnumJvmMemPoolType EnumJvmMemPoolTypeHeap =
       
   484         new EnumJvmMemPoolType("heap");
       
   485     private static final EnumJvmMemPoolType EnumJvmMemPoolTypeNonHeap =
       
   486         new EnumJvmMemPoolType("nonheap");
       
   487 
       
   488     private static final EnumJvmMemPoolThreshdSupport
       
   489         EnumJvmMemPoolThreshdSupported =
       
   490         new EnumJvmMemPoolThreshdSupport("supported");
       
   491     private static final EnumJvmMemPoolThreshdSupport
       
   492         EnumJvmMemPoolThreshdUnsupported =
       
   493         new EnumJvmMemPoolThreshdSupport("unsupported");
       
   494 
       
   495     private static final EnumJvmMemPoolCollectThreshdSupport
       
   496         EnumJvmMemPoolCollectThreshdSupported =
       
   497         new EnumJvmMemPoolCollectThreshdSupport("supported");
       
   498     private static final EnumJvmMemPoolCollectThreshdSupport
       
   499         EnumJvmMemPoolCollectThreshdUnsupported=
       
   500         new EnumJvmMemPoolCollectThreshdSupport("unsupported");
       
   501 
       
   502 
       
   503     static final MibLogger log = new MibLogger(JvmMemPoolEntryImpl.class);
       
   504 }