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