Brian Carlstrom | 5562174 | 2011-10-17 00:41:56 -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.List; |
Andreas Gampe | 6c957c5 | 2015-01-20 21:26:43 -0800 | [diff] [blame] | 19 | import java.util.concurrent.atomic.AtomicInteger; |
Andreas Gampe | 93f3da1 | 2014-07-26 01:13:13 -0700 | [diff] [blame] | 20 | import java.util.concurrent.CyclicBarrier; |
Brian Carlstrom | 5562174 | 2011-10-17 00:41:56 -0700 | [diff] [blame] | 21 | |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 22 | public class Main implements Runnable { |
Andreas Gampe | 93f3da1 | 2014-07-26 01:13:13 -0700 | [diff] [blame] | 23 | |
Andreas Gampe | ee576fa | 2015-01-15 08:02:22 -0800 | [diff] [blame] | 24 | // 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 Gampe | 6c957c5 | 2015-01-20 21:26:43 -0800 | [diff] [blame] | 26 | private final static long TIMEOUT_VALUE = 7; |
Andreas Gampe | ee576fa | 2015-01-15 08:02:22 -0800 | [diff] [blame] | 27 | |
Andreas Gampe | 6c957c5 | 2015-01-20 21:26:43 -0800 | [diff] [blame] | 28 | 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 Gampe | 93f3da1 | 2014-07-26 01:13:13 -0700 | [diff] [blame] | 36 | |
Brian Carlstrom | 5562174 | 2011-10-17 00:41:56 -0700 | [diff] [blame] | 37 | public static void main(String[] args) throws Exception { |
Andreas Gampe | 6c957c5 | 2015-01-20 21:26:43 -0800 | [diff] [blame] | 38 | Thread[] threads = new Thread[THREAD_COUNT]; |
Andreas Gampe | 93f3da1 | 2014-07-26 01:13:13 -0700 | [diff] [blame] | 39 | |
Andreas Gampe | 6c957c5 | 2015-01-20 21:26:43 -0800 | [diff] [blame] | 40 | // 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 Gampe | 93f3da1 | 2014-07-26 01:13:13 -0700 | [diff] [blame] | 44 | |
| 45 | for (int i = 0; i < threads.length; i++) { |
Andreas Gampe | 6c957c5 | 2015-01-20 21:26:43 -0800 | [diff] [blame] | 46 | threads[i] = new Thread(new Main(startBarrier)); |
| 47 | threads[i].start(); |
Brian Carlstrom | 5562174 | 2011-10-17 00:41:56 -0700 | [diff] [blame] | 48 | } |
Andreas Gampe | 93f3da1 | 2014-07-26 01:13:13 -0700 | [diff] [blame] | 49 | |
| 50 | // Wait for the threads to finish. |
Brian Carlstrom | 5562174 | 2011-10-17 00:41:56 -0700 | [diff] [blame] | 51 | for (Thread thread : threads) { |
| 52 | thread.join(); |
| 53 | } |
Andreas Gampe | 93f3da1 | 2014-07-26 01:13:13 -0700 | [diff] [blame] | 54 | |
| 55 | // Allocate objects to definitely run GC before quitting. |
Hiroshi Yamauchi | 779e705 | 2015-01-26 16:05:11 -0800 | [diff] [blame] | 56 | ArrayList<Object> l = new ArrayList<Object>(); |
Andreas Gampe | 93f3da1 | 2014-07-26 01:13:13 -0700 | [diff] [blame] | 57 | try { |
Andreas Gampe | 6c957c5 | 2015-01-20 21:26:43 -0800 | [diff] [blame] | 58 | for (int i = 0; i < 100000; i++) { |
| 59 | l.add(new ArrayList<Object>(i)); |
Andreas Gampe | 93f3da1 | 2014-07-26 01:13:13 -0700 | [diff] [blame] | 60 | } |
| 61 | } catch (OutOfMemoryError oom) { |
| 62 | } |
Hiroshi Yamauchi | 779e705 | 2015-01-26 16:05:11 -0800 | [diff] [blame] | 63 | // Make the (outer) ArrayList unreachable. Note it may still |
| 64 | // be reachable under an interpreter or a compiler without a |
| 65 | // liveness analysis. |
| 66 | l = null; |
Andreas Gampe | 6c957c5 | 2015-01-20 21:26:43 -0800 | [diff] [blame] | 67 | new ArrayList<Object>(50); |
Andreas Gampe | 93f3da1 | 2014-07-26 01:13:13 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Andreas Gampe | 6c957c5 | 2015-01-20 21:26:43 -0800 | [diff] [blame] | 70 | private Main(CyclicBarrier startBarrier) { |
| 71 | this.startBarrier = startBarrier; |
Andreas Gampe | 93f3da1 | 2014-07-26 01:13:13 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Andreas Gampe | 6c957c5 | 2015-01-20 21:26:43 -0800 | [diff] [blame] | 74 | private ArrayList<Object> store; |
| 75 | private CyclicBarrier startBarrier; |
Brian Carlstrom | 5562174 | 2011-10-17 00:41:56 -0700 | [diff] [blame] | 76 | |
| 77 | public void run() { |
Andreas Gampe | 93f3da1 | 2014-07-26 01:13:13 -0700 | [diff] [blame] | 78 | try { |
| 79 | work(); |
Andreas Gampe | 6c957c5 | 2015-01-20 21:26:43 -0800 | [diff] [blame] | 80 | } catch (Throwable t) { |
| 81 | // Any exception or error getting here is bad. |
| 82 | try { |
| 83 | // May need allocations... |
| 84 | t.printStackTrace(System.err); |
| 85 | } catch (Throwable tInner) { |
| 86 | } |
Andreas Gampe | 93f3da1 | 2014-07-26 01:13:13 -0700 | [diff] [blame] | 87 | System.exit(1); |
Brian Carlstrom | 5562174 | 2011-10-17 00:41:56 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
Andreas Gampe | 21b4bf8 | 2014-07-25 16:37:09 -0700 | [diff] [blame] | 90 | |
Andreas Gampe | 6c957c5 | 2015-01-20 21:26:43 -0800 | [diff] [blame] | 91 | private void work() throws Exception { |
| 92 | // Any exceptions except an OOME in the allocation loop are bad and handed off to the |
| 93 | // caller which should abort the whole runtime. |
| 94 | |
Andreas Gampe | 93f3da1 | 2014-07-26 01:13:13 -0700 | [diff] [blame] | 95 | ArrayList<Object> l = new ArrayList<Object>(); |
Andreas Gampe | 6c957c5 | 2015-01-20 21:26:43 -0800 | [diff] [blame] | 96 | store = l; // Keep it alive. |
Andreas Gampe | 93f3da1 | 2014-07-26 01:13:13 -0700 | [diff] [blame] | 97 | |
Andreas Gampe | 6c957c5 | 2015-01-20 21:26:43 -0800 | [diff] [blame] | 98 | // Wait for the start signal. |
| 99 | startBarrier.await(TIMEOUT_VALUE, java.util.concurrent.TimeUnit.MINUTES); |
Andreas Gampe | 93f3da1 | 2014-07-26 01:13:13 -0700 | [diff] [blame] | 100 | |
Andreas Gampe | 6c957c5 | 2015-01-20 21:26:43 -0800 | [diff] [blame] | 101 | // Allocate. |
| 102 | try { |
| 103 | for (int i = 0; i < MAX_SIZE; i++) { |
| 104 | l.add(new ArrayList<Object>(i)); |
Andreas Gampe | 93f3da1 | 2014-07-26 01:13:13 -0700 | [diff] [blame] | 105 | } |
Andreas Gampe | 6c957c5 | 2015-01-20 21:26:43 -0800 | [diff] [blame] | 106 | } catch (OutOfMemoryError oome) { |
| 107 | // Fine, we're done. |
Andreas Gampe | 93f3da1 | 2014-07-26 01:13:13 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Andreas Gampe | 6c957c5 | 2015-01-20 21:26:43 -0800 | [diff] [blame] | 110 | // Atomically increment the counter and check whether we were last. |
| 111 | int number = counter.incrementAndGet(); |
Andreas Gampe | 93f3da1 | 2014-07-26 01:13:13 -0700 | [diff] [blame] | 112 | |
Andreas Gampe | 6c957c5 | 2015-01-20 21:26:43 -0800 | [diff] [blame] | 113 | if (number < THREAD_COUNT) { |
| 114 | // Not last. |
| 115 | synchronized (gate) { |
| 116 | // Increment the wait counter. |
| 117 | waitCount++; |
| 118 | gate.wait(TIMEOUT_VALUE * 1000 * 60); |
| 119 | } |
| 120 | } else { |
| 121 | // Last. Wait until waitCount == THREAD_COUNT - 1. |
| 122 | for (int loops = 0; ; loops++) { |
| 123 | synchronized (gate) { |
| 124 | if (waitCount == THREAD_COUNT - 1) { |
| 125 | // OK, everyone's waiting. Notify and break out. |
| 126 | gate.notifyAll(); |
| 127 | break; |
| 128 | } else if (loops > 40) { |
| 129 | // 1s wait, too many tries. |
| 130 | System.out.println("Waited too long for the last thread."); |
| 131 | System.exit(1); |
| 132 | } |
| 133 | } |
| 134 | // Wait a bit. |
| 135 | Thread.sleep(25); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | store = null; // Allow GC to reclaim it. |
Andreas Gampe | 93f3da1 | 2014-07-26 01:13:13 -0700 | [diff] [blame] | 140 | } |
Brian Carlstrom | 5562174 | 2011-10-17 00:41:56 -0700 | [diff] [blame] | 141 | } |