Skip to content

Commit ea1c93c

Browse files
committed
v8
1 parent d722adc commit ea1c93c

7 files changed

Lines changed: 55 additions & 52 deletions

File tree

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <string.h>
88
#include <unistd.h>
99
#include <stdarg.h>
10-
#include "hotspot/vmStructs.h"
10+
#include "hotspot/vmStructs.inline.h"
1111
#include "vmEntry.h"
1212
#include "jniHelper.h"
1313
#include "jvmHeap.h"
@@ -79,7 +79,7 @@ DECLARE_LONG_CONSTANTS_DO(INIT_LONG_CONSTANT, INIT_LONG_CONSTANT)
7979
#undef INIT_INT_CONSTANT
8080
#undef INIT_LONG_CONSTANT
8181

82-
82+
jfieldID VMStructs::_eetop = NULL;
8383
jfieldID VMStructs::_klass = NULL;
8484
intptr_t VMStructs::_env_offset = -1;
8585
void* VMStructs::_java_thread_vtbl[6];
@@ -605,26 +605,27 @@ void VMStructs::checkNativeBinding(jvmtiEnv *jvmti, JNIEnv *jni,
605605
jvmti->Deallocate((unsigned char *)method_name);
606606
}
607607

608-
void VMThread::initialize(void* current) {
609-
VMThread* vm_thread = VMThread::cast(current);
608+
void* VMThread::initialize(jthread thread) {
609+
JNIEnv* env = VM::jni();
610+
jclass thread_class = env->FindClass("java/lang/Thread");
611+
if (thread_class == NULL) {
612+
env->ExceptionClear();
613+
return nullptr;
614+
}
615+
616+
// Get eetop field - a bridge from Java Thread to VMThread
617+
if ((_eetop = env->GetFieldID(thread_class, "eetop", "J")) == NULL) {
618+
// No such field - probably not a HotSpot JVM
619+
env->ExceptionClear();
620+
return nullptr;
621+
}
622+
623+
VMThread* vm_thread = fromJavaThread(env, thread);
610624
assert(vm_thread != nullptr);
611625
_has_native_thread_id = _thread_osthread_offset >= 0 && _osthread_id_offset >= 0;
612-
JNIEnv* env = VM::jni();
613626
_env_offset = (intptr_t)env - (intptr_t)vm_thread;
614627
memcpy(_java_thread_vtbl, vm_thread->vtable(), sizeof(_java_thread_vtbl));
615-
}
616-
617-
VMThread* VMThread::current() {
618-
return VMThread::cast(JVMThread::current());
619-
}
620-
621-
int VMThread::nativeThreadId(JNIEnv* jni, jthread thread) {
622-
// assert(_has_native_thread_id);
623-
if (_has_native_thread_id) {
624-
VMThread* vm_thread = cast(JVMThread::fromJavaThread(jni, thread));
625-
return vm_thread != NULL ? vm_thread->osThreadId() : -1;
626-
}
627-
return -1;
628+
return (void*)vm_thread;
628629
}
629630

630631
int VMThread::osThreadId() {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ class VMStructs {
351351
#undef DECLARE_LONG_CONSTANT_VAR
352352

353353

354+
static jfieldID _eetop;
354355
static jfieldID _klass;
355356
static intptr_t _env_offset;
356357
static void* _java_thread_vtbl[6];
@@ -690,13 +691,13 @@ enum JVMJavaThreadState {
690691

691692
DECLARE(VMThread)
692693
public:
693-
static void initialize(void* current);
694+
static void* initialize(jthread thread);
694695

695696
static inline VMThread* current();
696-
static int nativeThreadId(JNIEnv* jni, jthread thread);
697+
static inline int nativeThreadId(JNIEnv* jni, jthread thread);
698+
static inline VMThread* fromJavaThread(JNIEnv* env, jthread thread);
697699

698700
int osThreadId();
699-
700701
JNIEnv* jni();
701702

702703
const void** vtable() {

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,28 @@
66

77
#include "hotspot/vmStructs.h"
88
#include "jvmThread.h"
9+
910
VMThread* VMThread::current() {
10-
void* current = JVMThread::current();
11-
return VMThread::cast(current);
11+
return VMThread::cast(JVMThread::current());
12+
}
13+
14+
VMThread* VMThread::fromJavaThread(JNIEnv* env, jthread thread) {
15+
assert(_eetop != nullptr);
16+
if (_eetop != nullptr) {
17+
void* thr = (void*)env->GetLongField(thread, _eetop);
18+
return VMThread::cast(thr);
19+
} else {
20+
return nullptr;
21+
}
22+
}
23+
24+
int VMThread::nativeThreadId(JNIEnv* jni, jthread thread) {
25+
// assert(_has_native_thread_id);
26+
if (_has_native_thread_id) {
27+
VMThread* vm_thread = fromJavaThread(jni, thread);
28+
return vm_thread != NULL ? vm_thread->osThreadId() : -1;
29+
}
30+
return -1;
1231
}
1332

1433
VMNMethod* VMMethod::code() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
#include "common.h"
2323
#include "engine.h"
2424
#include "incbin.h"
25+
#include "jvmThread.h"
2526
#include "os.h"
2627
#include "otel_process_ctx.h"
2728
#include "profiler.h"
2829
#include "thread.h"
2930
#include "tsc.h"
3031
#include "vmEntry.h"
31-
#include "hotspot/vmStructs.h"
3232
#include "wallClock.h"
3333
#include <errno.h>
3434
#include <fstream>
@@ -296,7 +296,7 @@ Java_com_datadoghq_profiler_JavaProfiler_recordQueueEnd0(
296296
if (tid < 0) {
297297
return;
298298
}
299-
int origin_tid = VMThread::nativeThreadId(env, origin);
299+
int origin_tid = JVMThread::native_thread_id(env, origin);
300300
if (origin_tid < 0) {
301301
return;
302302
}

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
#include "jvmThread.h"
7+
#include "hotspot/vmStructs.inline.h"
78
#include "j9/j9Ext.h"
89

910
pthread_key_t JVMThread::_thread_key = pthread_key_t(-1);
@@ -22,17 +23,14 @@ bool JVMThread::initialize() {
2223
break;
2324
}
2425
}
25-
2626
assert(_tid != nullptr);
27-
assert(!VM::isHotspot() || _eetop != nullptr);
28-
29-
if (VM::isHotspot()) {
30-
VMThread::initialize(current_thread);
31-
}
32-
3327
return true;
3428
}
3529

30+
int JVMThread::native_thread_id(JNIEnv* jni, jthread thread) {
31+
return VM::isOpenJ9() ? J9Ext::GetOSThreadID(thread) : VMThread::nativeThreadId(jni, thread);
32+
}
33+
3634
void* JVMThread::current_thread_slow() {
3735
jthread thread;
3836
if (VM::jvmti()->GetCurrentThread(&thread) != JVMTI_ERROR_NONE) {
@@ -46,16 +44,9 @@ void* JVMThread::current_thread_slow() {
4644
return nullptr;
4745
}
4846

49-
// Get eetop field - a bridge from Java Thread to VMThread
50-
if ((_eetop = env->GetFieldID(thread_class, "eetop", "J")) == NULL) {
51-
// No such field - probably not a HotSpot JVM
52-
env->ExceptionClear();
53-
}
54-
5547
if (VM::isOpenJ9()) {
5648
return J9Ext::j9thread_self();
5749
} else {
58-
assert(_eetop != nullptr);
59-
return (void*)env->GetLongField(thread, _eetop);
50+
return VMThread::initialize(thread);
6051
}
6152
}

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,7 @@ class JVMThread {
4141
return _thread_key;
4242
}
4343

44-
static inline int native_thread_id(JNIEnv* jni, jthread thread) {
45-
return VM::isOpenJ9() ? J9Ext::GetOSThreadID(thread) : VMThread::nativeThreadId(jni, thread);
46-
}
47-
48-
static void* fromJavaThread(JNIEnv* env, jthread thread) {
49-
if (_eetop != nullptr) {
50-
return (void*)env->GetLongField(thread, _eetop);
51-
} else {
52-
return nullptr;
53-
}
54-
}
44+
static int native_thread_id(JNIEnv* jni, jthread thread);
5545

5646
static inline jlong javaThreadId(JNIEnv* env, jthread thread) {
5747
return env->GetLongField(thread, _tid);

ddprof-test/src/test/java/com/datadoghq/profiler/queue/QueueTimeTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ private Task(JavaProfiler profiler) {
4444
public void run() {
4545
profiler.setContext(1, 2);
4646
long now = profiler.getCurrentTicks();
47-
if (profiler.isThresholdExceeded(9, start, now)) {
47+
if (profiler.isThresholdExceeded(1, start, now)) {
48+
System.out.println("Emitting QueueTime event");
4849
profiler.recordQueueTime(start, now, getClass(), QueueTimeTest.class, ArrayBlockingQueue.class, 10, origin);
4950
}
5051
profiler.clearContext();

0 commit comments

Comments
 (0)