blob: 5389c2041a824f9acac77202168755ca35e8957c [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
17import java.util.ArrayList;
18import java.util.Arrays;
19import java.util.Collections;
20import java.util.HashMap;
21import java.util.List;
22import java.util.Map;
Elliott Hughes831afe42011-12-15 17:27:34 -080023import libcore.io.*;
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -070024
25// Run on host with:
26// javac ThreadTest.java && java ThreadStress && rm *.class
27class ThreadStress implements Runnable {
28
Brian Carlstrom4514d3c2011-10-21 17:01:31 -070029 public static final boolean DEBUG = false;
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -070030
31 enum Operation {
32 OOM(1),
Elliott Hughes831afe42011-12-15 17:27:34 -080033 SIGQUIT(19),
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080034 ALLOC(60),
35 STACKTRACE(20),
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -070036 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 Hughes831afe42011-12-15 17:27:34 -0800200 case SIGQUIT: {
201 try {
202 Libcore.os.kill(Libcore.os.getpid(), OsConstants.SIGQUIT);
203 } catch (ErrnoException ex) {
204 }
205 }
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700206 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 Carlstrom4514d3c2011-10-21 17:01:31 -0700226 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 Carlstrom7c6deaa2011-10-21 12:05:06 -0700232 }
233 break;
234 }
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800235 case STACKTRACE: {
236 Thread.currentThread().getStackTrace();
237 break;
238 }
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700239 default: {
240 throw new AssertionError(operation.toString());
241 }
242 }
243 }
Brian Carlstrom4514d3c2011-10-21 17:01:31 -0700244 } finally {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700245 if (DEBUG) {
246 System.out.println("Finishing ThreadStress for " + id);
247 }
248 }
249 }
250}