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