Skip to content

Commit c357017

Browse files
committed
apply spotless changes to srcs
1 parent c7e9183 commit c357017

15 files changed

Lines changed: 2374 additions & 2339 deletions

benchmarks/src/main/java/io/netty/loom/benchmark/DefaultSchedulerUtils.java

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,51 @@
99

1010
public class DefaultSchedulerUtils {
1111

12-
public static void setupDefaultScheduler(int parallelism) {
13-
int maxPoolSize = Integer.max(parallelism, 256);
14-
int minRunnable = Integer.max(parallelism / 2, 1);
15-
System.setProperty("jdk.virtualThreadScheduler.parallelism", Integer.toString(parallelism));
16-
System.setProperty("jdk.virtualThreadScheduler.maxPoolSize", Integer.toString(maxPoolSize));
17-
System.setProperty("jdk.virtualThreadScheduler.minRunnable", Integer.toString(minRunnable));
18-
}
12+
public static void setupDefaultScheduler(int parallelism) {
13+
int maxPoolSize = Integer.max(parallelism, 256);
14+
int minRunnable = Integer.max(parallelism / 2, 1);
15+
System.setProperty("jdk.virtualThreadScheduler.parallelism", Integer.toString(parallelism));
16+
System.setProperty("jdk.virtualThreadScheduler.maxPoolSize", Integer.toString(maxPoolSize));
17+
System.setProperty("jdk.virtualThreadScheduler.minRunnable", Integer.toString(minRunnable));
18+
}
1919

20-
public static void validateDefaultSchedulerParallelism(ThreadFactory vtFactory) throws InterruptedException {
21-
var sharedCounter = new AtomicLong();
22-
var waitToStart = new CyclicBarrier(2);
23-
var errors = new LongAdder();
24-
var completed = new CountDownLatch(2);
25-
// verify serial execution
26-
for (int i = 0; i < 2; i++) {
27-
vtFactory.newThread(() -> {
28-
try {
29-
waitToStart.await(10, TimeUnit.SECONDS);
30-
if (detectContentionFor(sharedCounter, TimeUnit.SECONDS.toNanos(1))) {
31-
errors.increment();
32-
}
33-
} catch (Throwable t) {
34-
errors.increment();
35-
} finally {
36-
completed.countDown();
37-
}
38-
}).start();
39-
}
40-
completed.await();
41-
if (errors.sum() != 0) {
42-
throw new IllegalStateException("The default Loom scheduler appear to have too much parallelism: check if the jdk.virtualThreadScheduler.* properties are still valid!");
43-
}
44-
}
20+
public static void validateDefaultSchedulerParallelism(ThreadFactory vtFactory) throws InterruptedException {
21+
var sharedCounter = new AtomicLong();
22+
var waitToStart = new CyclicBarrier(2);
23+
var errors = new LongAdder();
24+
var completed = new CountDownLatch(2);
25+
// verify serial execution
26+
for (int i = 0; i < 2; i++) {
27+
vtFactory.newThread(() -> {
28+
try {
29+
waitToStart.await(10, TimeUnit.SECONDS);
30+
if (detectContentionFor(sharedCounter, TimeUnit.SECONDS.toNanos(1))) {
31+
errors.increment();
32+
}
33+
} catch (Throwable t) {
34+
errors.increment();
35+
} finally {
36+
completed.countDown();
37+
}
38+
}).start();
39+
}
40+
completed.await();
41+
if (errors.sum() != 0) {
42+
throw new IllegalStateException(
43+
"The default Loom scheduler appear to have too much parallelism: check if the jdk.virtualThreadScheduler.* properties are still valid!");
44+
}
45+
}
4546

46-
private static boolean detectContentionFor(AtomicLong sharedCounter, long durationNs) {
47-
long start = System.nanoTime();
48-
long value = sharedCounter.get();
49-
while ((System.nanoTime() - start) < durationNs) {
50-
if (!sharedCounter.compareAndSet(value, value + 1L)) {
51-
return true;
52-
}
53-
value++;
54-
}
55-
return false;
56-
}
47+
private static boolean detectContentionFor(AtomicLong sharedCounter, long durationNs) {
48+
long start = System.nanoTime();
49+
long value = sharedCounter.get();
50+
while ((System.nanoTime() - start) < durationNs) {
51+
if (!sharedCounter.compareAndSet(value, value + 1L)) {
52+
return true;
53+
}
54+
value++;
55+
}
56+
return false;
57+
}
5758

5859
}

benchmarks/src/main/java/io/netty/loom/benchmark/GetCarrierThread.java

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,47 @@
1616
@Fork(value = 2, jvmArgs = {"--add-opens=java.base/java.lang=ALL-UNNAMED", "-XX:+UnlockExperimentalVMOptions"})
1717
public class GetCarrierThread {
1818

19-
private static final MethodHandle CARRIER_THREAD_FIELD;
20-
static {
21-
try {
22-
var field = Class.forName("java.lang.VirtualThread")
23-
.getDeclaredField("carrierThread");
24-
field.setAccessible(true);
25-
MethodHandle carrierThreadMh = MethodHandles.lookup().unreflectGetter(field);
26-
// adapt using VirtualThread into Thread
27-
CARRIER_THREAD_FIELD = carrierThreadMh.asType(MethodType.methodType(Thread.class, Thread.class));
28-
} catch (Throwable e) {
29-
throw new RuntimeException(e);
30-
}
31-
}
32-
33-
private static Thread getCarrierThread(Thread t) {
34-
if (!t.isVirtual()) {
35-
return t;
36-
}
37-
try {
38-
return (Thread) CARRIER_THREAD_FIELD.invokeExact(t);
39-
} catch (Throwable e) {
40-
throw new RuntimeException(e);
41-
}
42-
}
43-
44-
private Thread vThread;
45-
46-
@Setup
47-
public void init() {
48-
vThread = Thread.ofVirtual().unstarted(() -> {
49-
});
50-
}
51-
52-
@Benchmark
53-
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
54-
public Thread carrierThreadMh() {
55-
return getCarrierThread(vThread);
56-
}
57-
58-
@Benchmark
59-
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
60-
public Thread volatileCarrierThreadVh() {
61-
return LoomSupport.getCarrierThread(vThread);
62-
}
19+
private static final MethodHandle CARRIER_THREAD_FIELD;
20+
static {
21+
try {
22+
var field = Class.forName("java.lang.VirtualThread").getDeclaredField("carrierThread");
23+
field.setAccessible(true);
24+
MethodHandle carrierThreadMh = MethodHandles.lookup().unreflectGetter(field);
25+
// adapt using VirtualThread into Thread
26+
CARRIER_THREAD_FIELD = carrierThreadMh.asType(MethodType.methodType(Thread.class, Thread.class));
27+
} catch (Throwable e) {
28+
throw new RuntimeException(e);
29+
}
30+
}
31+
32+
private static Thread getCarrierThread(Thread t) {
33+
if (!t.isVirtual()) {
34+
return t;
35+
}
36+
try {
37+
return (Thread) CARRIER_THREAD_FIELD.invokeExact(t);
38+
} catch (Throwable e) {
39+
throw new RuntimeException(e);
40+
}
41+
}
42+
43+
private Thread vThread;
44+
45+
@Setup
46+
public void init() {
47+
vThread = Thread.ofVirtual().unstarted(() -> {
48+
});
49+
}
50+
51+
@Benchmark
52+
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
53+
public Thread carrierThreadMh() {
54+
return getCarrierThread(vThread);
55+
}
56+
57+
@Benchmark
58+
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
59+
public Thread volatileCarrierThreadVh() {
60+
return LoomSupport.getCarrierThread(vThread);
61+
}
6362
}

benchmarks/src/main/java/io/netty/loom/benchmark/GetScheduler.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@
2525
@Fork(value = 2, jvmArgs = {"--add-opens=java.base/java.lang=ALL-UNNAMED", "-XX:+UnlockExperimentalVMOptions"})
2626
public class GetScheduler {
2727

28-
private static final Executor vtExecutor = Executors.newVirtualThreadPerTaskExecutor();
28+
private static final Executor vtExecutor = Executors.newVirtualThreadPerTaskExecutor();
2929

30-
@Benchmark
31-
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
32-
public Thread.VirtualThreadScheduler fromJDKPublicMethod() {
33-
return CompletableFuture.supplyAsync(Thread.VirtualThreadScheduler::current, vtExecutor).join();
34-
}
30+
@Benchmark
31+
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
32+
public Thread.VirtualThreadScheduler fromJDKPublicMethod() {
33+
return CompletableFuture.supplyAsync(Thread.VirtualThreadScheduler::current, vtExecutor).join();
34+
}
3535

36-
@Benchmark
37-
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
38-
public Thread.VirtualThreadScheduler fromJDKInternalField() {
39-
return CompletableFuture.supplyAsync(() -> LoomSupport.getScheduler(Thread.currentThread()), vtExecutor).join();
40-
}
36+
@Benchmark
37+
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
38+
public Thread.VirtualThreadScheduler fromJDKInternalField() {
39+
return CompletableFuture.supplyAsync(() -> LoomSupport.getScheduler(Thread.currentThread()), vtExecutor).join();
40+
}
4141
}

benchmarks/src/main/java/io/netty/loom/benchmark/NettySchedulerBenchmark.java

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -26,64 +26,60 @@
2626
@Warmup(iterations = 3, time = 1)
2727
@Measurement(iterations = 5, time = 1)
2828
@Fork(value = 2, jvmArgs = {"--add-opens=java.base/java.lang=ALL-UNNAMED", "-XX:+UnlockExperimentalVMOptions",
29-
"-XX:-DoJVMTIVirtualThreadTransitions", "-Djdk.trackAllThreads=false", "-Djdk.virtualThreadScheduler.implClass=io.netty.loom.NettyScheduler"})
29+
"-XX:-DoJVMTIVirtualThreadTransitions", "-Djdk.trackAllThreads=false",
30+
"-Djdk.virtualThreadScheduler.implClass=io.netty.loom.NettyScheduler"})
3031
@State(Scope.Thread)
3132
public class NettySchedulerBenchmark {
3233

33-
@Param({"1000", "100000"})
34-
private int tasks;
34+
@Param({"1000", "100000"})
35+
private int tasks;
3536

36-
private VirtualMultithreadIoEventLoopGroup executorGroup;
37+
private VirtualMultithreadIoEventLoopGroup executorGroup;
3738

38-
private ThreadFactory vtFactory;
39+
private ThreadFactory vtFactory;
3940

40-
@Setup
41-
public void setup() throws ExecutionException, InterruptedException {
42-
executorGroup = new VirtualMultithreadIoEventLoopGroup(1, NioIoHandler.newFactory());
43-
vtFactory = executorGroup.submit(executorGroup::vThreadFactory).get();
44-
}
41+
@Setup
42+
public void setup() throws ExecutionException, InterruptedException {
43+
executorGroup = new VirtualMultithreadIoEventLoopGroup(1, NioIoHandler.newFactory());
44+
vtFactory = executorGroup.submit(executorGroup::vThreadFactory).get();
45+
}
4546

46-
@Benchmark
47-
public void global() {
48-
CountDownLatch countDown = new CountDownLatch(tasks);
49-
vtFactory.newThread(
50-
() -> {
51-
for (int i = 0; i < tasks; i++) {
52-
Thread.startVirtualThread(countDown::countDown);
53-
}
54-
}
55-
).start();
56-
try {
57-
countDown.await();
58-
} catch (InterruptedException e) {
59-
throw new RuntimeException(e);
60-
}
61-
}
47+
@Benchmark
48+
public void global() {
49+
CountDownLatch countDown = new CountDownLatch(tasks);
50+
vtFactory.newThread(() -> {
51+
for (int i = 0; i < tasks; i++) {
52+
Thread.startVirtualThread(countDown::countDown);
53+
}
54+
}).start();
55+
try {
56+
countDown.await();
57+
} catch (InterruptedException e) {
58+
throw new RuntimeException(e);
59+
}
60+
}
6261

63-
@Benchmark
64-
public void inheritFromParent() {
65-
CountDownLatch countDown = new CountDownLatch(tasks);
66-
vtFactory.newThread(
67-
() -> {
68-
Thread.VirtualThreadScheduler parentScheduler = LoomSupport.getScheduler(Thread.currentThread());
69-
// Simulate the behavior of the previous version
70-
// get the scheduler from the parent thread before starting, instead of pre-initializing the `vtFactory`.
71-
for (int i = 0; i < tasks; i++) {
72-
Thread.ofVirtual()
73-
.scheduler(parentScheduler)
74-
.start(countDown::countDown);
75-
}
76-
}
77-
).start();
78-
try {
79-
countDown.await();
80-
} catch (InterruptedException e) {
81-
throw new RuntimeException(e);
82-
}
83-
}
62+
@Benchmark
63+
public void inheritFromParent() {
64+
CountDownLatch countDown = new CountDownLatch(tasks);
65+
vtFactory.newThread(() -> {
66+
Thread.VirtualThreadScheduler parentScheduler = LoomSupport.getScheduler(Thread.currentThread());
67+
// Simulate the behavior of the previous version
68+
// get the scheduler from the parent thread before starting, instead of
69+
// pre-initializing the `vtFactory`.
70+
for (int i = 0; i < tasks; i++) {
71+
Thread.ofVirtual().scheduler(parentScheduler).start(countDown::countDown);
72+
}
73+
}).start();
74+
try {
75+
countDown.await();
76+
} catch (InterruptedException e) {
77+
throw new RuntimeException(e);
78+
}
79+
}
8480

85-
@TearDown
86-
public void tearDown() {
87-
executorGroup.shutdownGracefully();
88-
}
81+
@TearDown
82+
public void tearDown() {
83+
executorGroup.shutdownGracefully();
84+
}
8985
}

0 commit comments

Comments
 (0)