@@ -160,6 +160,9 @@ void WallClockASGCT::initialize(Arguments& args) {
160160 * We have to be extremely careful when accessing thread's data, so it may not be valid.
161161 */
162162void WallClockJVMTI::timerLoop () {
163+
164+ TEST_LOG (" VMTI::timerLoop()" );
165+
163166 // Check for enablement before attaching/dettaching the current thread
164167 if (!isEnabled ()) {
165168 return ;
@@ -170,6 +173,7 @@ void WallClockJVMTI::timerLoop() {
170173 return ;
171174 }
172175
176+
173177 // Notice:
174178 // We want to cache threads that are captured by collectThread(), so that we can
175179 // clean them up in cleanThreadRefs().
@@ -180,6 +184,21 @@ void WallClockJVMTI::timerLoop() {
180184
181185 // Attach to JVM as the first step
182186 VM::attachThread (" Datadog Profiler Wallclock Sampler" );
187+
188+ jthread self;
189+ jvmtiError error;
190+
191+ if ((error = jvmti->GetCurrentThread (&self)) != JVMTI_ERROR_NONE ) {
192+ TEST_LOG (" GetCurrentThread failed, %d" , (int )error);
193+ return ;
194+ }
195+
196+ JNIEnv* jni = VM::jni ();
197+ jclass thread_class = jni->FindClass (" java/lang/Thread" );
198+ assert (thread_class != nullptr );
199+ jmethodID thread_getId = jni->GetMethodID (thread_class, " getId" , " ()J" );
200+ assert (thread_getId != nullptr );
201+
183202 auto collectThreads = [&](std::vector<ThreadEntry>& threads) {
184203 jvmtiEnv* jvmti = VM::jvmti ();
185204 if (jvmti == nullptr ) {
@@ -191,11 +210,8 @@ void WallClockJVMTI::timerLoop() {
191210 return ;
192211 }
193212
194- JNIEnv* jni = VM::jni ();
195-
196213 ThreadFilter* threadFilter = Profiler::instance ()->threadFilter ();
197214 bool do_filter = threadFilter->enabled ();
198- int self = OS::threadId ();
199215
200216 // If filtering is enabled, collect the filtered TIDs first
201217 std::vector<int > filtered_tids;
@@ -207,46 +223,30 @@ void WallClockJVMTI::timerLoop() {
207223
208224 for (int i = 0 ; i < threads_count; i++) {
209225 jthread thread = threads_ptr[i];
210- if (thread != nullptr ) {
211- VMThread* nThread = VMThread::fromJavaThread (jni, thread);
212- if (nThread == nullptr ) {
213- continue ;
214- }
215- int tid = nThread->osThreadId ();
216- if (tid != self && (!do_filter ||
226+ if (thread == self) continue ;
227+ long tid = jni->CallLongMethod (thread, thread_getId);
228+
229+ if (!do_filter ||
217230 // Use binary search to efficiently find if tid is in filtered_tids
218- std::binary_search (filtered_tids.begin (), filtered_tids.end (), tid))) {
219- threads.push_back ({nThread, thread, tid});
220- }
231+ std::binary_search (filtered_tids.begin (), filtered_tids.end (), tid)) {
232+ threads.push_back ({thread, tid});
221233 }
222234 }
223235 };
224236
237+ ThreadStateResolver* const resolver = ThreadStateResolver::getInstance ();
238+
225239 auto sampleThreads = [&](ThreadEntry& thread_entry, int & num_failures, int & threads_already_exited, int & permission_denied) {
226240 static jint max_stack_depth = (jint)Profiler::instance ()->max_stack_depth ();
227241
228242 ExecutionEvent event;
229- VMThread* vm_thread = thread_entry.native ;
230- int raw_thread_state = vm_thread->state ();
231- bool is_initialized = raw_thread_state >= JVMJavaThreadState::_thread_in_native &&
232- raw_thread_state < JVMJavaThreadState::_thread_max_state;
233- OSThreadState state = OSThreadState::UNKNOWN ;
234- ExecutionMode mode = ExecutionMode::UNKNOWN ;
235- if (vm_thread == nullptr || !is_initialized) {
243+ jthread thread = thread_entry.java ;
244+
245+ event._thread_state = resolver->resolveThreadState (thread);
246+ if (event._thread_state == OSThreadState::TERMINATED ) {
236247 return false ;
237248 }
238- OSThreadState os_state = vm_thread->osThreadState ();
239- if (os_state == OSThreadState::TERMINATED ) {
240- return false ;
241- } else if (os_state == OSThreadState::UNKNOWN ) {
242- state = OSThreadState::RUNNABLE ;
243- } else {
244- state = os_state;
245- }
246- mode = getThreadExecutionMode ();
247-
248- event._thread_state = state;
249- event._execution_mode = mode;
249+ event._execution_mode = resolver->resolveThreadExecutionMode (thread);
250250 event._weight = 1 ;
251251
252252 Profiler::instance ()->recordJVMTISample (1 , thread_entry.tid , thread_entry.java , BCI_WALL , &event, false );
0 commit comments