Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 17 | import java.lang.reflect.*; |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 18 | import java.util.ArrayList; |
| 19 | import java.util.Arrays; |
| 20 | import java.util.Collections; |
| 21 | import java.util.HashMap; |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 22 | import java.util.HashSet; |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 23 | import java.util.List; |
| 24 | import java.util.Map; |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 25 | import java.util.Set; |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 26 | |
| 27 | // Run on host with: |
| 28 | // javac ThreadTest.java && java ThreadStress && rm *.class |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 29 | // 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 Cao | def3fcd | 2015-08-10 15:51:27 -0700 | [diff] [blame] | 35 | // -d X ............ number of daemon threads |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 36 | // -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 Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 47 | public class Main implements Runnable { |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 48 | |
Brian Carlstrom | 4514d3c | 2011-10-21 17:01:31 -0700 | [diff] [blame] | 49 | public static final boolean DEBUG = false; |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 50 | |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 51 | 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 Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 58 | |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 59 | private final static class OOM extends Operation { |
Andreas Gampe | 059e627 | 2016-01-05 12:57:56 -0800 | [diff] [blame] | 60 | private final static int ALLOC_SIZE = 1024; |
| 61 | |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 62 | @Override |
| 63 | public boolean perform() { |
| 64 | try { |
| 65 | List<byte[]> l = new ArrayList<byte[]>(); |
| 66 | while (true) { |
Andreas Gampe | 059e627 | 2016-01-05 12:57:56 -0800 | [diff] [blame] | 67 | l.add(new byte[ALLOC_SIZE]); |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 68 | } |
| 69 | } catch (OutOfMemoryError e) { |
| 70 | } |
| 71 | return true; |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 75 | 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 Marko | 5fe1026 | 2016-06-21 10:38:23 +0100 | [diff] [blame^] | 110 | } catch (OutOfMemoryError e) { |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 111 | } 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 Gampe | 059e627 | 2016-01-05 12:57:56 -0800 | [diff] [blame] | 121 | 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 Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 124 | @Override |
| 125 | public boolean perform() { |
| 126 | try { |
| 127 | List<byte[]> l = new ArrayList<byte[]>(); |
Andreas Gampe | 059e627 | 2016-01-05 12:57:56 -0800 | [diff] [blame] | 128 | 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 Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 148 | } |
| 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 Marko | 5fe1026 | 2016-06-21 10:38:23 +0100 | [diff] [blame^] | 158 | try { |
| 159 | Thread.currentThread().getStackTrace(); |
| 160 | } catch (OutOfMemoryError e) { |
| 161 | } |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 162 | 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 Gampe | 059e627 | 2016-01-05 12:57:56 -0800 | [diff] [blame] | 174 | private final static int SLEEP_TIME = 100; |
| 175 | |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 176 | @Override |
| 177 | public boolean perform() { |
| 178 | try { |
Andreas Gampe | 059e627 | 2016-01-05 12:57:56 -0800 | [diff] [blame] | 179 | Thread.sleep(SLEEP_TIME); |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 180 | } catch (InterruptedException ignored) { |
| 181 | } |
| 182 | return true; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private final static class TimedWait extends Operation { |
Andreas Gampe | 059e627 | 2016-01-05 12:57:56 -0800 | [diff] [blame] | 187 | private final static int SLEEP_TIME = 100; |
| 188 | |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 189 | 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 Gampe | 059e627 | 2016-01-05 12:57:56 -0800 | [diff] [blame] | 199 | lock.wait(SLEEP_TIME, 0); |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 200 | } 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 Gampe | 059e627 | 2016-01-05 12:57:56 -0800 | [diff] [blame] | 249 | frequencyMap.put(new Alloc(), 0.25); // 50/200 |
| 250 | frequencyMap.put(new LargeAlloc(), 0.05); // 10/200 |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 251 | 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 Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 270 | public static void main(String[] args) throws Exception { |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 271 | parseAndRun(args); |
| 272 | } |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 273 | |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 274 | 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 Gampe | 059e627 | 2016-01-05 12:57:56 -0800 | [diff] [blame] | 296 | } else if (split[0].equals("-largealloc")) { |
| 297 | op = new LargeAlloc(); |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 298 | } 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 Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 311 | |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 312 | if (in == null) { |
| 313 | in = new HashMap<Operation, Double>(); |
| 314 | } |
| 315 | in.put(op, d); |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 316 | |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 317 | 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 Cao | def3fcd | 2015-08-10 15:51:27 -0700 | [diff] [blame] | 339 | int numberOfDaemons = -1; |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 340 | 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 Chartier | 031768a | 2015-08-27 10:25:02 -0700 | [diff] [blame] | 347 | // args[0] is libarttest |
| 348 | for (int i = 1; i < args.length; i++) { |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 349 | if (args[i].equals("-n")) { |
| 350 | i++; |
| 351 | numberOfThreads = Integer.parseInt(args[i]); |
Man Cao | def3fcd | 2015-08-10 15:51:27 -0700 | [diff] [blame] | 352 | } else if (args[i].equals("-d")) { |
| 353 | i++; |
| 354 | numberOfDaemons = Integer.parseInt(args[i]); |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 355 | } 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 Cao | def3fcd | 2015-08-10 15:51:27 -0700 | [diff] [blame] | 381 | if (numberOfDaemons == -1) { |
| 382 | numberOfDaemons = 3; |
| 383 | } |
| 384 | |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 385 | 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 Cao | def3fcd | 2015-08-10 15:51:27 -0700 | [diff] [blame] | 402 | runTest(numberOfThreads, numberOfDaemons, operationsPerThread, lock, frequencyMap); |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Man Cao | def3fcd | 2015-08-10 15:51:27 -0700 | [diff] [blame] | 405 | 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 Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 412 | // 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 Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 417 | // Fill in the Operation[] array for each thread by laying |
| 418 | // down references to operation according to their desired |
| 419 | // frequency. |
Man Cao | def3fcd | 2015-08-10 15:51:27 -0700 | [diff] [blame] | 420 | // The first numberOfThreads elements are normal threads, the last |
| 421 | // numberOfDaemons elements are daemon threads. |
| 422 | final Main[] threadStresses = new Main[numberOfThreads + numberOfDaemons]; |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 423 | for (int t = 0; t < threadStresses.length; t++) { |
| 424 | Operation[] operations = new Operation[operationsPerThread]; |
| 425 | int o = 0; |
| 426 | LOOP: |
| 427 | while (true) { |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 428 | for (Operation op : frequencyMap.keySet()) { |
| 429 | int freq = (int)(frequencyMap.get(op) * operationsPerThread); |
| 430 | for (int f = 0; f < freq; f++) { |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 431 | if (o == operations.length) { |
| 432 | break LOOP; |
| 433 | } |
| 434 | operations[o] = op; |
| 435 | o++; |
| 436 | } |
| 437 | } |
| 438 | } |
Man Cao | def3fcd | 2015-08-10 15:51:27 -0700 | [diff] [blame] | 439 | // Randomize the operation order |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 440 | Collections.shuffle(Arrays.asList(operations)); |
Man Cao | def3fcd | 2015-08-10 15:51:27 -0700 | [diff] [blame] | 441 | threadStresses[t] = t < numberOfThreads ? new Main(lock, t, operations) : |
| 442 | new Daemon(lock, t, operations); |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 445 | // Enable to dump operation counts per thread to make sure its |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 446 | // sane compared to Operation.frequency |
| 447 | if (DEBUG) { |
| 448 | for (int t = 0; t < threadStresses.length; t++) { |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 449 | Operation[] operations = threadStresses[t].operations; |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 450 | 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 Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 461 | for (Operation op : frequencyMap.keySet()) { |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 462 | 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 Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 473 | final Main ts = threadStresses[r]; |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 474 | runners[r] = new Thread("Runner thread " + r) { |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 475 | final Main threadStress = ts; |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 476 | public void run() { |
| 477 | int id = threadStress.id; |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 478 | System.out.println("Starting worker for " + id); |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 479 | while (threadStress.nextOperation < operationsPerThread) { |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 480 | try { |
Mathieu Chartier | 72e36d8 | 2015-09-17 20:46:56 -0700 | [diff] [blame] | 481 | Thread thread = new Thread(ts, "Worker thread " + id); |
| 482 | thread.start(); |
| 483 | try { |
| 484 | thread.join(); |
| 485 | } catch (InterruptedException e) { |
| 486 | } |
| 487 | |
Mathieu Chartier | 9d3c3fc | 2015-08-18 11:42:03 -0700 | [diff] [blame] | 488 | 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 Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 495 | } |
Mathieu Chartier | 72e36d8 | 2015-09-17 20:46:56 -0700 | [diff] [blame] | 496 | // 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 Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 504 | } |
| 505 | }; |
| 506 | } |
| 507 | |
| 508 | // The notifier thread is a daemon just loops forever to wake |
| 509 | // up threads in Operation.WAIT |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 510 | if (lock != null) { |
| 511 | Thread notifier = new Thread("Notifier") { |
| 512 | public void run() { |
| 513 | while (true) { |
| 514 | synchronized (lock) { |
| 515 | lock.notifyAll(); |
| 516 | } |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 517 | } |
| 518 | } |
Andreas Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 519 | }; |
| 520 | notifier.setDaemon(true); |
| 521 | notifier.start(); |
| 522 | } |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 523 | |
Man Cao | def3fcd | 2015-08-10 15:51:27 -0700 | [diff] [blame] | 524 | // 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 Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 532 | 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 Cao | def3fcd | 2015-08-10 15:51:27 -0700 | [diff] [blame] | 540 | protected final Operation[] operations; |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 541 | private final Object lock; |
Man Cao | def3fcd | 2015-08-10 15:51:27 -0700 | [diff] [blame] | 542 | protected final int id; |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 543 | |
| 544 | private int nextOperation; |
| 545 | |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 546 | private Main(Object lock, int id, Operation[] operations) { |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 547 | 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 Gampe | f2fdc73 | 2014-06-11 08:20:47 -0700 | [diff] [blame] | 565 | if (!operation.perform()) { |
| 566 | return; |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 567 | } |
| 568 | } |
Brian Carlstrom | 4514d3c | 2011-10-21 17:01:31 -0700 | [diff] [blame] | 569 | } finally { |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 570 | if (DEBUG) { |
| 571 | System.out.println("Finishing ThreadStress for " + id); |
| 572 | } |
| 573 | } |
| 574 | } |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 575 | |
Man Cao | def3fcd | 2015-08-10 15:51:27 -0700 | [diff] [blame] | 576 | 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 Chartier | bf81547 | 2015-08-13 13:02:01 -0700 | [diff] [blame] | 597 | } 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 Cao | def3fcd | 2015-08-10 15:51:27 -0700 | [diff] [blame] | 600 | } finally { |
| 601 | if (DEBUG) { |
| 602 | System.out.println("Finishing ThreadStress Daemon for " + id); |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 608 | } |