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