0N/A/*
2362N/A * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage sun.management.snmp.jvminstr;
0N/A
0N/A
0N/A// java imports
0N/A//
0N/Aimport java.io.Serializable;
0N/Aimport java.util.List;
0N/Aimport java.util.Map;
0N/Aimport java.util.TreeMap;
0N/A
0N/A// jmx imports
0N/A//
0N/Aimport com.sun.jmx.snmp.SnmpOid;
0N/Aimport com.sun.jmx.snmp.SnmpStatusException;
0N/A
0N/A// jdmk imports
0N/A//
0N/Aimport com.sun.jmx.snmp.agent.SnmpMib;
0N/Aimport com.sun.jmx.snmp.agent.SnmpStandardObjectServer;
0N/A
0N/Aimport java.lang.management.MemoryManagerMXBean;
0N/Aimport java.lang.management.GarbageCollectorMXBean;
0N/Aimport java.lang.management.ManagementFactory;
0N/A
0N/Aimport sun.management.snmp.jvmmib.JvmMemGCTableMeta;
0N/Aimport sun.management.snmp.util.SnmpCachedData;
0N/Aimport sun.management.snmp.util.SnmpTableCache;
0N/Aimport sun.management.snmp.util.SnmpTableHandler;
0N/Aimport sun.management.snmp.util.MibLogger;
0N/Aimport sun.management.snmp.util.JvmContextFactory;
0N/A
0N/A/**
0N/A * The class is used for implementing the "JvmMemGCTable" table.
0N/A */
0N/Apublic class JvmMemGCTableMetaImpl extends JvmMemGCTableMeta {
0N/A
0N/A /**
0N/A * This class acts as a filter over the SnmpTableHandler
0N/A * used for the JvmMemoryManagerTable. It filters out
0N/A * (skip) all MemoryManagerMXBean that are not instances of
0N/A * GarbageCollectorMXBean so that only Garbage Collectors are
0N/A * seen. This is a better solution than relying on
0N/A * ManagementFactory.getGarbageCollectorMXBeans() because it makes it
0N/A * possible to guarantee the consistency betwen the MemoryManager table
0N/A * and the GCTable since both will be sharing the same cache.
0N/A **/
0N/A protected static class GCTableFilter {
0N/A
0N/A /**
0N/A * Returns the index that immediately follows the given
0N/A * <var>index</var>. The returned index is strictly greater
0N/A * than the given <var>index</var>, and is contained in the table.
0N/A * <br>If the given <var>index</var> is null, returns the first
0N/A * index in the table.
0N/A * <br>If there are no index after the given <var>index</var>,
0N/A * returns null.
0N/A * This method is an optimization for the case where the
0N/A * SnmpTableHandler is in fact an instance of SnmpCachedData.
0N/A **/
0N/A public SnmpOid getNext(SnmpCachedData datas, SnmpOid index) {
0N/A
0N/A final boolean dbg = log.isDebugOn();
0N/A
0N/A // We're going to loop until we find an instance of
0N/A // GarbageCollectorMXBean. First we attempt to find
0N/A // the next element whose OID follows the given index.
0N/A // If `index' is null, the insertion point is -1
0N/A // (the next is 0 = -insertion - 1)
0N/A //
0N/A final int insertion = (index==null)?-1:datas.find(index);
0N/A if (dbg) log.debug("GCTableFilter","oid="+index+
0N/A " at insertion="+insertion);
0N/A
0N/A int next;
0N/A if (insertion > -1) next = insertion+1;
0N/A else next = -insertion -1;
0N/A
0N/A // Now `next' points to the element that imediately
0N/A // follows the given `index'. We're going to loop
0N/A // through the table, starting at `next' (included),
0N/A // and return the first element which is an instance
0N/A // of GarbageCollectorMXBean.
0N/A //
0N/A for (;next<datas.indexes.length;next++) {
0N/A if (dbg) log.debug("GCTableFilter","next="+next);
0N/A final Object value = datas.datas[next];
0N/A if (dbg) log.debug("GCTableFilter","value["+next+"]=" +
0N/A ((MemoryManagerMXBean)value).getName());
0N/A if (value instanceof GarbageCollectorMXBean) {
0N/A // That's the next: return it.
0N/A if (dbg) log.debug("GCTableFilter",
0N/A ((MemoryManagerMXBean)value).getName() +
0N/A " is a GarbageCollectorMXBean.");
0N/A return datas.indexes[next];
0N/A }
0N/A if (dbg) log.debug("GCTableFilter",
0N/A ((MemoryManagerMXBean)value).getName() +
0N/A " is not a GarbageCollectorMXBean: " +
0N/A value.getClass().getName());
0N/A // skip to next index...
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the index that immediately follows the given
0N/A * <var>index</var>. The returned index is strictly greater
0N/A * than the given <var>index</var>, and is contained in the table.
0N/A * <br>If the given <var>index</var> is null, returns the first
0N/A * index in the table.
0N/A * <br>If there are no index after the given <var>index</var>,
0N/A * returns null.
0N/A **/
0N/A public SnmpOid getNext(SnmpTableHandler handler, SnmpOid index) {
0N/A
0N/A // try to call the optimized method
0N/A if (handler instanceof SnmpCachedData)
0N/A return getNext((SnmpCachedData)handler, index);
0N/A
0N/A // too bad - revert to non-optimized generic algorithm
0N/A SnmpOid next = index;
0N/A do {
0N/A next = handler.getNext(next);
0N/A final Object value = handler.getData(next);
0N/A if (value instanceof GarbageCollectorMXBean)
0N/A // That's the next! return it
0N/A return next;
0N/A // skip to next index...
0N/A } while (next != null);
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the data associated with the given index.
0N/A * If the given index is not found, null is returned.
0N/A * Note that returning null does not necessarily means that
0N/A * the index was not found.
0N/A **/
0N/A public Object getData(SnmpTableHandler handler, SnmpOid index) {
0N/A final Object value = handler.getData(index);
0N/A if (value instanceof GarbageCollectorMXBean) return value;
0N/A // Behaves as if there was nothing at this index...
0N/A //
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the given <var>index</var> is present.
0N/A **/
0N/A public boolean contains(SnmpTableHandler handler, SnmpOid index) {
0N/A if (handler.getData(index) instanceof GarbageCollectorMXBean)
0N/A return true;
0N/A // Behaves as if there was nothing at this index...
0N/A //
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A
0N/A private transient JvmMemManagerTableMetaImpl managers = null;
0N/A private static GCTableFilter filter = new GCTableFilter();
0N/A
0N/A
0N/A /**
0N/A * Constructor for the table. Initialize metadata for "JvmMemGCTableMeta".
0N/A */
0N/A public JvmMemGCTableMetaImpl(SnmpMib myMib,
0N/A SnmpStandardObjectServer objserv) {
0N/A super(myMib,objserv);
0N/A }
0N/A
0N/A // Returns a pointer to the JvmMemManager meta node - we're going
0N/A // to reuse its SnmpTableHandler by filtering out all that is
0N/A // not a GarbageCollectorMXBean.
0N/A private final JvmMemManagerTableMetaImpl getManagers(SnmpMib mib) {
0N/A if (managers == null) {
0N/A managers = (JvmMemManagerTableMetaImpl)
0N/A mib.getRegisteredTableMeta("JvmMemManagerTable");
0N/A }
0N/A return managers;
0N/A }
0N/A
0N/A /**
0N/A * Returns the JvmMemManagerTable SnmpTableHandler
0N/A **/
0N/A protected SnmpTableHandler getHandler(Object userData) {
0N/A JvmMemManagerTableMetaImpl managerTable= getManagers(theMib);
0N/A return managerTable.getHandler(userData);
0N/A }
0N/A
0N/A // See com.sun.jmx.snmp.agent.SnmpMibTable
0N/A protected SnmpOid getNextOid(Object userData)
0N/A throws SnmpStatusException {
0N/A // null means get the first OID.
0N/A return getNextOid(null,userData);
0N/A }
0N/A
0N/A // See com.sun.jmx.snmp.agent.SnmpMibTable
0N/A protected SnmpOid getNextOid(SnmpOid oid, Object userData)
0N/A throws SnmpStatusException {
0N/A final boolean dbg = log.isDebugOn();
0N/A try {
0N/A if (dbg) log.debug("getNextOid", "previous=" + oid);
0N/A
0N/A // Get the data handler.
0N/A //
0N/A SnmpTableHandler handler = getHandler(userData);
0N/A if (handler == null) {
0N/A // This should never happen.
0N/A // If we get here it's a bug.
0N/A //
0N/A if (dbg) log.debug("getNextOid", "handler is null!");
0N/A throw new
0N/A SnmpStatusException(SnmpStatusException.noSuchInstance);
0N/A }
0N/A
0N/A
0N/A // Get the next oid, using the GC filter.
0N/A //
0N/A final SnmpOid next = filter.getNext(handler,oid);
0N/A if (dbg) log.debug("getNextOid", "next=" + next);
0N/A
0N/A // if next is null: we reached the end of the table.
0N/A //
0N/A if (next == null)
0N/A throw new
0N/A SnmpStatusException(SnmpStatusException.noSuchInstance);
0N/A
0N/A return next;
0N/A } catch (RuntimeException x) {
0N/A // debug. This should never happen.
0N/A //
0N/A if (dbg) log.debug("getNextOid",x);
0N/A throw x;
0N/A }
0N/A }
0N/A
0N/A
0N/A // See com.sun.jmx.snmp.agent.SnmpMibTable
0N/A protected boolean contains(SnmpOid oid, Object userData) {
0N/A // Get the handler.
0N/A //
0N/A SnmpTableHandler handler = getHandler(userData);
0N/A
0N/A // handler should never be null.
0N/A //
0N/A if (handler == null)
0N/A return false;
0N/A return filter.contains(handler,oid);
0N/A }
0N/A
0N/A // See com.sun.jmx.snmp.agent.SnmpMibTable
0N/A public Object getEntry(SnmpOid oid)
0N/A throws SnmpStatusException {
0N/A
0N/A if (oid == null)
0N/A throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
0N/A
0N/A // Get the request contextual cache (userData).
0N/A //
0N/A final Map<Object, Object> m = JvmContextFactory.getUserData();
0N/A
0N/A // First look in the request contextual cache: maybe we've already
0N/A // created this entry...
0N/A //
0N/A
0N/A // We know in the case of this table that the index is an integer,
0N/A // it is thus the first OID arc of the index OID.
0N/A //
0N/A final long index = oid.getOidArc(0);
0N/A
0N/A // We're going to use this name to store/retrieve the entry in
0N/A // the request contextual cache.
0N/A //
0N/A // Revisit: Probably better programming to put all these strings
0N/A // in some interface.
0N/A //
0N/A final String entryTag = ((m==null)?null:("JvmMemGCTable.entry." +
0N/A index));
0N/A
0N/A // If the entry is in the cache, simply return it.
0N/A //
0N/A if (m != null) {
0N/A final Object entry = m.get(entryTag);
0N/A if (entry != null) return entry;
0N/A }
0N/A
0N/A // Entry was not in request cache. Make a new one.
0N/A //
0N/A // Get the data hanler.
0N/A //
0N/A SnmpTableHandler handler = getHandler(m);
0N/A
0N/A // handler should never be null.
0N/A //
0N/A if (handler == null)
0N/A throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
0N/A
0N/A // Use the filter to retrieve only GarabageCollectorMBean data.
0N/A //
0N/A final Object data = filter.getData(handler,oid);
0N/A
0N/A // data may be null if the OID we were given is not valid.
0N/A // (e.g. it identifies a MemoryManager which is not a
0N/A // GarbageCollector)
0N/A //
0N/A if (data == null)
0N/A throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
0N/A
0N/A // Make a new entryy (transient object that will be kept only
0N/A // for the duration of the request.
0N/A //
0N/A final Object entry =
0N/A new JvmMemGCEntryImpl((GarbageCollectorMXBean)data,(int)index);
0N/A
0N/A // Put the entry in the request cache in case we need it later
0N/A // in the processing of the request. Note that we could have
0N/A // optimized this by making JvmMemGCEntryImpl extend
0N/A // JvmMemManagerEntryImpl, and then make sure that
0N/A // JvmMemManagerTableMetaImpl creates an instance of JvmMemGCEntryImpl
0N/A // instead of JvmMemManagerEntryImpl when the associated data is
0N/A // an instance of GarbageCollectorMXBean. This would have made it
0N/A // possible to share the transient entry object.
0N/A // As it is, we may have two transient objects that points to
0N/A // the same underlying MemoryManagerMXBean (which is definitely
0N/A // not a problem - but is only a small dysatisfaction)
0N/A //
0N/A if (m != null && entry != null) {
0N/A m.put(entryTag,entry);
0N/A }
0N/A
0N/A return entry;
0N/A }
0N/A
0N/A static final MibLogger log = new MibLogger(JvmMemGCTableMetaImpl.class);
0N/A}