blob: 2199872ba6f08e6832b8ab84f61498ef338c20c1 [file] [log] [blame]
Brian Carlstrom55621742011-10-17 00:41:56 -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.List;
Andreas Gampe6c957c52015-01-20 21:26:43 -080019import java.util.concurrent.atomic.AtomicInteger;
Andreas Gampe93f3da12014-07-26 01:13:13 -070020import java.util.concurrent.CyclicBarrier;
Brian Carlstrom55621742011-10-17 00:41:56 -070021
Andreas Gampe1c83cbc2014-07-22 18:52:29 -070022public class Main implements Runnable {
Andreas Gampe93f3da12014-07-26 01:13:13 -070023
Andreas Gampeee576fa2015-01-15 08:02:22 -080024 // Timeout in minutes. Make it larger than the run-test timeout to get a native thread dump by
25 // ART on timeout when running on the host.
Andreas Gampe6c957c52015-01-20 21:26:43 -080026 private final static long TIMEOUT_VALUE = 7;
Andreas Gampeee576fa2015-01-15 08:02:22 -080027
Andreas Gampe6c957c52015-01-20 21:26:43 -080028 private final static long MAX_SIZE = 1000; // Maximum size of array-list to allocate.
29
30 private final static int THREAD_COUNT = 16;
31
32 // Use a couple of different forms of synchronizing to test some of these...
33 private final static AtomicInteger counter = new AtomicInteger();
34 private final static Object gate = new Object();
35 private volatile static int waitCount = 0;
Andreas Gampe93f3da12014-07-26 01:13:13 -070036
Brian Carlstrom55621742011-10-17 00:41:56 -070037 public static void main(String[] args) throws Exception {
Andreas Gampe6c957c52015-01-20 21:26:43 -080038 Thread[] threads = new Thread[THREAD_COUNT];
Andreas Gampe93f3da12014-07-26 01:13:13 -070039
Andreas Gampe6c957c52015-01-20 21:26:43 -080040 // This barrier is used to synchronize the threads starting to allocate.
41 // Note: Even though a barrier is not allocation-free, this one is fine, as it will be used
42 // before filling the heap.
43 CyclicBarrier startBarrier = new CyclicBarrier(threads.length);
Andreas Gampe93f3da12014-07-26 01:13:13 -070044
45 for (int i = 0; i < threads.length; i++) {
Andreas Gampe6c957c52015-01-20 21:26:43 -080046 threads[i] = new Thread(new Main(startBarrier));
47 threads[i].start();
Brian Carlstrom55621742011-10-17 00:41:56 -070048 }
Andreas Gampe93f3da12014-07-26 01:13:13 -070049
50 // Wait for the threads to finish.
Brian Carlstrom55621742011-10-17 00:41:56 -070051 for (Thread thread : threads) {
52 thread.join();
53 }
Andreas Gampe93f3da12014-07-26 01:13:13 -070054
55 // Allocate objects to definitely run GC before quitting.
Sebastien Hertz19ac0272015-02-24 17:39:50 +010056 allocateObjectsToRunGc();
57
Andreas Gampe6c957c52015-01-20 21:26:43 -080058 new ArrayList<Object>(50);
Andreas Gampe93f3da12014-07-26 01:13:13 -070059 }
60
Sebastien Hertz19ac0272015-02-24 17:39:50 +010061 private static void allocateObjectsToRunGc() {
62 ArrayList<Object> l = new ArrayList<Object>();
63 try {
64 for (int i = 0; i < 100000; i++) {
65 l.add(new ArrayList<Object>(i));
66 }
67 } catch (OutOfMemoryError oom) {
68 }
69 }
70
Andreas Gampe6c957c52015-01-20 21:26:43 -080071 private Main(CyclicBarrier startBarrier) {
72 this.startBarrier = startBarrier;
Andreas Gampe93f3da12014-07-26 01:13:13 -070073 }
74
Andreas Gampe6c957c52015-01-20 21:26:43 -080075 private ArrayList<Object> store;
76 private CyclicBarrier startBarrier;
Brian Carlstrom55621742011-10-17 00:41:56 -070077
78 public void run() {
Andreas Gampe93f3da12014-07-26 01:13:13 -070079 try {
80 work();
Andreas Gampe6c957c52015-01-20 21:26:43 -080081 } catch (Throwable t) {
82 // Any exception or error getting here is bad.
83 try {
84 // May need allocations...
Kevin Brodskyf6c66c32015-12-17 14:13:00 +000085 t.printStackTrace(System.out);
Andreas Gampe6c957c52015-01-20 21:26:43 -080086 } catch (Throwable tInner) {
87 }
Andreas Gampe93f3da12014-07-26 01:13:13 -070088 System.exit(1);
Brian Carlstrom55621742011-10-17 00:41:56 -070089 }
90 }
Andreas Gampe21b4bf82014-07-25 16:37:09 -070091
Andreas Gampe6c957c52015-01-20 21:26:43 -080092 private void work() throws Exception {
93 // Any exceptions except an OOME in the allocation loop are bad and handed off to the
94 // caller which should abort the whole runtime.
95
Andreas Gampe93f3da12014-07-26 01:13:13 -070096 ArrayList<Object> l = new ArrayList<Object>();
Andreas Gampe6c957c52015-01-20 21:26:43 -080097 store = l; // Keep it alive.
Andreas Gampe93f3da12014-07-26 01:13:13 -070098
Andreas Gampe6c957c52015-01-20 21:26:43 -080099 // Wait for the start signal.
100 startBarrier.await(TIMEOUT_VALUE, java.util.concurrent.TimeUnit.MINUTES);
Andreas Gampe93f3da12014-07-26 01:13:13 -0700101
Andreas Gampe6c957c52015-01-20 21:26:43 -0800102 // Allocate.
103 try {
104 for (int i = 0; i < MAX_SIZE; i++) {
105 l.add(new ArrayList<Object>(i));
Andreas Gampe93f3da12014-07-26 01:13:13 -0700106 }
Andreas Gampe6c957c52015-01-20 21:26:43 -0800107 } catch (OutOfMemoryError oome) {
108 // Fine, we're done.
Andreas Gampe93f3da12014-07-26 01:13:13 -0700109 }
110
Andreas Gampe6c957c52015-01-20 21:26:43 -0800111 // Atomically increment the counter and check whether we were last.
112 int number = counter.incrementAndGet();
Andreas Gampe93f3da12014-07-26 01:13:13 -0700113
Andreas Gampe6c957c52015-01-20 21:26:43 -0800114 if (number < THREAD_COUNT) {
115 // Not last.
116 synchronized (gate) {
117 // Increment the wait counter.
118 waitCount++;
119 gate.wait(TIMEOUT_VALUE * 1000 * 60);
120 }
121 } else {
122 // Last. Wait until waitCount == THREAD_COUNT - 1.
123 for (int loops = 0; ; loops++) {
124 synchronized (gate) {
125 if (waitCount == THREAD_COUNT - 1) {
126 // OK, everyone's waiting. Notify and break out.
127 gate.notifyAll();
128 break;
129 } else if (loops > 40) {
130 // 1s wait, too many tries.
131 System.out.println("Waited too long for the last thread.");
132 System.exit(1);
133 }
134 }
135 // Wait a bit.
136 Thread.sleep(25);
137 }
138 }
139
140 store = null; // Allow GC to reclaim it.
Andreas Gampe93f3da12014-07-26 01:13:13 -0700141 }
Brian Carlstrom55621742011-10-17 00:41:56 -0700142}