1 /*
2 * Copyright Amazon.com Inc. 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 */
24
25 package org.openjdk.bench.vm.gc.barriers.reads;
26
27 import org.openjdk.jmh.annotations.*;
28 import org.openjdk.jmh.infra.Blackhole;
29
30 import java.util.concurrent.TimeUnit;
31
32 @Warmup(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS)
33 @Measurement(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS)
34 @Fork(3)
35 @BenchmarkMode(Mode.AverageTime)
36 @OutputTimeUnit(TimeUnit.NANOSECONDS)
37 @State(Scope.Thread)
38 public class SameObjectLoaded {
39
40 Target obj1, obj2, obj3, obj4;
41
42 @Setup
43 public void setup() {
44 obj1 = new Target();
45 obj2 = new Target();
46 obj3 = new Target();
47 obj4 = new Target();
48 }
49
50 @Benchmark
51 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
52 public void loadSameTrivial1(Blackhole bh) {
53 bh.consume(this.obj1);
54 bh.consume(this.obj1);
55 bh.consume(this.obj1);
56 bh.consume(this.obj1);
57 }
58
59 @Benchmark
60 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
61 public void loadFieldTrivial1(Blackhole bh) {
62 bh.consume(this.obj1.x + this.obj2.x + this.obj3.x + this.obj4.x);
63 }
64
65 @Benchmark
66 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
67 public void loadSameInCountedLoopSmallBody(Blackhole bh) {
68 for (int i=0; i<1024; i++) {
69 bh.consume(this.obj1);
70 bh.consume(this.obj1);
71 bh.consume(this.obj1);
72 bh.consume(this.obj1);
73 }
74 }
75
76 @Benchmark
77 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
78 public void loadSameInCountedLoopSmallBody2(Blackhole bh) {
79 for (int i=0; i<1024; i++) {
80 bh.consume(this.obj1.x);
81 bh.consume(this.obj1.x);
82 bh.consume(this.obj1.x);
83 bh.consume(this.obj1.x);
84 }
85 }
86
87 @Benchmark
88 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
89 public void loadSameInCountedLoopTinyBody(Blackhole bh) {
90 for (int i=0; i<1024; i++) {
91 bh.consume(this.obj1);
92 }
93 }
94
95 @Benchmark
96 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
97 public void loadSameTrivialDominated1(Blackhole bh) {
98 bh.consume(this.obj1);
99
100 if (bh != null) {
101 bh.consume(this.obj1);
102 } else {
103 bh.consume(this.obj1);
104 }
105 }
106
107 @Benchmark
108 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
109 public void loadSameTrivialDominated2(Blackhole bh) {
110 bh.consume(this.obj1);
111
112 if (bh != null) {
113 bh.consume(123);
114 } else {
115 bh.consume(456);
116 }
117
118 bh.consume(this.obj1);
119 }
120
121 @Benchmark
122 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
123 public void loadSameTrivialDominated3(Blackhole bh) {
124 if (bh != null) {
125 bh.consume(this.obj1);
126 } else {
127 bh.consume(this.obj1);
128 }
129
130 if (bh != null) {
131 bh.consume(this.obj1);
132 } else {
133 bh.consume(this.obj1);
134 }
135
136 if (bh != null) {
137 bh.consume(this.obj1);
138 } else {
139 bh.consume(this.obj1);
140 }
141 }
142
143 @Benchmark
144 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
145 public void loadSameNewAllocation1(Blackhole bh) {
146 Target obj1 = new Target();
147 bh.consume(obj1);
148 }
149
150 @Benchmark
151 @CompilerControl(CompilerControl.Mode.DONT_INLINE)
152 public void loadSameNewAllocation2(Blackhole bh) {
153 this.obj1 = new Target();
154 bh.consume(this.obj1);
155 }
156
157 static class Target {
158 int x;
159 }
160 }
161