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