src/java.management/share/classes/sun/management/ThreadInfoCompositeData.java
changeset 52288 2b29df6dfa68
parent 49077 b1c42b3cd19b
child 58766 54ffb15c4839
equal deleted inserted replaced
52287:2f6c9127dd97 52288:2b29df6dfa68
    73     }
    73     }
    74 
    74 
    75     protected CompositeData getCompositeData() {
    75     protected CompositeData getCompositeData() {
    76         // Convert StackTraceElement[] to CompositeData[]
    76         // Convert StackTraceElement[] to CompositeData[]
    77         StackTraceElement[] stackTrace = threadInfo.getStackTrace();
    77         StackTraceElement[] stackTrace = threadInfo.getStackTrace();
    78         CompositeData[] stackTraceData =
    78         CompositeData[] stackTraceData = new CompositeData[stackTrace.length];
    79             new CompositeData[stackTrace.length];
       
    80         for (int i = 0; i < stackTrace.length; i++) {
    79         for (int i = 0; i < stackTrace.length; i++) {
    81             StackTraceElement ste = stackTrace[i];
    80             StackTraceElement ste = stackTrace[i];
    82             stackTraceData[i] = StackTraceElementCompositeData.toCompositeData(ste);
    81             stackTraceData[i] = StackTraceElementCompositeData.toCompositeData(ste);
    83         }
    82         }
    84 
    83 
    86         CompositeData lockInfoData =
    85         CompositeData lockInfoData =
    87             LockInfoCompositeData.toCompositeData(threadInfo.getLockInfo());
    86             LockInfoCompositeData.toCompositeData(threadInfo.getLockInfo());
    88 
    87 
    89         // Convert LockInfo[] and MonitorInfo[] to CompositeData[]
    88         // Convert LockInfo[] and MonitorInfo[] to CompositeData[]
    90         LockInfo[] lockedSyncs = threadInfo.getLockedSynchronizers();
    89         LockInfo[] lockedSyncs = threadInfo.getLockedSynchronizers();
    91         CompositeData[] lockedSyncsData =
    90         CompositeData[] lockedSyncsData = new CompositeData[lockedSyncs.length];
    92             new CompositeData[lockedSyncs.length];
       
    93         for (int i = 0; i < lockedSyncs.length; i++) {
    91         for (int i = 0; i < lockedSyncs.length; i++) {
    94             LockInfo li = lockedSyncs[i];
    92             LockInfo li = lockedSyncs[i];
    95             lockedSyncsData[i] = LockInfoCompositeData.toCompositeData(li);
    93             lockedSyncsData[i] = LockInfoCompositeData.toCompositeData(li);
    96         }
    94         }
    97 
    95 
    98         MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors();
    96         MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors();
    99         CompositeData[] lockedMonitorsData =
    97         CompositeData[] lockedMonitorsData = new CompositeData[lockedMonitors.length];
   100             new CompositeData[lockedMonitors.length];
       
   101         for (int i = 0; i < lockedMonitors.length; i++) {
    98         for (int i = 0; i < lockedMonitors.length; i++) {
   102             MonitorInfo mi = lockedMonitors[i];
    99             MonitorInfo mi = lockedMonitors[i];
   103             lockedMonitorsData[i] = MonitorInfoCompositeData.toCompositeData(mi);
   100             lockedMonitorsData[i] = MonitorInfoCompositeData.toCompositeData(mi);
   104         }
   101         }
   105 
   102 
   106         // CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
   103         // values may be null; can't use Map.of
   107         // THREAD_INFO_ATTRIBUTES!
   104         Map<String,Object> items = new HashMap<>();
   108         final Object[] threadInfoItemValues = {
   105         items.put(THREAD_ID,        threadInfo.getThreadId());
   109             threadInfo.getThreadId(),
   106         items.put(THREAD_NAME,      threadInfo.getThreadName());
   110             threadInfo.getThreadName(),
   107         items.put(THREAD_STATE,     threadInfo.getThreadState().name());
   111             threadInfo.getThreadState().name(),
   108         items.put(BLOCKED_TIME,     threadInfo.getBlockedTime());
   112             threadInfo.getBlockedTime(),
   109         items.put(BLOCKED_COUNT,    threadInfo.getBlockedCount());
   113             threadInfo.getBlockedCount(),
   110         items.put(WAITED_TIME,      threadInfo.getWaitedTime());
   114             threadInfo.getWaitedTime(),
   111         items.put(WAITED_COUNT,     threadInfo.getWaitedCount());
   115             threadInfo.getWaitedCount(),
   112         items.put(LOCK_INFO,        lockInfoData);
   116             lockInfoData,
   113         items.put(LOCK_NAME,        threadInfo.getLockName());
   117             threadInfo.getLockName(),
   114         items.put(LOCK_OWNER_ID,    threadInfo.getLockOwnerId());
   118             threadInfo.getLockOwnerId(),
   115         items.put(LOCK_OWNER_NAME,  threadInfo.getLockOwnerName());
   119             threadInfo.getLockOwnerName(),
   116         items.put(STACK_TRACE,      stackTraceData);
   120             stackTraceData,
   117         items.put(SUSPENDED,        threadInfo.isSuspended());
   121             threadInfo.isSuspended(),
   118         items.put(IN_NATIVE,        threadInfo.isInNative());
   122             threadInfo.isInNative(),
   119         items.put(LOCKED_MONITORS,  lockedMonitorsData);
   123             lockedMonitorsData,
   120         items.put(LOCKED_SYNCS,     lockedSyncsData);
   124             lockedSyncsData,
   121         items.put(DAEMON,           threadInfo.isDaemon());
   125             threadInfo.isDaemon(),
   122         items.put(PRIORITY,         threadInfo.getPriority());
   126             threadInfo.getPriority(),
       
   127         };
       
   128 
   123 
   129         try {
   124         try {
   130             return new CompositeDataSupport(compositeType(),
   125             return new CompositeDataSupport(ThreadInfoCompositeTypes.ofVersion(RUNTIME_VERSION), items);
   131                                             THREAD_INFO_ATTRIBTUES,
       
   132                                             threadInfoItemValues);
       
   133         } catch (OpenDataException e) {
   126         } catch (OpenDataException e) {
   134             // Should never reach here
   127             // Should never reach here
   135             throw new AssertionError(e);
   128             throw new AssertionError(e);
   136         }
   129         }
   137     }
   130     }
   181     private static final String[] V9_ATTRIBUTES = {
   174     private static final String[] V9_ATTRIBUTES = {
   182         DAEMON,
   175         DAEMON,
   183         PRIORITY,
   176         PRIORITY,
   184     };
   177     };
   185 
   178 
   186     private static final String[] THREAD_INFO_ATTRIBTUES =
       
   187         Stream.of(V5_ATTRIBUTES, V6_ATTRIBUTES, V9_ATTRIBUTES)
       
   188               .flatMap(Arrays::stream).toArray(String[]::new);
       
   189 
       
   190     public long threadId() {
   179     public long threadId() {
   191         return getLong(cdata, THREAD_ID);
   180         return getLong(cdata, THREAD_ID);
   192     }
   181     }
   193 
   182 
   194     public String threadName() {
   183     public String threadName() {
   363             throw new IllegalArgumentException(
   352             throw new IllegalArgumentException(
   364                 "Unexpected composite type for ThreadInfo of version " + version);
   353                 "Unexpected composite type for ThreadInfo of version " + version);
   365         }
   354         }
   366     }
   355     }
   367 
   356 
   368     public static CompositeType compositeType() {
   357     static final int RUNTIME_VERSION =  Runtime.version().feature();
   369         return ThreadInfoCompositeTypes.compositeTypes.get(0);
       
   370     }
       
   371 
       
   372     static class ThreadInfoCompositeTypes {
   358     static class ThreadInfoCompositeTypes {
   373         static final int CURRENT =  Runtime.version().feature();
       
   374         static final Map<Integer, CompositeType> compositeTypes = initCompositeTypes();
   359         static final Map<Integer, CompositeType> compositeTypes = initCompositeTypes();
   375         /*
   360         /*
   376          * Returns CompositeType of the given runtime version
   361          * Returns CompositeType of the given runtime version
   377          */
   362          */
   378         static CompositeType ofVersion(int version) {
   363         static CompositeType ofVersion(int version) {
   380         }
   365         }
   381 
   366 
   382         static Map<Integer, CompositeType> initCompositeTypes() {
   367         static Map<Integer, CompositeType> initCompositeTypes() {
   383             Map<Integer, CompositeType> types = new HashMap<>();
   368             Map<Integer, CompositeType> types = new HashMap<>();
   384             CompositeType ctype = initCompositeType();
   369             CompositeType ctype = initCompositeType();
   385             types.put(CURRENT, ctype);
   370             types.put(RUNTIME_VERSION, ctype);
   386             types.put(5, initV5CompositeType(ctype));
   371             types.put(5, initV5CompositeType(ctype));
   387             types.put(6, initV6CompositeType(ctype));
   372             types.put(6, initV6CompositeType(ctype));
   388             return types;
   373             return types;
   389         }
   374         }
   390 
   375