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