Path: blob/master/src/java.management/share/classes/sun/management/ThreadInfoCompositeData.java
41152 views
/*1* Copyright (c) 2004, 2019, 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 java.util.Arrays;31import java.util.HashMap;32import java.util.Map;33import java.util.stream.Stream;34import javax.management.openmbean.ArrayType;35import javax.management.openmbean.CompositeType;36import javax.management.openmbean.CompositeData;37import javax.management.openmbean.CompositeDataSupport;38import javax.management.openmbean.OpenDataException;39import javax.management.openmbean.OpenType;4041/**42* A CompositeData for ThreadInfo for the local management support.43* This class avoids the performance penalty paid to the44* construction of a CompositeData use in the local case.45*/46public class ThreadInfoCompositeData extends LazyCompositeData {47@SuppressWarnings("serial") // Not statically typed as Serializable48private final ThreadInfo threadInfo;49@SuppressWarnings("serial") // Not statically typed as Serializable50private final CompositeData cdata;5152private ThreadInfoCompositeData(ThreadInfo ti) {53this.threadInfo = ti;54this.cdata = null;55}5657private ThreadInfoCompositeData(CompositeData cd) {58this.threadInfo = null;59this.cdata = cd;60}6162public ThreadInfo getThreadInfo() {63return threadInfo;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 = new CompositeData[stackTrace.length];80for (int i = 0; i < stackTrace.length; i++) {81StackTraceElement ste = stackTrace[i];82stackTraceData[i] = StackTraceElementCompositeData.toCompositeData(ste);83}8485// Convert MonitorInfo[] and LockInfo[] to CompositeData[]86CompositeData lockInfoData =87LockInfoCompositeData.toCompositeData(threadInfo.getLockInfo());8889// Convert LockInfo[] and MonitorInfo[] to CompositeData[]90LockInfo[] lockedSyncs = threadInfo.getLockedSynchronizers();91CompositeData[] lockedSyncsData = new CompositeData[lockedSyncs.length];92for (int i = 0; i < lockedSyncs.length; i++) {93LockInfo li = lockedSyncs[i];94lockedSyncsData[i] = LockInfoCompositeData.toCompositeData(li);95}9697MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors();98CompositeData[] lockedMonitorsData = new CompositeData[lockedMonitors.length];99for (int i = 0; i < lockedMonitors.length; i++) {100MonitorInfo mi = lockedMonitors[i];101lockedMonitorsData[i] = MonitorInfoCompositeData.toCompositeData(mi);102}103104// values may be null; can't use Map.of105Map<String,Object> items = new HashMap<>();106items.put(THREAD_ID, threadInfo.getThreadId());107items.put(THREAD_NAME, threadInfo.getThreadName());108items.put(THREAD_STATE, threadInfo.getThreadState().name());109items.put(BLOCKED_TIME, threadInfo.getBlockedTime());110items.put(BLOCKED_COUNT, threadInfo.getBlockedCount());111items.put(WAITED_TIME, threadInfo.getWaitedTime());112items.put(WAITED_COUNT, threadInfo.getWaitedCount());113items.put(LOCK_INFO, lockInfoData);114items.put(LOCK_NAME, threadInfo.getLockName());115items.put(LOCK_OWNER_ID, threadInfo.getLockOwnerId());116items.put(LOCK_OWNER_NAME, threadInfo.getLockOwnerName());117items.put(STACK_TRACE, stackTraceData);118items.put(SUSPENDED, threadInfo.isSuspended());119items.put(IN_NATIVE, threadInfo.isInNative());120items.put(LOCKED_MONITORS, lockedMonitorsData);121items.put(LOCKED_SYNCS, lockedSyncsData);122items.put(DAEMON, threadInfo.isDaemon());123items.put(PRIORITY, threadInfo.getPriority());124125try {126return new CompositeDataSupport(ThreadInfoCompositeTypes.ofVersion(RUNTIME_VERSION), items);127} catch (OpenDataException e) {128// Should never reach here129throw new AssertionError(e);130}131}132133// Attribute names134private static final String THREAD_ID = "threadId";135private static final String THREAD_NAME = "threadName";136private static final String THREAD_STATE = "threadState";137private static final String BLOCKED_TIME = "blockedTime";138private static final String BLOCKED_COUNT = "blockedCount";139private static final String WAITED_TIME = "waitedTime";140private static final String WAITED_COUNT = "waitedCount";141private static final String LOCK_INFO = "lockInfo";142private static final String LOCK_NAME = "lockName";143private static final String LOCK_OWNER_ID = "lockOwnerId";144private static final String LOCK_OWNER_NAME = "lockOwnerName";145private static final String STACK_TRACE = "stackTrace";146private static final String SUSPENDED = "suspended";147private static final String IN_NATIVE = "inNative";148private static final String DAEMON = "daemon";149private static final String PRIORITY = "priority";150private static final String LOCKED_MONITORS = "lockedMonitors";151private static final String LOCKED_SYNCS = "lockedSynchronizers";152153private static final String[] V5_ATTRIBUTES = {154THREAD_ID,155THREAD_NAME,156THREAD_STATE,157BLOCKED_TIME,158BLOCKED_COUNT,159WAITED_TIME,160WAITED_COUNT,161LOCK_NAME,162LOCK_OWNER_ID,163LOCK_OWNER_NAME,164STACK_TRACE,165SUSPENDED,166IN_NATIVE167};168169private static final String[] V6_ATTRIBUTES = {170LOCK_INFO,171LOCKED_MONITORS,172LOCKED_SYNCS,173};174175private static final String[] V9_ATTRIBUTES = {176DAEMON,177PRIORITY,178};179180public long threadId() {181return getLong(cdata, THREAD_ID);182}183184public String threadName() {185// The ThreadName item cannot be null so we check that186// it is present with a non-null value.187String name = getString(cdata, THREAD_NAME);188if (name == null) {189throw new IllegalArgumentException("Invalid composite data: " +190"Attribute " + THREAD_NAME + " has null value");191}192return name;193}194195public Thread.State threadState() {196return Thread.State.valueOf(getString(cdata, THREAD_STATE));197}198199public long blockedTime() {200return getLong(cdata, BLOCKED_TIME);201}202203public long blockedCount() {204return getLong(cdata, BLOCKED_COUNT);205}206207public long waitedTime() {208return getLong(cdata, WAITED_TIME);209}210211public long waitedCount() {212return getLong(cdata, WAITED_COUNT);213}214215public String lockName() {216// The LockName and LockOwnerName can legitimately be null,217// we don't bother to check the value218return getString(cdata, LOCK_NAME);219}220221public long lockOwnerId() {222return getLong(cdata, LOCK_OWNER_ID);223}224225public String lockOwnerName() {226return getString(cdata, LOCK_OWNER_NAME);227}228229public boolean suspended() {230return getBoolean(cdata, SUSPENDED);231}232233public boolean inNative() {234return getBoolean(cdata, IN_NATIVE);235}236237/*238* if daemon attribute is not present, default to false.239*/240public boolean isDaemon() {241return cdata.containsKey(DAEMON) ? getBoolean(cdata, DAEMON) : false;242}243244/*245* if priority attribute is not present, default to norm priority.246*/247public int getPriority(){248return cdata.containsKey(PRIORITY) ? getInt(cdata, PRIORITY) : Thread.NORM_PRIORITY;249}250251public StackTraceElement[] stackTrace() {252CompositeData[] stackTraceData =253(CompositeData[]) cdata.get(STACK_TRACE);254255// The StackTrace item cannot be null, but if it is we will get256// a NullPointerException when we ask for its length.257StackTraceElement[] stackTrace =258new StackTraceElement[stackTraceData.length];259for (int i = 0; i < stackTraceData.length; i++) {260CompositeData cdi = stackTraceData[i];261stackTrace[i] = StackTraceElementCompositeData.from(cdi);262}263return stackTrace;264}265266/*267* lockInfo is a new attribute added in JDK 6 ThreadInfo268* If cd is a 5.0 version, construct the LockInfo object269* from the lockName value.270*/271public LockInfo lockInfo() {272if (cdata.containsKey(LOCK_INFO)) {273CompositeData lockInfoData = (CompositeData) cdata.get(LOCK_INFO);274return LockInfo.from(lockInfoData);275} else {276String lockName = lockName();277LockInfo lock = null;278if (lockName != null) {279String result[] = lockName.split("@");280if (result.length == 2) {281int identityHashCode = Integer.parseInt(result[1], 16);282lock = new LockInfo(result[0], identityHashCode);283}284}285return lock;286}287}288289/**290* Returns an empty array if locked_monitors attribute is not present.291*/292public MonitorInfo[] lockedMonitors() {293if (!cdata.containsKey(LOCKED_MONITORS)) {294return new MonitorInfo[0];295}296297CompositeData[] lockedMonitorsData =298(CompositeData[]) cdata.get(LOCKED_MONITORS);299300// The LockedMonitors item cannot be null, but if it is we will get301// a NullPointerException when we ask for its length.302MonitorInfo[] monitors =303new MonitorInfo[lockedMonitorsData.length];304for (int i = 0; i < lockedMonitorsData.length; i++) {305CompositeData cdi = lockedMonitorsData[i];306monitors[i] = MonitorInfo.from(cdi);307}308return monitors;309}310311/**312* Returns an empty array if locked_monitors attribute is not present.313*/314public LockInfo[] lockedSynchronizers() {315if (!cdata.containsKey(LOCKED_SYNCS)) {316return new LockInfo[0];317}318319CompositeData[] lockedSyncsData =320(CompositeData[]) cdata.get(LOCKED_SYNCS);321322// The LockedSynchronizers item cannot be null, but if it is we will323// get a NullPointerException when we ask for its length.324LockInfo[] locks = new LockInfo[lockedSyncsData.length];325for (int i = 0; i < lockedSyncsData.length; i++) {326CompositeData cdi = lockedSyncsData[i];327locks[i] = LockInfo.from(cdi);328}329return locks;330}331332/**333* Validate if the input CompositeData has the expected334* CompositeType (i.e. contain all attributes with expected335* names and types).336*/337public static void validateCompositeData(CompositeData cd) {338if (cd == null) {339throw new NullPointerException("Null CompositeData");340}341342CompositeType type = cd.getCompositeType();343int version;344if (Arrays.stream(V9_ATTRIBUTES).anyMatch(type::containsKey)) {345version = Runtime.version().feature();346} else if (Arrays.stream(V6_ATTRIBUTES).anyMatch(type::containsKey)) {347version = 6;348} else {349version = 5;350}351352if (!isTypeMatched(ThreadInfoCompositeTypes.ofVersion(version), type)) {353throw new IllegalArgumentException(354"Unexpected composite type for ThreadInfo of version " + version);355}356}357358static final int RUNTIME_VERSION = Runtime.version().feature();359static class ThreadInfoCompositeTypes {360static final Map<Integer, CompositeType> compositeTypes = initCompositeTypes();361/*362* Returns CompositeType of the given runtime version363*/364static CompositeType ofVersion(int version) {365return compositeTypes.get(version);366}367368static Map<Integer, CompositeType> initCompositeTypes() {369Map<Integer, CompositeType> types = new HashMap<>();370CompositeType ctype = initCompositeType();371types.put(RUNTIME_VERSION, ctype);372types.put(5, initV5CompositeType(ctype));373types.put(6, initV6CompositeType(ctype));374return types;375}376377static CompositeType initCompositeType() {378try {379return (CompositeType)MappedMXBeanType.toOpenType(ThreadInfo.class);380} catch (OpenDataException e) {381// Should never reach here382throw new AssertionError(e);383}384}385386static CompositeType initV5CompositeType(CompositeType threadInfoCompositeType) {387try {388OpenType<?>[] v5Types = new OpenType<?>[V5_ATTRIBUTES.length];389for (int i = 0; i < v5Types.length; i++) {390String name = V5_ATTRIBUTES[i];391v5Types[i] = name.equals(STACK_TRACE)392? new ArrayType<>(1, StackTraceElementCompositeData.v5CompositeType())393: threadInfoCompositeType.getType(name);394}395return new CompositeType("ThreadInfo",396"JDK 5 ThreadInfo",397V5_ATTRIBUTES,398V5_ATTRIBUTES,399v5Types);400} catch (OpenDataException e) {401// Should never reach here402throw new AssertionError(e);403}404}405406static CompositeType initV6CompositeType(CompositeType threadInfoCompositeType) {407try {408String[] v6Names = Stream.of(V5_ATTRIBUTES, V6_ATTRIBUTES)409.flatMap(Arrays::stream).toArray(String[]::new);410OpenType<?>[] v6Types = new OpenType<?>[v6Names.length];411for (int i = 0; i < v6Names.length; i++) {412String name = v6Names[i];413OpenType<?> ot = threadInfoCompositeType.getType(name);414if (name.equals(STACK_TRACE)) {415ot = new ArrayType<>(1, StackTraceElementCompositeData.v5CompositeType());416} else if (name.equals(LOCKED_MONITORS)) {417ot = new ArrayType<>(1, MonitorInfoCompositeData.v6CompositeType());418}419v6Types[i] = ot;420}421return new CompositeType("ThreadInfo",422"JDK 6 ThreadInfo",423v6Names,424v6Names,425v6Types);426} catch (OpenDataException e) {427// Should never reach here428throw new AssertionError(e);429}430}431}432private static final long serialVersionUID = 2464378539119753175L;433}434435436