Virtual threads

From HandWiki
Short description: Computational threads scheduled by a run-time library

In computer programming, virtual threads are threads that are scheduled by a runtime library instead of natively by the underlying operating system (OS). Virtual threads allows for tens of millions of preemptive tasks and events without swapping on a 2021 consumer-grade computer..,[1] compared to low thousands of operating system threads.[2] Preemptive execution[3] is important to performance gains through parallelism and fast preemptive response times for tens of millions of events. Earlier constructs that are not preemptive, such as coroutines or the largely single-threaded Node.js, introduce delays in responding to asynchronous events such as every incoming request in a server application[4]

Definition

The term Virtual threads has been used ambiguously over the years. For example, Intel[5] in 2007 spoke about their hyper-threading hardware as virtual threads. Virtual threads were commercialized with Google's Chrome browser in 2008[6] where virtual threads may hop physical threads. Virtual threads are truly virtual, created in user-space software

  • Virtual threads are preemptive
    • This is important for response performance, that the virtual thread can react to events without programmer intervention or before concluding a current task
    • Preemption requires knowledge of multi-threaded programming to avoid teared writes, data race and invisible writes by other threads
  • Virtual threads can hop over the execution units of all processors and cores
    • This allows for using all available hardware, a 10x increase on today's computers
    • In the go1.18 implementation, there are virtual thread queues per execution unit. There are additional virtual threads not allocated to an execution unit and an execution unit can steal virtual threads from another execution unit[7]
  • Virtual threads require no yield or similar interventions by the programmer
    • Virtual threads appear to execute continuously until they return or stop at a synchronization lock
    • Unlike coroutines, if a virtual thread is in an infinite loop, it does not block the program. Execution continues at a higher cpu load, even if there are more looping threads than available execution units
  • Virtual threads can exists in tens of millions by featuring small often managed stacks
    • This allows for many magnitudes more threads than from using OS threads
    • Go 1.18 can launch 15 million virtual threads on a 2021 consumer-grade computer, i.e. about 350,000 per gigabyte of main memory. This is enabled by goroutines having a resizable, less than 3 KiB stack
    • A consumer grade computer typically supports 3,000 OS threads an can through system configuration offer maybe 15,000
  • Virtual threads can be allocated quickly, similar to the rate of memory allocations
    • Because allocating a virtual thread is akin to allocate memory structures, they can be allocated very quickly, like 600,000 per second. This is not possible for OS threads that would crash the host long before this rate
    • The quicker ramp-up lessens the need for thread-pools of pre-launched threads to cater for sudden increases in traffic
    • In Go, a virtual thread is allocated using a function call preceded by the keyword go. The function call provides a closure of variable values guaranteed to be visible to the new goroutine. goroutines have no return value, so a goroutine that returns just disappears
  • Virtual threads share memory map like OS threads
    • Like OS threads, virtual threads share the memory across the process and can therefore freely share and access memory objects subject to synchronization
    • Some single-threaded architectures, such as the V8 ECMAScript engine used by Node.js, do not readily accept data that the particular thread did not allocate, requiring special zero-copy data types to be used when sharing data between threads
  • Virtual threads offer parallelism like OS threads
    • Parallelism means that multiple instructions are executed truly at the same time which typically leads to a magnitude of faster performance
    • This is different from the simpler concurrency, in which a single execution unit executes multiple threads shared in small time increments. The sharing makes each thread appear to be continuously executing. While concurrency is easier to implement and program, it do not offer any gains in performance

Underlying reasons

Java servers have featured extensive and memory consuming software constructs allowing dozens of pooled operating system threads to preemptively execute thousands of requests per second without the use of virtual threads. Key to performance here is to reduce the initial latency in thread processing and minimize the time operating system threads are blocked[8]

Virtual threads increase possible concurrency by many orders of magnitudes while the actual parallelism achieved is limited by available execution units and pipelining offered by present processors and processor cores. In 2021, a consumer grade computers typically offer a parallelism of tens of concurrent execution units.[9] For increased performance through parallelism, the language runtime need to use all present hardware,[10] not be single-threaded or feature global synchronization such as global interpreter lock

The many magnitudes of increase in possible preemptive items offered by virtual threads is achieved by the language runtime managing resizable thread stacks.[11] Those stacks are smaller in size than those of operating system threads. The maximum number of threads possible without swapping is proportional to the amount of main memory[12]

In order to support virtual threads efficiently, the language runtime has to be largely rewritten to prevent blocking calls from holding up an operating system thread assigned to execute a virtual thread[13] and to manage thread stacks.[14] An example of a retrofit of virtual threads is Java Loom.[15] An example of a new language designed for virtual threads is Go[16]

Complexity

Because virtual threads offer parallelism, the programmer needs to be skilled in multi-threaded programming and synchronization

Because a blocked virtual thread would block the OS thread it occupies at the moment, much effort must be taken in the runtime to handle blocking system calls. Typically, a thread from an pool of spare OS threads is used to execute the blocking call for the virtual thread so that the initially executing OS thread is not blocked

Management of the virtual thread stack requires care in the linker and short predictions of additional stack space requirements

Implementations

Google Chrome Browser

Virtual threads are used to serialize singleton input/output activities. When a virtual thread is executing, it can hop on different OS thread. The Chrome browser first appeared in 2008. Chrome's virtual threads are available to developers extending the browser

Go

goroutines became preemptive with go1.4 in 2014 and is since the most prominent application of virtual threads. A goroutine is a virtual thread. Go first appeared in 2009

Java Project Loom

Project Loom: Virtual threads is a lightweight user-mode scheduled alternative to standard OS managed threads. Virtual threads are mapped to OS threads in a many-to-many relationship. Work on project loom by Oracle started in 2017. Loom has the goal of implementing virtual threads for performance, at the same time simplifying thread handling across OS threads, concurrent threads and virtual threads. As of 2022, Project Loom is available as early-access using JDK 19

See also

References

  1. Rudell, Harald (2022-03-19). "massivevirtualparallelism". https://codeberg.org/haraldrudell/massivevirtualparallelism/src/branch/main/README.md. 
  2. baeldung (2022-01-02). "Maximum Number of Threads Per Process in Linux | Baeldung on Linux" (in en-US). https://www.baeldung.com/linux/max-threads-per-process. 
  3. "Go 1.14 Release Notes - The Go Programming Language". https://go.dev/doc/go1.14#runtime. 
  4. Node.js. "Don't Block the Event Loop (or the Worker Pool)" (in en). https://nodejs.org/en/docs/guides/dont-block-the-event-loop/. 
  5. "Intel Technology Journal". https://www.intel.com/content/dam/www/public/us/en/documents/research/2007-vol11-iss-4-intel-technology-journal.pdf. 
  6. "Threading and Tasks in Chrome". https://chromium.googlesource.com/chromium/src/+/HEAD/docs/threading_and_tasks.md#core-concepts. 
  7. Lu, Genchi (2021-07-22). "Java’s Thread Model and Golang Goroutine" (in en). https://medium.com/@genchilu/javas-thread-model-and-golang-goroutine-f1325ca2df0c. 
  8. "Principles to Handle Thousands of Connections in Java Using Netty - DZone Performance" (in en). https://dzone.com/articles/thousands-of-socket-connections-in-java-practical. 
  9. "MacBook Pro 14-inch and MacBook Pro 16-inch" (in en-US). https://www.apple.com/macbook-pro-14-and-16/. 
  10. "Frequently Asked Questions (FAQ) - The Go Programming Language". https://go.dev/doc/faq#number_cpus. 
  11. "JEP draft: Virtual Threads (Preview)". https://openjdk.java.net/jeps/8277131#Memory-use-and-interaction-with-garbage-collection. 
  12. Rudell, Harald (2022-03-22). "Maximum number of virtual threads in Go". https://www.reddit.com/r/golang/comments/tifbow/comment/i1owqo9. 
  13. Szczukocki, Denis (2020-03-18). "Difference Between Thread and Virtual Thread in Java | Baeldung" (in en-US). https://www.baeldung.com/java-virtual-thread-vs-thread. 
  14. "Why you can have millions of Goroutines but only thousands of Java Threads" (in en-us). 2018-04-12. https://rcoh.me/posts/why-you-can-have-a-million-go-routines-but-only-1000-java-threads/. 
  15. "Main - Main - OpenJDK Wiki". https://wiki.openjdk.java.net/display/loom/Main. 
  16. "The Go Programming Language". 2022-03-22. https://go.dev/. 

External links