Slide 1

Slide 1 text

06 Dec 2023 Balkrishna Rawool Fantastic Virtual Threads and How to Monitor Them

Slide 2

Slide 2 text

2 Balkrishna Rawool IT Chapter Lead, ING Bank @BalaRawool

Slide 3

Slide 3 text

Fantastic virtual threads

Slide 4

Slide 4 text

4 Virtual Threads JVM OS Platform Threads Platform Threads: Ø Thin wrapper over OS threads Ø One-to-one mapping with OS threads Ø Thread pools Virtual Threads Virtual Threads: Ø Lightweight user threads Ø Highly scalable Ø No need for pooling

Slide 5

Slide 5 text

8 Virtual Thread Scheduler Platform/ Carrier Thread Virtual Threads

Slide 6

Slide 6 text

How to create virtual threads?

Slide 7

Slide 7 text

Be aware of pinning! 10 Ø Pinning is when virtual thread cannot be unmounted from the carrier thread. Ø It happens when: Ø Virtual thread is executing a synchronized code-block/ method or Ø Virtual thread is executing a native call Ø In case of ‘synchronized’ Ø Problem if you’re doing long-running/ blocking operations inside synchronized Ø Use ReentrantLock Ø In future, this may not pin the virtual thread

Slide 8

Slide 8 text

How to monitor them (virtual threads)?

Slide 9

Slide 9 text

Monitoring toolset 12 Ø JFR (JDK Flight Recorder) Ø JMC (JDK Mission Control) Ø VisualVM Ø jcmd

Slide 10

Slide 10 text

jcmd 13 Ø Helps us to send diagnostic command requests to the JVM Ø Many commands are available Ø JFR Ø Garbage collection Ø JVM Ø Must be used on the same machine where the JVM is running Ø Usage: Ø New: Thread dump in JSON format jcmd [options] jcmd Thread.dump_to_file –format=json

Slide 11

Slide 11 text

JFR (JDK Flight Recorder) 14 Ø An event-based tracing framework, built into the Java Runtime. Ø It gathers detailed low-level information about how a JVM or a Java application is behaving. Ø It maintains low overhead (< 1%). Ø Data is continuously recorded, optionally written to disk. Ø It records data about JVM events. Ø Types of events: Ø Duration events Ø Instant events Ø Sample events Ø Custom events

Slide 12

Slide 12 text

JFR (JDK Flight Recorder) 15 Ø New events added for supporting virtual threads: Ø jdk.VirtualThreadStart Ø jdk.VirtualThreadEnd Ø jdk.VirtualThreadPinned Ø jdk.VirtualThreadSubmitFailed Ø Starting JFR Ø During starting the application Ø Using jcmd Ø From JMC Ø JEP-349 Enables JFR Event Streaming java –XX:StartFlightRecording:filename=./recordings/myapp.jfr,dumponexit=true … jcmd 1234 JFR.start jcmd 1234 JFR.dmp filename=./recordings/myapp.jfr

Slide 13

Slide 13 text

JMC 16 Ø Control panel for managing, monitoring and troubleshooting JVM applications Ø Gives a lot of info including CPU usage, Memory usage, Threads and MBeans Ø You can also run jcmd commands from JMC Ø Views: Ø MBean Server: Info from JMX (Java Management Extension) Ø JDK Flight Recorder Ø Plug-ins

Slide 14

Slide 14 text

Visual VM 17 Ø Visual interface for viewing detailed information about Java applications Ø Connects with local and remote applications. Ø Provides info about CPU usage, Memory usage, Threads, Classes etc. Ø In terms of features quite similar to JMC

Slide 15

Slide 15 text

Monitoring toolset - summary 18 Ø JFR is quite powerful and provide lots of info with low overhead Ø jcmd is quite useful for creating/managing recordings and for other diagnostics Ø JMC and VisualVM are feature rich and provide visual interface Ø Virtual thread support Ø 4 new events in JFR Ø Thread dump in JSON format via jcmd Ø JFR and jcmd are available as part of JDK. JMC and VisualVM are available externally.

Slide 16

Slide 16 text

Solve problem with our app

Slide 17

Slide 17 text

Source code 21 github.com/balkrishnarawool

Slide 18

Slide 18 text

Connect with me at 23 @BalaRawool Please don’t forget to rate the talk!

Slide 19

Slide 19 text

No content