1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2017 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.InvocationHandler; 18*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Method; 19*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Proxy; 20*795d594fSAndroid Build Coastguard Worker 21*795d594fSAndroid Build Coastguard Worker // Note that this is run with a tiny heap. 22*795d594fSAndroid Build Coastguard Worker // See b/260228356 for some discussion. It's unclear if and how this reliably forces an 23*795d594fSAndroid Build Coastguard Worker // OOME. For now, we're keeping this around because it appears to have detected some bugs 24*795d594fSAndroid Build Coastguard Worker // in the past. It may need revisiting. 25*795d594fSAndroid Build Coastguard Worker 26*795d594fSAndroid Build Coastguard Worker public class Main { 27*795d594fSAndroid Build Coastguard Worker static final int numberOfThreads = 5; 28*795d594fSAndroid Build Coastguard Worker static final int totalOperations = 10000; 29*795d594fSAndroid Build Coastguard Worker 30*795d594fSAndroid Build Coastguard Worker final static Object lockObject = new Object(); 31*795d594fSAndroid Build Coastguard Worker static SimpleInterface inf; 32*795d594fSAndroid Build Coastguard Worker static volatile boolean finish = false; 33*795d594fSAndroid Build Coastguard Worker main(String[] args)34*795d594fSAndroid Build Coastguard Worker public static void main(String[] args) throws Exception { 35*795d594fSAndroid Build Coastguard Worker // Wait for system daemons to start, so that we minimize the chances of 36*795d594fSAndroid Build Coastguard Worker // a system daemon still starting up if/when we run out of memory. 37*795d594fSAndroid Build Coastguard Worker try { 38*795d594fSAndroid Build Coastguard Worker Thread.sleep(100); 39*795d594fSAndroid Build Coastguard Worker } catch (InterruptedException e) { 40*795d594fSAndroid Build Coastguard Worker System.out.println("Unexpected interrupt:" + e); 41*795d594fSAndroid Build Coastguard Worker } 42*795d594fSAndroid Build Coastguard Worker 43*795d594fSAndroid Build Coastguard Worker inf = (SimpleInterface)Proxy.newProxyInstance(SimpleInterface.class.getClassLoader(), 44*795d594fSAndroid Build Coastguard Worker new Class[] { SimpleInterface.class }, new EmptyInvocationHandler()); 45*795d594fSAndroid Build Coastguard Worker Thread garbageThread = new Thread(new GarbageRunner()); 46*795d594fSAndroid Build Coastguard Worker garbageThread.start(); 47*795d594fSAndroid Build Coastguard Worker 48*795d594fSAndroid Build Coastguard Worker final Thread[] threads = new Thread[numberOfThreads]; 49*795d594fSAndroid Build Coastguard Worker for (int t = 0; t < threads.length; t++) { 50*795d594fSAndroid Build Coastguard Worker threads[t] = new Thread((t % 2 == 0) ? new ProxyRunner() : new SyncRunner()); 51*795d594fSAndroid Build Coastguard Worker } 52*795d594fSAndroid Build Coastguard Worker for (Thread t : threads) { 53*795d594fSAndroid Build Coastguard Worker t.start(); 54*795d594fSAndroid Build Coastguard Worker } 55*795d594fSAndroid Build Coastguard Worker 56*795d594fSAndroid Build Coastguard Worker // Now wait. 57*795d594fSAndroid Build Coastguard Worker for (Thread t : threads) { 58*795d594fSAndroid Build Coastguard Worker t.join(); 59*795d594fSAndroid Build Coastguard Worker } 60*795d594fSAndroid Build Coastguard Worker finish = true; 61*795d594fSAndroid Build Coastguard Worker garbageThread.join(); 62*795d594fSAndroid Build Coastguard Worker } 63*795d594fSAndroid Build Coastguard Worker 64*795d594fSAndroid Build Coastguard Worker private static interface SimpleInterface { 65*795d594fSAndroid Build Coastguard Worker // Add some primitives to force some allocation when calling. foo(int i1, int i2, int i3, int i4, int i5, int i6)66*795d594fSAndroid Build Coastguard Worker public void foo(int i1, int i2, int i3, int i4, int i5, int i6); 67*795d594fSAndroid Build Coastguard Worker } 68*795d594fSAndroid Build Coastguard Worker 69*795d594fSAndroid Build Coastguard Worker private static class EmptyInvocationHandler implements InvocationHandler { invoke(Object proxy, Method method, Object[] args)70*795d594fSAndroid Build Coastguard Worker public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 71*795d594fSAndroid Build Coastguard Worker return null; 72*795d594fSAndroid Build Coastguard Worker } 73*795d594fSAndroid Build Coastguard Worker } 74*795d594fSAndroid Build Coastguard Worker 75*795d594fSAndroid Build Coastguard Worker private static class ProxyRunner implements Runnable { run()76*795d594fSAndroid Build Coastguard Worker public void run() { 77*795d594fSAndroid Build Coastguard Worker int count = totalOperations; 78*795d594fSAndroid Build Coastguard Worker while (count > 0) { 79*795d594fSAndroid Build Coastguard Worker synchronized (lockObject) { 80*795d594fSAndroid Build Coastguard Worker try { 81*795d594fSAndroid Build Coastguard Worker inf.foo(10000 - count, 11000 - count, 12000 - count, 13000 - count, 82*795d594fSAndroid Build Coastguard Worker 14000 - count, 15000 - count); 83*795d594fSAndroid Build Coastguard Worker } catch (OutOfMemoryError e) { 84*795d594fSAndroid Build Coastguard Worker // Ignore errors. This is the test for b/69121347 - see an exception 85*795d594fSAndroid Build Coastguard Worker // instead of native abort. 86*795d594fSAndroid Build Coastguard Worker } 87*795d594fSAndroid Build Coastguard Worker } 88*795d594fSAndroid Build Coastguard Worker count--; 89*795d594fSAndroid Build Coastguard Worker } 90*795d594fSAndroid Build Coastguard Worker } 91*795d594fSAndroid Build Coastguard Worker } 92*795d594fSAndroid Build Coastguard Worker 93*795d594fSAndroid Build Coastguard Worker private static class SyncRunner implements Runnable { run()94*795d594fSAndroid Build Coastguard Worker public void run() { 95*795d594fSAndroid Build Coastguard Worker int count = totalOperations; 96*795d594fSAndroid Build Coastguard Worker while (count > 0) { 97*795d594fSAndroid Build Coastguard Worker synchronized (lockObject) { 98*795d594fSAndroid Build Coastguard Worker // "Wait" a small amount of time. 99*795d594fSAndroid Build Coastguard Worker long start = System.nanoTime(); 100*795d594fSAndroid Build Coastguard Worker long delta = 10 * 1000; // 10 us. 101*795d594fSAndroid Build Coastguard Worker long elapsed; 102*795d594fSAndroid Build Coastguard Worker do { 103*795d594fSAndroid Build Coastguard Worker elapsed = System.nanoTime(); 104*795d594fSAndroid Build Coastguard Worker } while (elapsed - start < delta); 105*795d594fSAndroid Build Coastguard Worker } 106*795d594fSAndroid Build Coastguard Worker count--; 107*795d594fSAndroid Build Coastguard Worker } 108*795d594fSAndroid Build Coastguard Worker } 109*795d594fSAndroid Build Coastguard Worker } 110*795d594fSAndroid Build Coastguard Worker 111*795d594fSAndroid Build Coastguard Worker private static class GarbageRunner implements Runnable { run()112*795d594fSAndroid Build Coastguard Worker public void run() { 113*795d594fSAndroid Build Coastguard Worker while (!finish) { 114*795d594fSAndroid Build Coastguard Worker // Some random allocations adding up to almost 2M. 115*795d594fSAndroid Build Coastguard Worker for (int i = 0; i < 188; i++) { 116*795d594fSAndroid Build Coastguard Worker try { 117*795d594fSAndroid Build Coastguard Worker byte b[] = new byte[i * 100 + 10]; 118*795d594fSAndroid Build Coastguard Worker } catch (OutOfMemoryError e) { 119*795d594fSAndroid Build Coastguard Worker // Ignore. This is just to improve chances that an OOME is thrown during 120*795d594fSAndroid Build Coastguard Worker // proxy invocation. 121*795d594fSAndroid Build Coastguard Worker } 122*795d594fSAndroid Build Coastguard Worker } 123*795d594fSAndroid Build Coastguard Worker try { 124*795d594fSAndroid Build Coastguard Worker Thread.sleep(10); 125*795d594fSAndroid Build Coastguard Worker } catch (Exception e) { 126*795d594fSAndroid Build Coastguard Worker System.out.println("Unexpected exception in sleep():" + e); 127*795d594fSAndroid Build Coastguard Worker } 128*795d594fSAndroid Build Coastguard Worker } 129*795d594fSAndroid Build Coastguard Worker } 130*795d594fSAndroid Build Coastguard Worker } 131*795d594fSAndroid Build Coastguard Worker } 132