Skip to content

Commit

Permalink
all tasks profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
d-netto committed Sep 24, 2024
1 parent 037dc51 commit 7cb8d0e
Show file tree
Hide file tree
Showing 13 changed files with 377 additions and 214 deletions.
1 change: 0 additions & 1 deletion src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3927,7 +3927,6 @@ void jl_gc_init(void)
JL_MUTEX_INIT(&heapsnapshot_lock, "heapsnapshot_lock");
JL_MUTEX_INIT(&finalizers_lock, "finalizers_lock");
uv_mutex_init(&page_profile_lock);
uv_mutex_init(&gc_perm_lock);
uv_mutex_init(&gc_threads_lock);
uv_cond_init(&gc_threads_cond);
uv_sem_init(&gc_sweep_assists_needed, 0);
Expand Down
17 changes: 10 additions & 7 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,8 @@ static void init_global_mutexes(void) {
JL_MUTEX_INIT(&typecache_lock, "typecache_lock");
}

extern uv_mutex_t array_to_string_print_lock;

JL_DLLEXPORT void julia_init(JL_IMAGE_SEARCH rel)
{
// initialize many things, in no particular order
Expand All @@ -747,6 +749,10 @@ JL_DLLEXPORT void julia_init(JL_IMAGE_SEARCH rel)
// Make sure we finalize the tls callback before starting any threads.
(void)jl_get_pgcstack();

// Initialize a few locks...
uv_mutex_init(&gc_perm_lock);
uv_mutex_init(&array_to_string_print_lock);

// initialize backtraces
jl_init_profile_lock();
#ifdef _OS_WINDOWS_
Expand All @@ -773,6 +779,7 @@ JL_DLLEXPORT void julia_init(JL_IMAGE_SEARCH rel)
jl_io_loop = uv_default_loop(); // this loop will internal events (spawning process etc.),
// best to call this first, since it also initializes libuv
jl_init_uv();
jl_init_threading();
init_stdio();
restore_fp_env();
if (jl_options.handle_signals == JL_OPTIONS_HANDLE_SIGNALS_ON)
Expand Down Expand Up @@ -818,7 +825,6 @@ JL_DLLEXPORT void julia_init(JL_IMAGE_SEARCH rel)
jl_init_rand();
jl_init_runtime_ccall();
jl_init_tasks();
jl_init_threading();
jl_init_threadinginfra();
if (jl_options.handle_signals == JL_OPTIONS_HANDLE_SIGNALS_ON)
jl_install_default_signal_handlers();
Expand Down Expand Up @@ -855,8 +861,6 @@ JL_DLLEXPORT void julia_init(JL_IMAGE_SEARCH rel)

void jl_init_heartbeat(void);

extern uv_mutex_t array_to_string_print_lock;

static NOINLINE void _finish_julia_init(JL_IMAGE_SEARCH rel, jl_ptls_t ptls, jl_task_t *ct)
{
JL_TIMING(JULIA_INIT, JULIA_INIT);
Expand Down Expand Up @@ -892,8 +896,9 @@ static NOINLINE void _finish_julia_init(JL_IMAGE_SEARCH rel, jl_ptls_t ptls, jl_
}

if (jl_base_module == NULL) {
// nthreads > 1 requires code in Base
jl_atomic_store_relaxed(&jl_n_threads, 1);
const int num_min_mutator_threads = 1; // main thread
// nthreads > num_min_mutator_threads requires code in Base
jl_atomic_store_relaxed(&jl_n_threads, num_min_mutator_threads);
jl_n_markthreads = 0;
jl_n_sweepthreads = 0;
jl_n_gcthreads = 0;
Expand All @@ -904,8 +909,6 @@ static NOINLINE void _finish_julia_init(JL_IMAGE_SEARCH rel, jl_ptls_t ptls, jl_
jl_start_gc_threads();
uv_barrier_wait(&thread_init_done);

uv_mutex_init(&array_to_string_print_lock);

jl_init_heartbeat();

jl_gc_enable(1);
Expand Down
8 changes: 8 additions & 0 deletions src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ JL_DLLEXPORT void jl_unlock_profile_wr(void) JL_NOTSAFEPOINT JL_NOTSAFEPOINT_LEA
int jl_lock_stackwalk(void) JL_NOTSAFEPOINT JL_NOTSAFEPOINT_ENTER;
void jl_unlock_stackwalk(int lockret) JL_NOTSAFEPOINT JL_NOTSAFEPOINT_LEAVE;

jl_task_t *jl_get_random_task(void) JL_NOTSAFEPOINT;
void jl_rec_backtrace(jl_task_t *t) JL_NOTSAFEPOINT;
extern volatile struct _jl_bt_element_t *bt_data_prof;
extern volatile size_t bt_size_max;
extern volatile size_t bt_size_cur;
extern volatile int running;
extern volatile int profile_all_tasks;

// number of cycles since power-on
static inline uint64_t cycleclock(void) JL_NOTSAFEPOINT
{
Expand Down
62 changes: 22 additions & 40 deletions src/partr.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ JL_DLLEXPORT uint32_t jl_rand_ptls(uint32_t max, uint32_t unbias)
return cong(max, -(uint64_t)-unbias, &ptls->rngseed);
}

jl_ptls_t jl_threadfun_preamble(void *arg, uint8_t state)
{
jl_threadarg_t *targ = (jl_threadarg_t*)arg;
// initialize this thread (set tid and create heap)
jl_ptls_t ptls = jl_init_threadtls(targ->tid);
void *stack_lo, *stack_hi;
jl_init_stack_limits(0, &stack_lo, &stack_hi);
// warning: this changes `jl_current_task`, so be careful not to call that from this function
jl_task_t *ct = jl_init_root_task(ptls, stack_lo, stack_hi);
JL_GC_PROMISE_ROOTED(ct);
// wait for all threads
jl_gc_state_set(ptls, state, 0);
uv_barrier_wait(targ->barrier);
free(targ);
return ptls;
}

// initialize the threading infrastructure
// (called only by the main thread)
void jl_init_threadinginfra(void)
Expand Down Expand Up @@ -123,19 +140,7 @@ void jl_parallel_gc_threadfun(void *arg)
{
jl_threadarg_t *targ = (jl_threadarg_t*)arg;

// initialize this thread (set tid and create heap)
jl_ptls_t ptls = jl_init_threadtls(targ->tid);
void *stack_lo, *stack_hi;
jl_init_stack_limits(0, &stack_lo, &stack_hi);
// warning: this changes `jl_current_task`, so be careful not to call that from this function
jl_task_t *ct = jl_init_root_task(ptls, stack_lo, stack_hi);
JL_GC_PROMISE_ROOTED(ct);
// wait for all threads
jl_gc_state_set(ptls, JL_GC_PARALLEL_COLLECTOR_THREAD, 0);
uv_barrier_wait(targ->barrier);

// free the thread argument here
free(targ);
jl_ptls_t ptls = jl_threadfun_preamble(targ, JL_GC_PARALLEL_COLLECTOR_THREAD);

while (1) {
uv_mutex_lock(&gc_threads_lock);
Expand All @@ -158,19 +163,8 @@ void jl_concurrent_gc_threadfun(void *arg)
{
jl_threadarg_t *targ = (jl_threadarg_t*)arg;

// initialize this thread (set tid and create heap)
jl_ptls_t ptls = jl_init_threadtls(targ->tid);
void *stack_lo, *stack_hi;
jl_init_stack_limits(0, &stack_lo, &stack_hi);
// warning: this changes `jl_current_task`, so be careful not to call that from this function
jl_task_t *ct = jl_init_root_task(ptls, stack_lo, stack_hi);
JL_GC_PROMISE_ROOTED(ct);
// wait for all threads
jl_gc_state_set(ptls, JL_GC_CONCURRENT_COLLECTOR_THREAD, 0);
uv_barrier_wait(targ->barrier);

// free the thread argument here
free(targ);
jl_ptls_t ptls = jl_threadfun_preamble(targ, JL_GC_CONCURRENT_COLLECTOR_THREAD);
(void)ptls;

while (1) {
assert(jl_atomic_load_relaxed(&ptls->gc_state) == JL_GC_CONCURRENT_COLLECTOR_THREAD);
Expand All @@ -184,20 +178,8 @@ void jl_threadfun(void *arg)
{
jl_threadarg_t *targ = (jl_threadarg_t*)arg;

// initialize this thread (set tid, create heap, set up root task)
jl_ptls_t ptls = jl_init_threadtls(targ->tid);
void *stack_lo, *stack_hi;
jl_init_stack_limits(0, &stack_lo, &stack_hi);
// warning: this changes `jl_current_task`, so be careful not to call that from this function
jl_task_t *ct = jl_init_root_task(ptls, stack_lo, stack_hi);
JL_GC_PROMISE_ROOTED(ct);

// wait for all threads
jl_gc_state_set(ptls, JL_GC_STATE_SAFE, 0);
uv_barrier_wait(targ->barrier);

// free the thread argument here
free(targ);
jl_ptls_t ptls = jl_threadfun_preamble(targ, JL_GC_STATE_SAFE);
jl_task_t *ct = jl_current_task;

(void)jl_gc_unsafe_enter(ptls);
jl_finish_task(ct); // noreturn
Expand Down
14 changes: 7 additions & 7 deletions src/signal-handling.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ extern "C" {
#include <threading.h>

// Profiler control variables
// Note: these "static" variables are also used in "signals-*.c"
static volatile jl_bt_element_t *bt_data_prof = NULL;
static volatile size_t bt_size_max = 0;
static volatile size_t bt_size_cur = 0;
volatile jl_bt_element_t *bt_data_prof = NULL;
volatile size_t bt_size_max = 0;
volatile size_t bt_size_cur = 0;
static volatile uint64_t nsecprof = 0;
static volatile int running = 0;
static const uint64_t GIGA = 1000000000ULL;
volatile int running = 0;
volatile int profile_all_tasks = 0;
static const uint64_t GIGA = 1000000000ULL;
// Timers to take samples at intervals
JL_DLLEXPORT void jl_profile_stop_timer(void);
JL_DLLEXPORT int jl_profile_start_timer(void);
JL_DLLEXPORT int jl_profile_start_timer(uint8_t);
// File-descriptor for safe logging on signal handling
int jl_sig_fd;

Expand Down
Loading

0 comments on commit 7cb8d0e

Please sign in to comment.