Skip to content

Commit

Permalink
Save the thread ID in SamplingProfiler
Browse files Browse the repository at this point in the history
Summary:
The sampling profiler currently records the thread ID during stack
walking. This works fine on POSIX platforms because the signal handler
actually runs on the runtime thread, but causes problems on Windows,
where it occurs on the timer thread.

To fix this, record the thread ID when the runtime is registered for a
particular thread, and just use that when walking the stack.

Reviewed By: rozele

Differential Revision: D69664487

fbshipit-source-id: d6d1d46729709505a563efbd498f9bc253ba2ef8
  • Loading branch information
neildhar authored and facebook-github-bot committed Feb 18, 2025
1 parent 945c9f8 commit bb18a56
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
3 changes: 3 additions & 0 deletions include/hermes/VM/Profiler/SamplingProfiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ class SamplingProfiler {
/// Prellocated map that contains thread names mapping.
ThreadNamesMap threadNames_;

/// Thread ID of the currently registered thread.
ThreadId threadID_;

/// Unique GC event extra info strings container.
std::unordered_set<std::string> gcEventExtraInfoSet_;

Expand Down
7 changes: 4 additions & 3 deletions lib/VM/Profiler/SamplingProfiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,14 @@ uint32_t SamplingProfiler::walkRuntimeStack(
}
}
}
sampleStorage.tid = oscompat::global_thread_id();
sampleStorage.tid = threadID_;
sampleStorage.timeStamp = std::chrono::steady_clock::now();
return count;
}

SamplingProfiler::SamplingProfiler(Runtime &runtime) : runtime_{runtime} {
threadNames_[oscompat::global_thread_id()] = oscompat::thread_name();
SamplingProfiler::SamplingProfiler(Runtime &runtime)
: threadID_{oscompat::global_thread_id()}, runtime_{runtime} {
threadNames_[threadID_] = oscompat::thread_name();
sampling_profiler::Sampler::get()->registerRuntime(this);
}

Expand Down
1 change: 1 addition & 0 deletions lib/VM/Profiler/SamplingProfilerPosix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ void SamplingProfiler::setRuntimeThread() {
auto profiler = static_cast<sampling_profiler::SamplingProfilerPosix *>(this);
std::lock_guard<std::mutex> lock(profiler->runtimeDataLock_);
profiler->currentThread_ = pthread_self();
threadID_ = oscompat::global_thread_id();
}

} // namespace vm
Expand Down
1 change: 1 addition & 0 deletions lib/VM/Profiler/SamplingProfilerWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ void SamplingProfiler::setRuntimeThread() {
std::lock_guard<std::mutex> lock(profiler->runtimeDataLock_);
CloseHandle(profiler->currentThread_);
profiler->currentThread_ = openCurrentThread();
threadID_ = oscompat::global_thread_id();
}

} // namespace vm
Expand Down

0 comments on commit bb18a56

Please sign in to comment.