1 /*
2  * Copyright (C) 2015 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.TreeMap;
18 
19 public class Main {
20   private static TreeMap treeMap = new TreeMap();
21 
main(String[] args)22   public static void main(String[] args) {
23     System.loadLibrary(args[0]);
24     testHomogeneousCompaction();
25     System.out.println("Done.");
26   }
27 
allocateStuff()28   private static void allocateStuff() {
29     for (int i = 0; i < 1000; ++i) {
30       Object o = new Object();
31       treeMap.put(o.hashCode(), o);
32     }
33   }
34 
testHomogeneousCompaction()35   public static void testHomogeneousCompaction() {
36     System.out.println("Attempting homogeneous compaction");
37     final boolean supportHSC = supportHomogeneousSpaceCompact();
38     Object o = new Object();
39     long addressBefore = objectAddress(o);
40     long addressAfter;
41     allocateStuff();
42     final boolean success = performHomogeneousSpaceCompact();
43     allocateStuff();
44     System.out.println("Homogeneous compaction support=" + supportHSC + " success=" + success);
45     if (supportHSC != success) {
46       System.out.println("error: Expected " + supportHSC + " but got " + success);
47     }
48     if (success) {
49       allocateStuff();
50       addressAfter = objectAddress(o);
51       // This relies on the compaction copying from one space to another space and there being no
52       // overlap.
53       if (addressBefore == addressAfter) {
54         System.out.println("error: Expected different adddress " + addressBefore + " vs " +
55             addressAfter);
56       }
57     }
58     if (supportHSC) {
59       incrementDisableMovingGC();
60       if (performHomogeneousSpaceCompact()) {
61         System.out.println("error: Compaction succeeded when moving GC is disabled");
62       }
63       decrementDisableMovingGC();
64       if (!performHomogeneousSpaceCompact()) {
65         System.out.println("error: Compaction failed when moving GC is enabled");
66       }
67     }
68   }
69 
70   // Methods to get access to ART internals.
supportHomogeneousSpaceCompact()71   private static native boolean supportHomogeneousSpaceCompact();
performHomogeneousSpaceCompact()72   private static native boolean performHomogeneousSpaceCompact();
incrementDisableMovingGC()73   private static native void incrementDisableMovingGC();
decrementDisableMovingGC()74   private static native void decrementDisableMovingGC();
objectAddress(Object object)75   private static native long objectAddress(Object object);
76 }
77