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