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