test/micro/org/openjdk/micro/hotspot/gc/g1/WriteBarrier.java
branchJEP-230-microbenchmarks-branch
changeset 56913 013359fdfeb2
parent 56905 d4ab0656f48e
equal deleted inserted replaced
56909:7cf3051d8572 56913:013359fdfeb2
       
     1 /*
       
     2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package org.openjdk.micro.hotspot.gc.g1;
       
    26 
       
    27 import java.lang.management.GarbageCollectorMXBean;
       
    28 import java.lang.management.ManagementFactory;
       
    29 import java.lang.management.MemoryPoolMXBean;
       
    30 import java.util.HashMap;
       
    31 import java.util.List;
       
    32 import java.util.LinkedList;
       
    33 import java.util.concurrent.TimeUnit;
       
    34 import org.openjdk.jmh.annotations.Benchmark;
       
    35 import org.openjdk.jmh.annotations.Fork;
       
    36 import org.openjdk.jmh.annotations.Level;
       
    37 import org.openjdk.jmh.annotations.Measurement;
       
    38 import org.openjdk.jmh.annotations.OperationsPerInvocation;
       
    39 import org.openjdk.jmh.annotations.OutputTimeUnit;
       
    40 import org.openjdk.jmh.annotations.Scope;
       
    41 import org.openjdk.jmh.annotations.Setup;
       
    42 import org.openjdk.jmh.annotations.State;
       
    43 import org.openjdk.jmh.annotations.TearDown;
       
    44 import org.openjdk.jmh.annotations.Warmup;
       
    45 
       
    46 /**
       
    47  * Several different tests cases of reference writes that might require write
       
    48  * barrier depending on the GC used. Multiple sub-classes available with
       
    49  * specific command line options set to test G1, Parallel GC and CMS.
       
    50  *
       
    51  * @author staffan.friberg@oracle.com (sfriberg)
       
    52  */
       
    53 @State(Scope.Benchmark)
       
    54 @OutputTimeUnit(TimeUnit.MILLISECONDS)
       
    55 @Warmup(iterations = 5)
       
    56 @Measurement(iterations = 5)
       
    57 @Fork(jvmArgsAppend = {"-XX:+UseG1GC", "-Xmx256m", "-Xms256m", "-Xmn64m"}, value = 5)
       
    58 public class WriteBarrier {
       
    59 
       
    60     // Datastructures that enables writes between different parts and regions on the heap
       
    61     private Object oldReferee_region1;
       
    62     private Object oldReferee_region2;
       
    63     private Object youngReferee_region3;
       
    64     private Object youngReferee_region4;
       
    65     private Object nullReferee = null;
       
    66 
       
    67     private static final int OLD_REFERENCES_LENGTH = 131072;
       
    68     private final Holder[] oldReferences = new Holder[OLD_REFERENCES_LENGTH];
       
    69     private Holder oldReference_region1;
       
    70     private Holder youngReference_region3;
       
    71 
       
    72     // Keep alive to avoid them being garbage collected but not used in benchmarks
       
    73     private final LinkedList<Holder> padding = new LinkedList<>();
       
    74     private final LinkedList<Holder> liveData = new LinkedList<>();
       
    75     private final HashMap<String, Long> gcCount = new HashMap<>();
       
    76 
       
    77     /**
       
    78      * Setup method for the benchmarks
       
    79      *
       
    80      * Allocate objects in a certain order to make sure the end up on the heap
       
    81      * in the right way to later use them in tests.
       
    82      */
       
    83     @Setup
       
    84     public void setup() {
       
    85         // Allocate together and System.gc to move them to Old Space and
       
    86         // keep in the same region by doing a fast promotion
       
    87         oldReferee_region1 = new Object();
       
    88         oldReference_region1 = new Holder(oldReferee_region1);
       
    89         System.gc();
       
    90 
       
    91         // Fill up old space to 80%
       
    92         List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
       
    93         for (MemoryPoolMXBean pool : pools) {
       
    94             if (pool.getName().contains("Old Gen")) {
       
    95                 pool.setUsageThreshold((pool.getUsage().getMax() / 5) * 4);
       
    96 
       
    97                 while (!pool.isUsageThresholdExceeded()) {
       
    98                     // Allocate new referee and and then increase live data count
       
    99                     // and force promotion until heap is full enough. The last
       
   100                     // oldReferee will most likely be located in a different region
       
   101                     // compared to the the initially allocated objects.
       
   102                     oldReferee_region2 = new Object();
       
   103                     for (int i = 0; i < 10000; i++) {
       
   104                         liveData.add(new Holder(new byte[512], new Object()));
       
   105                     }
       
   106                 }
       
   107                 break;
       
   108             }
       
   109         }
       
   110         int index = 0;
       
   111         for (Holder holder : liveData) {
       
   112             if (index < oldReferences.length) {
       
   113                 oldReferences[index++] = holder;
       
   114             }
       
   115         }
       
   116 
       
   117         // Allocate reference and referee together to keep them in same region
       
   118         // Allocate Object first so they are located in the same memory order
       
   119         // as objects in old space
       
   120         youngReferee_region3 = new Object();
       
   121         youngReference_region3 = new Holder(youngReferee_region3);
       
   122 
       
   123         // Allocate padding and a new referee to make sure the reference and
       
   124         // referee are in different regions
       
   125         for (int i = 0; i < 2000; i++) {
       
   126             Holder tempHolder = new Holder(new byte[500], new Object());
       
   127             padding.add(tempHolder);
       
   128             youngReferee_region4 = tempHolder.getReference();
       
   129         }
       
   130 
       
   131         /*
       
   132          * Get GC numbers after all allocation but before any benchmark execution
       
   133          * starts to verify that no GCs happen during the benchmarking it self as
       
   134          * object will then move.
       
   135          */
       
   136         List<GarbageCollectorMXBean> gcBeans = ManagementFactory.getGarbageCollectorMXBeans();
       
   137         for (GarbageCollectorMXBean gcBean : gcBeans) {
       
   138             gcCount.put(gcBean.getName(), gcBean.getCollectionCount());
       
   139         }
       
   140     }
       
   141 
       
   142     /**
       
   143      * Invalidate any benchmark result if a GC occurs during execution of
       
   144      * benchmark as moving objects will destroy the assumptions of the tests
       
   145      */
       
   146     @TearDown(Level.Iteration)
       
   147     public void checkGCCount() {
       
   148         List<GarbageCollectorMXBean> gcBeans = ManagementFactory.getGarbageCollectorMXBeans();
       
   149         for (GarbageCollectorMXBean gcBean : gcBeans) {
       
   150             if (gcBean.getCollectionCount() != gcCount.get(gcBean.getName())) {
       
   151                 throw new RuntimeException("A GC has happened during iteration and the microbenchmark result is invalid.");
       
   152             }
       
   153         }
       
   154     }
       
   155 
       
   156     /**
       
   157      * Write a reference in an object located in the old space and where the
       
   158      * written pointer is to a old object in the same region
       
   159      */
       
   160     @Benchmark
       
   161     public void oldPointingToOldInSameRegion() {
       
   162         oldReference_region1.setReference(oldReferee_region1);
       
   163     }
       
   164 
       
   165     /**
       
   166      * Write a reference in an object located in the old space and where the
       
   167      * written pointer is to a old object in a different region
       
   168      */
       
   169     @Benchmark
       
   170     public void oldPointingToOldInDifferentRegion() {
       
   171         oldReference_region1.setReference(oldReferee_region2);
       
   172     }
       
   173 
       
   174     /**
       
   175      * Write a reference in an object located in the old space and where the
       
   176      * written pointer is to an object in the young space
       
   177      */
       
   178     @Benchmark
       
   179     public void oldPointingToYoungInDifferentRegion() {
       
   180         oldReference_region1.setReference(youngReferee_region3);
       
   181     }
       
   182 
       
   183     /**
       
   184      * Write a reference in an object located in the young space and where the
       
   185      * written pointer is to an object in the old space
       
   186      */
       
   187     @Benchmark
       
   188     public void youngPointingToOldInDifferentRegion() {
       
   189         youngReference_region3.setReference(oldReferee_region2);
       
   190     }
       
   191 
       
   192     /**
       
   193      * Write a reference in an object located in the young space and where the
       
   194      * written pointer is to a young object in the same region
       
   195      */
       
   196     @Benchmark
       
   197     public void youngPointingToYoungInSameRegion() {
       
   198         youngReference_region3.setReference(youngReferee_region3);
       
   199     }
       
   200 
       
   201     /**
       
   202      * Write a reference in an object located in the young space and where the
       
   203      * written pointer is to a young object in a different region
       
   204      */
       
   205     @Benchmark
       
   206     public void youngPointingToYoungInDifferentRegion() {
       
   207         youngReference_region3.setReference(youngReferee_region4);
       
   208     }
       
   209 
       
   210     /**
       
   211      * Write by compiler provable null to an object located in old space
       
   212      */
       
   213     @Benchmark
       
   214     public void oldPointingToExplicitNull() {
       
   215         oldReference_region1.setReference(null);
       
   216     }
       
   217 
       
   218     /**
       
   219      * Write by compiler unprovable null to an object located in old space
       
   220      */
       
   221     @Benchmark
       
   222     public void oldPointingToImplicitNull() {
       
   223         oldReference_region1.setReference(nullReferee);
       
   224     }
       
   225 
       
   226     /**
       
   227      * Write by compiler provable null to an object located in young space
       
   228      */
       
   229     @Benchmark
       
   230     public void youngPointingToExplicitNull() {
       
   231         youngReference_region3.setReference(null);
       
   232     }
       
   233 
       
   234     /**
       
   235      * Write by compiler unprovable null to an object located in young space
       
   236      */
       
   237     @Benchmark
       
   238     public void youngPointingToImplicitNull() {
       
   239         youngReference_region3.setReference(nullReferee);
       
   240     }
       
   241 
       
   242     /**
       
   243      * Iterate and update over many old references to point to a young object.
       
   244      * Since they are in different regions we will need to check the card, and
       
   245      * since we will update many different reference in different memory
       
   246      * locations/cards the card will need to be queued as no filtering will
       
   247      * catch it.
       
   248      */
       
   249     @Benchmark
       
   250     @OperationsPerInvocation(value = OLD_REFERENCES_LENGTH)
       
   251     public void manyOldPointingToYoung() {
       
   252         for (Holder oldReference : oldReferences) {
       
   253             oldReference.setReference(youngReferee_region3);
       
   254         }
       
   255     }
       
   256 
       
   257     /**
       
   258      * Iterate and update over many old references to point to explicit null.
       
   259      */
       
   260     @Benchmark
       
   261     @OperationsPerInvocation(value = OLD_REFERENCES_LENGTH)
       
   262     public void manyOldPointingToExplicitNull() {
       
   263         for (Holder oldReference : oldReferences) {
       
   264             oldReference.setReference(null);
       
   265         }
       
   266     }
       
   267 
       
   268     /**
       
   269      * Iterate and update over many old references to point to implicit null.
       
   270      */
       
   271     @Benchmark
       
   272     @OperationsPerInvocation(value = OLD_REFERENCES_LENGTH)
       
   273     public void manyOldPointingToImplicitNull() {
       
   274         for (Holder oldReference : oldReferences) {
       
   275             oldReference.setReference(nullReferee);
       
   276         }
       
   277     }
       
   278 
       
   279     /*
       
   280      * Holder object for reference and padding
       
   281      */
       
   282     static class Holder {
       
   283 
       
   284         private Object reference;
       
   285         private final byte[] padding;
       
   286 
       
   287         public Holder(Object reference) {
       
   288             this(null, reference);
       
   289         }
       
   290 
       
   291         public Holder(byte[] padding, Object reference) {
       
   292             this.padding = padding;
       
   293             this.reference = reference;
       
   294         }
       
   295 
       
   296         public void setReference(Object reference) {
       
   297             this.reference = reference;
       
   298         }
       
   299 
       
   300         public Object getReference() {
       
   301             return this.reference;
       
   302         }
       
   303     }
       
   304 }