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.ExtentLocal; 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.ExtentLocalsData.*; 33 34 /** 35 * Tests ExtentLocal 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.ExtentLocalsExecutorService", 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 ExtentLocals { 51 52 private static final Integer THE_ANSWER = 42; 53 54 // Test 1: make sure ExtentLocal.get() is hoisted out of loops. 55 56 @Benchmark 57 public void thousandAdds_ExtentLocal(Blackhole bh) throws Exception { 58 int result = 0; 59 for (int i = 0; i < 1_000; i++) { 60 result += ExtentLocalsData.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 += ExtentLocalsData.tl1.get(); 70 } 71 bh.consume(result); 72 } 73 74 // Test 2: stress the ExtentLocal cache. 75 // The idea here is to use a bunch of bound values cyclically, which 76 // stresses the ExtentLocal cache. 77 78 int combine(int n, int i1, int i2, int i3, int i4, int i5, int i6) { 79 return n + ((i1 ^ i2 >>> 6) + (i3 << 7) + i4 - i5 | i6); 80 } 81 82 @Benchmark 83 public int sixValues_ExtentLocal() throws Exception { 84 int result = 0; 85 for (int i = 0 ; i < 166; i++) { 86 result = combine(result, sl1.get(), sl2.get(), sl3.get(), sl4.get(), sl5.get(), sl6.get()); 87 } 88 return result; 89 } 90 91 @Benchmark 92 public int sixValues_ThreadLocal() throws Exception { 93 int result = 0; 94 for (int i = 0 ; i < 166; i++) { 95 result = combine(result, tl1.get(), tl2.get(), tl3.get(), tl4.get(), tl5.get(), tl6.get()); 96 } 97 return result; 98 } 99 100 // Test 3: The cost of bind, then get 101 // This is the worst case for ExtentLocals because we have to create 102 // a binding, link it in, then search the current bindings. In addition, we 103 // create a cache entry for the bound value, then we immediately have to 104 // destroy it. 105 106 @Benchmark 107 @OutputTimeUnit(TimeUnit.NANOSECONDS) 108 public int CreateBindThenGetThenRemove_ExtentLocal() throws Exception { 109 return ExtentLocal.where(sl1, THE_ANSWER).call(sl1::get); 110 } 111 112 113 // Create a Carrier ahead of time: might be slightly faster 114 private static final ExtentLocal.Carrier HOLD_42 = ExtentLocal.where(sl1, 42); 115 @Benchmark 116 @OutputTimeUnit(TimeUnit.NANOSECONDS) 117 public int bindThenGetThenRemove_ExtentLocal() throws Exception { 118 return HOLD_42.call(sl1::get); 119 } 120 121 @Benchmark 122 @OutputTimeUnit(TimeUnit.NANOSECONDS) 123 public int bindThenGetThenRemove_ThreadLocal() throws Exception { 124 try { 125 tl1.set(THE_ANSWER); 126 return tl1.get(); 127 } finally { 128 tl1.remove(); 129 } 130 } 131 132 // This has no exact equivalent in ExtentLocal, but it's provided here for 133 // information. 134 @Benchmark 135 @OutputTimeUnit(TimeUnit.NANOSECONDS) 136 public int bindThenGetNoRemove_ThreadLocal() throws Exception { 137 tl1.set(THE_ANSWER); 138 return tl1.get(); 139 } 140 141 // Test 4: The cost of binding, but not using any result 142 143 @Benchmark 144 @OutputTimeUnit(TimeUnit.NANOSECONDS) 145 public Object bind_ExtentLocal() throws Exception { 146 return HOLD_42.call(this::getClass); 147 } 148 149 @Benchmark 150 @OutputTimeUnit(TimeUnit.NANOSECONDS) 151 public Object bind_ThreadLocal() throws Exception { 152 try { 153 tl1.set(THE_ANSWER); 154 return this.getClass(); 155 } finally { 156 tl1.remove(); 157 } 158 } 159 160 // Simply set a ThreadLocal so that the caller can see it 161 // This has no exact equivalent in ExtentLocal, but it's provided here for 162 // information. 163 @Benchmark 164 @OutputTimeUnit(TimeUnit.NANOSECONDS) 165 public void setNoRemove_ThreadLocal() throws Exception { 166 tl1.set(THE_ANSWER); 167 } 168 169 // This is the closest I can think of to setNoRemove_ThreadLocal in that it 170 // returns a value in a ExtentLocal container. The container must already 171 // be bound to an AtomicReference for this to work. 172 @Benchmark 173 @OutputTimeUnit(TimeUnit.NANOSECONDS) 174 public void setNoRemove_ExtentLocal() throws Exception { 175 sl_atomicRef.get().setPlain(THE_ANSWER); 176 } 177 178 // Test 5: A simple counter 179 180 @Benchmark 181 @OutputTimeUnit(TimeUnit.NANOSECONDS) 182 public void counter_ExtentLocal() { 183 sl_atomicInt.get().setPlain( 184 sl_atomicInt.get().getPlain() + 1); 185 } 186 187 @Benchmark 188 @OutputTimeUnit(TimeUnit.NANOSECONDS) 189 public void counter_ThreadLocal() { 190 // Very slow: 191 // tl1.set(tl1.get() + 1); 192 var ctr = tl_atomicInt.get(); 193 ctr.setPlain(ctr.getPlain() + 1); 194 } 195 }