File: ThreadUtils.java

package info (click to toggle)
openjdk-11 11.0.22%2B7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 781,236 kB
  • sloc: java: 5,174,754; xml: 1,192,262; cpp: 1,133,036; ansic: 461,316; javascript: 162,554; sh: 16,859; objc: 13,683; python: 4,753; asm: 3,570; makefile: 2,894; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (264 lines) | stat: -rw-r--r-- 10,046 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package nsk.monitoring.share.thread;

import nsk.share.log.Log;
import nsk.share.TestFailure;
import java.lang.management.LockInfo;
import java.lang.management.MonitorInfo;
import java.lang.management.ThreadInfo;

public final class ThreadUtils {
        private ThreadUtils() {
        }

        /**
         * Obtain human readable string representation of lock info.
         *
         * @param info lock ingo
         * @return string representation of info
         */
        public static String strLockInfo(LockInfo info) {
                if (info == null)
                        return "LockInfo: " + null;
                else
                        return "LockInfo: className: " + info.getClassName() + " identityHashCode: " + info.getIdentityHashCode();
        }

        /**
         * Obtain human readable string representation of monitor info.
         *
         * @param info monitor info
         * @return string representation of info
         */
        public static String strMonitorInfo(MonitorInfo info) {
                if (info == null)
                        return "MonitorInfo: null";
                else
                        return "MonitorInfo: lockedStackDepth: " + info.getLockedStackDepth() + " lockedStackFrame: " + info.getLockedStackFrame() + strLockInfo(info);
        }


        /**
         * Obtain human readable string representation of array of lock info.
         *
         * @param infos array of lock info
         * @return string representation of infos
         */
        public static String strLockInfoArr(LockInfo[] infos) {
                String s = "";
                for (LockInfo info : infos)
                        s += strLockInfo(info) + " ";
                return s;
        }

        /**
         * Obtain human readable string representation of array of monitor info.
         *
         * @param infos array of monitor info
         * @return string representation of infos
         */
        public static String strMonitorInfoArr(MonitorInfo[] infos) {
                String s = "";
                for (MonitorInfo info : infos)
                        s += strMonitorInfo(info) + " ";
                return s;
        }

        /**
         * Print monitor information.
         *
         * @param log log
         * @param monitorInfo monitor info
         */
        public static void printMonitorInfo(Log log, MonitorInfo monitorInfo) {
                log.info(strMonitorInfo(monitorInfo));
        }

        /**
         * Print monitor information array.
         *
         * @param log log
         * @param monitorInfos array of monitor info
         */
        public static void printMonitorInfo(Log log, MonitorInfo[] monitorInfos) {
                for (MonitorInfo monitorInfo : monitorInfos)
                        printMonitorInfo(log, monitorInfo);
        }

        /**
         * Print lock information.
         *
         * @param log log
         * @param lockInfo lock info
         */
        public static void printLockInfo(Log log, LockInfo lockInfo) {
                log.info(strLockInfo(lockInfo));
        }

        /**
         * Print lock information array.
         *
         * @param log log
         * @param lockInfo array of lock info
         */
        public static void printLockInfo(Log log, LockInfo[] lockInfos) {
                for (LockInfo lockInfo : lockInfos)
                        printLockInfo(log, lockInfo);
        }

        public static final String INDENT = "   ";

        /**
         * Print all thread information for given ThreadInfo.
         *
         * @param log log
         * @param ti thread info
         */
        public static void threadInfo(Log log, ThreadInfo info) {
                //log.info("Thread info: thread " + info.getThreadId() + " " + info.getThreadName() " state: " + info.getThreadState());
                threadDump(log, info);
                log.info("Blocked count: " + info.getBlockedCount() + " time: " + info.getBlockedTime());
                log.info("Waited count: " + info.getWaitedCount() + " time: " + info.getWaitedTime());
                LockInfo lockInfo = info.getLockInfo();
                log.info("Lock: " + strLockInfo(lockInfo));
                log.info("Lock name: " + info.getLockName() + " ownerId: " + info.getLockOwnerId() + " ownerName: " + info.getLockOwnerName());
                MonitorInfo[] monitorInfo = info.getLockedMonitors();
                log.info("Locked monitors: total " + monitorInfo.length);
                printMonitorInfo(log, info.getLockedMonitors());
                LockInfo[] lockInfos = info.getLockedSynchronizers();
                log.info("Locked synchronizers: total " + lockInfos.length);
                printLockInfo(log, lockInfos);
        }

        /**
         * Print thread dump information for given ThreadInfo.
         *
         * @param log log
         * @param ti thread info
         */
        public static void threadDump(Log log, ThreadInfo ti) {
                String s = "";
                s += "\"";
                s += ti.getThreadName();
                s += "\"";
                s += " Id=" + ti.getThreadId();
                s += " in " + ti.getThreadState();
                if (ti.getLockName() != null) {
                        s += " on lock=" + ti.getLockName();
                }
                if (ti.isSuspended()) {
                        s += " (suspended)";
                }
                if (ti.isInNative()) {
                        s += " (running in native)";
                }
                if (ti.getLockOwnerName() != null)
                        s += " owned by " + ti.getLockOwnerName() + " Id=" + ti.getLockOwnerId();
                log.info(s);
                printStackTrace(log, ti);
        }

        /**
         * Print stack trace information.
         *
         * @param log log
         * @param ti thread info
         */
        public static void printStackTrace(Log log, ThreadInfo ti) {
                StackTraceElement[] stacktrace = ti.getStackTrace();
                MonitorInfo[] monitors = ti.getLockedMonitors();
                for (int i = 0; i < stacktrace.length; i++) {
                        StackTraceElement ste = stacktrace[i];
                        log.info(INDENT + "at " + ste.toString());

                        for (MonitorInfo mi : monitors) {
                                if (mi.getLockedStackDepth() == i) {
                                        log.info(INDENT + "  - locked " + mi);
                                }
                        }
                }
                System.out.println();

        }

        /**
         * Dump information about threads.
         *
         * @param log log
         * @param tinfos array of thread information
         */
        public static void threadDump(Log log, ThreadInfo[] tinfos) {
                log.info("Java thread dump");
                for (ThreadInfo ti : tinfos) {
                        threadDump(log, ti);
                        LockInfo[] syncs = ti.getLockedSynchronizers();
                        printLockInfo(log, syncs);
                }
        }

        public final static int waitThreadStateRetries = 10;
        public final static long waitThreadStateSleepTime = 100;

        public static void waitThreadState(Thread thread, Thread.State state) {
                int retries = 0;
                long ctime = System.currentTimeMillis();
                while (thread.getState() != state) {
                        if (retries++ > waitThreadStateRetries)
                                throw new TestFailure("Thread " + thread + " with current state " + thread.getState() + " did not reach state " + state + " with number of  retries: " + retries + ", time: " + (System.currentTimeMillis() - ctime));
                        try {
                                Thread.sleep(waitThreadStateSleepTime);
                        } catch (InterruptedException e) {
                        }
                }
        }

        /**
         * Verify condition and throw TestFailure if it does not hold.
         *
         * @param condition boolean condition
         * @param message TestFailure message
         */
        public static void verify(boolean condition, String message) {
                if (!condition)
                        throw new TestFailure(message);
        }

        /**
         * Obtain human readable string representation of array of thread ids.
         *
         * @param ids array of ids
         * @return string representation of ids
         */
        public static String strIds(long[] ids) {
                if (ids == null)
                        return null;
                String s = "";
                for (int i = 0; i < ids.length; ++i) {
                        s += ids[i];
                        s += ' ';
                }
                return s;
        }
}