src/java.management/share/classes/java/lang/management/ThreadInfo.java
changeset 49077 b1c42b3cd19b
parent 47216 71c04702a3d5
child 55120 b0513c833960
equal deleted inserted replaced
49076:1d879babed52 49077:b1c42b3cd19b
     1 /*
     1 /*
     2  * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package java.lang.management;
    26 package java.lang.management;
    27 
    27 
       
    28 import javax.management.openmbean.ArrayType;
    28 import javax.management.openmbean.CompositeData;
    29 import javax.management.openmbean.CompositeData;
    29 import sun.management.ManagementFactoryHelper;
    30 import sun.management.ManagementFactoryHelper;
    30 import sun.management.ThreadInfoCompositeData;
    31 import sun.management.ThreadInfoCompositeData;
    31 import static java.lang.Thread.State.*;
    32 import static java.lang.Thread.State.*;
    32 
    33 
   108     private Thread.State threadState;
   109     private Thread.State threadState;
   109     private int          priority;
   110     private int          priority;
   110     private StackTraceElement[] stackTrace;
   111     private StackTraceElement[] stackTrace;
   111     private MonitorInfo[]       lockedMonitors;
   112     private MonitorInfo[]       lockedMonitors;
   112     private LockInfo[]          lockedSynchronizers;
   113     private LockInfo[]          lockedSynchronizers;
   113 
       
   114     private static MonitorInfo[] EMPTY_MONITORS = new MonitorInfo[0];
   114     private static MonitorInfo[] EMPTY_MONITORS = new MonitorInfo[0];
   115     private static LockInfo[] EMPTY_SYNCS = new LockInfo[0];
   115     private static LockInfo[] EMPTY_SYNCS = new LockInfo[0];
   116 
   116 
   117     /**
   117     /**
   118      * Constructor of ThreadInfo created by the JVM
   118      * Constructor of ThreadInfo created by the JVM
   262     }
   262     }
   263 
   263 
   264     /*
   264     /*
   265      * Constructs a {@code ThreadInfo} object from a
   265      * Constructs a {@code ThreadInfo} object from a
   266      * {@link CompositeData CompositeData}.
   266      * {@link CompositeData CompositeData}.
       
   267      *
       
   268      * @throws IllegalArgumentException if the given CompositeData does not
       
   269      * contain all of the attributes defined for ThreadInfo of version <= N.
       
   270      *
       
   271      * @see ThreadInfo#from
   267      */
   272      */
   268     private ThreadInfo(CompositeData cd) {
   273     private ThreadInfo(CompositeData cd) {
   269         ThreadInfoCompositeData ticd = ThreadInfoCompositeData.getInstance(cd);
   274         ThreadInfoCompositeData ticd = ThreadInfoCompositeData.getInstance(cd);
   270 
   275 
   271         threadId = ticd.threadId();
   276         threadId = ticd.threadId();
   279         lockOwnerName = ticd.lockOwnerName();
   284         lockOwnerName = ticd.lockOwnerName();
   280         threadState = ticd.threadState();
   285         threadState = ticd.threadState();
   281         suspended = ticd.suspended();
   286         suspended = ticd.suspended();
   282         inNative = ticd.inNative();
   287         inNative = ticd.inNative();
   283         stackTrace = ticd.stackTrace();
   288         stackTrace = ticd.stackTrace();
   284 
   289         lock = ticd.lockInfo();
   285         // 6.0 attributes
   290         lockedMonitors = ticd.lockedMonitors();
   286         if (ticd.hasV6()) {
   291         lockedSynchronizers = ticd.lockedSynchronizers();
   287             lock = ticd.lockInfo();
   292         daemon = ticd.isDaemon();
   288             lockedMonitors = ticd.lockedMonitors();
   293         priority = ticd.getPriority();
   289             lockedSynchronizers = ticd.lockedSynchronizers();
       
   290         } else {
       
   291             // lockInfo is a new attribute added in 1.6 ThreadInfo
       
   292             // If cd is a 5.0 version, construct the LockInfo object
       
   293             //  from the lockName value.
       
   294             if (lockName != null) {
       
   295                 String result[] = lockName.split("@");
       
   296                 if (result.length == 2) {
       
   297                     int identityHashCode = Integer.parseInt(result[1], 16);
       
   298                     lock = new LockInfo(result[0], identityHashCode);
       
   299                 } else {
       
   300                     assert result.length == 2;
       
   301                     lock = null;
       
   302                 }
       
   303             } else {
       
   304                 lock = null;
       
   305             }
       
   306             lockedMonitors = EMPTY_MONITORS;
       
   307             lockedSynchronizers = EMPTY_SYNCS;
       
   308         }
       
   309 
       
   310         // 9.0 attributes
       
   311         if (ticd.isCurrentVersion()) {
       
   312             daemon = ticd.isDaemon();
       
   313             priority = ticd.getPriority();
       
   314         } else {
       
   315             // Not ideal, but unclear what else we can do.
       
   316             daemon = false;
       
   317             priority = Thread.NORM_PRIORITY;
       
   318         }
       
   319     }
   294     }
   320 
   295 
   321     /**
   296     /**
   322      * Returns the ID of the thread associated with this {@code ThreadInfo}.
   297      * Returns the ID of the thread associated with this {@code ThreadInfo}.
   323      *
   298      *
   690     private static final int MAX_FRAMES = 8;
   665     private static final int MAX_FRAMES = 8;
   691 
   666 
   692     /**
   667     /**
   693      * Returns a {@code ThreadInfo} object represented by the
   668      * Returns a {@code ThreadInfo} object represented by the
   694      * given {@code CompositeData}.
   669      * given {@code CompositeData}.
   695      * The given {@code CompositeData} must contain the following attributes
   670      *
   696      * unless otherwise specified below:
   671      * <a id="attributes"></a>
       
   672      * A {@code CompositeData} representing a {@code ThreadInfo} of
       
   673      * version <em>N</em> must contain all of the attributes defined
       
   674      * in version &le; <em>N</em> unless specified otherwise.
       
   675      * The same rule applies the composite type of the given
       
   676      * {@code CompositeData} and transitively to attributes whose
       
   677      * {@linkplain CompositeData#getCompositeType() type} or
       
   678      * {@linkplain ArrayType#getElementOpenType() component type} is
       
   679      * {@code CompositeType}.
       
   680      * <p>
       
   681      * A {@code CompositeData} representing {@code ThreadInfo} of
       
   682      * version <em>N</em> contains {@code "stackTrace"} attribute and
       
   683      * {@code "lockedMonitors"} attribute representing
       
   684      * an array of {@code StackTraceElement} and
       
   685      * an array of {@link MonitorInfo} respectively
       
   686      * and their types are of version <em>N</em>.
       
   687      * The {@code "lockedStackFrame"} attribute in
       
   688      * {@link MonitorInfo#from(CompositeData) MonitorInfo}'s composite type
       
   689      * must represent {@code StackTraceElement} of the same version <em>N</em>.
       
   690      * Otherwise, this method will throw {@code IllegalArgumentException}.
       
   691      *
   697      * <table class="striped" style="margin-left:2em">
   692      * <table class="striped" style="margin-left:2em">
   698      * <caption style="display:none">The attributes and their types the given CompositeData contains</caption>
   693      * <caption style="display:none">The attributes and their types for ThreadInfo's composite data</caption>
   699      * <thead>
   694      * <thead>
   700      * <tr>
   695      * <tr>
   701      *   <th scope="col">Attribute Name</th>
   696      *   <th scope="col">Attribute Name</th>
   702      *   <th scope="col">Type</th>
   697      *   <th scope="col">Type</th>
       
   698      *   <th scope="col">Since</th>
   703      * </tr>
   699      * </tr>
   704      * </thead>
   700      * </thead>
   705      * <tbody style="text-align:left">
   701      * <tbody style="text-align:left">
   706      * <tr>
   702      * <tr>
   707      *   <th scope="row">threadId</th>
   703      *   <th scope="row">threadId</th>
   708      *   <td>{@code java.lang.Long}</td>
   704      *   <td>{@code java.lang.Long}</td>
       
   705      *   <td>5</td>
   709      * </tr>
   706      * </tr>
   710      * <tr>
   707      * <tr>
   711      *   <th scope="row">threadName</th>
   708      *   <th scope="row">threadName</th>
   712      *   <td>{@code java.lang.String}</td>
   709      *   <td>{@code java.lang.String}</td>
       
   710      *   <td>5</td>
   713      * </tr>
   711      * </tr>
   714      * <tr>
   712      * <tr>
   715      *   <th scope="row">threadState</th>
   713      *   <th scope="row">threadState</th>
   716      *   <td>{@code java.lang.String}</td>
   714      *   <td>{@code java.lang.String}</td>
       
   715      *   <td>5</td>
   717      * </tr>
   716      * </tr>
   718      * <tr>
   717      * <tr>
   719      *   <th scope="row">suspended</th>
   718      *   <th scope="row">suspended</th>
   720      *   <td>{@code java.lang.Boolean}</td>
   719      *   <td>{@code java.lang.Boolean}</td>
       
   720      *   <td>5</td>
   721      * </tr>
   721      * </tr>
   722      * <tr>
   722      * <tr>
   723      *   <th scope="row">inNative</th>
   723      *   <th scope="row">inNative</th>
   724      *   <td>{@code java.lang.Boolean}</td>
   724      *   <td>{@code java.lang.Boolean}</td>
       
   725      *   <td>5</td>
   725      * </tr>
   726      * </tr>
   726      * <tr>
   727      * <tr>
   727      *   <th scope="row">blockedCount</th>
   728      *   <th scope="row">blockedCount</th>
   728      *   <td>{@code java.lang.Long}</td>
   729      *   <td>{@code java.lang.Long}</td>
       
   730      *   <td>5</td>
   729      * </tr>
   731      * </tr>
   730      * <tr>
   732      * <tr>
   731      *   <th scope="row">blockedTime</th>
   733      *   <th scope="row">blockedTime</th>
   732      *   <td>{@code java.lang.Long}</td>
   734      *   <td>{@code java.lang.Long}</td>
       
   735      *   <td>5</td>
   733      * </tr>
   736      * </tr>
   734      * <tr>
   737      * <tr>
   735      *   <th scope="row">waitedCount</th>
   738      *   <th scope="row">waitedCount</th>
   736      *   <td>{@code java.lang.Long}</td>
   739      *   <td>{@code java.lang.Long}</td>
       
   740      *   <td>5</td>
   737      * </tr>
   741      * </tr>
   738      * <tr>
   742      * <tr>
   739      *   <th scope="row">waitedTime</th>
   743      *   <th scope="row">waitedTime</th>
   740      *   <td>{@code java.lang.Long}</td>
   744      *   <td>{@code java.lang.Long}</td>
       
   745      *   <td>5</td>
       
   746      * </tr>
       
   747      * <tr>
       
   748      *   <th scope="row">lockName</th>
       
   749      *   <td>{@code java.lang.String}</td>
       
   750      *   <td>5</td>
       
   751      * </tr>
       
   752      * <tr>
       
   753      *   <th scope="row">lockOwnerId</th>
       
   754      *   <td>{@code java.lang.Long}</td>
       
   755      *   <td>5</td>
       
   756      * </tr>
       
   757      * <tr>
       
   758      *   <th scope="row">lockOwnerName</th>
       
   759      *   <td>{@code java.lang.String}</td>
       
   760      *   <td>5</td>
       
   761      * </tr>
       
   762      * <tr>
       
   763      *   <th scope="row"><a id="StackTrace">stackTrace</a></th>
       
   764      *   <td>{@code javax.management.openmbean.CompositeData[]}, each element
       
   765      *       is a {@code CompositeData} representing {@code StackTraceElement}
       
   766      *       as specified <a href="#stackTraceElement">below</a>.
       
   767      *   </td>
       
   768      *   <td>5</td>
   741      * </tr>
   769      * </tr>
   742      * <tr>
   770      * <tr>
   743      *   <th scope="row">lockInfo</th>
   771      *   <th scope="row">lockInfo</th>
   744      *   <td>{@code javax.management.openmbean.CompositeData}
   772      *   <td>{@code javax.management.openmbean.CompositeData}
   745      *       - the mapped type for {@link LockInfo} as specified in the
   773      *       - the mapped type for {@link LockInfo} as specified in the
   746      *         {@link LockInfo#from} method.
   774      *         {@link LockInfo#from} method.
   747      *       <p>
   775      *       <p>
   748      *       If {@code cd} does not contain this attribute,
   776      *       If the given {@code CompositeData} does not contain this attribute,
   749      *       the {@code LockInfo} object will be constructed from
   777      *       the {@code LockInfo} object will be constructed from
   750      *       the value of the {@code lockName} attribute. </td>
   778      *       the value of the {@code lockName} attribute.</td>
   751      * </tr>
   779      *    <td>6</td>
   752      * <tr>
       
   753      *   <th scope="row">lockName</th>
       
   754      *   <td>{@code java.lang.String}</td>
       
   755      * </tr>
       
   756      * <tr>
       
   757      *   <th scope="row">lockOwnerId</th>
       
   758      *   <td>{@code java.lang.Long}</td>
       
   759      * </tr>
       
   760      * <tr>
       
   761      *   <th scope="row">lockOwnerName</th>
       
   762      *   <td>{@code java.lang.String}</td>
       
   763      * </tr>
       
   764      * <tr>
       
   765      *   <th scope="row"><a id="StackTrace">stackTrace</a></th>
       
   766      *   <td>{@code javax.management.openmbean.CompositeData[]}
       
   767      *       <p>
       
   768      *       Each element is a {@code CompositeData} representing
       
   769      *       StackTraceElement containing the following attributes:
       
   770      *       <table class="striped" style="margin-left:2em">
       
   771      *       <caption style="display:none">The attributes and their types the given CompositeData contains</caption>
       
   772      *       <thead style="text-align:center">
       
   773      *       <tr>
       
   774      *         <th scope="col">Attribute Name</th>
       
   775      *         <th scope="col">Type</th>
       
   776      *       </tr>
       
   777      *       </thead>
       
   778      *       <tbody style="text-align:left">
       
   779      *       <tr>
       
   780      *         <th scope="row">moduleName</th>
       
   781      *         <td>{@code java.lang.String}</td>
       
   782      *       </tr>
       
   783      *       <tr>
       
   784      *         <th scope="row">moduleVersion</th>
       
   785      *         <td>{@code java.lang.String}</td>
       
   786      *       </tr>
       
   787      *       <tr>
       
   788      *         <th scope="row">className</th>
       
   789      *         <td>{@code java.lang.String}</td>
       
   790      *       </tr>
       
   791      *       <tr>
       
   792      *         <th scope="row">methodName</th>
       
   793      *         <td>{@code java.lang.String}</td>
       
   794      *       </tr>
       
   795      *       <tr>
       
   796      *         <th scope="row">fileName</th>
       
   797      *         <td>{@code java.lang.String}</td>
       
   798      *       </tr>
       
   799      *       <tr>
       
   800      *         <th scope="row">lineNumber</th>
       
   801      *         <td>{@code java.lang.Integer}</td>
       
   802      *       </tr>
       
   803      *       <tr>
       
   804      *         <th scope="row">nativeMethod</th>
       
   805      *         <td>{@code java.lang.Boolean}</td>
       
   806      *       </tr>
       
   807      *       </tbody>
       
   808      *       </table>
       
   809      *   </td>
       
   810      * </tr>
   780      * </tr>
   811      * <tr>
   781      * <tr>
   812      *   <th scope="row">lockedMonitors</th>
   782      *   <th scope="row">lockedMonitors</th>
   813      *   <td>{@code javax.management.openmbean.CompositeData[]}
   783      *   <td>{@code javax.management.openmbean.CompositeData[]}
   814      *       whose element type is the mapped type for
   784      *       whose element type is the mapped type for
   815      *       {@link MonitorInfo} as specified in the
   785      *       {@link MonitorInfo} as specified in the
   816      *       {@link MonitorInfo#from Monitor.from} method.
   786      *       {@link MonitorInfo#from MonitorInfo.from} method.
   817      *       <p>
   787      *       <p>
   818      *       If {@code cd} does not contain this attribute,
   788      *       If the given {@code CompositeData} does not contain this attribute,
   819      *       this attribute will be set to an empty array. </td>
   789      *       this attribute will be set to an empty array.</td>
       
   790      *    <td>6</td>
   820      * </tr>
   791      * </tr>
   821      * <tr>
   792      * <tr>
   822      *   <th scope="row">lockedSynchronizers</th>
   793      *   <th scope="row">lockedSynchronizers</th>
   823      *   <td>{@code javax.management.openmbean.CompositeData[]}
   794      *   <td>{@code javax.management.openmbean.CompositeData[]}
   824      *       whose element type is the mapped type for
   795      *       whose element type is the mapped type for
   825      *       {@link LockInfo} as specified in the {@link LockInfo#from} method.
   796      *       {@link LockInfo} as specified in the {@link LockInfo#from} method.
   826      *       <p>
   797      *       <p>
   827      *       If {@code cd} does not contain this attribute,
   798      *       If the given {@code CompositeData} does not contain this attribute,
   828      *       this attribute will be set to an empty array. </td>
   799      *       this attribute will be set to an empty array.</td>
       
   800      *    <td>6</td>
   829      * </tr>
   801      * </tr>
   830      * <tr>
   802      * <tr>
   831      *   <th scope="row">daemon</th>
   803      *   <th scope="row">daemon</th>
   832      *   <td>{@code java.lang.Boolean}</td>
   804      *   <td>{@code java.lang.Boolean}
       
   805      *       <p>
       
   806      *       If the given {@code CompositeData} does not contain this attribute,
       
   807      *       this attribute will be set to {@code false}.</td>
       
   808      *    <td>9</td>
   833      * </tr>
   809      * </tr>
   834      * <tr>
   810      * <tr>
   835      *   <th scope="row">priority</th>
   811      *   <th scope="row">priority</th>
   836      *   <td>{@code java.lang.Integer}</td>
   812      *   <td>{@code java.lang.Integer}
       
   813      *       <p>
       
   814      *       If the given {@code CompositeData} does not contain this attribute,
       
   815      *       This attribute will be set to {@link Thread#NORM_PRIORITY}.</td>
       
   816      *    <td>9</td>
   837      * </tr>
   817      * </tr>
   838      * </tbody>
   818      * </tbody>
   839      * </table>
   819      * </table>
   840      *
   820      *
       
   821      * <a id="stackTraceElement">A {@code CompositeData} representing
       
   822      * {@code StackTraceElement}</a> of version <em>N</em> must contain
       
   823      * all of the attributes defined in version &le; <em>N</em>
       
   824      * unless specified otherwise.
       
   825      *
       
   826      * <table class="striped" style="margin-left:2em">
       
   827      * <caption style="display:none">The attributes and their types for StackTraceElement's composite data</caption>
       
   828      * <thead style="text-align:center">
       
   829      * <tr>
       
   830      *   <th scope="col">Attribute Name</th>
       
   831      *   <th scope="col">Type</th>
       
   832      *   <th scope="col">Since</th>
       
   833      * </tr>
       
   834      * </thead>
       
   835      * <tbody style="text-align:left">
       
   836      * <tr>
       
   837      *   <th scope="row">classLoaderName</th>
       
   838      *   <td>{@code java.lang.String}</td>
       
   839      *   <td>9</td>
       
   840      * </tr>
       
   841      * <tr>
       
   842      *   <th scope="row">moduleName</th>
       
   843      *   <td>{@code java.lang.String}</td>
       
   844      *   <td>9</td>
       
   845      * </tr>
       
   846      * <tr>
       
   847      *   <th scope="row">moduleVersion</th>
       
   848      *   <td>{@code java.lang.String}</td>
       
   849      *   <td>9</td>
       
   850      * </tr>
       
   851      * <tr>
       
   852      *   <th scope="row">className</th>
       
   853      *   <td>{@code java.lang.String}</td>
       
   854      *   <td>5</td>
       
   855      * </tr>
       
   856      * <tr>
       
   857      *   <th scope="row">methodName</th>
       
   858      *   <td>{@code java.lang.String}</td>
       
   859      *   <td>5</td>
       
   860      * </tr>
       
   861      * <tr>
       
   862      *   <th scope="row">fileName</th>
       
   863      *   <td>{@code java.lang.String}</td>
       
   864      *   <td>5</td>
       
   865      * </tr>
       
   866      * <tr>
       
   867      *   <th scope="row">lineNumber</th>
       
   868      *   <td>{@code java.lang.Integer}</td>
       
   869      *   <td>5</td>
       
   870      * </tr>
       
   871      * <tr>
       
   872      *   <th scope="row">nativeMethod</th>
       
   873      *   <td>{@code java.lang.Boolean}</td>
       
   874      *   <td>5</td>
       
   875      * </tr>
       
   876      * </tbody>
       
   877      * </table>
       
   878      *
   841      * @param cd {@code CompositeData} representing a {@code ThreadInfo}
   879      * @param cd {@code CompositeData} representing a {@code ThreadInfo}
   842      *
   880      *
   843      * @throws IllegalArgumentException if {@code cd} does not
   881      * @throws IllegalArgumentException if the given {@code cd} and
   844      *   represent a {@code ThreadInfo} with the attributes described
   882      *         its composite type does not contain all of
   845      *   above.
   883      *         <a href="#attributes">the attributes</a> defined for a
       
   884      *         {@code ThreadInfo} of a specific runtime version.
   846      *
   885      *
   847      * @return a {@code ThreadInfo} object represented
   886      * @return a {@code ThreadInfo} object represented
   848      *         by {@code cd} if {@code cd} is not {@code null};
   887      *         by {@code cd} if {@code cd} is not {@code null};
   849      *         {@code null} otherwise.
   888      *         {@code null} otherwise.
   850      *
   889      *