8306647: Implementation of Structured Concurrency (Preview)

8306572: Implementation of Scoped Values (Preview)

Co-authored-by: Alan Bateman <alanb@openjdk.org>
Co-authored-by: Andrew Haley <aph@openjdk.org>
Reviewed-by: psandoz, dfuchs, mchung
This commit is contained in:
Alan Bateman 2023-06-07 06:41:09 +00:00
parent a08c5cb3f1
commit f1c7afcc3f
36 changed files with 3510 additions and 2969 deletions

View file

@ -0,0 +1,226 @@
/*
* Copyright (c) 2022, red Hat, Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.java.lang;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import static org.openjdk.bench.java.lang.ScopedValuesData.*;
/**
* Tests ScopedValue
*/
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations=4, time=1)
@Measurement(iterations=10, time=1)
@Threads(1)
@Fork(value = 1,
jvmArgsPrepend = {"-Djmh.executor.class=org.openjdk.bench.java.lang.ScopedValuesExecutorService",
"-Djmh.executor=CUSTOM",
"-Djmh.blackhole.mode=COMPILER",
"--enable-preview"})
@State(Scope.Thread)
@SuppressWarnings("preview")
public class ScopedValues {
private static final Integer THE_ANSWER = 42;
// Test 1: make sure ScopedValue.get() is hoisted out of loops.
@Benchmark
public void thousandAdds_ScopedValue(Blackhole bh) throws Exception {
int result = 0;
for (int i = 0; i < 1_000; i++) {
result += ScopedValuesData.sl1.get();
}
bh.consume(result);
}
@Benchmark
public void thousandAdds_ThreadLocal(Blackhole bh) throws Exception {
int result = 0;
for (int i = 0; i < 1_000; i++) {
result += ScopedValuesData.tl1.get();
}
bh.consume(result);
}
@Benchmark
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public int thousandIsBoundQueries(Blackhole bh) throws Exception {
var result = 0;
for (int i = 0; i < 1_000; i++) {
result += ScopedValuesData.sl1.isBound() ? 1 : 0;
}
return result;
}
@Benchmark
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public int thousandMaybeGets(Blackhole bh) throws Exception {
int result = 0;
for (int i = 0; i < 1_000; i++) {
if (ScopedValuesData.sl1.isBound()) {
result += ScopedValuesData.sl1.get();
}
}
return result;
}
// Test 2: stress the ScopedValue cache.
// The idea here is to use a bunch of bound values cyclically, which
// stresses the ScopedValue cache.
int combine(int n, int i1, int i2, int i3, int i4, int i5, int i6) {
return n + ((i1 ^ i2 >>> 6) + (i3 << 7) + i4 - i5 | i6);
}
@Benchmark
public int sixValues_ScopedValue() throws Exception {
int result = 0;
for (int i = 0 ; i < 166; i++) {
result = combine(result, sl1.get(), sl2.get(), sl3.get(), sl4.get(), sl5.get(), sl6.get());
}
return result;
}
@Benchmark
public int sixValues_ThreadLocal() throws Exception {
int result = 0;
for (int i = 0 ; i < 166; i++) {
result = combine(result, tl1.get(), tl2.get(), tl3.get(), tl4.get(), tl5.get(), tl6.get());
}
return result;
}
// Test 3: The cost of bind, then get
// This is the worst case for ScopedValues because we have to create
// a binding, link it in, then search the current bindings. In addition, we
// create a cache entry for the bound value, then we immediately have to
// destroy it.
@Benchmark
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public int CreateBindThenGetThenRemove_ScopedValue() throws Exception {
return ScopedValue.where(sl1, THE_ANSWER).call(sl1::get);
}
// Create a Carrier ahead of time: might be slightly faster
private static final ScopedValue.Carrier HOLD_42 = ScopedValue.where(sl1, 42);
@Benchmark
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public int bindThenGetThenRemove_ScopedValue() throws Exception {
return HOLD_42.call(sl1::get);
}
@Benchmark
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public int bindThenGetThenRemove_ThreadLocal() throws Exception {
try {
tl1.set(THE_ANSWER);
return tl1.get();
} finally {
tl1.remove();
}
}
// This has no exact equivalent in ScopedValue, but it's provided here for
// information.
@Benchmark
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public int bindThenGetNoRemove_ThreadLocal() throws Exception {
tl1.set(THE_ANSWER);
return tl1.get();
}
// Test 4: The cost of binding, but not using any result
@Benchmark
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Object bind_ScopedValue() throws Exception {
return HOLD_42.call(aCallable);
}
private static final Callable<Class<?>> aCallable = () -> ScopedValues.class;
// Same, but make sure that Carrier.get(Supplier) is no slower
// than Carrier.call(Callable).
@Benchmark
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Object bindViaGet_ScopedValue() {
return HOLD_42.get(aSupplier);
}
private static final Supplier<Class<?>> aSupplier = () -> ScopedValues.class;
@Benchmark
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Object bind_ThreadLocal() throws Exception {
try {
tl1.set(THE_ANSWER);
return this.getClass();
} finally {
tl1.remove();
}
}
// Simply set a ThreadLocal so that the caller can see it
// This has no exact equivalent in ScopedValue, but it's provided here for
// information.
@Benchmark
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public void setNoRemove_ThreadLocal() throws Exception {
tl1.set(THE_ANSWER);
}
// This is the closest I can think of to setNoRemove_ThreadLocal in that it
// returns a value in a ScopedValue container. The container must already
// be bound to an AtomicReference for this to work.
@Benchmark
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public void setNoRemove_ScopedValue() throws Exception {
sl_atomicRef.get().setPlain(THE_ANSWER);
}
// Test 5: A simple counter
@Benchmark
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public void counter_ScopedValue() {
sl_atomicInt.get().setPlain(
sl_atomicInt.get().getPlain() + 1);
}
@Benchmark
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public void counter_ThreadLocal() {
// Very slow:
// tl1.set(tl1.get() + 1);
var ctr = tl_atomicInt.get();
ctr.setPlain(ctr.getPlain() + 1);
}
}

View file

@ -0,0 +1,71 @@
/*
* Copyright (c) 2021, Red Hat, Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.java.lang;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
@SuppressWarnings("preview")
public class ScopedValuesData {
static final ScopedValue<Integer> sl1 = ScopedValue.newInstance();
static final ThreadLocal<Integer> tl1 = new ThreadLocal<>();
static final ScopedValue<Integer> sl2 = ScopedValue.newInstance();
static final ScopedValue<Integer> sl3 = ScopedValue.newInstance();
static final ScopedValue<Integer> sl4 = ScopedValue.newInstance();
static final ScopedValue<Integer> sl5 = ScopedValue.newInstance();
static final ScopedValue<Integer> sl6 = ScopedValue.newInstance();
static final ScopedValue<AtomicInteger> sl_atomicInt = ScopedValue.newInstance();
static final ScopedValue<Integer> unbound = ScopedValue.newInstance();
static final ScopedValue<AtomicReference<Integer>> sl_atomicRef = ScopedValue.newInstance();
static final ThreadLocal<Integer> tl2 = new ThreadLocal<>();
static final ThreadLocal<Integer> tl3 = new ThreadLocal<>();
static final ThreadLocal<Integer> tl4 = new ThreadLocal<>();
static final ThreadLocal<Integer> tl5 = new ThreadLocal<>();
static final ThreadLocal<Integer> tl6 = new ThreadLocal<>();
static final ThreadLocal<AtomicInteger> tl_atomicInt = new ThreadLocal<>();
static final ScopedValue.Carrier VALUES = ScopedValue
.where(sl1, 42).where(sl2, 2).where(sl3, 3)
.where(sl4, 4).where(sl5, 5).where(sl6, 6);
public static void run(Runnable action) {
try {
tl1.set(42); tl2.set(2); tl3.set(3); tl4.set(4); tl5.set(5); tl6.set(6);
tl1.get(); // Create the ScopedValue cache as a side effect
tl_atomicInt.set(new AtomicInteger());
VALUES.where(sl_atomicInt, new AtomicInteger())
.where(sl_atomicRef, new AtomicReference<>())
.run(action);
} finally {
tl1.remove(); tl2.remove(); tl3.remove(); tl4.remove(); tl5.remove(); tl6.remove();
tl_atomicInt.remove();
}
}
}

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.bench.java.lang;
import java.util.concurrent.*;
public class ScopedValuesExecutorService extends ThreadPoolExecutor {
public ScopedValuesExecutorService(int corePoolSize, String prefix) {
super(1, 1, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),
new AThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
}
}
class AThreadFactory implements ThreadFactory {
public Thread newThread(Runnable action) {
return new Thread() {
public void run() {
ScopedValuesData.run(action);
}
};
}
}