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 { |
| 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 Gampe | d6d3f0e | 2016-10-10 20:01:41 -0700 | [diff] [blame^] | 26 | prefetchClassNames(); |
| 27 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 28 | doTest(l); |
| 29 | } |
| 30 | |
Andreas Gampe | d6d3f0e | 2016-10-10 20:01:41 -0700 | [diff] [blame^] | 31 | // 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 Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 42 | public static void doTest(ArrayList<Object> l) throws Exception { |
| 43 | setupCallback(); |
| 44 | |
| 45 | enableAllocationTracking(null, true); |
| 46 | |
| 47 | l.add(new Object()); |
| 48 | l.add(new Integer(1)); |
| 49 | |
| 50 | enableAllocationTracking(null, false); |
| 51 | |
| 52 | l.add(new Float(1.0f)); |
| 53 | |
| 54 | enableAllocationTracking(Thread.currentThread(), true); |
| 55 | |
| 56 | l.add(new Short((short)0)); |
| 57 | |
| 58 | enableAllocationTracking(Thread.currentThread(), false); |
| 59 | |
| 60 | l.add(new Byte((byte)0)); |
| 61 | |
| 62 | System.out.println("Tracking on same thread"); |
| 63 | |
| 64 | testThread(l, true, true); |
| 65 | |
| 66 | l.add(new Byte((byte)0)); |
| 67 | |
| 68 | System.out.println("Tracking on same thread, not disabling tracking"); |
| 69 | |
| 70 | testThread(l, true, false); |
| 71 | |
| 72 | System.out.println("Tracking on different thread"); |
| 73 | |
| 74 | testThread(l, false, true); |
| 75 | |
| 76 | l.add(new Byte((byte)0)); |
| 77 | } |
| 78 | |
| 79 | private static void testThread(final ArrayList<Object> l, final boolean sameThread, |
| 80 | final boolean disableTracking) throws Exception { |
| 81 | final SimpleBarrier startBarrier = new SimpleBarrier(1); |
| 82 | final SimpleBarrier trackBarrier = new SimpleBarrier(1); |
| 83 | final SimpleBarrier disableBarrier = new SimpleBarrier(1); |
| 84 | |
| 85 | Thread t = new Thread() { |
| 86 | public void run() { |
| 87 | try { |
| 88 | startBarrier.dec(); |
| 89 | trackBarrier.waitFor(); |
| 90 | } catch (Exception e) { |
| 91 | e.printStackTrace(System.out); |
| 92 | System.exit(1); |
| 93 | } |
| 94 | |
| 95 | l.add(new Double(0.0)); |
| 96 | |
| 97 | if (disableTracking) { |
| 98 | enableAllocationTracking(sameThread ? this : Thread.currentThread(), false); |
| 99 | } |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | t.start(); |
| 104 | startBarrier.waitFor(); |
| 105 | enableAllocationTracking(sameThread ? t : Thread.currentThread(), true); |
| 106 | trackBarrier.dec(); |
| 107 | |
| 108 | t.join(); |
| 109 | } |
| 110 | |
| 111 | private static class SimpleBarrier { |
| 112 | int count; |
| 113 | |
| 114 | public SimpleBarrier(int i) { |
| 115 | count = i; |
| 116 | } |
| 117 | |
| 118 | public synchronized void dec() throws Exception { |
| 119 | count--; |
| 120 | notifyAll(); |
| 121 | } |
| 122 | |
| 123 | public synchronized void waitFor() throws Exception { |
| 124 | while (count != 0) { |
| 125 | wait(); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | private static native void setupCallback(); |
| 131 | private static native void enableAllocationTracking(Thread thread, boolean enable); |
| 132 | } |