1 /*
2  * Copyright (C) 2017 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.lang.reflect.InvocationHandler;
18 import java.lang.reflect.Method;
19 import java.lang.reflect.Proxy;
20 
21 public class Main {
22     static final int numberOfThreads = 5;
23     static final int totalOperations = 10000;
24 
25     final static Object lockObject = new Object();
26     static SimpleInterface inf;
27     static volatile boolean finish = false;
28 
main(String[] args)29     public static void main(String[] args) throws Exception {
30         inf = (SimpleInterface)Proxy.newProxyInstance(SimpleInterface.class.getClassLoader(),
31             new Class[] { SimpleInterface.class }, new EmptyInvocationHandler());
32 
33         Thread garbageThread = new Thread(new GarbageRunner());
34         garbageThread.start();
35 
36         final Thread[] threads = new Thread[numberOfThreads];
37         for (int t = 0; t < threads.length; t++) {
38             threads[t] = new Thread((t % 2 == 0) ? new ProxyRunner() : new SyncRunner());
39         }
40         for (Thread t : threads) {
41             t.start();
42         }
43 
44         // Now wait.
45         for (Thread t : threads) {
46             t.join();
47         }
48         finish = true;
49         garbageThread.join();
50     }
51 
52     private static interface SimpleInterface {
53         // Add some primitives to force some allocation when calling.
foo(int i1, int i2, int i3, int i4, int i5, int i6)54         public void foo(int i1, int i2, int i3, int i4, int i5, int i6);
55     }
56 
57     private static class EmptyInvocationHandler implements InvocationHandler {
invoke(Object proxy, Method method, Object[] args)58         public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
59             return null;
60         }
61     }
62 
63     private static class ProxyRunner implements Runnable {
run()64         public void run() {
65             int count = totalOperations;
66             while (count > 0) {
67                 synchronized (lockObject) {
68                     try {
69                         inf.foo(10000 - count, 11000 - count, 12000 - count, 13000 - count,
70                                 14000 - count, 15000 - count);
71                     } catch (OutOfMemoryError e) {
72                         // Ignore errors. This is the test for b/69121347 - see an exception
73                         // instead of native abort.
74                     }
75               }
76               count--;
77             }
78         }
79     }
80 
81     private static class SyncRunner implements Runnable {
run()82         public void run() {
83             int count = totalOperations;
84             while (count > 0) {
85                 synchronized (lockObject) {
86                     // "Wait" a small amount of time.
87                     long start = System.nanoTime();
88                     long delta = 10 * 1000;  // 10 us.
89                     long elapsed;
90                     do {
91                       elapsed = System.nanoTime();
92                     } while (elapsed - start < delta);
93                 }
94                 count--;
95             }
96         }
97     }
98 
99     private static class GarbageRunner implements Runnable {
run()100         public void run() {
101             while (!finish) {
102                 // Some random allocations adding up to almost 2M.
103                 for (int i = 0; i < 188; i++) {
104                     try {
105                         byte b[] = new byte[i * 100 + 10];
106                     } catch (OutOfMemoryError e) {
107                         // Ignore. This is just to improve chances that an OOME is thrown during
108                         // proxy invocation.
109                     }
110                 }
111                 try {
112                     Thread.sleep(10);
113                 } catch (Exception e) {
114                 }
115             }
116         }
117     }
118 }
119