Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fantastic Virtual Threads and How to Monitor Them

Fantastic Virtual Threads and How to Monitor Them

Balkrishna Rawool

January 12, 2024
Tweet

More Decks by Balkrishna Rawool

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. Monitoring toolset 12 Ø JFR (JDK Flight Recorder) Ø JMC

    (JDK Mission Control) Ø VisualVM Ø jcmd
  4. 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 <process id/main class> <command> [options] jcmd <process id/main class> Thread.dump_to_file –format=json <file>
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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.