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