Skip to content

Centralize Java thread detection and type-safe thread state handling#462

Open
zhengyu123 wants to merge 11 commits intomainfrom
zgu/java_thread
Open

Centralize Java thread detection and type-safe thread state handling#462
zhengyu123 wants to merge 11 commits intomainfrom
zgu/java_thread

Conversation

@zhengyu123
Copy link
Copy Markdown
Contributor

@zhengyu123 zhengyu123 commented Apr 10, 2026

What does this PR do?:
Centralize Java thread detection and improve thread state handling

Motivation:
Consolidate and cleanup code for consistence, maintainability and readability.

Additional Notes:

Consolidate Java thread detection
Java thread detection was scattered across VMThread instance methods, each calling cachedIsJavaThread() independently. Thread state comparisons used magic integers throughout (>= 4, < 12, ==
8, == 10, == 11), making the code fragile and hard to audit.

Centralized VMThread::isJavaThread(VMThread)*
Replaces the old cachedIsJavaThread() pattern with a single static method that:

  • Returns false immediately for nullptr (native/JNI threads outside the JVM)
  • Consults the ProfiledThread cache for previously determined results (TYPE_JAVA_THREAD / TYPE_NOT_JAVA_THREAD)
  • Falls back to vtable comparison (hasJavaThreadVtable()) for uncached threads, then stores the result

JVMJavaThreadState enum
Names the previously magic thread state integers with HotSpot-matching constants (_thread_in_Java, _thread_blocked, _thread_blocked_trans, etc.). A new convertJvmExecutionState() switch maps
them to ExecutionMode in a type-safe, exhaustive way.

Safer vtable reads
hasJavaThreadVtable() now uses SafeAccess::load() for the vtable slot comparison, guarding against unmapped memory access.

Cleaner ProfiledThread flag API
Thread type is stored as a 2-bit field in _misc_flags via setJavaThread(bool) / threadType(), replacing the ad-hoc flag manipulation in the old code.

How to test the change?:

  • Passed CI tests
  • Passed stress tests

For Datadog employees:

  • If this PR touches code that signs or publishes builds or packages, or handles
    credentials of any kind, I've requested a review from @DataDog/security-design-and-guidance.
  • This PR doesn't touch any of that.
  • JIRA: PROF-14160

Unsure? Have a question? Request a review!

@dd-octo-sts
Copy link
Copy Markdown

dd-octo-sts bot commented Apr 10, 2026

CI Test Results

Run: #24268357704 | Commit: 2552224 | Duration: 21m 30s (longest job)

All 32 test jobs passed

Status Overview

JDK glibc-aarch64/debug glibc-amd64/debug musl-aarch64/debug musl-amd64/debug
8 - - -
8-ibm - - -
8-j9 - -
8-librca - -
8-orcl - - -
11 - - -
11-j9 - -
11-librca - -
17 - -
17-graal - -
17-j9 - -
17-librca - -
21 - -
21-graal - -
21-librca - -
25 - -
25-graal - -
25-librca - -

Legend: ✅ passed | ❌ failed | ⚪ skipped | 🚫 cancelled

Summary: Total: 32 | Passed: 32 | Failed: 0


Updated: 2026-04-11 00:22:37 UTC

@zhengyu123 zhengyu123 changed the title Zgu/java thread Centralize Java thread detection and type-safe thread state handling Apr 10, 2026
@zhengyu123 zhengyu123 marked this pull request as ready for review April 11, 2026 00:48
@zhengyu123 zhengyu123 requested a review from a team as a code owner April 11, 2026 00:48
@zhengyu123 zhengyu123 requested review from jbachorik and rkennke April 11, 2026 00:48
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