Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/management/ThreadInfoCompositeData.java
38827 views
/*1* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.management;2627import java.lang.management.ThreadInfo;28import java.lang.management.MonitorInfo;29import java.lang.management.LockInfo;30import javax.management.openmbean.CompositeType;31import javax.management.openmbean.CompositeData;32import javax.management.openmbean.CompositeDataSupport;33import javax.management.openmbean.OpenDataException;34import javax.management.openmbean.OpenType;3536/**37* A CompositeData for ThreadInfo for the local management support.38* This class avoids the performance penalty paid to the39* construction of a CompositeData use in the local case.40*/41public class ThreadInfoCompositeData extends LazyCompositeData {42private final ThreadInfo threadInfo;43private final CompositeData cdata;44private final boolean currentVersion;4546private ThreadInfoCompositeData(ThreadInfo ti) {47this.threadInfo = ti;48this.currentVersion = true;49this.cdata = null;50}5152private ThreadInfoCompositeData(CompositeData cd) {53this.threadInfo = null;54this.currentVersion = ThreadInfoCompositeData.isCurrentVersion(cd);55this.cdata = cd;56}5758public ThreadInfo getThreadInfo() {59return threadInfo;60}6162public boolean isCurrentVersion() {63return currentVersion;64}6566public static ThreadInfoCompositeData getInstance(CompositeData cd) {67validateCompositeData(cd);68return new ThreadInfoCompositeData(cd);69}7071public static CompositeData toCompositeData(ThreadInfo ti) {72ThreadInfoCompositeData ticd = new ThreadInfoCompositeData(ti);73return ticd.getCompositeData();74}7576protected CompositeData getCompositeData() {77// Convert StackTraceElement[] to CompositeData[]78StackTraceElement[] stackTrace = threadInfo.getStackTrace();79CompositeData[] stackTraceData =80new CompositeData[stackTrace.length];81for (int i = 0; i < stackTrace.length; i++) {82StackTraceElement ste = stackTrace[i];83stackTraceData[i] = StackTraceElementCompositeData.toCompositeData(ste);84}8586// Convert MonitorInfo[] and LockInfo[] to CompositeData[]87CompositeData lockInfoData =88LockInfoCompositeData.toCompositeData(threadInfo.getLockInfo());8990// Convert LockInfo[] and MonitorInfo[] to CompositeData[]91LockInfo[] lockedSyncs = threadInfo.getLockedSynchronizers();92CompositeData[] lockedSyncsData =93new CompositeData[lockedSyncs.length];94for (int i = 0; i < lockedSyncs.length; i++) {95LockInfo li = lockedSyncs[i];96lockedSyncsData[i] = LockInfoCompositeData.toCompositeData(li);97}9899MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors();100CompositeData[] lockedMonitorsData =101new CompositeData[lockedMonitors.length];102for (int i = 0; i < lockedMonitors.length; i++) {103MonitorInfo mi = lockedMonitors[i];104lockedMonitorsData[i] = MonitorInfoCompositeData.toCompositeData(mi);105}106107// CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH108// threadInfoItemNames!109final Object[] threadInfoItemValues = {110new Long(threadInfo.getThreadId()),111threadInfo.getThreadName(),112threadInfo.getThreadState().name(),113new Long(threadInfo.getBlockedTime()),114new Long(threadInfo.getBlockedCount()),115new Long(threadInfo.getWaitedTime()),116new Long(threadInfo.getWaitedCount()),117lockInfoData,118threadInfo.getLockName(),119new Long(threadInfo.getLockOwnerId()),120threadInfo.getLockOwnerName(),121stackTraceData,122new Boolean(threadInfo.isSuspended()),123new Boolean(threadInfo.isInNative()),124lockedMonitorsData,125lockedSyncsData,126};127128try {129return new CompositeDataSupport(threadInfoCompositeType,130threadInfoItemNames,131threadInfoItemValues);132} catch (OpenDataException e) {133// Should never reach here134throw new AssertionError(e);135}136}137138// Attribute names139private static final String THREAD_ID = "threadId";140private static final String THREAD_NAME = "threadName";141private static final String THREAD_STATE = "threadState";142private static final String BLOCKED_TIME = "blockedTime";143private static final String BLOCKED_COUNT = "blockedCount";144private static final String WAITED_TIME = "waitedTime";145private static final String WAITED_COUNT = "waitedCount";146private static final String LOCK_INFO = "lockInfo";147private static final String LOCK_NAME = "lockName";148private static final String LOCK_OWNER_ID = "lockOwnerId";149private static final String LOCK_OWNER_NAME = "lockOwnerName";150private static final String STACK_TRACE = "stackTrace";151private static final String SUSPENDED = "suspended";152private static final String IN_NATIVE = "inNative";153private static final String LOCKED_MONITORS = "lockedMonitors";154private static final String LOCKED_SYNCS = "lockedSynchronizers";155156private static final String[] threadInfoItemNames = {157THREAD_ID,158THREAD_NAME,159THREAD_STATE,160BLOCKED_TIME,161BLOCKED_COUNT,162WAITED_TIME,163WAITED_COUNT,164LOCK_INFO,165LOCK_NAME,166LOCK_OWNER_ID,167LOCK_OWNER_NAME,168STACK_TRACE,169SUSPENDED,170IN_NATIVE,171LOCKED_MONITORS,172LOCKED_SYNCS,173};174175// New attributes added in 6.0 ThreadInfo176private static final String[] threadInfoV6Attributes = {177LOCK_INFO,178LOCKED_MONITORS,179LOCKED_SYNCS,180};181182// Current version of ThreadInfo183private static final CompositeType threadInfoCompositeType;184// Previous version of ThreadInfo185private static final CompositeType threadInfoV5CompositeType;186private static final CompositeType lockInfoCompositeType;187static {188try {189threadInfoCompositeType = (CompositeType)190MappedMXBeanType.toOpenType(ThreadInfo.class);191// Form a CompositeType for JDK 5.0 ThreadInfo version192String[] itemNames =193threadInfoCompositeType.keySet().toArray(new String[0]);194int numV5Attributes = threadInfoItemNames.length -195threadInfoV6Attributes.length;196String[] v5ItemNames = new String[numV5Attributes];197String[] v5ItemDescs = new String[numV5Attributes];198OpenType<?>[] v5ItemTypes = new OpenType<?>[numV5Attributes];199int i = 0;200for (String n : itemNames) {201if (isV5Attribute(n)) {202v5ItemNames[i] = n;203v5ItemDescs[i] = threadInfoCompositeType.getDescription(n);204v5ItemTypes[i] = threadInfoCompositeType.getType(n);205i++;206}207}208209threadInfoV5CompositeType =210new CompositeType("java.lang.management.ThreadInfo",211"J2SE 5.0 java.lang.management.ThreadInfo",212v5ItemNames,213v5ItemDescs,214v5ItemTypes);215} catch (OpenDataException e) {216// Should never reach here217throw new AssertionError(e);218}219220// Each CompositeData object has its CompositeType associated221// with it. So we can get the CompositeType representing LockInfo222// from a mapped CompositeData for any LockInfo object.223// Thus we construct a random LockInfo object and pass it224// to LockInfoCompositeData to do the conversion.225Object o = new Object();226LockInfo li = new LockInfo(o.getClass().getName(),227System.identityHashCode(o));228CompositeData cd = LockInfoCompositeData.toCompositeData(li);229lockInfoCompositeType = cd.getCompositeType();230}231232private static boolean isV5Attribute(String itemName) {233for (String n : threadInfoV6Attributes) {234if (itemName.equals(n)) {235return false;236}237}238return true;239}240241public static boolean isCurrentVersion(CompositeData cd) {242if (cd == null) {243throw new NullPointerException("Null CompositeData");244}245246return isTypeMatched(threadInfoCompositeType, cd.getCompositeType());247}248249public long threadId() {250return getLong(cdata, THREAD_ID);251}252253public String threadName() {254// The ThreadName item cannot be null so we check that255// it is present with a non-null value.256String name = getString(cdata, THREAD_NAME);257if (name == null) {258throw new IllegalArgumentException("Invalid composite data: " +259"Attribute " + THREAD_NAME + " has null value");260}261return name;262}263264public Thread.State threadState() {265return Thread.State.valueOf(getString(cdata, THREAD_STATE));266}267268public long blockedTime() {269return getLong(cdata, BLOCKED_TIME);270}271272public long blockedCount() {273return getLong(cdata, BLOCKED_COUNT);274}275276public long waitedTime() {277return getLong(cdata, WAITED_TIME);278}279280public long waitedCount() {281return getLong(cdata, WAITED_COUNT);282}283284public String lockName() {285// The LockName and LockOwnerName can legitimately be null,286// we don't bother to check the value287return getString(cdata, LOCK_NAME);288}289290public long lockOwnerId() {291return getLong(cdata, LOCK_OWNER_ID);292}293294public String lockOwnerName() {295return getString(cdata, LOCK_OWNER_NAME);296}297298public boolean suspended() {299return getBoolean(cdata, SUSPENDED);300}301302public boolean inNative() {303return getBoolean(cdata, IN_NATIVE);304}305306public StackTraceElement[] stackTrace() {307CompositeData[] stackTraceData =308(CompositeData[]) cdata.get(STACK_TRACE);309310// The StackTrace item cannot be null, but if it is we will get311// a NullPointerException when we ask for its length.312StackTraceElement[] stackTrace =313new StackTraceElement[stackTraceData.length];314for (int i = 0; i < stackTraceData.length; i++) {315CompositeData cdi = stackTraceData[i];316stackTrace[i] = StackTraceElementCompositeData.from(cdi);317}318return stackTrace;319}320321// 6.0 new attributes322public LockInfo lockInfo() {323CompositeData lockInfoData = (CompositeData) cdata.get(LOCK_INFO);324return LockInfo.from(lockInfoData);325}326327public MonitorInfo[] lockedMonitors() {328CompositeData[] lockedMonitorsData =329(CompositeData[]) cdata.get(LOCKED_MONITORS);330331// The LockedMonitors item cannot be null, but if it is we will get332// a NullPointerException when we ask for its length.333MonitorInfo[] monitors =334new MonitorInfo[lockedMonitorsData.length];335for (int i = 0; i < lockedMonitorsData.length; i++) {336CompositeData cdi = lockedMonitorsData[i];337monitors[i] = MonitorInfo.from(cdi);338}339return monitors;340}341342public LockInfo[] lockedSynchronizers() {343CompositeData[] lockedSyncsData =344(CompositeData[]) cdata.get(LOCKED_SYNCS);345346// The LockedSynchronizers item cannot be null, but if it is we will347// get a NullPointerException when we ask for its length.348LockInfo[] locks = new LockInfo[lockedSyncsData.length];349for (int i = 0; i < lockedSyncsData.length; i++) {350CompositeData cdi = lockedSyncsData[i];351locks[i] = LockInfo.from(cdi);352}353return locks;354}355356/** Validate if the input CompositeData has the expected357* CompositeType (i.e. contain all attributes with expected358* names and types).359*/360public static void validateCompositeData(CompositeData cd) {361if (cd == null) {362throw new NullPointerException("Null CompositeData");363}364365CompositeType type = cd.getCompositeType();366boolean currentVersion = true;367if (!isTypeMatched(threadInfoCompositeType, type)) {368currentVersion = false;369// check if cd is an older version370if (!isTypeMatched(threadInfoV5CompositeType, type)) {371throw new IllegalArgumentException(372"Unexpected composite type for ThreadInfo");373}374}375376CompositeData[] stackTraceData =377(CompositeData[]) cd.get(STACK_TRACE);378if (stackTraceData == null) {379throw new IllegalArgumentException(380"StackTraceElement[] is missing");381}382if (stackTraceData.length > 0) {383StackTraceElementCompositeData.validateCompositeData(stackTraceData[0]);384}385386// validate v6 attributes387if (currentVersion) {388CompositeData li = (CompositeData) cd.get(LOCK_INFO);389if (li != null) {390if (!isTypeMatched(lockInfoCompositeType,391li.getCompositeType())) {392throw new IllegalArgumentException(393"Unexpected composite type for \"" +394LOCK_INFO + "\" attribute.");395}396}397398CompositeData[] lms = (CompositeData[]) cd.get(LOCKED_MONITORS);399if (lms == null) {400throw new IllegalArgumentException("MonitorInfo[] is null");401}402if (lms.length > 0) {403MonitorInfoCompositeData.validateCompositeData(lms[0]);404}405406CompositeData[] lsyncs = (CompositeData[]) cd.get(LOCKED_SYNCS);407if (lsyncs == null) {408throw new IllegalArgumentException("LockInfo[] is null");409}410if (lsyncs.length > 0) {411if (!isTypeMatched(lockInfoCompositeType,412lsyncs[0].getCompositeType())) {413throw new IllegalArgumentException(414"Unexpected composite type for \"" +415LOCKED_SYNCS + "\" attribute.");416}417}418419}420}421422private static final long serialVersionUID = 2464378539119753175L;423}424425426