0N/A/*
2362N/A * Copyright (c) 2003, 2004, 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// java imports
0N/A//
0N/Aimport java.io.Serializable;
0N/Aimport java.lang.management.ThreadInfo;
0N/Aimport java.lang.management.ManagementFactory;
0N/Aimport java.lang.management.ThreadMXBean;
0N/A
0N/A// jmx imports
0N/A//
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.SnmpOid;
0N/Aimport com.sun.jmx.snmp.SnmpDefinitions;
0N/Aimport com.sun.jmx.snmp.SnmpOidTable;
0N/Aimport com.sun.jmx.snmp.SnmpOidRecord;
0N/A
0N/Aimport sun.management.snmp.jvmmib.JvmThreadInstanceEntryMBean;
0N/Aimport sun.management.snmp.jvmmib.JVM_MANAGEMENT_MIBOidTable;
0N/Aimport sun.management.snmp.util.MibLogger;
0N/A
0N/A/**
0N/A * The class is used for implementing the "JvmThreadInstanceEntry" group.
0N/A */
0N/Apublic class JvmThreadInstanceEntryImpl
0N/A implements JvmThreadInstanceEntryMBean, Serializable {
0N/A
0N/A public final static class ThreadStateMap {
0N/A public final static class Byte0 {
0N/A public final static byte inNative = (byte)0x80; // bit 1
0N/A public final static byte suspended = (byte)0x40; // bit 2
0N/A public final static byte newThread = (byte)0x20; // bit 3
0N/A public final static byte runnable = (byte)0x10; // bit 4
0N/A public final static byte blocked = (byte)0x08; // bit 5
0N/A public final static byte terminated = (byte)0x04; // bit 6
0N/A public final static byte waiting = (byte)0x02; // bit 7
0N/A public final static byte timedWaiting = (byte)0x01; // bit 8
0N/A }
0N/A public final static class Byte1 {
0N/A public final static byte other = (byte)0x80; // bit 9
0N/A public final static byte reserved10 = (byte)0x40; // bit 10
0N/A public final static byte reserved11 = (byte)0x20; // bit 11
0N/A public final static byte reserved12 = (byte)0x10; // bit 12
0N/A public final static byte reserved13 = (byte)0x08; // bit 13
0N/A public final static byte reserved14 = (byte)0x04; // bit 14
0N/A public final static byte reserved15 = (byte)0x02; // bit 15
0N/A public final static byte reserved16 = (byte)0x01; // bit 16
0N/A }
0N/A
0N/A public final static byte mask0 = (byte)0x3F;
0N/A public final static byte mask1 = (byte)0x80;
0N/A
0N/A private static void setBit(byte[] bitmap, int index, byte state) {
0N/A bitmap[index] = (byte) (bitmap[index] | state);
0N/A }
0N/A public static void setNative(byte[] bitmap) {
0N/A setBit(bitmap,0,Byte0.inNative);
0N/A }
0N/A public static void setSuspended(byte[] bitmap) {
0N/A setBit(bitmap,0,Byte0.suspended);
0N/A }
0N/A public static void setState(byte[] bitmap, Thread.State state) {
0N/A switch(state) {
0N/A case BLOCKED:
0N/A setBit(bitmap,0,Byte0.blocked);
0N/A return;
0N/A case NEW:
0N/A setBit(bitmap,0,Byte0.newThread);
0N/A return;
0N/A case RUNNABLE:
0N/A setBit(bitmap,0,Byte0.runnable);
0N/A return;
0N/A case TERMINATED:
0N/A setBit(bitmap,0,Byte0.terminated);
0N/A return;
0N/A case TIMED_WAITING:
0N/A setBit(bitmap,0,Byte0.timedWaiting);
0N/A return;
0N/A case WAITING:
0N/A setBit(bitmap,0,Byte0.waiting);
0N/A return;
0N/A }
0N/A }
0N/A
0N/A public static void checkOther(byte[] bitmap) {
0N/A if (((bitmap[0]&mask0)==(byte)0x00) &&
0N/A ((bitmap[1]&mask1)==(byte)0x00))
0N/A setBit(bitmap,1,Byte1.other);
0N/A }
0N/A
0N/A public static Byte[] getState(ThreadInfo info) {
0N/A byte[] bitmap = new byte[] {(byte)0x00, (byte)0x00};
0N/A try {
0N/A final Thread.State state = info.getThreadState();
0N/A final boolean inNative = info.isInNative();
0N/A final boolean suspended = info.isSuspended();
0N/A log.debug("getJvmThreadInstState",
0N/A "[State=" + state +
0N/A ",isInNative=" + inNative +
0N/A ",isSuspended=" + suspended + "]");
0N/A setState(bitmap,state);
0N/A if (inNative) setNative(bitmap);
0N/A if (suspended) setSuspended(bitmap);
0N/A checkOther(bitmap);
0N/A } catch (RuntimeException r) {
0N/A bitmap[0]=(byte)0x00;
0N/A bitmap[1]=Byte1.other;
0N/A log.trace("getJvmThreadInstState",
0N/A "Unexpected exception: " + r);
0N/A log.debug("getJvmThreadInstState",r);
0N/A }
0N/A Byte[] result = { new Byte(bitmap[0]), new Byte(bitmap[1]) };
0N/A return result;
0N/A }
0N/A }
0N/A
0N/A private final ThreadInfo info;
0N/A private final Byte[] index;
0N/A
0N/A /**
0N/A * Constructor for the "JvmThreadInstanceEntry" group.
0N/A */
0N/A public JvmThreadInstanceEntryImpl(ThreadInfo info,
0N/A Byte[] index) {
0N/A this.info = info;
0N/A this.index = index;
0N/A }
0N/A
0N/A
0N/A private static String jvmThreadInstIndexOid = null;
0N/A public static String getJvmThreadInstIndexOid()
0N/A throws SnmpStatusException {
0N/A if (jvmThreadInstIndexOid == null) {
0N/A final SnmpOidTable table = new JVM_MANAGEMENT_MIBOidTable();
0N/A final SnmpOidRecord record =
0N/A table.resolveVarName("jvmThreadInstIndex");
0N/A jvmThreadInstIndexOid = record.getOid();
0N/A }
0N/A return jvmThreadInstIndexOid;
0N/A }
0N/A
0N/A
0N/A
0N/A /**
0N/A * Getter for the "JvmThreadInstLockedOwnerId" variable.
0N/A */
0N/A public String getJvmThreadInstLockOwnerPtr() throws SnmpStatusException {
0N/A long id = info.getLockOwnerId();
0N/A
0N/A if(id == -1)
0N/A return new String("0.0");
0N/A
0N/A SnmpOid oid = JvmThreadInstanceTableMetaImpl.makeOid(id);
0N/A
0N/A return getJvmThreadInstIndexOid() + "." + oid.toString();
0N/A }
0N/A
0N/A private String validDisplayStringTC(String str) {
0N/A return JVM_MANAGEMENT_MIB_IMPL.validDisplayStringTC(str);
0N/A }
0N/A
0N/A private String validJavaObjectNameTC(String str) {
0N/A return JVM_MANAGEMENT_MIB_IMPL.validJavaObjectNameTC(str);
0N/A }
0N/A
0N/A private String validPathElementTC(String str) {
0N/A return JVM_MANAGEMENT_MIB_IMPL.validPathElementTC(str);
0N/A }
0N/A
0N/A /**
0N/A * Getter for the "JvmThreadInstLockName" variable.
0N/A */
0N/A public String getJvmThreadInstLockName() throws SnmpStatusException {
0N/A return validJavaObjectNameTC(info.getLockName());
0N/A }
0N/A
0N/A /**
0N/A * Getter for the "JvmThreadInstName" variable.
0N/A */
0N/A public String getJvmThreadInstName() throws SnmpStatusException {
0N/A return validJavaObjectNameTC(info.getThreadName());
0N/A }
0N/A
0N/A /**
0N/A * Getter for the "JvmThreadInstCpuTimeNs" variable.
0N/A */
0N/A public Long getJvmThreadInstCpuTimeNs() throws SnmpStatusException {
0N/A long l = 0;
0N/A final ThreadMXBean tmb = JvmThreadingImpl.getThreadMXBean();
0N/A
0N/A try {
0N/A if (tmb.isThreadCpuTimeSupported()) {
0N/A l = tmb.getThreadCpuTime(info.getThreadId());
0N/A log.debug("getJvmThreadInstCpuTimeNs", "Cpu time ns : " + l);
0N/A
0N/A //Cpu time measurement is disabled or the id is not valid.
0N/A if(l == -1) l = 0;
0N/A }
0N/A } catch (UnsatisfiedLinkError e) {
0N/A // XXX Revisit: catch TO BE EVENTUALLY REMOVED
0N/A log.debug("getJvmThreadInstCpuTimeNs",
0N/A "Operation not supported: " + e);
0N/A }
0N/A return new Long(l);
0N/A }
0N/A
0N/A /**
0N/A * Getter for the "JvmThreadInstBlockTimeMs" variable.
0N/A */
0N/A public Long getJvmThreadInstBlockTimeMs() throws SnmpStatusException {
0N/A long l = 0;
0N/A
0N/A final ThreadMXBean tmb = JvmThreadingImpl.getThreadMXBean();
0N/A
0N/A if (tmb.isThreadContentionMonitoringSupported()) {
0N/A l = info.getBlockedTime();
0N/A
0N/A //Monitoring is disabled
0N/A if(l == -1) l = 0;
0N/A }
0N/A return new Long(l);
0N/A }
0N/A
0N/A /**
0N/A * Getter for the "JvmThreadInstBlockCount" variable.
0N/A */
0N/A public Long getJvmThreadInstBlockCount() throws SnmpStatusException {
0N/A return new Long(info.getBlockedCount());
0N/A }
0N/A
0N/A /**
0N/A * Getter for the "JvmThreadInstWaitTimeMs" variable.
0N/A */
0N/A public Long getJvmThreadInstWaitTimeMs() throws SnmpStatusException {
0N/A long l = 0;
0N/A
0N/A final ThreadMXBean tmb = JvmThreadingImpl.getThreadMXBean();
0N/A
0N/A if (tmb.isThreadContentionMonitoringSupported()) {
0N/A l = info.getWaitedTime();
0N/A
0N/A //Monitoring is disabled
0N/A if(l == -1) l = 0;
0N/A }
0N/A return new Long(l);
0N/A }
0N/A
0N/A /**
0N/A * Getter for the "JvmThreadInstWaitCount" variable.
0N/A */
0N/A public Long getJvmThreadInstWaitCount() throws SnmpStatusException {
0N/A return new Long(info.getWaitedCount());
0N/A }
0N/A
0N/A /**
0N/A * Getter for the "JvmThreadInstState" variable.
0N/A */
0N/A public Byte[] getJvmThreadInstState()
0N/A throws SnmpStatusException {
0N/A return ThreadStateMap.getState(info);
0N/A }
0N/A
0N/A /**
0N/A * Getter for the "JvmThreadInstId" variable.
0N/A */
0N/A public Long getJvmThreadInstId() throws SnmpStatusException {
0N/A return new Long(info.getThreadId());
0N/A }
0N/A
0N/A /**
0N/A * Getter for the "JvmThreadInstIndex" variable.
0N/A */
0N/A public Byte[] getJvmThreadInstIndex() throws SnmpStatusException {
0N/A return index;
0N/A }
0N/A
0N/A /**
0N/A * Getter for the "JvmThreadInstStackTrace" variable.
0N/A */
0N/A private String getJvmThreadInstStackTrace() throws SnmpStatusException {
0N/A StackTraceElement[] stackTrace = info.getStackTrace();
0N/A //We append the stack trace in a buffer
0N/A // XXX Revisit: should check isDebugOn()
0N/A StringBuffer b = new StringBuffer();
0N/A final int stackSize = stackTrace.length;
0N/A log.debug("getJvmThreadInstStackTrace", "Stack size : " + stackSize);
0N/A for(int i = 0; i < stackSize; i++) {
0N/A log.debug("getJvmThreadInstStackTrace", "Append " +
0N/A stackTrace[i].toString());
0N/A b.append(stackTrace[i].toString());
0N/A //Append \n at the end of each line except the last one
0N/A if(i < stackSize)
0N/A b.append("\n");
0N/A }
0N/A //The stack trace is truncated if its size exceeds 255.
0N/A return validPathElementTC(b.toString());
0N/A }
0N/A static final MibLogger log =
0N/A new MibLogger(JvmThreadInstanceEntryImpl.class);
0N/A}