Skip to content

Implement ThreadMXBean thread info, deadlock detection and locked monitor and synchronizer dumps - #65

Merged
Project516 merged 1 commit into
masterfrom
p516/thread-info
Nov 6, 2026
Merged

Project516 merged 1 commit into
masterfrom
p516/thread-info

Conversation

@Project516

Copy link
Copy Markdown
Owner

Implements the ThreadMXBean natives that describe threads, so getThreadInfo, dumpAllThreads and deadlock detection work as on HotSpot.

  • getThreadInfo1 and dumpThreads0 build ThreadInfo from the VM's thread records as HotSpot's ThreadSnapshot does: state as Thread.getState() reports it, the lock a thread is blocked on or waiting for and its owner, the park blocker of a parked thread, and the stack trace to maxDepth. A carrier thread reports waiting on its mounted virtual thread.
  • findMonitorDeadlockedThreads0 and findDeadlockedThreads0 port find_deadlocks_at_safepoint: cycles over blocked monitors, and with synchronizers the park blocker's exclusiveOwnerThread.
  • With lockedMonitors, each frame's locked monitors come from its synchronized blocks, found in the method's exception table, and the locals that hold the lock objects. Synchronized methods use Frame.lock. A monitor no frame accounts for is listed at depth -1.
  • With lockedSynchronizers, the owned ownable synchronizers come from a walk of the class statics and thread stacks, as HotSpot inspects the heap.
  • Blocked and waited counts are kept. Times are -1 and CPU time is off, because contention monitoring stays off. VMManagementImpl reports object monitor and synchronizer usage as supported.

The monitor and lock fast paths are unchanged: no new stores on monitorenter, monitorexit or setExclusiveOwnerThread. The only new per-thread state is written when a thread blocks, waits or sleeps, and the snapshot restores the waiting monitor.

ReentrantLock, read lock and write lock loops (3M uncontended lock and unlock, master against this branch, 10 alternating pairs, minimum time in ms): reentrant 778 against 791, read 1776 against 1770, write 1335 against 1315. The spread between runs is far larger than these differences.

Tests: test/java/ThreadInfos.java matches HotSpot. It covers a monitor deadlock, a ReentrantLock deadlock, blocked, waiting, timed waiting, sleeping and parked threads, the caller's own locks in one frame and in a synchronized method, depth limits and dumpAllThreads. Seven ThreadMXBean tests and the four CarrierThreadInfo tests join passing.txt with their durations in times.tsv. All 45 java/lang/management and CarrierThreadInfo tests in passing.txt pass. The suite has 189 passing, 0 failing and 3 skipped, and pnpm typecheck passes. A scratch program with recursive locks, a lock held across wait and a lock held in a hot loop also matches HotSpot.

docs/support.md no longer says getThreadInfo and dumpThreads are missing.

Noticed, not fixed:

  • ThreadMXBean/SynchronizationStatistics needs contention monitoring, which stays off.
  • Locked synchronizers cost a walk over the reachable heap per request, so a large heap makes dumpAllThreads(true, true) slow. HotSpot pays the same kind of cost.
  • Hand-written bytecode that does not follow the compiler's synchronized-block shape lists its monitors at depth -1.

@coderabbitai

coderabbitai Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 34 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 2bfbc33d-f5fc-4584-b655-0f62855abc04
Commits

Reviewing files that changed from the base of the PR and between d8e3d25 and c1c98f7.

Files ignored due to path filters: 1

  • test/jtreg/times.tsv is excluded by !**/*.tsv
Files selected for processing: 8

  • docs/support.md
  • src/jvm.ts
  • src/natives/lang.ts
  • src/natives/management.ts
  • src/snapshot.ts
  • src/thread.ts
  • test/java/ThreadInfos.java
  • test/jtreg/passing.txt
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@project516-review-bot project516-review-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR implements ThreadMXBean thread info, deadlock detection, and locked monitor/synchronizer dumps. It adds per-thread blocked/waited counts, snapshot logic to capture thread state (blocker, owner, frames), synchronized block analysis via exception tables to report locked monitors per frame, a heap walk from roots to find owned synchronizers, and a cycle detector for monitor and synchronizer deadlocks. Carrier threads report waiting on their mounted virtual thread. Contention monitoring stays off so times are -1. Tests pass and match HotSpot output.


review-bot, model nvidia/nemotron-3-ultra-550b-a55b:free, verdict approve

@Project516
Project516 merged commit 85702e3 into master Nov 6, 2026
16 checks passed
@Project516
Project516 deleted the p516/thread-info branch November 6, 2026 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant