Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame] | 1 | /* |
Mathieu Chartier | eebc3af | 2016-02-29 18:13:38 -0800 | [diff] [blame] | 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.lang.reflect.Method; |
| 18 | import java.util.Map; |
| 19 | |
| 20 | public class Main implements Runnable { |
| 21 | static final int numberOfThreads = 4; |
| 22 | static final int totalOperations = 1000; |
| 23 | static Method enableAllocTrackingMethod; |
| 24 | static Object holder; |
| 25 | static volatile boolean trackingThreadDone = false; |
| 26 | int threadIndex; |
| 27 | |
| 28 | Main(int index) { |
| 29 | threadIndex = index; |
| 30 | } |
| 31 | |
| 32 | public static void main(String[] args) throws Exception { |
| 33 | Class klass = Class.forName("org.apache.harmony.dalvik.ddmc.DdmVmInternal"); |
| 34 | if (klass == null) { |
| 35 | throw new AssertionError("Couldn't find DdmVmInternal class"); |
| 36 | } |
| 37 | enableAllocTrackingMethod = klass.getDeclaredMethod("enableRecentAllocations", |
| 38 | Boolean.TYPE); |
| 39 | if (enableAllocTrackingMethod == null) { |
| 40 | throw new AssertionError("Couldn't find enableRecentAllocations method"); |
| 41 | } |
| 42 | |
| 43 | final Thread[] threads = new Thread[numberOfThreads]; |
| 44 | for (int t = 0; t < threads.length; t++) { |
| 45 | threads[t] = new Thread(new Main(t)); |
| 46 | threads[t].start(); |
| 47 | } |
| 48 | for (Thread t : threads) { |
| 49 | t.join(); |
| 50 | } |
| 51 | System.out.println("Finishing"); |
| 52 | } |
| 53 | |
| 54 | public void run() { |
| 55 | if (threadIndex == 0) { |
| 56 | for (int i = 0; i < totalOperations; ++i) { |
| 57 | try { |
| 58 | enableAllocTrackingMethod.invoke(null, true); |
| 59 | holder = new Object(); |
| 60 | enableAllocTrackingMethod.invoke(null, false); |
| 61 | } catch (Exception e) { |
| 62 | System.out.println(e); |
| 63 | return; |
| 64 | } |
| 65 | } |
| 66 | trackingThreadDone = true; |
| 67 | } else { |
| 68 | while (!trackingThreadDone) { |
| 69 | holder = new Object(); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |