Skip to content

Commit

Permalink
skip heartbeat thread from CPU profile (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-netto committed Jan 6, 2025
1 parent 9b68b76 commit 4dc0745
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ extern volatile size_t profile_bt_size_max;
extern volatile size_t profile_bt_size_cur;
extern volatile int profile_running;
extern volatile int profile_all_tasks;
extern int heartbeat_tid; // Mostly used to ensure we skip this thread in the CPU profiler. XXX: not implemented on Windows
// Ensures that we can safely read the `live_tasks`field of every TLS when profiling.
// We want to avoid the case that a GC gets interleaved with `jl_profile_task` and shrinks
// the `live_tasks` array while we are reading it or frees tasks that are being profiled.
Expand Down
4 changes: 4 additions & 0 deletions src/signals-mach.c
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,10 @@ void *mach_profile_listener(void *arg)
for (int idx = nthreads; idx-- > 0; ) {
// Stop the threads in random order.
int i = randperm[idx];
// skip heartbeat thread
if (i == heartbeat_tid) {
continue;
}
jl_profile_thread_mach(i);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/signals-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,10 @@ static void *signal_listener(void *arg)
for (int idx = nthreads; idx-- > 0; ) {
// Stop the threads in the random order.
int i = randperm[idx];
// skip heartbeat thread
if (i == heartbeat_tid) {
continue;
}
// do backtrace for profiler
if (profile_running) {
jl_profile_thread_unix(i, &signal_context);
Expand Down
2 changes: 2 additions & 0 deletions src/threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,7 @@ JL_DLLEXPORT int jl_setaffinity(int16_t tid, char *mask, int cpumasksize) {
#include <time.h>

volatile int heartbeat_enabled;
int heartbeat_tid; // Mostly used to ensure we skip this thread in the CPU profiler. XXX: not implemented on Windows
uv_thread_t heartbeat_uvtid;
uv_sem_t heartbeat_on_sem, // jl_heartbeat_enable -> thread
heartbeat_off_sem; // thread -> jl_heartbeat_enable
Expand Down Expand Up @@ -1272,6 +1273,7 @@ void jl_heartbeat_threadfun(void *arg)
jl_adopt_thread();
jl_task_t *ct = jl_current_task;
jl_ptls_t ptls = ct->ptls;
heartbeat_tid = ptls->tid;

// Don't hold up GC, this thread doesn't participate.
uint8_t gc_state = jl_gc_safe_enter(ptls);
Expand Down

0 comments on commit 4dc0745

Please sign in to comment.