Skip to content

Commit d48fd77

Browse files
committed
all tasks profiler
1 parent 8654a8a commit d48fd77

File tree

5 files changed

+25
-28
lines changed

5 files changed

+25
-28
lines changed

src/julia_internal.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,17 @@ JL_DLLEXPORT void jl_unlock_profile_wr(void) JL_NOTSAFEPOINT JL_NOTSAFEPOINT_LEA
206206
int jl_lock_stackwalk(void) JL_NOTSAFEPOINT JL_NOTSAFEPOINT_ENTER;
207207
void jl_unlock_stackwalk(int lockret) JL_NOTSAFEPOINT JL_NOTSAFEPOINT_LEAVE;
208208

209-
arraylist_t jl_get_all_tasks_arraylist(void) JL_NOTSAFEPOINT;
209+
arraylist_t *jl_get_all_tasks_arraylist(void) JL_NOTSAFEPOINT;
210210
int jl_rec_backtrace(jl_task_t *t) JL_NOTSAFEPOINT;
211211
extern volatile struct _jl_bt_element_t *profile_bt_data_prof;
212212
extern volatile size_t profile_bt_size_max;
213213
extern volatile size_t profile_bt_size_cur;
214214
extern volatile int profile_running;
215215
extern volatile int profile_all_tasks;
216+
STATIC_INLINE int all_tasks_profile_running(void) JL_NOTSAFEPOINT
217+
{
218+
return profile_running && profile_all_tasks;
219+
}
216220

217221
// number of cycles since power-on
218222
static inline uint64_t cycleclock(void) JL_NOTSAFEPOINT

src/signals-unix.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -724,11 +724,11 @@ void jl_profile_task_unix(void)
724724
// Get a random task for which we know that we won't
725725
// drop a sample in the profiler
726726
// TODO(Diogo, Nick): should we put an upper bound on the number of iterations?
727-
arraylist_t tasks = jl_get_all_tasks_arraylist();
727+
arraylist_t *tasks = jl_get_all_tasks_arraylist();
728728
jl_task_t *t = NULL;
729729
uint64_t seed = jl_rand();
730730
while (1) {
731-
t = tasks.items[cong(tasks.len, UINT64_MAX, &seed)];
731+
t = tasks->items[cong(tasks->len, UINT64_MAX, &seed)];
732732
assert(t == NULL || jl_is_task(t));
733733
if (t == NULL) {
734734
continue;
@@ -739,7 +739,8 @@ void jl_profile_task_unix(void)
739739
}
740740
break;
741741
}
742-
arraylist_free(&tasks);
742+
arraylist_free(tasks);
743+
free(tasks);
743744
assert(t != NULL);
744745

745746
int tid = jl_rec_backtrace(t);

src/signals-win.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ static DWORD WINAPI profile_bt( LPVOID lparam )
398398
while (1) {
399399
DWORD timeout_ms = nsecprof / (GIGA / 1000);
400400
Sleep(timeout_ms > 0 ? timeout_ms : 1);
401-
if (running) {
401+
if (profile_running) {
402402
if (jl_profile_is_buffer_full()) {
403403
jl_profile_stop_timer(); // does not change the thread state
404404
SuspendThread(GetCurrentThread());
@@ -420,16 +420,16 @@ static DWORD WINAPI profile_bt( LPVOID lparam )
420420

421421
jl_ptls_t ptls = jl_atomic_load_relaxed(&jl_all_tls_states)[0]; // given only profiling hMainThread
422422

423-
// store threadid but add 1 as 0 is preserved to indicate end of block
423+
// META_OFFSET_THREADID store threadid but add 1 as 0 is preserved to indicate end of block
424424
profile_bt_data_prof[profile_bt_size_cur++].uintptr = ptls->tid + 1;
425425

426-
// store task id (never null)
426+
// META_OFFSET_TASKID store task id (never null)
427427
profile_bt_data_prof[profile_bt_size_cur++].jlvalue = (jl_value_t*)jl_atomic_load_relaxed(&ptls->current_task);
428428

429-
// store cpu cycle clock
429+
// META_OFFSET_CPUCYCLECLOCK store cpu cycle clock
430430
profile_bt_data_prof[profile_bt_size_cur++].uintptr = cycleclock();
431431

432-
// store whether thread is sleeping but add 1 as 0 is preserved to indicate end of block
432+
// META_OFFSET_SLEEPSTATE store whether thread is sleeping but add 1 as 0 is preserved to indicate end of block
433433
profile_bt_data_prof[profile_bt_size_cur++].uintptr = jl_atomic_load_relaxed(&ptls->sleep_check_state) + 1;
434434

435435
// Mark the end of this block with two 0's
@@ -477,21 +477,21 @@ JL_DLLEXPORT int jl_profile_start_timer(uint8_t all_tasks)
477477
return -2;
478478
}
479479
}
480-
if (running == 0) {
480+
if (profile_running == 0) {
481481
// Failure to change the timer resolution is not fatal. However, it is important to
482482
// ensure that the timeBeginPeriod/timeEndPeriod is paired.
483483
if (TIMERR_NOERROR != timeBeginPeriod(timecaps.wPeriodMin))
484484
timecaps.wPeriodMin = 0;
485485
}
486486
profile_all_tasks = all_tasks;
487-
running = 1; // set `running` finally
487+
profile_running = 1; // set `profile_running` finally
488488
return 0;
489489
}
490490
JL_DLLEXPORT void jl_profile_stop_timer(void)
491491
{
492-
if (running && timecaps.wPeriodMin)
492+
if (profile_running && timecaps.wPeriodMin)
493493
timeEndPeriod(timecaps.wPeriodMin);
494-
running = 0;
494+
profile_running = 0;
495495
profile_all_tasks = 0;
496496
}
497497

src/stackwalk.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -867,20 +867,14 @@ _os_ptr_munge(uintptr_t ptr)
867867
#define _OS_PTR_UNMUNGE(_ptr) _os_ptr_munge((uintptr_t)(_ptr))
868868
#endif
869869

870-
871-
STATIC_INLINE int all_tasks_profile_running(void)
872-
{
873-
return profile_running && profile_all_tasks;
874-
}
875-
876870
int jl_rec_backtrace(jl_task_t *t) JL_NOTSAFEPOINT
877871
{
878872
jl_task_t *ct = NULL;
879873
jl_ptls_t ptls = NULL;
880874
int16_t tid = INT16_MAX;
881875
if (!all_tasks_profile_running()) {
882-
jl_task_t *ct = jl_current_task;
883-
jl_ptls_t ptls = ct->ptls;
876+
ct = jl_current_task;
877+
ptls = ct->ptls;
884878
ptls->bt_size = 0;
885879
tid = ptls->tid;
886880
}

src/task.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,12 +1116,11 @@ JL_DLLEXPORT jl_task_t *jl_get_current_task(void)
11161116
}
11171117

11181118
extern int gc_first_tid;
1119-
11201119
// Select a task at random to profile. Racy: `live_tasks` can change at any time.
1121-
arraylist_t jl_get_all_tasks_arraylist(void) JL_NOTSAFEPOINT
1120+
arraylist_t *jl_get_all_tasks_arraylist(void) JL_NOTSAFEPOINT
11221121
{
1123-
arraylist_t tasks;
1124-
arraylist_new(&tasks, 0);
1122+
arraylist_t *tasks = (arraylist_t*)malloc_s(sizeof(arraylist_t));
1123+
arraylist_new(tasks, 0);
11251124
size_t nthreads = jl_atomic_load_acquire(&jl_n_threads);
11261125
jl_ptls_t *allstates = jl_atomic_load_relaxed(&jl_all_tls_states);
11271126
for (size_t i = 0; i < nthreads; i++) {
@@ -1135,21 +1134,20 @@ arraylist_t jl_get_all_tasks_arraylist(void) JL_NOTSAFEPOINT
11351134
}
11361135
jl_task_t *t = ptls2->root_task;
11371136
if (t->stkbuf != NULL) {
1138-
arraylist_push(&tasks, t);
1137+
arraylist_push(tasks, t);
11391138
}
11401139
small_arraylist_t *live_tasks = &ptls2->gc_tls.heap.live_tasks;
11411140
size_t n = mtarraylist_length(live_tasks);
11421141
for (size_t i = 0; i < n; i++) {
11431142
jl_task_t *t = (jl_task_t*)mtarraylist_get(live_tasks, i);
11441143
if (t->stkbuf != NULL) {
1145-
arraylist_push(&tasks, t);
1144+
arraylist_push(tasks, t);
11461145
}
11471146
}
11481147
}
11491148
return tasks;
11501149
}
11511150

1152-
11531151
#ifdef JL_HAVE_ASYNCIFY
11541152
JL_DLLEXPORT jl_ucontext_t *task_ctx_ptr(jl_task_t *t)
11551153
{

0 commit comments

Comments
 (0)