1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker 3*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project 4*795d594fSAndroid Build Coastguard Worker * 5*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 6*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 7*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 8*795d594fSAndroid Build Coastguard Worker * 9*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 10*795d594fSAndroid Build Coastguard Worker * 11*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 12*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 13*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 15*795d594fSAndroid Build Coastguard Worker * limitations under the License. 16*795d594fSAndroid Build Coastguard Worker */ 17*795d594fSAndroid Build Coastguard Worker 18*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Method; 19*795d594fSAndroid Build Coastguard Worker import java.util.Map; 20*795d594fSAndroid Build Coastguard Worker 21*795d594fSAndroid Build Coastguard Worker import dalvik.system.VMDebug; 22*795d594fSAndroid Build Coastguard Worker 23*795d594fSAndroid Build Coastguard Worker public class Main implements Runnable { 24*795d594fSAndroid Build Coastguard Worker static final int numberOfThreads = 4; 25*795d594fSAndroid Build Coastguard Worker static final int totalOperations = 1000; 26*795d594fSAndroid Build Coastguard Worker static final int maxStackDepth = 128; 27*795d594fSAndroid Build Coastguard Worker static Method enableAllocTrackingMethod; 28*795d594fSAndroid Build Coastguard Worker static Object holder; 29*795d594fSAndroid Build Coastguard Worker static volatile boolean trackingThreadDone = false; 30*795d594fSAndroid Build Coastguard Worker int threadIndex; 31*795d594fSAndroid Build Coastguard Worker Main(int index)32*795d594fSAndroid Build Coastguard Worker Main(int index) { 33*795d594fSAndroid Build Coastguard Worker threadIndex = index; 34*795d594fSAndroid Build Coastguard Worker } 35*795d594fSAndroid Build Coastguard Worker main(String[] args)36*795d594fSAndroid Build Coastguard Worker public static void main(String[] args) throws Exception { 37*795d594fSAndroid Build Coastguard Worker Class<?> klass = Class.forName("org.apache.harmony.dalvik.ddmc.DdmVmInternal"); 38*795d594fSAndroid Build Coastguard Worker if (klass == null) { 39*795d594fSAndroid Build Coastguard Worker throw new AssertionError("Couldn't find DdmVmInternal class"); 40*795d594fSAndroid Build Coastguard Worker } 41*795d594fSAndroid Build Coastguard Worker enableAllocTrackingMethod = klass.getDeclaredMethod("setRecentAllocationsTrackingEnabled", 42*795d594fSAndroid Build Coastguard Worker boolean.class); 43*795d594fSAndroid Build Coastguard Worker if (enableAllocTrackingMethod == null) { 44*795d594fSAndroid Build Coastguard Worker throw new AssertionError("Couldn't find setRecentAllocationsTrackingEnabled method"); 45*795d594fSAndroid Build Coastguard Worker } 46*795d594fSAndroid Build Coastguard Worker 47*795d594fSAndroid Build Coastguard Worker final Thread[] threads = new Thread[numberOfThreads]; 48*795d594fSAndroid Build Coastguard Worker for (int t = 0; t < threads.length; t++) { 49*795d594fSAndroid Build Coastguard Worker threads[t] = new Thread(new Main(t)); 50*795d594fSAndroid Build Coastguard Worker threads[t].start(); 51*795d594fSAndroid Build Coastguard Worker } 52*795d594fSAndroid Build Coastguard Worker for (Thread t : threads) { 53*795d594fSAndroid Build Coastguard Worker t.join(); 54*795d594fSAndroid Build Coastguard Worker } 55*795d594fSAndroid Build Coastguard Worker System.out.println("Finishing"); 56*795d594fSAndroid Build Coastguard Worker } 57*795d594fSAndroid Build Coastguard Worker run()58*795d594fSAndroid Build Coastguard Worker public void run() { 59*795d594fSAndroid Build Coastguard Worker if (threadIndex == 0) { 60*795d594fSAndroid Build Coastguard Worker for (int i = 0; i < totalOperations; ++i) { 61*795d594fSAndroid Build Coastguard Worker try { 62*795d594fSAndroid Build Coastguard Worker VMDebug.setAllocTrackerStackDepth(i % (maxStackDepth + 1)); 63*795d594fSAndroid Build Coastguard Worker enableAllocTrackingMethod.invoke(null, true); 64*795d594fSAndroid Build Coastguard Worker holder = new Object(); 65*795d594fSAndroid Build Coastguard Worker enableAllocTrackingMethod.invoke(null, false); 66*795d594fSAndroid Build Coastguard Worker } catch (Exception e) { 67*795d594fSAndroid Build Coastguard Worker System.out.println(e); 68*795d594fSAndroid Build Coastguard Worker return; 69*795d594fSAndroid Build Coastguard Worker } 70*795d594fSAndroid Build Coastguard Worker } 71*795d594fSAndroid Build Coastguard Worker trackingThreadDone = true; 72*795d594fSAndroid Build Coastguard Worker } else { 73*795d594fSAndroid Build Coastguard Worker while (!trackingThreadDone) { 74*795d594fSAndroid Build Coastguard Worker holder = new Object(); 75*795d594fSAndroid Build Coastguard Worker } 76*795d594fSAndroid Build Coastguard Worker } 77*795d594fSAndroid Build Coastguard Worker } 78*795d594fSAndroid Build Coastguard Worker } 79