blob: fc8a112e490d7af8d965cf76114fe4767fb10f96 [file] [log] [blame]
Andreas Gampe27fa96c2016-10-07 15:05:24 -07001/*
2 * Copyright (C) 2016 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;
18
19public class Main {
20 public static void main(String[] args) throws Exception {
21 System.loadLibrary(args[1]);
22
23 // Use a list to ensure objects must be allocated.
24 ArrayList<Object> l = new ArrayList<>(100);
25
Andreas Gamped6d3f0e2016-10-10 20:01:41 -070026 prefetchClassNames();
27
Andreas Gampe27fa96c2016-10-07 15:05:24 -070028 doTest(l);
29 }
30
Andreas Gamped6d3f0e2016-10-10 20:01:41 -070031 // Pre-resolve class names so the strings don't have to be allocated as a side effect of
32 // callback printing.
33 private static void prefetchClassNames() {
34 Object.class.getName();
35 Integer.class.getName();
36 Float.class.getName();
37 Short.class.getName();
38 Byte.class.getName();
39 Double.class.getName();
40 }
41
Andreas Gampe27fa96c2016-10-07 15:05:24 -070042 public static void doTest(ArrayList<Object> l) throws Exception {
Andreas Gampe2427aae2016-10-17 18:05:19 -070043 // Disable the global registration from OnLoad, to get into a known state.
44 enableAllocationTracking(null, false);
45
46 // Enable actual logging callback.
47 setupObjectAllocCallback(true);
Andreas Gampe27fa96c2016-10-07 15:05:24 -070048
49 enableAllocationTracking(null, true);
50
51 l.add(new Object());
52 l.add(new Integer(1));
53
54 enableAllocationTracking(null, false);
55
56 l.add(new Float(1.0f));
57
58 enableAllocationTracking(Thread.currentThread(), true);
59
60 l.add(new Short((short)0));
61
62 enableAllocationTracking(Thread.currentThread(), false);
63
64 l.add(new Byte((byte)0));
65
66 System.out.println("Tracking on same thread");
67
68 testThread(l, true, true);
69
70 l.add(new Byte((byte)0));
71
72 System.out.println("Tracking on same thread, not disabling tracking");
73
74 testThread(l, true, false);
75
76 System.out.println("Tracking on different thread");
77
78 testThread(l, false, true);
79
80 l.add(new Byte((byte)0));
Andreas Gampe2427aae2016-10-17 18:05:19 -070081
82 // Disable actual logging callback and re-enable tracking, so we can keep the event enabled and
83 // check that shutdown works correctly.
84 setupObjectAllocCallback(false);
85 enableAllocationTracking(null, true);
Andreas Gampe27fa96c2016-10-07 15:05:24 -070086 }
87
88 private static void testThread(final ArrayList<Object> l, final boolean sameThread,
89 final boolean disableTracking) throws Exception {
90 final SimpleBarrier startBarrier = new SimpleBarrier(1);
91 final SimpleBarrier trackBarrier = new SimpleBarrier(1);
92 final SimpleBarrier disableBarrier = new SimpleBarrier(1);
93
Andreas Gampe2427aae2016-10-17 18:05:19 -070094 final Thread thisThread = Thread.currentThread();
95
Andreas Gampe27fa96c2016-10-07 15:05:24 -070096 Thread t = new Thread() {
97 public void run() {
98 try {
99 startBarrier.dec();
100 trackBarrier.waitFor();
101 } catch (Exception e) {
102 e.printStackTrace(System.out);
103 System.exit(1);
104 }
105
106 l.add(new Double(0.0));
107
108 if (disableTracking) {
Andreas Gampe2427aae2016-10-17 18:05:19 -0700109 enableAllocationTracking(sameThread ? this : thisThread, false);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700110 }
111 }
112 };
113
114 t.start();
115 startBarrier.waitFor();
116 enableAllocationTracking(sameThread ? t : Thread.currentThread(), true);
117 trackBarrier.dec();
118
119 t.join();
120 }
121
122 private static class SimpleBarrier {
123 int count;
124
125 public SimpleBarrier(int i) {
126 count = i;
127 }
128
129 public synchronized void dec() throws Exception {
130 count--;
131 notifyAll();
132 }
133
134 public synchronized void waitFor() throws Exception {
135 while (count != 0) {
136 wait();
137 }
138 }
139 }
140
Andreas Gampe2427aae2016-10-17 18:05:19 -0700141 private static native void setupObjectAllocCallback(boolean enable);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700142 private static native void enableAllocationTracking(Thread thread, boolean enable);
143}