jdk/src/jdk.snmp/share/classes/sun/management/snmp/jvminstr/JvmThreadInstanceTableMetaImpl.java
changeset 27188 a1aa62221649
parent 27187 020b64fc02df
child 27189 b90845965ee9
equal deleted inserted replaced
27187:020b64fc02df 27188:a1aa62221649
     1 /*
       
     2  * Copyright (c) 2003, 2012, Oracle and/or its affiliates. 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package sun.management.snmp.jvminstr;
       
    26 
       
    27 // java imports
       
    28 //
       
    29 import com.sun.jmx.mbeanserver.Util;
       
    30 import java.io.Serializable;
       
    31 import java.util.Vector;
       
    32 import java.util.Map;
       
    33 import java.util.TreeMap;
       
    34 import java.util.Enumeration;
       
    35 
       
    36 import java.lang.management.ThreadInfo;
       
    37 import java.lang.management.ManagementFactory;
       
    38 
       
    39 // jmx imports
       
    40 //
       
    41 import javax.management.MBeanServer;
       
    42 import javax.management.ObjectName;
       
    43 import com.sun.jmx.snmp.SnmpCounter;
       
    44 import com.sun.jmx.snmp.SnmpCounter64;
       
    45 import com.sun.jmx.snmp.SnmpGauge;
       
    46 import com.sun.jmx.snmp.SnmpInt;
       
    47 import com.sun.jmx.snmp.SnmpUnsignedInt;
       
    48 import com.sun.jmx.snmp.SnmpIpAddress;
       
    49 import com.sun.jmx.snmp.SnmpTimeticks;
       
    50 import com.sun.jmx.snmp.SnmpOpaque;
       
    51 import com.sun.jmx.snmp.SnmpString;
       
    52 import com.sun.jmx.snmp.SnmpStringFixed;
       
    53 import com.sun.jmx.snmp.SnmpOid;
       
    54 import com.sun.jmx.snmp.SnmpNull;
       
    55 import com.sun.jmx.snmp.SnmpValue;
       
    56 import com.sun.jmx.snmp.SnmpVarBind;
       
    57 import com.sun.jmx.snmp.SnmpStatusException;
       
    58 
       
    59 // jdmk imports
       
    60 //
       
    61 import com.sun.jmx.snmp.agent.SnmpIndex;
       
    62 import com.sun.jmx.snmp.agent.SnmpMib;
       
    63 import com.sun.jmx.snmp.agent.SnmpMibTable;
       
    64 import com.sun.jmx.snmp.agent.SnmpMibSubRequest;
       
    65 import com.sun.jmx.snmp.agent.SnmpStandardObjectServer;
       
    66 
       
    67 import sun.management.snmp.jvmmib.JvmThreadInstanceEntryMBean;
       
    68 import sun.management.snmp.jvmmib.JvmThreadInstanceTableMeta;
       
    69 import sun.management.snmp.util.SnmpTableCache;
       
    70 import sun.management.snmp.util.SnmpCachedData;
       
    71 import sun.management.snmp.util.SnmpTableHandler;
       
    72 import sun.management.snmp.util.MibLogger;
       
    73 import sun.management.snmp.util.JvmContextFactory;
       
    74 
       
    75 /**
       
    76  * The class is used for implementing the "JvmThreadInstanceTable" group.
       
    77  */
       
    78 public class JvmThreadInstanceTableMetaImpl
       
    79     extends JvmThreadInstanceTableMeta {
       
    80 
       
    81     static final long serialVersionUID = -8432271929226397492L;
       
    82 
       
    83     /**
       
    84      * Maximum depth of the stacktrace that might be returned through
       
    85      * SNMP.
       
    86      *
       
    87      * Since we do not export the stack trace through SNMP, we set
       
    88      * MAX_STACK_TRACE_DEPTH=0 so that ThreadMXBean.getThreadInfo(long) does
       
    89      * not compute the stack trace.
       
    90      *
       
    91      **/
       
    92     public static final int MAX_STACK_TRACE_DEPTH=0;
       
    93 
       
    94     /**
       
    95      * Translate from a long to a Oid. Arc follow the long big-endian order.
       
    96      * @param l The long to make the index from
       
    97      * @return The arc array.
       
    98      */
       
    99     static SnmpOid makeOid(long l) {
       
   100         long[] x =  new long [8];
       
   101         x[0] = (l >> 56) & 0xFF;
       
   102         x[1] =  (l >> 48) & 0x00FF;
       
   103         x[2] =  (l >> 40) & 0x0000FF;
       
   104         x[3] =  (l >> 32) & 0x000000FF;
       
   105         x[4] =  (l >> 24) & 0x00000000FF;
       
   106         x[5] =  (l >> 16) & 0x0000000000FF;
       
   107         x[6] =  (l >> 8)  & 0x000000000000FF;
       
   108         x[7] =  l         & 0x00000000000000FF;
       
   109         return new SnmpOid(x);
       
   110     }
       
   111 
       
   112     /**
       
   113      * Translate an Oid to a thread id. Arc follow the long big-endian order.
       
   114      * @param oid The oid to make the id from
       
   115      * @return The thread id.
       
   116      */
       
   117     static long makeId(SnmpOid oid) {
       
   118         long id = 0;
       
   119         long[] arcs = oid.longValue(false);
       
   120 
       
   121         id |= arcs[0] << 56;
       
   122         id |= arcs[1] << 48;
       
   123         id |= arcs[2] << 40;
       
   124         id |= arcs[3] << 32;
       
   125         id |= arcs[4] << 24;
       
   126         id |= arcs[5] << 16;
       
   127         id |= arcs[6] << 8;
       
   128         id |= arcs[7];
       
   129 
       
   130         return id;
       
   131     }
       
   132 
       
   133     /**
       
   134      * A concrete implementation of {@link SnmpTableCache}, for the
       
   135      * JvmThreadInstanceTable.
       
   136      **/
       
   137     private static class JvmThreadInstanceTableCache
       
   138         extends SnmpTableCache {
       
   139 
       
   140         static final long serialVersionUID = 4947330124563406878L;
       
   141         final private JvmThreadInstanceTableMetaImpl meta;
       
   142 
       
   143         /**
       
   144          * Create a weak cache for the JvmThreadInstanceTable.
       
   145          * @param validity validity of the cached data, in ms.
       
   146          **/
       
   147         JvmThreadInstanceTableCache(JvmThreadInstanceTableMetaImpl meta,
       
   148                                    long validity) {
       
   149             this.validity = validity;
       
   150             this.meta     = meta;
       
   151         }
       
   152 
       
   153         /**
       
   154          * Call <code>getTableDatas(JvmContextFactory.getUserData())</code>.
       
   155          **/
       
   156         public SnmpTableHandler getTableHandler() {
       
   157             final Map<Object, Object> userData = JvmContextFactory.getUserData();
       
   158             return getTableDatas(userData);
       
   159         }
       
   160 
       
   161         /**
       
   162          * Return a table handler containing the Thread indexes.
       
   163          * Indexes are computed from the ThreadId.
       
   164          **/
       
   165         protected SnmpCachedData updateCachedDatas(Object userData) {
       
   166 
       
   167             // We are getting all the thread ids. WARNING.
       
   168             // Some of them will be not valid when accessed for data...
       
   169             // See getEntry
       
   170             long[] id = JvmThreadingImpl.getThreadMXBean().getAllThreadIds();
       
   171 
       
   172 
       
   173             // Time stamp for the cache
       
   174             final long time = System.currentTimeMillis();
       
   175 
       
   176             SnmpOid indexes[] = new SnmpOid[id.length];
       
   177             final TreeMap<SnmpOid, Object> table =
       
   178                     new TreeMap<>(SnmpCachedData.oidComparator);
       
   179             for(int i = 0; i < id.length; i++) {
       
   180                 log.debug("", "Making index for thread id [" + id[i] +"]");
       
   181                 //indexes[i] = makeOid(id[i]);
       
   182                 SnmpOid oid = makeOid(id[i]);
       
   183                 table.put(oid, oid);
       
   184             }
       
   185 
       
   186             return new SnmpCachedData(time, table);
       
   187         }
       
   188 
       
   189     }
       
   190 
       
   191 
       
   192     // The weak cache for this table.
       
   193     protected SnmpTableCache cache;
       
   194 
       
   195     /**
       
   196      * Constructor for the table. Initialize metadata for
       
   197      * "JvmThreadInstanceTableMeta".
       
   198      * The reference on the MBean server is updated so the entries created
       
   199      * through an SNMP SET will be AUTOMATICALLY REGISTERED in Java DMK.
       
   200      */
       
   201     public JvmThreadInstanceTableMetaImpl(SnmpMib myMib,
       
   202                                           SnmpStandardObjectServer objserv) {
       
   203         super(myMib, objserv);
       
   204         cache = new JvmThreadInstanceTableCache(this,
       
   205                             ((JVM_MANAGEMENT_MIB_IMPL)myMib).validity());
       
   206         log.debug("JvmThreadInstanceTableMetaImpl", "Create Thread meta");
       
   207     }
       
   208 
       
   209     // See com.sun.jmx.snmp.agent.SnmpMibTable
       
   210     protected SnmpOid getNextOid(Object userData)
       
   211         throws SnmpStatusException {
       
   212         log.debug("JvmThreadInstanceTableMetaImpl", "getNextOid");
       
   213         // null means get the first OID.
       
   214         return getNextOid(null,userData);
       
   215     }
       
   216 
       
   217     // See com.sun.jmx.snmp.agent.SnmpMibTable
       
   218     protected SnmpOid getNextOid(SnmpOid oid, Object userData)
       
   219         throws SnmpStatusException {
       
   220         log.debug("getNextOid", "previous=" + oid);
       
   221 
       
   222 
       
   223         // Get the data handler.
       
   224         //
       
   225         SnmpTableHandler handler = getHandler(userData);
       
   226         if (handler == null) {
       
   227             // This should never happen.
       
   228             // If we get here it's a bug.
       
   229             //
       
   230             log.debug("getNextOid", "handler is null!");
       
   231             throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
       
   232         }
       
   233 
       
   234         // Get the next oid
       
   235         //
       
   236         SnmpOid next = oid;
       
   237         while(true) {
       
   238             next = handler.getNext(next);
       
   239             if (next == null) break;
       
   240             if (getJvmThreadInstance(userData,next) != null) break;
       
   241         }
       
   242 
       
   243         log.debug("*** **** **** **** getNextOid", "next=" + next);
       
   244 
       
   245         // if next is null: we reached the end of the table.
       
   246         //
       
   247         if (next == null)
       
   248             throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
       
   249 
       
   250         return next;
       
   251     }
       
   252 
       
   253     // See com.sun.jmx.snmp.agent.SnmpMibTable
       
   254     protected boolean contains(SnmpOid oid, Object userData) {
       
   255 
       
   256         // Get the handler.
       
   257         //
       
   258         SnmpTableHandler handler = getHandler(userData);
       
   259 
       
   260         // handler should never be null.
       
   261         //
       
   262         if (handler == null)
       
   263             return false;
       
   264         if(!handler.contains(oid))
       
   265             return false;
       
   266 
       
   267         JvmThreadInstanceEntryImpl inst = getJvmThreadInstance(userData, oid);
       
   268         return (inst != null);
       
   269     }
       
   270 
       
   271 
       
   272     // See com.sun.jmx.snmp.agent.SnmpMibTable
       
   273     public Object getEntry(SnmpOid oid)
       
   274         throws SnmpStatusException {
       
   275         log.debug("*** **** **** **** getEntry", "oid [" + oid + "]");
       
   276         if (oid == null || oid.getLength() != 8) {
       
   277             log.debug("getEntry", "Invalid oid [" + oid + "]");
       
   278             throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
       
   279         }
       
   280 
       
   281         // Get the request contextual cache (userData).
       
   282         //
       
   283         final Map<Object,Object> m = JvmContextFactory.getUserData();
       
   284 
       
   285         // Get the handler.
       
   286         //
       
   287         SnmpTableHandler handler = getHandler(m);
       
   288 
       
   289         // handler should never be null.
       
   290         //
       
   291         if (handler == null || !handler.contains(oid))
       
   292             throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
       
   293 
       
   294         final JvmThreadInstanceEntryImpl entry = getJvmThreadInstance(m,oid);
       
   295 
       
   296         if (entry == null)
       
   297             throw new SnmpStatusException(SnmpStatusException.noSuchInstance);
       
   298 
       
   299         return entry;
       
   300     }
       
   301 
       
   302     /**
       
   303      * Get the SnmpTableHandler that holds the jvmThreadInstanceTable data.
       
   304      * First look it up in the request contextual cache, and if it is
       
   305      * not found, obtain it from the weak cache.
       
   306      * <br>The request contextual cache will be released at the end of the
       
   307      * current requests, and is used only to process this request.
       
   308      * <br>The weak cache is shared by all requests, and is only
       
   309      * recomputed when it is found to be obsolete.
       
   310      * <br>Note that the data put in the request contextual cache is
       
   311      *     never considered to be obsolete, in order to preserve data
       
   312      *     coherency.
       
   313      **/
       
   314     protected SnmpTableHandler getHandler(Object userData) {
       
   315         final Map<Object, Object> m;
       
   316         if (userData instanceof Map) m=Util.cast(userData);
       
   317         else m=null;
       
   318 
       
   319         // Look in the contextual cache.
       
   320         if (m != null) {
       
   321             final SnmpTableHandler handler =
       
   322                 (SnmpTableHandler)m.get("JvmThreadInstanceTable.handler");
       
   323             if (handler != null) return handler;
       
   324         }
       
   325 
       
   326         // No handler in contextual cache, make a new one.
       
   327         final SnmpTableHandler handler = cache.getTableHandler();
       
   328 
       
   329         if (m != null && handler != null )
       
   330             m.put("JvmThreadInstanceTable.handler",handler);
       
   331 
       
   332         return handler;
       
   333     }
       
   334 
       
   335     private ThreadInfo getThreadInfo(long id) {
       
   336         return JvmThreadingImpl.getThreadMXBean().
       
   337                   getThreadInfo(id,MAX_STACK_TRACE_DEPTH);
       
   338     }
       
   339 
       
   340     private ThreadInfo getThreadInfo(SnmpOid oid) {
       
   341         return getThreadInfo(makeId(oid));
       
   342     }
       
   343 
       
   344     private JvmThreadInstanceEntryImpl getJvmThreadInstance(Object userData,
       
   345                                                             SnmpOid oid) {
       
   346         JvmThreadInstanceEntryImpl cached = null;
       
   347         String entryTag = null;
       
   348         Map<Object, Object> map = null;
       
   349         final boolean dbg = log.isDebugOn();
       
   350 
       
   351         if (userData instanceof Map) {
       
   352             map = Util.cast(userData);
       
   353 
       
   354             // We're going to use this name to store/retrieve the entry in
       
   355             // the request contextual cache.
       
   356             //
       
   357             // Revisit: Probably better programming to put all these strings
       
   358             //          in some interface.
       
   359             //
       
   360             entryTag = "JvmThreadInstanceTable.entry." + oid.toString();
       
   361 
       
   362             cached = (JvmThreadInstanceEntryImpl) map.get(entryTag);
       
   363         }
       
   364 
       
   365         // If the entry is in the cache, simply return it.
       
   366         //
       
   367         if (cached != null) {
       
   368             if (dbg) log.debug("*** getJvmThreadInstance",
       
   369                                "Entry found in cache: " + entryTag);
       
   370             return cached;
       
   371         }
       
   372 
       
   373         if (dbg) log.debug("*** getJvmThreadInstance", "Entry [" +
       
   374                            oid + "] is not in cache");
       
   375 
       
   376         // Entry not in cache. We will create one if needed.
       
   377         //
       
   378         ThreadInfo info = null;
       
   379         try {
       
   380             info = getThreadInfo(oid);
       
   381         } catch (RuntimeException r) {
       
   382             log.trace("*** getJvmThreadInstance",
       
   383                       "Failed to get thread info for rowOid: " + oid);
       
   384             log.debug("*** getJvmThreadInstance",r);
       
   385         }
       
   386 
       
   387         // No thread by that id => no entry.
       
   388         //
       
   389         if(info == null) {
       
   390             if (dbg) log.debug("*** getJvmThreadInstance",
       
   391                                "No entry by that oid [" + oid + "]");
       
   392             return null;
       
   393         }
       
   394 
       
   395         cached = new JvmThreadInstanceEntryImpl(info, oid.toByte());
       
   396         if (map != null) map.put(entryTag, cached);
       
   397         if (dbg) log.debug("*** getJvmThreadInstance",
       
   398                            "Entry created for Thread OID [" + oid + "]");
       
   399         return cached;
       
   400     }
       
   401 
       
   402      static final MibLogger log =
       
   403         new MibLogger(JvmThreadInstanceTableMetaImpl.class);
       
   404 }