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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
/*
* 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.share.runner;
import nsk.share.Wicket;
import nsk.share.gc.OOMStress;
import nsk.share.log.*;
import nsk.share.test.Stresser;
import nsk.share.test.ExecutionController;
import nsk.share.TestBug;
import java.util.List;
import java.util.ArrayList;
/**
* Helper to assist in running threads.
*
* This class starts a number of threads which run some tasks in cycle.
* They exit after some time or after some iterations as
* determined by RunParams.
*/
public class ThreadsRunner implements MultiRunner, LogAware, RunParamsAware {
private Log log;
private RunParams runParams;
private List<Runnable> runnables = new ArrayList<Runnable>();
private List<ManagedThread> threads = new ArrayList<ManagedThread>();
private Wicket wicket = new Wicket();
private Wicket finished;
private boolean started = false;
private boolean successful = true;
public ThreadsRunner() {
this(RunParams.getInstance());
}
public ThreadsRunner(RunParams runParams) {
setRunParams(runParams);
}
public final void setLog(Log log) {
this.log = log;
}
private class ManagedThread extends Thread {
private Stresser stresser;
private Throwable exception;
private Runnable test;
private boolean shouldWait;
public ManagedThread(Runnable test) {
super(test.toString());
this.test = test;
this.shouldWait = true;
this.stresser = new Stresser(this.getName(), runParams.getStressOptions());
}
public void run() {
wicket.waitFor();
try {
stresser.start(runParams.getIterations());
while (!this.isInterrupted() && stresser.iteration()) {
test.run();
yield();
}
waitForOtherThreads();
} catch (OutOfMemoryError oom) {
waitForOtherThreads();
if (test instanceof OOMStress) {
// Test stressing OOM, not a failure.
log.info("Caught OutOfMemoryError in OOM stress test, omitting exception.");
} else {
failWithException(oom);
}
} catch (Throwable t) {
waitForOtherThreads();
failWithException(t);
} finally {
stresser.finish();
}
}
private void waitForOtherThreads() {
if (shouldWait) {
shouldWait = false;
finished.unlock();
finished.waitFor();
} else {
throw new TestBug("Waiting a second time is not premitted");
}
}
private void failWithException(Throwable t) {
log.debug("Exception in ");
log.debug(test);
log.debug(t);
exception = t;
}
public void forceFinish() {
stresser.forceFinish();
if (runParams.isInterruptThreads()) {
log.debug("Interrupting: " + this);
this.interrupt();
}
}
public final Throwable getException() {
return exception;
}
public final ExecutionController getExecutionController() {
return stresser;
}
}
public void add(Runnable runnable) {
runnables.add(runnable);
}
public void remove(Runnable runnable) {
runnables.remove(runnable);
}
public void removeAll() {
runnables.clear();
}
private Runnable get(int index) {
return (Runnable) runnables.get(index);
}
public Thread getThread(int index) {
return threads.get(index);
}
private int getCount() {
return runnables.size();
}
private void prepare() {
}
private void create() {
int threadCount = runnables.size();
finished = new Wicket(threadCount);
for (int i = 0; i < threadCount; ++i) {
threads.add(new ManagedThread(get(i)));
}
}
/**
* Start threads that run the tasks.
*/
public void start() {
if (started) {
return;
}
create();
prepare();
for (int i = 0; i < threads.size(); ++i) {
Thread t = (Thread) threads.get(i);
log.debug("Starting " + t);
t.start();
}
wicket.unlock();
started = true;
}
/**
* Stop threads that run the tasks.
*/
public void forceFinish() {
log.info("Forcing threads to finish");
for (int i = 0; i < threads.size(); i++) {
ManagedThread thread = threads.get(i);
thread.forceFinish();
}
}
/**
* Join threads that run the tasks.
*/
public void join() throws InterruptedException {
for (int i = 0; i < threads.size(); ++i) {
Thread t = (Thread) threads.get(i);
//log.debug("Joining " + t);
t.join();
}
}
private int dumpFailures() {
int n = 0;
for (int i = 0; i < threads.size(); i++) {
ManagedThread thread = threads.get(i);
Throwable exception = thread.getException();
if (exception != null) {
if (n == 0) {
log.error("Failures summary:");
}
++n;
log.error(exception);
}
}
if (n == 0) {
log.info("No unexpected exceptions/errors are thrown");
}
return n;
}
private ManagedThread findManagedThread(Thread t) {
for (int i = 0; i < threads.size(); i++) {
ManagedThread mt = threads.get(i);
if (mt == t) {
return mt;
}
}
return null;
}
/**
* Run threads as determined by RunParams.
*
* Start threads, run for some time or for some number of iterations,
* then join and report if there were any exceptions.
*
* This method may additionally run other threads (as determined by RunParams):
* - thread that does System.gc() in cycle, @see GCRunner
* - thread that prints memory information in cycle, @see MemDiag
* - thread that prints information about FinMemoryObject's in cycle, @see FinDiag
* - thread that prints information about AllMemoryObject's in cycle, @see AllDiag
*
* @return true if there were no exceptions, false otherwise
*/
public void run() {
if (runParams.isRunGCThread()) {
add(new GCRunner());
}
if (runParams.isRunFinThread()) {
add(new FinRunner());
}
if (runParams.isRunMemDiagThread()) {
add(new MemDiag());
}
try {
start();
join();
successful = dumpFailures() == 0;
} catch (Throwable t) {
log.info("Unexpected exception during the run.");
log.info(t);
successful = false;
}
}
public boolean isSuccessful() {
return successful;
}
public ExecutionController getExecutionController() {
Thread ct = Thread.currentThread();
ManagedThread t = findManagedThread(ct);
if (t != null) {
return t.getExecutionController();
} else {
throw new TestBug("Unable to find managed thread for thread (this method should be called from one of managed threads): " + ct);
}
}
public void runForever() {
start();
}
public final void setRunParams(RunParams runParams) {
this.runParams = runParams;
}
}
|