Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shut down profiler thread when no more threads are being sampled #228

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions libcoz/profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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() {
Expand All @@ -309,6 +313,7 @@ thread_state* profiler::get_thread_state() {

void profiler::remove_thread() {
_thread_states.remove(gettid());
_num_threads_running -= 1;
}

/**
Expand All @@ -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;

Expand Down
5 changes: 5 additions & 0 deletions libcoz/profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ class profiler {
/// 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
}
Expand Down Expand Up @@ -223,6 +227,7 @@ class profiler {
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
uint64_t _num_threads_running; //< Number of threads that are currently being sampled
Copy link
Contributor

@camelid camelid Jun 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be atomic since it's shared across threads. uint64_t is also inconsistent with the other integer sizes used here.

Suggested change
uint64_t _num_threads_running; //< Number of threads that are currently being sampled
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
Expand Down