Skip to content

Commit a84e36c

Browse files
committed
More cleanup
1 parent a6115a9 commit a84e36c

9 files changed

Lines changed: 34 additions & 21 deletions

File tree

ddprof-lib/src/main/cpp/hotspot/vmStructs.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,8 +678,10 @@ ExecutionMode VMThread::getExecutionMode() {
678678

679679
OSThreadState VMThread::getOSThreadState() {
680680
VMThread* vm_thread = VMThread::current();
681-
assert(vm_thread != nullptr);
682-
int raw_thread_state = vm_thread ? vm_thread->state() : 0;
681+
if (vm_thread == nullptr) {
682+
return OSThreadState::UNKNOWN;
683+
}
684+
int raw_thread_state = vm_thread->state();
683685
bool is_java_thread = raw_thread_state >= 4 && raw_thread_state < 12;
684686
OSThreadState state = OSThreadState::UNKNOWN;
685687
if (is_java_thread) {

ddprof-lib/src/main/cpp/javaApi.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "counters.h"
2222
#include "common.h"
2323
#include "engine.h"
24+
#include "hotspot/vmStructs.h"
2425
#include "incbin.h"
2526
#include "jvmThread.h"
2627
#include "os.h"
@@ -295,7 +296,7 @@ Java_com_datadoghq_profiler_JavaProfiler_recordQueueEnd0(
295296
if (tid < 0) {
296297
return;
297298
}
298-
int origin_tid = JVMThread::native_thread_id(env, origin);
299+
int origin_tid = JVMThread::nativeThreadId(env, origin);
299300
if (origin_tid < 0) {
300301
return;
301302
}

ddprof-lib/src/main/cpp/jvmThread.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
#include "jvmThread.h"
77
#include "hotspot/vmStructs.inline.h"
88
#include "j9/j9Ext.h"
9+
#include "vmEntry.h"
910

1011
pthread_key_t JVMThread::_thread_key = pthread_key_t(-1);
1112
jfieldID JVMThread::_tid = nullptr;
1213

1314
bool JVMThread::initialize() {
14-
void* current_thread = current_thread_slow();
15+
void* current_thread = currentThreadSlow();
1516
if (current_thread == nullptr) {
1617
return false;
1718
}
@@ -26,11 +27,11 @@ bool JVMThread::initialize() {
2627
return _thread_key != pthread_key_t(-1);
2728
}
2829

29-
int JVMThread::native_thread_id(JNIEnv* jni, jthread thread) {
30+
int JVMThread::nativeThreadId(JNIEnv* jni, jthread thread) {
3031
return VM::isOpenJ9() ? J9Ext::GetOSThreadID(thread) : VMThread::nativeThreadId(jni, thread);
3132
}
3233

33-
void* JVMThread::current_thread_slow() {
34+
void* JVMThread::currentThreadSlow() {
3435
jthread thread;
3536
if (VM::jvmti()->GetCurrentThread(&thread) != JVMTI_ERROR_NONE) {
3637
return nullptr;

ddprof-lib/src/main/cpp/jvmThread.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@
88

99
#include <cassert>
1010
#include <jni.h>
11+
#include <jvmti.h>
1112
#include <pthread.h>
1213

13-
#include "hotspot/vmStructs.h"
14-
#include "j9/j9Ext.h"
15-
#include "vmEntry.h"
16-
1714
/**
1815
* JVMThread represents a native JVM thread that is JVM implementation agnostic
1916
*/
@@ -23,7 +20,7 @@ class JVMThread {
2320
static jfieldID _tid;
2421

2522
public:
26-
static bool is_initialized() {
23+
static bool isInitialized() {
2724
return _thread_key != pthread_key_t(-1);
2825
}
2926

@@ -33,14 +30,15 @@ class JVMThread {
3330
*/
3431
static bool initialize();
3532
static inline void* current() {
33+
assert(isInitialized());
3634
return pthread_getspecific(_thread_key);
3735
}
3836

3937
static inline pthread_key_t key() {
4038
return _thread_key;
4139
}
4240

43-
static int native_thread_id(JNIEnv* jni, jthread thread);
41+
static int nativeThreadId(JNIEnv* jni, jthread thread);
4442

4543
static inline jlong javaThreadId(JNIEnv* env, jthread thread) {
4644
return env->GetLongField(thread, _tid);
@@ -51,7 +49,7 @@ class JVMThread {
5149
}
5250

5351
private:
54-
static void* current_thread_slow();
52+
static void* currentThreadSlow();
5553
};
5654

5755
#endif // _JVMTHREAD_H

ddprof-lib/src/main/cpp/perfEvents_linux.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ static void **_pthread_entry = NULL;
168168
// pthread_setspecific(). HotSpot puts VMThread into TLS on thread start, and
169169
// resets on thread end.
170170
static int pthread_setspecific_hook(pthread_key_t key, const void *value) {
171-
assert(JVMThread::is_initialized());
171+
assert(JVMThread::isInitialized());
172172
if (JVMThread::key() != key) {
173173
return pthread_setspecific(key, value);
174174
}

ddprof-lib/src/main/cpp/profiler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void Profiler::onThreadEnd(jvmtiEnv *jvmti, JNIEnv *jni, jthread thread) {
142142
ProfiledThread::release();
143143
} else {
144144
// ProfiledThread already cleaned up - try to get tid from JVMTI as fallback
145-
tid = JVMThread::native_thread_id(jni, thread);
145+
tid = JVMThread::nativeThreadId(jni, thread);
146146
if (tid < 0) {
147147
// No ProfiledThread AND can't get tid from JVMTI - nothing we can do
148148
return;
@@ -1154,7 +1154,7 @@ void Profiler::updateThreadName(jvmtiEnv *jvmti, JNIEnv *jni, jthread thread,
11541154
native_thread_id = ProfiledThread::currentTid();
11551155
assert(native_thread_id != -1);
11561156
} else {
1157-
native_thread_id = JVMThread::native_thread_id(jni, thread);
1157+
native_thread_id = JVMThread::nativeThreadId(jni, thread);
11581158
}
11591159

11601160
if (native_thread_id >= 0 &&
@@ -1274,7 +1274,7 @@ Engine *Profiler::selectAllocEngine(Arguments &args) {
12741274
}
12751275

12761276
Error Profiler::checkJvmCapabilities() {
1277-
if (!JVMThread::is_initialized()) {
1277+
if (!JVMThread::isInitialized()) {
12781278
return Error("Could not find JVMThread bridge. Unsupported JVM?");
12791279
}
12801280

ddprof-lib/src/main/cpp/stackWalker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ __attribute__((no_sanitize("address"))) int StackWalker::walkVM(void* ucontext,
848848
}
849849

850850
void StackWalker::checkFault(ProfiledThread* thrd) {
851-
if (!JVMThread::is_initialized()) {
851+
if (!JVMThread::isInitialized()) {
852852
// JVM has not been loaded or VMStructs have not been initialized yet
853853
return;
854854
}

ddprof-lib/src/main/cpp/threadState.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef JAVA_PROFILER_LIBRARY_THREAD_STATE_H
2-
#define JAVA_PROFILER_LIBRARY_THREAD_STATE_H
1+
#ifndef _THREADSTATE_H
2+
#define _THREADSTATE_H
33

44

55
enum class OSThreadState : int {
@@ -28,4 +28,4 @@ enum class ExecutionMode : int {
2828
inline ExecutionMode getThreadExecutionMode();
2929
inline OSThreadState getOSThreadState();
3030

31-
#endif // JAVA_PROFILER_LIBRARY_THREAD_STATE_H
31+
#endif // _THREADSTATE_H

ddprof-lib/src/main/cpp/threadState.inline.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/*
2+
* Copyright The async-profiler authors
3+
* Copyright 2026 Datadog, Inc
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef _THREADSTATE_INLINE_H
8+
#define _THREADSTATE_INLINE_H
9+
110
#include "threadState.h"
211
#include "hotspot/vmStructs.h"
312

@@ -10,3 +19,5 @@ inline OSThreadState getOSThreadState() {
1019
return VM::isHotspot() ? VMThread::getOSThreadState() :
1120
OSThreadState::UNKNOWN;
1221
}
22+
23+
#endif // _THREADSTATE_INLINE_H

0 commit comments

Comments
 (0)