Merge pull request #237 from camelid/shutdown

Shut down profiler thread when no more threads are being sampled
This commit is contained in:
Emery Berger 2024-07-07 16:18:34 -04:00 committed by GitHub
commit e0c48e801d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 3 deletions

View File

@ -81,7 +81,7 @@ void profiler::startup(const string& outfile,
// Begin sampling in the main thread
thread_state* state = add_thread();
REQUIRE(state) << "Failed to add thread state";
REQUIRE(state) << "Failed to add thread";
begin_sampling(state);
}
@ -300,7 +300,11 @@ void profiler::shutdown() {
}
thread_state* profiler::add_thread() {
return _thread_states.insert(gettid());
thread_state* inserted = _thread_states.insert(gettid());
if (inserted != nullptr) {
_num_threads_running += 1;
}
return inserted;
}
thread_state* profiler::get_thread_state() {
@ -309,6 +313,7 @@ thread_state* profiler::get_thread_state() {
void profiler::remove_thread() {
_thread_states.remove(gettid());
_num_threads_running -= 1;
}
/**
@ -318,7 +323,7 @@ void* profiler::start_thread(void* p) {
thread_start_arg* arg = reinterpret_cast<thread_start_arg*>(p);
thread_state* state = get_instance().add_thread();
REQUIRE(state) << "Failed to add thread state";
REQUIRE(state) << "Failed to add thread";
state->local_delay = arg->_parent_delay_time;

View File

@ -132,6 +132,10 @@ public:
/// Force threads to catch up on delays, and stop sampling before the thread exits
void handle_pthread_exit(void* result) __attribute__((noreturn)) {
end_sampling();
// If no more threads being sampled, shut down the profiler
if (_num_threads_running == 0) {
shutdown();
}
real::pthread_exit(result);
abort(); // Silence g++ warning about noreturn
}
@ -223,6 +227,7 @@ private:
spinlock _latency_points_lock; //< Spinlock that protects the latency points map
static_map<pid_t, thread_state> _thread_states; //< Map from thread IDs to thread-local state
std::atomic<size_t> _num_threads_running; //< Number of threads that are currently being sampled
std::atomic<bool> _experiment_active; //< Is an experiment running?
std::atomic<size_t> _global_delay; //< The global delay time required