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 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 17 | package art; |
| 18 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 19 | import java.util.ArrayList; |
Andreas Gampe | 115b498 | 2017-04-12 19:06:12 -0700 | [diff] [blame] | 20 | import java.util.Arrays; |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 21 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 22 | public class Test904 { |
| 23 | public static void run() throws Exception { |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 24 | // Use a list to ensure objects must be allocated. |
| 25 | ArrayList<Object> l = new ArrayList<>(100); |
| 26 | |
Andreas Gampe | d6d3f0e | 2016-10-10 20:01:41 -0700 | [diff] [blame] | 27 | prefetchClassNames(); |
| 28 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 29 | doTest(l); |
| 30 | } |
| 31 | |
Andreas Gampe | d6d3f0e | 2016-10-10 20:01:41 -0700 | [diff] [blame] | 32 | // 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 Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 43 | public static void doTest(ArrayList<Object> l) throws Exception { |
Andreas Gampe | 2427aae | 2016-10-17 18:05:19 -0700 | [diff] [blame] | 44 | // 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 Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 49 | |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 50 | System.out.println(Arrays.toString(getTrackingEventMessages( |
| 51 | new Thread[] { Thread.currentThread(), }))); |
Andreas Gampe | 115b498 | 2017-04-12 19:06:12 -0700 | [diff] [blame] | 52 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 53 | 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 Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 70 | System.out.println(Arrays.toString(getTrackingEventMessages( |
| 71 | new Thread[] { Thread.currentThread(), }))); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 72 | System.out.println("Tracking on same thread"); |
| 73 | |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 74 | Thread test_thread = testThread(l, true, true); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 75 | |
| 76 | l.add(new Byte((byte)0)); |
| 77 | |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 78 | System.out.println(Arrays.toString(getTrackingEventMessages( |
| 79 | new Thread[] { Thread.currentThread(), test_thread, }))); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 80 | System.out.println("Tracking on same thread, not disabling tracking"); |
| 81 | |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 82 | test_thread = testThread(l, true, false); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 83 | |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 84 | System.out.println(Arrays.toString(getTrackingEventMessages( |
| 85 | new Thread[] { Thread.currentThread(), test_thread, }))); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 86 | System.out.println("Tracking on different thread"); |
| 87 | |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 88 | test_thread = testThread(l, false, true); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 89 | |
| 90 | l.add(new Byte((byte)0)); |
Andreas Gampe | 2427aae | 2016-10-17 18:05:19 -0700 | [diff] [blame] | 91 | |
| 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 Gampe | 115b498 | 2017-04-12 19:06:12 -0700 | [diff] [blame] | 95 | |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 96 | System.out.println(Arrays.toString(getTrackingEventMessages( |
| 97 | new Thread[] { Thread.currentThread(), test_thread, }))); |
Andreas Gampe | 115b498 | 2017-04-12 19:06:12 -0700 | [diff] [blame] | 98 | |
Andreas Gampe | 2427aae | 2016-10-17 18:05:19 -0700 | [diff] [blame] | 99 | enableAllocationTracking(null, true); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 102 | private static Thread testThread(final ArrayList<Object> l, final boolean sameThread, |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 103 | 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 Gampe | 2427aae | 2016-10-17 18:05:19 -0700 | [diff] [blame] | 108 | final Thread thisThread = Thread.currentThread(); |
| 109 | |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 110 | 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 Gampe | 2427aae | 2016-10-17 18:05:19 -0700 | [diff] [blame] | 123 | enableAllocationTracking(sameThread ? this : thisThread, false); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | t.start(); |
| 129 | startBarrier.waitFor(); |
| 130 | enableAllocationTracking(sameThread ? t : Thread.currentThread(), true); |
| 131 | trackBarrier.dec(); |
| 132 | |
| 133 | t.join(); |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 134 | return t; |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 135 | } |
| 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 Gampe | 2427aae | 2016-10-17 18:05:19 -0700 | [diff] [blame] | 156 | private static native void setupObjectAllocCallback(boolean enable); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 157 | private static native void enableAllocationTracking(Thread thread, boolean enable); |
Alex Light | d4102ba | 2018-04-30 12:47:22 -0700 | [diff] [blame] | 158 | private static native String[] getTrackingEventMessages(Thread[] threads); |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame] | 159 | } |