1 /*
   2  * Copyright (c) 2014 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.bench.vm.compiler;
  26 
  27 import org.openjdk.jmh.annotations.Benchmark;
  28 import org.openjdk.jmh.annotations.BenchmarkMode;
  29 import org.openjdk.jmh.annotations.Mode;
  30 import org.openjdk.jmh.annotations.OutputTimeUnit;
  31 
  32 import java.util.concurrent.TimeUnit;
  33 
  34 /**
  35  * Tests how well the JVM can remove stores after allocation of objects.
  36  */
  37 @BenchmarkMode(Mode.AverageTime)
  38 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  39 public class PostAllocationStores {
  40 
  41     /** Tests allocation with explicit stores of null/zero to all fields. */
  42     @Benchmark
  43     public Object testAllocationWithNullStores() throws Exception {
  44         return new TestWithNullStores();
  45     }
  46 
  47     /**
  48      * Tests allocation with explicit stores of non-null/non-zero to all fields. This test exists as a complement to the
  49      * one above.
  50      */
  51     @Benchmark
  52     public Object testAllocationWithNonNullStores() throws Exception {
  53         return new TestWithNonNullStores();
  54     }
  55 
  56     /** Tests allocation with explicit stores of null/zero to all fields, where all fields are volatile. */
  57     @Benchmark
  58     public Object testAllocationWithNullVolatileStores() throws Exception {
  59         return new TestWithNullVolatileStores();
  60     }
  61 
  62     /** Tests allocation without any explicit stores to any fields. */
  63     @Benchmark
  64     public Object testAllocationWithoutStores() throws Exception {
  65         return new TestWithoutStores();
  66     }
  67 
  68     static class TestWithNullStores {
  69         Object objectField1;
  70         Object objectField2;
  71         Object objectField3;
  72         int intField1;
  73         int intField2;
  74         long longField1;
  75 
  76         public TestWithNullStores() {
  77             objectField1 = null;
  78             objectField2 = null;
  79             objectField3 = null;
  80             intField1 = 0;
  81             intField2 = 0;
  82             longField1 = 0L;
  83         }
  84     }
  85 
  86     static class TestWithNonNullStores {
  87         Object objectField1;
  88         Object objectField2;
  89         Object objectField3;
  90         int intField1;
  91         int intField2;
  92         long longField1;
  93 
  94         public TestWithNonNullStores() {
  95             objectField1 = this;
  96             objectField2 = this;
  97             objectField3 = this;
  98             intField1 = 4;
  99             intField2 = 7;
 100             longField1 = 2L;
 101         }
 102     }
 103 
 104     static class TestWithNullVolatileStores {
 105         volatile Object objectField1;
 106         volatile Object objectField2;
 107         volatile Object objectField3;
 108         volatile int intField1;
 109         volatile int intField2;
 110         volatile long longField1;
 111 
 112         public TestWithNullVolatileStores() {
 113             objectField1 = null;
 114             objectField2 = null;
 115             objectField3 = null;
 116             intField1 = 0;
 117             intField2 = 0;
 118             longField1 = 0L;
 119         }
 120     }
 121 
 122     static class TestWithoutStores {
 123         Object objectField1;
 124         Object objectField2;
 125         Object objectField3;
 126         int intField1;
 127         int intField2;
 128         long longField1;
 129     }
 130 
 131 }