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 
 24 
 25 package org.openjdk.bench.java.lang;
 26 
 27 import java.util.concurrent.TimeUnit;
 28 import org.openjdk.jmh.annotations.*;
 29 import org.openjdk.jmh.infra.Blackhole;
 30 
 31 import static org.openjdk.bench.java.lang.ScopeLocalsData.*;
 32 
 33 /**
 34  * Tests java.lang.ScopeLocal
 35  */
 36 @BenchmarkMode(Mode.AverageTime)
 37 @OutputTimeUnit(TimeUnit.MICROSECONDS)
 38 @Warmup(iterations=4, time=1)
 39 @Measurement(iterations=10, time=1)
 40 @Threads(1)
 41 @Fork(value = 1,
 42       jvmArgsPrepend = {"-Djmh.executor.class=org.openjdk.bench.java.lang.ScopeLocalsExecutorService", "-Djmh.executor=CUSTOM", "-Djmh.blackhole.mode=COMPILER", "--enable-preview"})
 43 @State(Scope.Thread)
 44 @SuppressWarnings("preview")
 45 public class ScopeLocals {
 46 
 47     private static final Integer THE_ANSWER = 42;
 48 
 49     // Test 1: make sure ScopeLocal.get() is hoisted out of loops.
 50 
 51     @Benchmark
 52     public void thousandAdds_ScopeLocal(Blackhole bh) throws Exception {
 53         int result = 0;
 54         for (int i = 0; i < 1_000; i++) {
 55             result += ScopeLocalsData.sl1.get();
 56         }
 57         bh.consume(result);
 58     }
 59 
 60     @Benchmark
 61     public void thousandAdds_ThreadLocal(Blackhole bh) throws Exception {
 62         int result = 0;
 63         for (int i = 0; i < 1_000; i++) {
 64             result += ScopeLocalsData.tl1.get();
 65         }
 66         bh.consume(result);
 67     }
 68 
 69     // Test 2: stress the ScopeLocal cache.
 70     // The idea here is to use a bunch of bound values cyclically, which
 71     // stresses the ScopeLocal cache.
 72 
 73     int combine(int n, int i1, int i2, int i3, int i4, int i5, int i6) {
 74         return n + ((i1 ^ i2 >>> 6) + (i3 << 7) + i4 - i5 | i6);
 75     }
 76 
 77     @Benchmark
 78     public int sixValues_ScopeLocal() throws Exception {
 79         int result = 0;
 80         for (int i = 0 ; i < 166; i++) {
 81             result = combine(result, sl1.get(), sl2.get(), sl3.get(), sl4.get(), sl5.get(), sl6.get());
 82         }
 83         return result;
 84     }
 85 
 86     @Benchmark
 87     public int sixValues_ThreadLocal() throws Exception {
 88         int result = 0;
 89         for (int i = 0 ; i < 166; i++) {
 90             result = combine(result, tl1.get(), tl2.get(), tl3.get(), tl4.get(), tl5.get(), tl6.get());
 91         }
 92         return result;
 93     }
 94 
 95     // Test 3: The cost of bind, then get
 96     // This is the worst case for ScopeLocals because we have to create
 97     // a binding, link it in, then search the current bindings. In addition, we
 98     // create a cache entry for the bound value, then we immediately have to
 99     // destroy it.
100 
101     @Benchmark
102     @OutputTimeUnit(TimeUnit.NANOSECONDS)
103     public int CreateBindThenGetThenRemove_ScopeLocal() throws Exception {
104         return ScopeLocal.where(sl1, THE_ANSWER).call(sl1::get);
105     }
106 
107 
108     // Create a Carrier ahead of time: might be slightly faster
109     private static final ScopeLocal.Carrier HOLD_42 = ScopeLocal.where(sl1, 42);
110     @Benchmark
111     @OutputTimeUnit(TimeUnit.NANOSECONDS)
112     public int bindThenGetThenRemove_ScopeLocal() throws Exception {
113         return HOLD_42.call(sl1::get);
114     }
115 
116     @Benchmark
117     @OutputTimeUnit(TimeUnit.NANOSECONDS)
118     public int bindThenGetThenRemove_ThreadLocal() throws Exception {
119         try {
120             tl1.set(THE_ANSWER);
121             return tl1.get();
122         } finally {
123             tl1.remove();
124         }
125     }
126 
127     // This has no exact equivalent in ScopeLocal, but it's provided here for
128     // information.
129     @Benchmark
130     @OutputTimeUnit(TimeUnit.NANOSECONDS)
131     public int bindThenGetNoRemove_ThreadLocal() throws Exception {
132         tl1.set(THE_ANSWER);
133         return tl1.get();
134     }
135 
136     // Test 4: The cost of binding, but not using any result
137 
138     @Benchmark
139     @OutputTimeUnit(TimeUnit.NANOSECONDS)
140     public Object bind_ScopeLocal() throws Exception {
141         return HOLD_42.call(this::getClass);
142     }
143 
144     @Benchmark
145     @OutputTimeUnit(TimeUnit.NANOSECONDS)
146     public Object TWR_bind_ScopeLocal() throws Exception {
147         try (var x = ScopeLocal.where(unbound, THE_ANSWER).bind()) {
148             return getClass();
149         }
150     }
151 
152     @Benchmark
153     @OutputTimeUnit(TimeUnit.NANOSECONDS)
154     public Object bind_ThreadLocal() throws Exception {
155         try {
156             tl1.set(THE_ANSWER);
157             return this.getClass();
158         } finally {
159             tl1.remove();
160         }
161     }
162 
163     // Simply set a ThreadLocal so that the caller can see it
164     // This has no exact equivalent in ScopeLocal, but it's provided here for
165     // information.
166     @Benchmark
167     @OutputTimeUnit(TimeUnit.NANOSECONDS)
168     public void setNoRemove_ThreadLocal() throws Exception {
169         tl1.set(THE_ANSWER);
170     }
171 
172     // This is the closest I can think of to setNoRemove_ThreadLocal in that it
173     // returns a value in a ScopeLocal container. The container must already
174     // be bound to an AtomicReference for this to work.
175     @Benchmark
176     @OutputTimeUnit(TimeUnit.NANOSECONDS)
177     public void setNoRemove_ScopeLocal() throws Exception {
178         sl_atomicRef.get().setPlain(THE_ANSWER);
179     }
180 
181     // Test 5: A simple counter
182 
183     @Benchmark
184     @OutputTimeUnit(TimeUnit.NANOSECONDS)
185     public void counter_ScopeLocal() {
186         sl_atomicInt.get().setPlain(
187                 sl_atomicInt.get().getPlain() + 1);
188     }
189 
190     @Benchmark
191     @OutputTimeUnit(TimeUnit.NANOSECONDS)
192     public void counter_ThreadLocal() {
193         // Very slow:
194         // tl1.set(tl1.get() + 1);
195         var ctr = tl_atomicInt.get();
196         ctr.setPlain(ctr.getPlain() + 1);
197     }
198 }