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 | |
Elliott Hughes | 9989960 | 2014-04-28 11:25:17 -0700 | [diff] [blame] | 17 | import android.system.ErrnoException; |
| 18 | import android.system.Os; |
| 19 | import android.system.OsConstants; |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 20 | import java.util.ArrayList; |
| 21 | import java.util.Arrays; |
| 22 | import java.util.Collections; |
| 23 | import java.util.HashMap; |
| 24 | import java.util.List; |
| 25 | import java.util.Map; |
| 26 | |
| 27 | // Run on host with: |
| 28 | // javac ThreadTest.java && java ThreadStress && rm *.class |
| 29 | class ThreadStress implements Runnable { |
| 30 | |
Brian Carlstrom | 4514d3c | 2011-10-21 17:01:31 -0700 | [diff] [blame] | 31 | public static final boolean DEBUG = false; |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 32 | |
| 33 | enum Operation { |
| 34 | OOM(1), |
Elliott Hughes | 831afe4 | 2011-12-15 17:27:34 -0800 | [diff] [blame] | 35 | SIGQUIT(19), |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 36 | ALLOC(60), |
| 37 | STACKTRACE(20), |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 38 | EXIT(50), |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 39 | |
| 40 | SLEEP(25), |
| 41 | TIMED_WAIT(10), |
| 42 | WAIT(15); |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 43 | |
| 44 | private final int frequency; |
| 45 | Operation(int frequency) { |
| 46 | this.frequency = frequency; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | public static void main(String[] args) throws Exception { |
| 51 | |
| 52 | final int numberOfThreads = 5; |
| 53 | final int totalOperations = 1000; |
| 54 | final int operationsPerThread = totalOperations/numberOfThreads; |
| 55 | |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 56 | // Lock used to notify threads performing Operation.WAIT |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 57 | final Object lock = new Object(); |
| 58 | |
| 59 | // Each thread is going to do operationsPerThread |
| 60 | // operations. The distribution of operations is determined by |
| 61 | // the Operation.frequency values. We fill out an Operation[] |
| 62 | // for each thread with the operations it is to perform. The |
| 63 | // Operation[] is shuffled so that there is more random |
| 64 | // interactions between the threads. |
| 65 | |
| 66 | // The simple-minded filling in of Operation[] based on |
| 67 | // Operation.frequency below won't have even have close to a |
| 68 | // reasonable distribution if the count of Operation |
| 69 | // frequencies is greater than the total number of |
| 70 | // operations. So here we do a quick sanity check in case |
| 71 | // people tweak the constants above. |
| 72 | int operationCount = 0; |
| 73 | for (Operation op : Operation.values()) { |
| 74 | operationCount += op.frequency; |
| 75 | } |
| 76 | if (operationCount > operationsPerThread) { |
| 77 | throw new AssertionError(operationCount + " > " + operationsPerThread); |
| 78 | } |
| 79 | |
| 80 | // Fill in the Operation[] array for each thread by laying |
| 81 | // down references to operation according to their desired |
| 82 | // frequency. |
| 83 | final ThreadStress[] threadStresses = new ThreadStress[numberOfThreads]; |
| 84 | for (int t = 0; t < threadStresses.length; t++) { |
| 85 | Operation[] operations = new Operation[operationsPerThread]; |
| 86 | int o = 0; |
| 87 | LOOP: |
| 88 | while (true) { |
| 89 | for (Operation op : Operation.values()) { |
| 90 | for (int f = 0; f < op.frequency; f++) { |
| 91 | if (o == operations.length) { |
| 92 | break LOOP; |
| 93 | } |
| 94 | operations[o] = op; |
| 95 | o++; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | // Randomize the oepration order |
| 100 | Collections.shuffle(Arrays.asList(operations)); |
| 101 | threadStresses[t] = new ThreadStress(lock, t, operations); |
| 102 | } |
| 103 | |
| 104 | // Enable to dump operation counds per thread to make sure its |
| 105 | // sane compared to Operation.frequency |
| 106 | if (DEBUG) { |
| 107 | for (int t = 0; t < threadStresses.length; t++) { |
| 108 | Operation[] operations = new Operation[operationsPerThread]; |
| 109 | Map<Operation, Integer> distribution = new HashMap<Operation, Integer>(); |
| 110 | for (Operation operation : operations) { |
| 111 | Integer ops = distribution.get(operation); |
| 112 | if (ops == null) { |
| 113 | ops = 1; |
| 114 | } else { |
| 115 | ops++; |
| 116 | } |
| 117 | distribution.put(operation, ops); |
| 118 | } |
| 119 | System.out.println("Distribution for " + t); |
| 120 | for (Operation op : Operation.values()) { |
| 121 | System.out.println(op + " = " + distribution.get(op)); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // Create the runners for each thread. The runner Thread |
| 127 | // ensures that thread that exit due to Operation.EXIT will be |
| 128 | // restarted until they reach their desired |
| 129 | // operationsPerThread. |
| 130 | Thread[] runners = new Thread[numberOfThreads]; |
| 131 | for (int r = 0; r < runners.length; r++) { |
| 132 | final ThreadStress ts = threadStresses[r]; |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 133 | runners[r] = new Thread("Runner thread " + r) { |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 134 | final ThreadStress threadStress = ts; |
| 135 | public void run() { |
| 136 | int id = threadStress.id; |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 137 | System.out.println("Starting worker for " + id); |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 138 | while (threadStress.nextOperation < operationsPerThread) { |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 139 | Thread thread = new Thread(ts, "Worker thread " + id); |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 140 | thread.start(); |
| 141 | try { |
| 142 | thread.join(); |
| 143 | } catch (InterruptedException e) { |
| 144 | } |
| 145 | System.out.println("Thread exited for " + id + " with " |
| 146 | + (operationsPerThread - threadStress.nextOperation) |
| 147 | + " operations remaining."); |
| 148 | } |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 149 | System.out.println("Finishing worker for " + id); |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 150 | } |
| 151 | }; |
| 152 | } |
| 153 | |
| 154 | // The notifier thread is a daemon just loops forever to wake |
| 155 | // up threads in Operation.WAIT |
Mathieu Chartier | 5f51d4b | 2013-12-03 14:24:05 -0800 | [diff] [blame] | 156 | Thread notifier = new Thread("Notifier") { |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 157 | public void run() { |
| 158 | while (true) { |
| 159 | synchronized (lock) { |
| 160 | lock.notifyAll(); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | }; |
| 165 | notifier.setDaemon(true); |
| 166 | notifier.start(); |
| 167 | |
| 168 | for (int r = 0; r < runners.length; r++) { |
| 169 | runners[r].start(); |
| 170 | } |
| 171 | for (int r = 0; r < runners.length; r++) { |
| 172 | runners[r].join(); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | private final Operation[] operations; |
| 177 | private final Object lock; |
| 178 | private final int id; |
| 179 | |
| 180 | private int nextOperation; |
| 181 | |
| 182 | private ThreadStress(Object lock, int id, Operation[] operations) { |
| 183 | this.lock = lock; |
| 184 | this.id = id; |
| 185 | this.operations = operations; |
| 186 | } |
| 187 | |
| 188 | public void run() { |
| 189 | try { |
| 190 | if (DEBUG) { |
| 191 | System.out.println("Starting ThreadStress " + id); |
| 192 | } |
| 193 | while (nextOperation < operations.length) { |
| 194 | Operation operation = operations[nextOperation]; |
| 195 | if (DEBUG) { |
| 196 | System.out.println("ThreadStress " + id |
| 197 | + " operation " + nextOperation |
| 198 | + " is " + operation); |
| 199 | } |
| 200 | nextOperation++; |
| 201 | switch (operation) { |
| 202 | case EXIT: { |
| 203 | return; |
| 204 | } |
Elliott Hughes | 831afe4 | 2011-12-15 17:27:34 -0800 | [diff] [blame] | 205 | case SIGQUIT: { |
| 206 | try { |
Elliott Hughes | 9989960 | 2014-04-28 11:25:17 -0700 | [diff] [blame] | 207 | Os.kill(Os.getpid(), OsConstants.SIGQUIT); |
Elliott Hughes | 831afe4 | 2011-12-15 17:27:34 -0800 | [diff] [blame] | 208 | } catch (ErrnoException ex) { |
| 209 | } |
| 210 | } |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 211 | case SLEEP: { |
| 212 | try { |
| 213 | Thread.sleep(100); |
| 214 | } catch (InterruptedException ignored) { |
| 215 | } |
| 216 | } |
| 217 | case TIMED_WAIT: { |
| 218 | synchronized (lock) { |
| 219 | try { |
| 220 | lock.wait(100, 0); |
| 221 | } catch (InterruptedException ignored) { |
| 222 | } |
| 223 | } |
| 224 | break; |
| 225 | } |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 226 | case WAIT: { |
| 227 | synchronized (lock) { |
| 228 | try { |
| 229 | lock.wait(); |
Elliott Hughes | 4cd121e | 2013-01-07 17:35:41 -0800 | [diff] [blame] | 230 | } catch (InterruptedException ignored) { |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | break; |
| 234 | } |
| 235 | case OOM: { |
| 236 | try { |
| 237 | List<byte[]> l = new ArrayList<byte[]>(); |
| 238 | while (true) { |
| 239 | l.add(new byte[1024]); |
| 240 | } |
| 241 | } catch (OutOfMemoryError e) { |
| 242 | } |
| 243 | break; |
| 244 | } |
| 245 | case ALLOC: { |
Brian Carlstrom | 4514d3c | 2011-10-21 17:01:31 -0700 | [diff] [blame] | 246 | try { |
| 247 | List<byte[]> l = new ArrayList<byte[]>(); |
| 248 | for (int i = 0; i < 1024; i++) { |
| 249 | l.add(new byte[1024]); |
| 250 | } |
| 251 | } catch (OutOfMemoryError e) { |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 252 | } |
| 253 | break; |
| 254 | } |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 255 | case STACKTRACE: { |
| 256 | Thread.currentThread().getStackTrace(); |
| 257 | break; |
| 258 | } |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 259 | default: { |
| 260 | throw new AssertionError(operation.toString()); |
| 261 | } |
| 262 | } |
| 263 | } |
Brian Carlstrom | 4514d3c | 2011-10-21 17:01:31 -0700 | [diff] [blame] | 264 | } finally { |
Brian Carlstrom | 7c6deaa | 2011-10-21 12:05:06 -0700 | [diff] [blame] | 265 | if (DEBUG) { |
| 266 | System.out.println("Finishing ThreadStress for " + id); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |