blob: a2848fb917d29a887ffa79e5a40ec2b0780d362e [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
Andreas Gampe46651672017-04-07 09:00:04 -070017package art;
18
Andreas Gampe27fa96c2016-10-07 15:05:24 -070019import java.util.ArrayList;
Andreas Gampe115b4982017-04-12 19:06:12 -070020import java.util.Arrays;
Andreas Gampe27fa96c2016-10-07 15:05:24 -070021
Andreas Gampe46651672017-04-07 09:00:04 -070022public class Test904 {
23 public static void run() throws Exception {
Andreas Gampe27fa96c2016-10-07 15:05:24 -070024 // Use a list to ensure objects must be allocated.
25 ArrayList<Object> l = new ArrayList<>(100);
26
Andreas Gamped6d3f0e2016-10-10 20:01:41 -070027 prefetchClassNames();
28
Andreas Gampe27fa96c2016-10-07 15:05:24 -070029 doTest(l);
30 }
31
Andreas Gamped6d3f0e2016-10-10 20:01:41 -070032 // Pre-resolve class names so the strings don't have to be allocated as a side effect of
33 // callback printing.
34 private static void prefetchClassNames() {
35 Object.class.getName();
36 Integer.class.getName();
37 Float.class.getName();
38 Short.class.getName();
39 Byte.class.getName();
40 Double.class.getName();
41 }
42
Andreas Gampe27fa96c2016-10-07 15:05:24 -070043 public static void doTest(ArrayList<Object> l) throws Exception {
Andreas Gampe2427aae2016-10-17 18:05:19 -070044 // Disable the global registration from OnLoad, to get into a known state.
45 enableAllocationTracking(null, false);
46
47 // Enable actual logging callback.
48 setupObjectAllocCallback(true);
Andreas Gampe27fa96c2016-10-07 15:05:24 -070049
Alex Lightd4102ba2018-04-30 12:47:22 -070050 System.out.println(Arrays.toString(getTrackingEventMessages(
51 new Thread[] { Thread.currentThread(), })));
Andreas Gampe115b4982017-04-12 19:06:12 -070052
Andreas Gampe27fa96c2016-10-07 15:05:24 -070053 enableAllocationTracking(null, true);
54
55 l.add(new Object());
56 l.add(new Integer(1));
57
58 enableAllocationTracking(null, false);
59
60 l.add(new Float(1.0f));
61
62 enableAllocationTracking(Thread.currentThread(), true);
63
64 l.add(new Short((short)0));
65
66 enableAllocationTracking(Thread.currentThread(), false);
67
68 l.add(new Byte((byte)0));
69
Alex Lightd4102ba2018-04-30 12:47:22 -070070 System.out.println(Arrays.toString(getTrackingEventMessages(
71 new Thread[] { Thread.currentThread(), })));
Andreas Gampe27fa96c2016-10-07 15:05:24 -070072 System.out.println("Tracking on same thread");
73
Alex Lightd4102ba2018-04-30 12:47:22 -070074 Thread test_thread = testThread(l, true, true);
Andreas Gampe27fa96c2016-10-07 15:05:24 -070075
76 l.add(new Byte((byte)0));
77
Alex Lightd4102ba2018-04-30 12:47:22 -070078 System.out.println(Arrays.toString(getTrackingEventMessages(
79 new Thread[] { Thread.currentThread(), test_thread, })));
Andreas Gampe27fa96c2016-10-07 15:05:24 -070080 System.out.println("Tracking on same thread, not disabling tracking");
81
Alex Lightd4102ba2018-04-30 12:47:22 -070082 test_thread = testThread(l, true, false);
Andreas Gampe27fa96c2016-10-07 15:05:24 -070083
Alex Lightd4102ba2018-04-30 12:47:22 -070084 System.out.println(Arrays.toString(getTrackingEventMessages(
85 new Thread[] { Thread.currentThread(), test_thread, })));
Andreas Gampe27fa96c2016-10-07 15:05:24 -070086 System.out.println("Tracking on different thread");
87
Alex Lightd4102ba2018-04-30 12:47:22 -070088 test_thread = testThread(l, false, true);
Andreas Gampe27fa96c2016-10-07 15:05:24 -070089
90 l.add(new Byte((byte)0));
Andreas Gampe2427aae2016-10-17 18:05:19 -070091
92 // Disable actual logging callback and re-enable tracking, so we can keep the event enabled and
93 // check that shutdown works correctly.
94 setupObjectAllocCallback(false);
Andreas Gampe115b4982017-04-12 19:06:12 -070095
Alex Lightd4102ba2018-04-30 12:47:22 -070096 System.out.println(Arrays.toString(getTrackingEventMessages(
97 new Thread[] { Thread.currentThread(), test_thread, })));
Andreas Gampe115b4982017-04-12 19:06:12 -070098
Andreas Gampe2427aae2016-10-17 18:05:19 -070099 enableAllocationTracking(null, true);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700100 }
101
Alex Lightd4102ba2018-04-30 12:47:22 -0700102 private static Thread testThread(final ArrayList<Object> l, final boolean sameThread,
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700103 final boolean disableTracking) throws Exception {
104 final SimpleBarrier startBarrier = new SimpleBarrier(1);
105 final SimpleBarrier trackBarrier = new SimpleBarrier(1);
106 final SimpleBarrier disableBarrier = new SimpleBarrier(1);
107
Andreas Gampe2427aae2016-10-17 18:05:19 -0700108 final Thread thisThread = Thread.currentThread();
109
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700110 Thread t = new Thread() {
111 public void run() {
112 try {
113 startBarrier.dec();
114 trackBarrier.waitFor();
115 } catch (Exception e) {
116 e.printStackTrace(System.out);
117 System.exit(1);
118 }
119
120 l.add(new Double(0.0));
121
122 if (disableTracking) {
Andreas Gampe2427aae2016-10-17 18:05:19 -0700123 enableAllocationTracking(sameThread ? this : thisThread, false);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700124 }
125 }
126 };
127
128 t.start();
129 startBarrier.waitFor();
130 enableAllocationTracking(sameThread ? t : Thread.currentThread(), true);
131 trackBarrier.dec();
132
133 t.join();
Alex Lightd4102ba2018-04-30 12:47:22 -0700134 return t;
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700135 }
136
137 private static class SimpleBarrier {
138 int count;
139
140 public SimpleBarrier(int i) {
141 count = i;
142 }
143
144 public synchronized void dec() throws Exception {
145 count--;
146 notifyAll();
147 }
148
149 public synchronized void waitFor() throws Exception {
150 while (count != 0) {
151 wait();
152 }
153 }
154 }
155
Andreas Gampe2427aae2016-10-17 18:05:19 -0700156 private static native void setupObjectAllocCallback(boolean enable);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700157 private static native void enableAllocationTracking(Thread thread, boolean enable);
Alex Lightd4102ba2018-04-30 12:47:22 -0700158 private static native String[] getTrackingEventMessages(Thread[] threads);
Andreas Gampe27fa96c2016-10-07 15:05:24 -0700159}