blob: acd8e8b344fc775e2a4f79660ec3c1030597eb5f [file] [log] [blame]
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andreas Gampe1c83cbc2014-07-22 18:52:29 -070017import java.lang.reflect.*;
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -070018import java.util.ArrayList;
19import java.util.Arrays;
20import java.util.Collections;
21import java.util.HashMap;
Andreas Gampef2fdc732014-06-11 08:20:47 -070022import java.util.HashSet;
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -070023import java.util.List;
24import java.util.Map;
Andreas Gampef2fdc732014-06-11 08:20:47 -070025import java.util.Set;
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -070026
27// Run on host with:
28// javac ThreadTest.java && java ThreadStress && rm *.class
Andreas Gampef2fdc732014-06-11 08:20:47 -070029// Through run-test:
30// test/run-test {run-test-args} 004-ThreadStress [Main {ThreadStress-args}]
31// (It is important to pass Main if you want to give parameters...)
32//
33// ThreadStress command line parameters:
34// -n X ............ number of threads
Man Caodef3fcd2015-08-10 15:51:27 -070035// -d X ............ number of daemon threads
Andreas Gampef2fdc732014-06-11 08:20:47 -070036// -o X ............ number of overall operations
37// -t X ............ number of operations per thread
38// --dumpmap ....... print the frequency map
39// -oom:X .......... frequency of OOM (double)
40// -alloc:X ........ frequency of Alloc
41// -stacktrace:X ... frequency of StackTrace
42// -exit:X ......... frequency of Exit
43// -sleep:X ........ frequency of Sleep
44// -wait:X ......... frequency of Wait
45// -timedwait:X .... frequency of TimedWait
46
Andreas Gampe1c83cbc2014-07-22 18:52:29 -070047public class Main implements Runnable {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -070048
Brian Carlstrom4514d3c2011-10-21 17:01:31 -070049 public static final boolean DEBUG = false;
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -070050
Andreas Gampef2fdc732014-06-11 08:20:47 -070051 private static abstract class Operation {
52 /**
53 * Perform the action represented by this operation. Returns true if the thread should
54 * continue.
55 */
56 public abstract boolean perform();
57 }
Elliott Hughes4cd121e2013-01-07 17:35:41 -080058
Andreas Gampef2fdc732014-06-11 08:20:47 -070059 private final static class OOM extends Operation {
Andreas Gampe059e6272016-01-05 12:57:56 -080060 private final static int ALLOC_SIZE = 1024;
61
Andreas Gampef2fdc732014-06-11 08:20:47 -070062 @Override
63 public boolean perform() {
64 try {
65 List<byte[]> l = new ArrayList<byte[]>();
66 while (true) {
Andreas Gampe059e6272016-01-05 12:57:56 -080067 l.add(new byte[ALLOC_SIZE]);
Andreas Gampef2fdc732014-06-11 08:20:47 -070068 }
69 } catch (OutOfMemoryError e) {
70 }
71 return true;
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -070072 }
73 }
74
Andreas Gampef2fdc732014-06-11 08:20:47 -070075 private final static class SigQuit extends Operation {
76 private final static int sigquit;
77 private final static Method kill;
78 private final static int pid;
79
80 static {
81 int pidTemp = -1;
82 int sigquitTemp = -1;
83 Method killTemp = null;
84
85 try {
86 Class<?> osClass = Class.forName("android.system.Os");
87 Method getpid = osClass.getDeclaredMethod("getpid");
88 pidTemp = (Integer)getpid.invoke(null);
89
90 Class<?> osConstants = Class.forName("android.system.OsConstants");
91 Field sigquitField = osConstants.getDeclaredField("SIGQUIT");
92 sigquitTemp = (Integer)sigquitField.get(null);
93
94 killTemp = osClass.getDeclaredMethod("kill", int.class, int.class);
95 } catch (Exception e) {
96 if (!e.getClass().getName().equals("ErrnoException")) {
97 e.printStackTrace(System.out);
98 }
99 }
100
101 pid = pidTemp;
102 sigquit = sigquitTemp;
103 kill = killTemp;
104 }
105
106 @Override
107 public boolean perform() {
108 try {
109 kill.invoke(null, pid, sigquit);
Vladimir Marko5fe10262016-06-21 10:38:23 +0100110 } catch (OutOfMemoryError e) {
Andreas Gampef2fdc732014-06-11 08:20:47 -0700111 } catch (Exception e) {
112 if (!e.getClass().getName().equals("ErrnoException")) {
113 e.printStackTrace(System.out);
114 }
115 }
116 return true;
117 }
118 }
119
120 private final static class Alloc extends Operation {
Andreas Gampe059e6272016-01-05 12:57:56 -0800121 private final static int ALLOC_SIZE = 1024; // Needs to be small enough to not be in LOS.
122 private final static int ALLOC_COUNT = 1024;
123
Andreas Gampef2fdc732014-06-11 08:20:47 -0700124 @Override
125 public boolean perform() {
126 try {
127 List<byte[]> l = new ArrayList<byte[]>();
Andreas Gampe059e6272016-01-05 12:57:56 -0800128 for (int i = 0; i < ALLOC_COUNT; i++) {
129 l.add(new byte[ALLOC_SIZE]);
130 }
131 } catch (OutOfMemoryError e) {
132 }
133 return true;
134 }
135 }
136
137 private final static class LargeAlloc extends Operation {
138 private final static int PAGE_SIZE = 4096;
139 private final static int PAGE_SIZE_MODIFIER = 10; // Needs to be large enough for LOS.
140 private final static int ALLOC_COUNT = 100;
141
142 @Override
143 public boolean perform() {
144 try {
145 List<byte[]> l = new ArrayList<byte[]>();
146 for (int i = 0; i < ALLOC_COUNT; i++) {
147 l.add(new byte[PAGE_SIZE_MODIFIER * PAGE_SIZE]);
Andreas Gampef2fdc732014-06-11 08:20:47 -0700148 }
149 } catch (OutOfMemoryError e) {
150 }
151 return true;
152 }
153 }
154
155 private final static class StackTrace extends Operation {
156 @Override
157 public boolean perform() {
Vladimir Marko5fe10262016-06-21 10:38:23 +0100158 try {
159 Thread.currentThread().getStackTrace();
160 } catch (OutOfMemoryError e) {
161 }
Andreas Gampef2fdc732014-06-11 08:20:47 -0700162 return true;
163 }
164 }
165
166 private final static class Exit extends Operation {
167 @Override
168 public boolean perform() {
169 return false;
170 }
171 }
172
173 private final static class Sleep extends Operation {
Andreas Gampe059e6272016-01-05 12:57:56 -0800174 private final static int SLEEP_TIME = 100;
175
Andreas Gampef2fdc732014-06-11 08:20:47 -0700176 @Override
177 public boolean perform() {
178 try {
Andreas Gampe059e6272016-01-05 12:57:56 -0800179 Thread.sleep(SLEEP_TIME);
Andreas Gampef2fdc732014-06-11 08:20:47 -0700180 } catch (InterruptedException ignored) {
181 }
182 return true;
183 }
184 }
185
186 private final static class TimedWait extends Operation {
Andreas Gampe059e6272016-01-05 12:57:56 -0800187 private final static int SLEEP_TIME = 100;
188
Andreas Gampef2fdc732014-06-11 08:20:47 -0700189 private final Object lock;
190
191 public TimedWait(Object lock) {
192 this.lock = lock;
193 }
194
195 @Override
196 public boolean perform() {
197 synchronized (lock) {
198 try {
Andreas Gampe059e6272016-01-05 12:57:56 -0800199 lock.wait(SLEEP_TIME, 0);
Andreas Gampef2fdc732014-06-11 08:20:47 -0700200 } catch (InterruptedException ignored) {
201 }
202 }
203 return true;
204 }
205 }
206
207 private final static class Wait extends Operation {
208 private final Object lock;
209
210 public Wait(Object lock) {
211 this.lock = lock;
212 }
213
214 @Override
215 public boolean perform() {
216 synchronized (lock) {
217 try {
218 lock.wait();
219 } catch (InterruptedException ignored) {
220 }
221 }
222 return true;
223 }
224 }
225
226 private final static class SyncAndWork extends Operation {
227 private final Object lock;
228
229 public SyncAndWork(Object lock) {
230 this.lock = lock;
231 }
232
233 @Override
234 public boolean perform() {
235 synchronized (lock) {
236 try {
237 Thread.sleep((int)(Math.random()*10));
238 } catch (InterruptedException ignored) {
239 }
240 }
241 return true;
242 }
243 }
244
245 private final static Map<Operation, Double> createDefaultFrequencyMap(Object lock) {
246 Map<Operation, Double> frequencyMap = new HashMap<Operation, Double>();
247 frequencyMap.put(new OOM(), 0.005); // 1/200
248 frequencyMap.put(new SigQuit(), 0.095); // 19/200
Andreas Gampe059e6272016-01-05 12:57:56 -0800249 frequencyMap.put(new Alloc(), 0.25); // 50/200
250 frequencyMap.put(new LargeAlloc(), 0.05); // 10/200
Andreas Gampef2fdc732014-06-11 08:20:47 -0700251 frequencyMap.put(new StackTrace(), 0.1); // 20/200
252 frequencyMap.put(new Exit(), 0.25); // 50/200
253 frequencyMap.put(new Sleep(), 0.125); // 25/200
254 frequencyMap.put(new TimedWait(lock), 0.05); // 10/200
255 frequencyMap.put(new Wait(lock), 0.075); // 15/200
256
257 return frequencyMap;
258 }
259
260 private final static Map<Operation, Double> createLockFrequencyMap(Object lock) {
261 Map<Operation, Double> frequencyMap = new HashMap<Operation, Double>();
262 frequencyMap.put(new Sleep(), 0.2);
263 frequencyMap.put(new TimedWait(lock), 0.2);
264 frequencyMap.put(new Wait(lock), 0.2);
265 frequencyMap.put(new SyncAndWork(lock), 0.4);
266
267 return frequencyMap;
268 }
269
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700270 public static void main(String[] args) throws Exception {
Andreas Gampef2fdc732014-06-11 08:20:47 -0700271 parseAndRun(args);
272 }
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700273
Andreas Gampef2fdc732014-06-11 08:20:47 -0700274 private static Map<Operation, Double> updateFrequencyMap(Map<Operation, Double> in,
275 Object lock, String arg) {
276 String split[] = arg.split(":");
277 if (split.length != 2) {
278 throw new IllegalArgumentException("Can't split argument " + arg);
279 }
280 double d;
281 try {
282 d = Double.parseDouble(split[1]);
283 } catch (Exception e) {
284 throw new IllegalArgumentException(e);
285 }
286 if (d < 0) {
287 throw new IllegalArgumentException(arg + ": value must be >= 0.");
288 }
289 Operation op = null;
290 if (split[0].equals("-oom")) {
291 op = new OOM();
292 } else if (split[0].equals("-sigquit")) {
293 op = new SigQuit();
294 } else if (split[0].equals("-alloc")) {
295 op = new Alloc();
Andreas Gampe059e6272016-01-05 12:57:56 -0800296 } else if (split[0].equals("-largealloc")) {
297 op = new LargeAlloc();
Andreas Gampef2fdc732014-06-11 08:20:47 -0700298 } else if (split[0].equals("-stacktrace")) {
299 op = new StackTrace();
300 } else if (split[0].equals("-exit")) {
301 op = new Exit();
302 } else if (split[0].equals("-sleep")) {
303 op = new Sleep();
304 } else if (split[0].equals("-wait")) {
305 op = new Wait(lock);
306 } else if (split[0].equals("-timedwait")) {
307 op = new TimedWait(lock);
308 } else {
309 throw new IllegalArgumentException("Unknown arg " + arg);
310 }
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700311
Andreas Gampef2fdc732014-06-11 08:20:47 -0700312 if (in == null) {
313 in = new HashMap<Operation, Double>();
314 }
315 in.put(op, d);
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700316
Andreas Gampef2fdc732014-06-11 08:20:47 -0700317 return in;
318 }
319
320 private static void normalize(Map<Operation, Double> map) {
321 double sum = 0;
322 for (Double d : map.values()) {
323 sum += d;
324 }
325 if (sum == 0) {
326 throw new RuntimeException("No elements!");
327 }
328 if (sum != 1.0) {
329 // Avoid ConcurrentModificationException.
330 Set<Operation> tmp = new HashSet<>(map.keySet());
331 for (Operation op : tmp) {
332 map.put(op, map.get(op) / sum);
333 }
334 }
335 }
336
337 public static void parseAndRun(String[] args) throws Exception {
338 int numberOfThreads = -1;
Man Caodef3fcd2015-08-10 15:51:27 -0700339 int numberOfDaemons = -1;
Andreas Gampef2fdc732014-06-11 08:20:47 -0700340 int totalOperations = -1;
341 int operationsPerThread = -1;
342 Object lock = new Object();
343 Map<Operation, Double> frequencyMap = null;
344 boolean dumpMap = false;
345
346 if (args != null) {
Mathieu Chartier031768a2015-08-27 10:25:02 -0700347 // args[0] is libarttest
348 for (int i = 1; i < args.length; i++) {
Andreas Gampef2fdc732014-06-11 08:20:47 -0700349 if (args[i].equals("-n")) {
350 i++;
351 numberOfThreads = Integer.parseInt(args[i]);
Man Caodef3fcd2015-08-10 15:51:27 -0700352 } else if (args[i].equals("-d")) {
353 i++;
354 numberOfDaemons = Integer.parseInt(args[i]);
Andreas Gampef2fdc732014-06-11 08:20:47 -0700355 } else if (args[i].equals("-o")) {
356 i++;
357 totalOperations = Integer.parseInt(args[i]);
358 } else if (args[i].equals("-t")) {
359 i++;
360 operationsPerThread = Integer.parseInt(args[i]);
361 } else if (args[i].equals("--locks-only")) {
362 lock = new Object();
363 frequencyMap = createLockFrequencyMap(lock);
364 } else if (args[i].equals("--dumpmap")) {
365 dumpMap = true;
366 } else {
367 frequencyMap = updateFrequencyMap(frequencyMap, lock, args[i]);
368 }
369 }
370 }
371
372 if (totalOperations != -1 && operationsPerThread != -1) {
373 throw new IllegalArgumentException(
374 "Specified both totalOperations and operationsPerThread");
375 }
376
377 if (numberOfThreads == -1) {
378 numberOfThreads = 5;
379 }
380
Man Caodef3fcd2015-08-10 15:51:27 -0700381 if (numberOfDaemons == -1) {
382 numberOfDaemons = 3;
383 }
384
Andreas Gampef2fdc732014-06-11 08:20:47 -0700385 if (totalOperations == -1) {
386 totalOperations = 1000;
387 }
388
389 if (operationsPerThread == -1) {
390 operationsPerThread = totalOperations/numberOfThreads;
391 }
392
393 if (frequencyMap == null) {
394 frequencyMap = createDefaultFrequencyMap(lock);
395 }
396 normalize(frequencyMap);
397
398 if (dumpMap) {
399 System.out.println(frequencyMap);
400 }
401
Man Caodef3fcd2015-08-10 15:51:27 -0700402 runTest(numberOfThreads, numberOfDaemons, operationsPerThread, lock, frequencyMap);
Andreas Gampef2fdc732014-06-11 08:20:47 -0700403 }
404
Man Caodef3fcd2015-08-10 15:51:27 -0700405 public static void runTest(final int numberOfThreads, final int numberOfDaemons,
406 final int operationsPerThread, final Object lock,
407 Map<Operation, Double> frequencyMap) throws Exception {
408 // Each normal thread is going to do operationsPerThread
409 // operations. Each daemon thread will loop over all
410 // the operations and will not stop.
411 // The distribution of operations is determined by
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700412 // the Operation.frequency values. We fill out an Operation[]
413 // for each thread with the operations it is to perform. The
414 // Operation[] is shuffled so that there is more random
415 // interactions between the threads.
416
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700417 // Fill in the Operation[] array for each thread by laying
418 // down references to operation according to their desired
419 // frequency.
Man Caodef3fcd2015-08-10 15:51:27 -0700420 // The first numberOfThreads elements are normal threads, the last
421 // numberOfDaemons elements are daemon threads.
422 final Main[] threadStresses = new Main[numberOfThreads + numberOfDaemons];
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700423 for (int t = 0; t < threadStresses.length; t++) {
424 Operation[] operations = new Operation[operationsPerThread];
425 int o = 0;
426 LOOP:
427 while (true) {
Andreas Gampef2fdc732014-06-11 08:20:47 -0700428 for (Operation op : frequencyMap.keySet()) {
429 int freq = (int)(frequencyMap.get(op) * operationsPerThread);
430 for (int f = 0; f < freq; f++) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700431 if (o == operations.length) {
432 break LOOP;
433 }
434 operations[o] = op;
435 o++;
436 }
437 }
438 }
Man Caodef3fcd2015-08-10 15:51:27 -0700439 // Randomize the operation order
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700440 Collections.shuffle(Arrays.asList(operations));
Man Caodef3fcd2015-08-10 15:51:27 -0700441 threadStresses[t] = t < numberOfThreads ? new Main(lock, t, operations) :
442 new Daemon(lock, t, operations);
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700443 }
444
Andreas Gampef2fdc732014-06-11 08:20:47 -0700445 // Enable to dump operation counts per thread to make sure its
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700446 // sane compared to Operation.frequency
447 if (DEBUG) {
448 for (int t = 0; t < threadStresses.length; t++) {
Andreas Gampef2fdc732014-06-11 08:20:47 -0700449 Operation[] operations = threadStresses[t].operations;
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700450 Map<Operation, Integer> distribution = new HashMap<Operation, Integer>();
451 for (Operation operation : operations) {
452 Integer ops = distribution.get(operation);
453 if (ops == null) {
454 ops = 1;
455 } else {
456 ops++;
457 }
458 distribution.put(operation, ops);
459 }
460 System.out.println("Distribution for " + t);
Andreas Gampef2fdc732014-06-11 08:20:47 -0700461 for (Operation op : frequencyMap.keySet()) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700462 System.out.println(op + " = " + distribution.get(op));
463 }
464 }
465 }
466
467 // Create the runners for each thread. The runner Thread
468 // ensures that thread that exit due to Operation.EXIT will be
469 // restarted until they reach their desired
470 // operationsPerThread.
471 Thread[] runners = new Thread[numberOfThreads];
472 for (int r = 0; r < runners.length; r++) {
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700473 final Main ts = threadStresses[r];
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800474 runners[r] = new Thread("Runner thread " + r) {
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700475 final Main threadStress = ts;
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700476 public void run() {
477 int id = threadStress.id;
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800478 System.out.println("Starting worker for " + id);
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700479 while (threadStress.nextOperation < operationsPerThread) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700480 try {
Mathieu Chartier72e36d82015-09-17 20:46:56 -0700481 Thread thread = new Thread(ts, "Worker thread " + id);
482 thread.start();
483 try {
484 thread.join();
485 } catch (InterruptedException e) {
486 }
487
Mathieu Chartier9d3c3fc2015-08-18 11:42:03 -0700488 System.out.println("Thread exited for " + id + " with "
489 + (operationsPerThread - threadStress.nextOperation)
490 + " operations remaining.");
491 } catch (OutOfMemoryError e) {
492 // Ignore OOME since we need to print "Finishing worker" for the test
493 // to pass.
494 }
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700495 }
Mathieu Chartier72e36d82015-09-17 20:46:56 -0700496 // Keep trying to print "Finishing worker" until it succeeds.
497 while (true) {
498 try {
499 System.out.println("Finishing worker");
500 break;
501 } catch (OutOfMemoryError e) {
502 }
503 }
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700504 }
505 };
506 }
507
508 // The notifier thread is a daemon just loops forever to wake
509 // up threads in Operation.WAIT
Andreas Gampef2fdc732014-06-11 08:20:47 -0700510 if (lock != null) {
511 Thread notifier = new Thread("Notifier") {
512 public void run() {
513 while (true) {
514 synchronized (lock) {
515 lock.notifyAll();
516 }
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700517 }
518 }
Andreas Gampef2fdc732014-06-11 08:20:47 -0700519 };
520 notifier.setDaemon(true);
521 notifier.start();
522 }
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700523
Man Caodef3fcd2015-08-10 15:51:27 -0700524 // Create and start the daemon threads.
525 for (int r = 0; r < numberOfDaemons; r++) {
526 Main daemon = threadStresses[numberOfThreads + r];
527 Thread t = new Thread(daemon, "Daemon thread " + daemon.id);
528 t.setDaemon(true);
529 t.start();
530 }
531
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700532 for (int r = 0; r < runners.length; r++) {
533 runners[r].start();
534 }
535 for (int r = 0; r < runners.length; r++) {
536 runners[r].join();
537 }
538 }
539
Man Caodef3fcd2015-08-10 15:51:27 -0700540 protected final Operation[] operations;
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700541 private final Object lock;
Man Caodef3fcd2015-08-10 15:51:27 -0700542 protected final int id;
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700543
544 private int nextOperation;
545
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700546 private Main(Object lock, int id, Operation[] operations) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700547 this.lock = lock;
548 this.id = id;
549 this.operations = operations;
550 }
551
552 public void run() {
553 try {
554 if (DEBUG) {
555 System.out.println("Starting ThreadStress " + id);
556 }
557 while (nextOperation < operations.length) {
558 Operation operation = operations[nextOperation];
559 if (DEBUG) {
560 System.out.println("ThreadStress " + id
561 + " operation " + nextOperation
562 + " is " + operation);
563 }
564 nextOperation++;
Andreas Gampef2fdc732014-06-11 08:20:47 -0700565 if (!operation.perform()) {
566 return;
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700567 }
568 }
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700569 } finally {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700570 if (DEBUG) {
571 System.out.println("Finishing ThreadStress for " + id);
572 }
573 }
574 }
Andreas Gampe1c83cbc2014-07-22 18:52:29 -0700575
Man Caodef3fcd2015-08-10 15:51:27 -0700576 private static class Daemon extends Main {
577 private Daemon(Object lock, int id, Operation[] operations) {
578 super(lock, id, operations);
579 }
580
581 public void run() {
582 try {
583 if (DEBUG) {
584 System.out.println("Starting ThreadStress Daemon " + id);
585 }
586 int i = 0;
587 while (true) {
588 Operation operation = operations[i];
589 if (DEBUG) {
590 System.out.println("ThreadStress Daemon " + id
591 + " operation " + i
592 + " is " + operation);
593 }
594 operation.perform();
595 i = (i + 1) % operations.length;
596 }
Mathieu Chartierbf815472015-08-13 13:02:01 -0700597 } catch (OutOfMemoryError e) {
598 // Catch OutOfMemoryErrors since these can cause the test to fail it they print
599 // the stack trace after "Finishing worker".
Man Caodef3fcd2015-08-10 15:51:27 -0700600 } finally {
601 if (DEBUG) {
602 System.out.println("Finishing ThreadStress Daemon for " + id);
603 }
604 }
605 }
606 }
607
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700608}