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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.openjdk.bench.vm.compiler; 24 25 import org.openjdk.jmh.annotations.Benchmark; 26 import org.openjdk.jmh.annotations.BenchmarkMode; 27 import org.openjdk.jmh.annotations.Fork; 28 import org.openjdk.jmh.annotations.Measurement; 29 import org.openjdk.jmh.annotations.Mode; 30 import org.openjdk.jmh.annotations.OutputTimeUnit; 31 import org.openjdk.jmh.annotations.Warmup; 32 33 import java.util.concurrent.TimeUnit; 34 35 /** 36 * Tests how well the JVM can remove stores after allocation of objects. 37 */ 38 @BenchmarkMode(Mode.AverageTime) 39 @OutputTimeUnit(TimeUnit.NANOSECONDS) 40 @Warmup(iterations = 4, time = 2, timeUnit = TimeUnit.SECONDS) 41 @Measurement(iterations = 4, time = 2, timeUnit = TimeUnit.SECONDS) 42 @Fork(value = 3) 43 public class PostAllocationStores { 44 45 /** Tests allocation with explicit stores of null/zero to all fields. */ 46 @Benchmark 47 public Object testAllocationWithNullStores() throws Exception { 48 return new TestWithNullStores(); 49 } 50 51 /** 52 * Tests allocation with explicit stores of non-null/non-zero to all fields. This test exists as a complement to the 53 * one above. 54 */ 55 @Benchmark 56 public Object testAllocationWithNonNullStores() throws Exception { 57 return new TestWithNonNullStores(); 58 } 59 60 /** Tests allocation with explicit stores of null/zero to all fields, where all fields are volatile. */ 61 @Benchmark 62 public Object testAllocationWithNullVolatileStores() throws Exception { 63 return new TestWithNullVolatileStores(); 64 } 65 66 /** Tests allocation without any explicit stores to any fields. */ 67 @Benchmark 68 public Object testAllocationWithoutStores() throws Exception { 69 return new TestWithoutStores(); 70 } 71 72 static class TestWithNullStores { 73 Object objectField1; 74 Object objectField2; 75 Object objectField3; 76 int intField1; 77 int intField2; 78 long longField1; 79 80 public TestWithNullStores() { 81 objectField1 = null; 82 objectField2 = null; 83 objectField3 = null; 84 intField1 = 0; 85 intField2 = 0; 86 longField1 = 0L; 87 } 88 } 89 90 static class TestWithNonNullStores { 91 Object objectField1; 92 Object objectField2; 93 Object objectField3; 94 int intField1; 95 int intField2; 96 long longField1; 97 98 @SuppressWarnings("initialization") 99 public TestWithNonNullStores() { 100 objectField1 = this; 101 objectField2 = this; 102 objectField3 = this; 103 intField1 = 4; 104 intField2 = 7; 105 longField1 = 2L; 106 } 107 } 108 109 static class TestWithNullVolatileStores { 110 volatile Object objectField1; 111 volatile Object objectField2; 112 volatile Object objectField3; 113 volatile int intField1; 114 volatile int intField2; 115 volatile long longField1; 116 117 public TestWithNullVolatileStores() { 118 objectField1 = null; 119 objectField2 = null; 120 objectField3 = null; 121 intField1 = 0; 122 intField2 = 0; 123 longField1 = 0L; 124 } 125 } 126 127 static class TestWithoutStores { 128 Object objectField1; 129 Object objectField2; 130 Object objectField3; 131 int intField1; 132 int intField2; 133 long longField1; 134 } 135 136 }