Skip to content

Commit 5e1d1b7

Browse files
committed
8302870: More information needed from failures in vmTestbase ThreadUtils.waitThreadState
Reviewed-by: dholmes, sspitsyn
1 parent 7f35389 commit 5e1d1b7

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

test/hotspot/jtreg/vmTestbase/nsk/monitoring/share/thread/ThreadUtils.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,8 @@
2525

2626
import nsk.share.log.Log;
2727
import nsk.share.TestFailure;
28+
29+
import java.io.PrintStream;
2830
import java.lang.management.LockInfo;
2931
import java.lang.management.MonitorInfo;
3032
import java.lang.management.ThreadInfo;
@@ -203,6 +205,14 @@ public static void printStackTrace(Log log, ThreadInfo ti) {
203205

204206
}
205207

208+
public static void printStackTrace(PrintStream out, Thread t) {
209+
StackTraceElement[] stacktrace = t.getStackTrace();
210+
for (int i = 0; i < stacktrace.length; i++) {
211+
StackTraceElement ste = stacktrace[i];
212+
out.println(INDENT + "at " + ste.toString());
213+
}
214+
}
215+
206216
/**
207217
* Dump information about threads.
208218
*
@@ -218,20 +228,33 @@ public static void threadDump(Log log, ThreadInfo[] tinfos) {
218228
}
219229
}
220230

231+
// Most uses of waitThreadState usually succeed without retries.
232+
// These values should avoid spurious failures.
221233
public final static int waitThreadStateRetries = 10;
222-
public final static long waitThreadStateSleepTime = 100;
234+
public final static long waitThreadStateSleepTime = 1000;
223235

224236
public static void waitThreadState(Thread thread, Thread.State state) {
225237
int retries = 0;
226-
long ctime = System.currentTimeMillis();
238+
long startTime = System.currentTimeMillis();
239+
long elapsedTime = 0;
227240
while (thread.getState() != state) {
228-
if (retries++ > waitThreadStateRetries)
229-
throw new TestFailure("Thread " + thread + " with current state " + thread.getState() + " did not reach state " + state + " with number of retries: " + retries + ", time: " + (System.currentTimeMillis() - ctime));
241+
if (retries++ > waitThreadStateRetries) {
242+
// Failed to see desired state, give info to help diagnose the isuse.
243+
// Show the thread, and fail with Exception showing retries and time taken.
244+
elapsedTime = System.currentTimeMillis() - startTime;
245+
System.err.println("waitThreadState: problem thread: " + thread);
246+
printStackTrace(System.err, thread);
247+
throw new TestFailure("Thread " + thread + " with current state " + thread.getState() + " did not reach state "
248+
+ state + " with number of retries: " + retries + ", time: " + elapsedTime);
249+
}
230250
try {
231251
Thread.sleep(waitThreadStateSleepTime);
232252
} catch (InterruptedException e) {
233253
}
234254
}
255+
// Show retries and time, so we know what "normal" looks like:
256+
elapsedTime = System.currentTimeMillis() - startTime;
257+
System.out.println("waitThreadState: OK. retries: " + retries + " time: " + elapsedTime);
235258
}
236259

237260
/**

0 commit comments

Comments
 (0)