Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | import java.util.ArrayList; |
| 18 | |
| 19 | public class Main { |
| 20 | public static void main(String[] args) throws Exception { |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 21 | // Use a list to ensure objects must be allocated. |
| 22 | ArrayList<Object> l = new ArrayList<>(100); |
| 23 | |
Andreas Gampe | d6d3f0e | 2016-10-10 20:01:41 -0700 | [diff] [blame] | 24 | prefetchClassNames(); |
| 25 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 26 | doTest(l); |
| 27 | } |
| 28 | |
Andreas Gampe | d6d3f0e | 2016-10-10 20:01:41 -0700 | [diff] [blame] | 29 | // 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 Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 40 | public static void doTest(ArrayList<Object> l) throws Exception { |
Andreas Gampe | 2427aae | 2016-10-17 18:05:19 -0700 | [diff] [blame] | 41 | // 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 Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 46 | |
| 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 Gampe | 2427aae | 2016-10-17 18:05:19 -0700 | [diff] [blame] | 79 | |
| 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 Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 84 | } |
| 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 Gampe | 2427aae | 2016-10-17 18:05:19 -0700 | [diff] [blame] | 92 | final Thread thisThread = Thread.currentThread(); |
| 93 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 94 | 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 Gampe | 2427aae | 2016-10-17 18:05:19 -0700 | [diff] [blame] | 107 | enableAllocationTracking(sameThread ? this : thisThread, false); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 108 | } |
| 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 Gampe | 2427aae | 2016-10-17 18:05:19 -0700 | [diff] [blame] | 139 | private static native void setupObjectAllocCallback(boolean enable); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 140 | private static native void enableAllocationTracking(Thread thread, boolean enable); |
| 141 | } |