blob: 5dccc689c2db152fda7a8fda6d1cbef7c6cc5e39 [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
Elliott Hughes99899602014-04-28 11:25:17 -070017import android.system.ErrnoException;
18import android.system.Os;
19import android.system.OsConstants;
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -070020import java.util.ArrayList;
21import java.util.Arrays;
22import java.util.Collections;
23import java.util.HashMap;
24import java.util.List;
25import java.util.Map;
26
27// Run on host with:
28// javac ThreadTest.java && java ThreadStress && rm *.class
29class ThreadStress implements Runnable {
30
Brian Carlstrom4514d3c2011-10-21 17:01:31 -070031 public static final boolean DEBUG = false;
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -070032
33 enum Operation {
34 OOM(1),
Elliott Hughes831afe42011-12-15 17:27:34 -080035 SIGQUIT(19),
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080036 ALLOC(60),
37 STACKTRACE(20),
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -070038 EXIT(50),
Elliott Hughes4cd121e2013-01-07 17:35:41 -080039
40 SLEEP(25),
41 TIMED_WAIT(10),
42 WAIT(15);
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -070043
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 Hughes4cd121e2013-01-07 17:35:41 -080056 // Lock used to notify threads performing Operation.WAIT
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -070057 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 Chartier5f51d4b2013-12-03 14:24:05 -0800133 runners[r] = new Thread("Runner thread " + r) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700134 final ThreadStress threadStress = ts;
135 public void run() {
136 int id = threadStress.id;
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800137 System.out.println("Starting worker for " + id);
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700138 while (threadStress.nextOperation < operationsPerThread) {
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800139 Thread thread = new Thread(ts, "Worker thread " + id);
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700140 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 Chartier5f51d4b2013-12-03 14:24:05 -0800149 System.out.println("Finishing worker for " + id);
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700150 }
151 };
152 }
153
154 // The notifier thread is a daemon just loops forever to wake
155 // up threads in Operation.WAIT
Mathieu Chartier5f51d4b2013-12-03 14:24:05 -0800156 Thread notifier = new Thread("Notifier") {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700157 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 Hughes831afe42011-12-15 17:27:34 -0800205 case SIGQUIT: {
206 try {
Elliott Hughes99899602014-04-28 11:25:17 -0700207 Os.kill(Os.getpid(), OsConstants.SIGQUIT);
Elliott Hughes831afe42011-12-15 17:27:34 -0800208 } catch (ErrnoException ex) {
209 }
210 }
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800211 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 Carlstrom7c6deaa2011-10-21 12:05:06 -0700226 case WAIT: {
227 synchronized (lock) {
228 try {
229 lock.wait();
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800230 } catch (InterruptedException ignored) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700231 }
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 Carlstrom4514d3c2011-10-21 17:01:31 -0700246 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 Carlstrom7c6deaa2011-10-21 12:05:06 -0700252 }
253 break;
254 }
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800255 case STACKTRACE: {
256 Thread.currentThread().getStackTrace();
257 break;
258 }
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700259 default: {
260 throw new AssertionError(operation.toString());
261 }
262 }
263 }
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700264 } finally {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700265 if (DEBUG) {
266 System.out.println("Finishing ThreadStress for " + id);
267 }
268 }
269 }
270}