1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws Exception {
// Use a list to ensure objects must be allocated.
ArrayList<Object> l = new ArrayList<>(100);
prefetchClassNames();
doTest(l);
}
// Pre-resolve class names so the strings don't have to be allocated as a side effect of
// callback printing.
private static void prefetchClassNames() {
Object.class.getName();
Integer.class.getName();
Float.class.getName();
Short.class.getName();
Byte.class.getName();
Double.class.getName();
}
public static void doTest(ArrayList<Object> l) throws Exception {
// Disable the global registration from OnLoad, to get into a known state.
enableAllocationTracking(null, false);
// Enable actual logging callback.
setupObjectAllocCallback(true);
enableAllocationTracking(null, true);
l.add(new Object());
l.add(new Integer(1));
enableAllocationTracking(null, false);
l.add(new Float(1.0f));
enableAllocationTracking(Thread.currentThread(), true);
l.add(new Short((short)0));
enableAllocationTracking(Thread.currentThread(), false);
l.add(new Byte((byte)0));
System.out.println("Tracking on same thread");
testThread(l, true, true);
l.add(new Byte((byte)0));
System.out.println("Tracking on same thread, not disabling tracking");
testThread(l, true, false);
System.out.println("Tracking on different thread");
testThread(l, false, true);
l.add(new Byte((byte)0));
// Disable actual logging callback and re-enable tracking, so we can keep the event enabled and
// check that shutdown works correctly.
setupObjectAllocCallback(false);
enableAllocationTracking(null, true);
}
private static void testThread(final ArrayList<Object> l, final boolean sameThread,
final boolean disableTracking) throws Exception {
final SimpleBarrier startBarrier = new SimpleBarrier(1);
final SimpleBarrier trackBarrier = new SimpleBarrier(1);
final SimpleBarrier disableBarrier = new SimpleBarrier(1);
final Thread thisThread = Thread.currentThread();
Thread t = new Thread() {
public void run() {
try {
startBarrier.dec();
trackBarrier.waitFor();
} catch (Exception e) {
e.printStackTrace(System.out);
System.exit(1);
}
l.add(new Double(0.0));
if (disableTracking) {
enableAllocationTracking(sameThread ? this : thisThread, false);
}
}
};
t.start();
startBarrier.waitFor();
enableAllocationTracking(sameThread ? t : Thread.currentThread(), true);
trackBarrier.dec();
t.join();
}
private static class SimpleBarrier {
int count;
public SimpleBarrier(int i) {
count = i;
}
public synchronized void dec() throws Exception {
count--;
notifyAll();
}
public synchronized void waitFor() throws Exception {
while (count != 0) {
wait();
}
}
}
private static native void setupObjectAllocCallback(boolean enable);
private static native void enableAllocationTracking(Thread thread, boolean enable);
}
|