Home | History | Annotate | Download | only in src
      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 
     22   public static void main(String[] args) {
     23     System.loadLibrary(args[0]);
     24     testHomogeneousCompaction();
     25     testCollectorTransitions();
     26     System.out.println("Done.");
     27   }
     28 
     29   private static void allocateStuff() {
     30     for (int i = 0; i < 1000; ++i) {
     31       Object o = new Object();
     32       treeMap.put(o.hashCode(), o);
     33     }
     34   }
     35 
     36   public static void testHomogeneousCompaction() {
     37     System.out.println("Attempting homogeneous compaction");
     38     final boolean supportHSC = supportHomogeneousSpaceCompact();
     39     Object o = new Object();
     40     long addressBefore = objectAddress(o);
     41     long addressAfter;
     42     allocateStuff();
     43     final boolean success = performHomogeneousSpaceCompact();
     44     allocateStuff();
     45     System.out.println("Homogeneous compaction support=" + supportHSC + " success=" + success);
     46     if (supportHSC != success) {
     47       System.out.println("error: Expected " + supportHSC + " but got " + success);
     48     }
     49     if (success) {
     50       allocateStuff();
     51       addressAfter = objectAddress(o);
     52       // This relies on the compaction copying from one space to another space and there being no
     53       // overlap.
     54       if (addressBefore == addressAfter) {
     55         System.out.println("error: Expected different adddress " + addressBefore + " vs " +
     56             addressAfter);
     57       }
     58     }
     59     if (supportHSC) {
     60       incrementDisableMovingGC();
     61       if (performHomogeneousSpaceCompact()) {
     62         System.out.println("error: Compaction succeeded when moving GC is disabled");
     63       }
     64       decrementDisableMovingGC();
     65       if (!performHomogeneousSpaceCompact()) {
     66         System.out.println("error: Compaction failed when moving GC is enabled");
     67       }
     68     }
     69   }
     70 
     71   private static void testCollectorTransitions() {
     72     if (supportCollectorTransition()) {
     73       Object o = new Object();
     74       // Transition to semi-space collector.
     75       allocateStuff();
     76       transitionToSS();
     77       allocateStuff();
     78       long addressBefore = objectAddress(o);
     79       Runtime.getRuntime().gc();
     80       long addressAfter = objectAddress(o);
     81       if (addressBefore == addressAfter) {
     82         System.out.println("error: Expected different adddress " + addressBefore + " vs " +
     83             addressAfter);
     84       }
     85       // Transition back to CMS.
     86       transitionToCMS();
     87       allocateStuff();
     88       addressBefore = objectAddress(o);
     89       Runtime.getRuntime().gc();
     90       addressAfter = objectAddress(o);
     91       if (addressBefore != addressAfter) {
     92         System.out.println("error: Expected same adddress " + addressBefore + " vs " +
     93             addressAfter);
     94       }
     95     }
     96   }
     97 
     98   // Methods to get access to ART internals.
     99   private static native boolean supportHomogeneousSpaceCompact();
    100   private static native boolean performHomogeneousSpaceCompact();
    101   private static native void incrementDisableMovingGC();
    102   private static native void decrementDisableMovingGC();
    103   private static native long objectAddress(Object object);
    104   private static native boolean supportCollectorTransition();
    105   private static native void transitionToSS();
    106   private static native void transitionToCMS();
    107 }
    108