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