<div dir="ltr">I vaguely remember Alan or Ron saying that timed waits for VTs are not yet as efficient as they want them to be (though I'm not sure if that is the case for JDK 23 still). So, I would expect that the major performance difference you are seeing might be because of the sleep calls (and in realistic scenarios, I'm guessing the same would be true for other timed waits).</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Liam Miller-Cushon <<a href="mailto:cushon@google.com">cushon@google.com</a>> ezt írta (időpont: 2024. máj. 29., Sze, 0:44):<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hello,<div><br>JEP 444 [1] and the docs in [2] mention that virtual threads should not be pooled, and suggest semaphores as one possible alternative.</div><div><br>My colleague Chi Wang has been investigating virtual thread performance, and has found that creating one virtual thread per task and synchronizing on a semaphore can result in worse performance on machines with large numbers of cores.<br><br>A benchmark run on a 128 core machine is included below. It submits numTasks tasks to the executor determined by the strategy. The task itself is mixed with CPU and I/O work (simulated by fibonacci and sleep). The parallelism is set to 600 for all strategies.</div><div><br></div><div>* Strategy 1 is the baseline where it just submits all the tasks to a ForkJoinPool whose pool size is 600.<br>* Strategy 2 uses the method suggested by JEP 444.<br>* Strategy 3 uses a fixed thread pool of 600 backed by virtual threads.<br><br>Note that, with 100K and 1M tasks, strategy 2 has a CPU time regression that seems to increase with the number of cores. This result can be reproduced on the real-world program that is being migrated to a virtual-thread-per-task model.<br><br>Diffing the cpu profile between strategy 1 and strategy 2 showed that most of the CPU regression comes from method `java.util.concurrent.ForkJoinPool.scan(java.util.concurrent.ForkJoinPool$WorkQueue, long, int)`.<br><br>Are there any ideas for why the semaphore strategy uses more CPU than pooling virtual threads on machines with a large number of cores?<br><br>Thanks,<br>Liam<br><br>[1] <a href="https://openjdk.org/jeps/444#Do-not-pool-virtual-threads" target="_blank">https://openjdk.org/jeps/444#Do-not-pool-virtual-threads</a><br>[2] <a href="https://docs.oracle.com/en/java/javase/22/core/virtual-threads.html#GUID-2BCFC2DD-7D84-4B0C-9222-97F9C7C6C521" target="_blank">https://docs.oracle.com/en/java/javase/22/core/virtual-threads.html#GUID-2BCFC2DD-7D84-4B0C-9222-97F9C7C6C521</a><br><br><font face="monospace">import java.util.concurrent.ExecutorService;<br>import java.util.concurrent.Executors;<br>import java.util.concurrent.ForkJoinPool;<br>import java.util.concurrent.Semaphore;<br><br>public class Main {<br><br>  private static Semaphore semaphore = null;<br><br>  public static void main(String[] args) {<br>    int strategy = 0;<br>    int parallelism = 600;<br>    int numTasks = 10000;<br><br>    if (args.length > 1) {<br>      strategy = Integer.parseInt(args[1]);<br>    }<br><br>    if (args.length > 2) {<br>      numTasks = Integer.parseInt(args[2]);<br>    }<br><br>    ExecutorService executor;<br>    switch (strategy) {<br>      case 1 -> {<br>        executor = new ForkJoinPool(parallelism);<br>      }<br>      case 2 -> {<br>        executor = Executors.newVirtualThreadPerTaskExecutor();<br>        semaphore = new Semaphore(parallelism);<br>      }<br>      case 3 -> {<br>        executor = Executors.newFixedThreadPool(parallelism, Thread.ofVirtual().factory());<br>      }<br>      default -> {<br>        throw new IllegalArgumentException();<br>      }<br>    }<br><br>    try (executor) {<br>      for (var i = 0; i < numTasks; ++i) {<br>        executor.execute(Main::task);<br>      }<br>    }<br>  }<br><br>  private static void task() {<br>    if (semaphore != null) {<br>      try {<br>        semaphore.acquire();<br>      } catch (InterruptedException e) {<br>        throw new IllegalStateException();<br>      }<br>    }<br><br>    try {<br>      fibonacci(20);<br>      try {<br>        Thread.sleep(10);<br>      } catch (InterruptedException e) {<br>      }<br>      fibonacci(20);<br>      try {<br>        Thread.sleep(10);<br>      } catch (InterruptedException e) {<br>      }<br>      fibonacci(20);<br>    } finally {<br>      if (semaphore != null) {<br>        semaphore.release();<br>      }<br>    }<br>  }<br><br>  private static int fibonacci(int n) {<br>    if (n == 0) {<br>      return 0;<br>    } else if (n == 1) {<br>      return 1;<br>    } else {<br>      return fibonacci(n - 1) + fibonacci(n - 2);<br>    }<br>  }<br>}</font><br><br><font face="monospace"># openjdk full version "23-ea+24-1995"<br># AMD Ryzen Threadripper PRO 3995WX, hyperthreading enabled<br><br></font></div><div><font face="monospace">$ hyperfine --parameter-scan strategy 1 3 --parameter-list numTasks 10000,100000,1000000 './jdk-23/bin/java Main -- {strategy} {numTasks}'</font></div><div><font face="monospace"><br>Benchmark 1: ./jdk-23/bin/java Main -- 1 10000<br>  Time (mean ± σ):     658.3 ms ±  24.4 ms    [User: 26808.8 ms, System: 493.7 ms]</font><br><font face="monospace">  Range (min … max):   613.1 ms … 702.0 ms    10 runs</font><br><font face="monospace"> </font><br><font face="monospace">Benchmark 2: ./jdk-23/bin/java Main -- 2 10000</font><br><font face="monospace">  Time (mean ± σ):     486.9 ms ±  28.5 ms    [User: 14804.4 ms, System: 501.5 ms]</font><br><font face="monospace">  Range (min … max):   451.0 ms … 533.4 ms    10 runs</font><br><font face="monospace"> </font><br><font face="monospace">Benchmark 3: ./jdk-23/bin/java Main -- 3 10000</font><br><font face="monospace">  Time (mean ± σ):     452.0 ms ±  10.8 ms    [User: 6598.1 ms, System: 335.8 ms]</font><br><font face="monospace">  Range (min … max):   435.6 ms … 470.6 ms    10 runs</font><br><font face="monospace"> </font><br><font face="monospace">Benchmark 4: ./jdk-23/bin/java Main -- 1 100000</font><br><font face="monospace">  Time (mean ± σ):      3.668 s ±  0.028 s    [User: 38.469 s, System: 1.188 s]</font><br><font face="monospace">  Range (min … max):    3.628 s …  3.704 s    10 runs</font><br><font face="monospace"> </font><br><font face="monospace">Benchmark 5: ./jdk-23/bin/java Main -- 2 100000</font><br><font face="monospace">  Time (mean ± σ):      3.612 s ±  0.042 s    [User: 65.924 s, System: 2.072 s]</font><br><font face="monospace">  Range (min … max):    3.563 s …  3.687 s    10 runs</font><br><font face="monospace"> </font><br><font face="monospace">Benchmark 6: ./jdk-23/bin/java Main -- 3 100000</font><br><font face="monospace">  Time (mean ± σ):      3.503 s ±  0.008 s    [User: 27.791 s, System: 1.211 s]</font><br><font face="monospace">  Range (min … max):    3.492 s …  3.515 s    10 runs</font><br><font face="monospace"> </font><br><font face="monospace">Benchmark 7: ./jdk-23/bin/java Main -- 1 1000000</font><br><font face="monospace">  Time (mean ± σ):     34.093 s ±  0.031 s    [User: 206.235 s, System: 14.313 s]</font><br><font face="monospace">  Range (min … max):   34.015 s … 34.120 s    10 runs</font><br><font face="monospace"> </font><br><font face="monospace">Benchmark 8: ./jdk-23/bin/java Main -- 2 1000000</font><br><font face="monospace">  Time (mean ± σ):     34.354 s ±  0.063 s    [User: 330.215 s, System: 17.501 s]</font><br><font face="monospace">  Range (min … max):   34.267 s … 34.479 s    10 runs</font><br><font face="monospace"> </font><br><font face="monospace">Benchmark 9: ./jdk-23/bin/java Main -- 3 1000000</font><br><font face="monospace">  Time (mean ± σ):     34.551 s ±  1.018 s    [User: 238.050 s, System: 10.258 s]</font><br><font face="monospace">  Range (min … max):   34.124 s … 37.420 s    10 runs</font><br><font face="monospace"> </font><br><font face="monospace">Summary</font><br><font face="monospace">  ./jdk-23/bin/java Main -- 3 10000 ran</font><br><font face="monospace">    1.08 ± 0.07 times faster than ./jdk-23/bin/java Main -- 2 10000</font><br><font face="monospace">    1.46 ± 0.06 times faster than ./jdk-23/bin/java Main -- 1 10000</font><br><font face="monospace">    7.75 ± 0.19 times faster than ./jdk-23/bin/java Main -- 3 100000</font><br><font face="monospace">    7.99 ± 0.21 times faster than ./jdk-23/bin/java Main -- 2 100000</font><br><font face="monospace">    8.12 ± 0.20 times faster than ./jdk-23/bin/java Main -- 1 100000</font><br><font face="monospace">   75.43 ± 1.80 times faster than ./jdk-23/bin/java Main -- 1 1000000</font><br><font face="monospace">   76.01 ± 1.82 times faster than ./jdk-23/bin/java Main -- 2 1000000</font><br><font face="monospace">   76.44 ± 2.90 times faster than ./jdk-23/bin/java Main -- 3 1000000</font><br></div></div>
</blockquote></div>