Skip to content

Commit 4f7feb1

Browse files
vtjnashKristofferC
authored andcommitted
add support for async backtraces of Tasks on any thread (#51430)
(cherry picked from commit e5c6340)
1 parent 4aeef21 commit 4f7feb1

File tree

13 files changed

+531
-327
lines changed

13 files changed

+531
-327
lines changed

src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ endif
4343
SRCS := \
4444
jltypes gf typemap smallintset ast builtins module interpreter symbol \
4545
dlload sys init task array staticdata toplevel jl_uv datatype \
46-
simplevector runtime_intrinsics precompile jloptions \
46+
simplevector runtime_intrinsics precompile jloptions mtarraylist \
4747
threading partr stackwalk gc gc-debug gc-pages gc-stacks gc-alloc-profiler method \
4848
jlapi signal-handling safepoint timing subtype rtutils gc-heap-snapshot \
4949
crc32c APInt-C processor ircode opaque_closure codegen-stubs coverage runtime_ccall

src/gc-stacks.c

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static void _jl_free_stack(jl_ptls_t ptls, void *stkbuf, size_t bufsz)
112112
if (bufsz <= pool_sizes[JL_N_STACK_POOLS - 1]) {
113113
unsigned pool_id = select_pool(bufsz);
114114
if (pool_sizes[pool_id] == bufsz) {
115-
arraylist_push(&ptls->heap.free_stacks[pool_id], stkbuf);
115+
small_arraylist_push(&ptls->heap.free_stacks[pool_id], stkbuf);
116116
return;
117117
}
118118
}
@@ -141,7 +141,7 @@ void jl_release_task_stack(jl_ptls_t ptls, jl_task_t *task)
141141
#ifdef _COMPILER_ASAN_ENABLED_
142142
__asan_unpoison_stack_memory((uintptr_t)stkbuf, bufsz);
143143
#endif
144-
arraylist_push(&ptls->heap.free_stacks[pool_id], stkbuf);
144+
small_arraylist_push(&ptls->heap.free_stacks[pool_id], stkbuf);
145145
}
146146
}
147147
}
@@ -156,9 +156,9 @@ JL_DLLEXPORT void *jl_malloc_stack(size_t *bufsz, jl_task_t *owner) JL_NOTSAFEPO
156156
if (ssize <= pool_sizes[JL_N_STACK_POOLS - 1]) {
157157
unsigned pool_id = select_pool(ssize);
158158
ssize = pool_sizes[pool_id];
159-
arraylist_t *pool = &ptls->heap.free_stacks[pool_id];
159+
small_arraylist_t *pool = &ptls->heap.free_stacks[pool_id];
160160
if (pool->len > 0) {
161-
stk = arraylist_pop(pool);
161+
stk = small_arraylist_pop(pool);
162162
}
163163
}
164164
else {
@@ -177,8 +177,8 @@ JL_DLLEXPORT void *jl_malloc_stack(size_t *bufsz, jl_task_t *owner) JL_NOTSAFEPO
177177
}
178178
*bufsz = ssize;
179179
if (owner) {
180-
arraylist_t *live_tasks = &ptls->heap.live_tasks;
181-
arraylist_push(live_tasks, owner);
180+
small_arraylist_t *live_tasks = &ptls->heap.live_tasks;
181+
mtarraylist_push(live_tasks, owner);
182182
}
183183
return stk;
184184
}
@@ -202,7 +202,7 @@ void sweep_stack_pools(void)
202202

203203
// free half of stacks that remain unused since last sweep
204204
for (int p = 0; p < JL_N_STACK_POOLS; p++) {
205-
arraylist_t *al = &ptls2->heap.free_stacks[p];
205+
small_arraylist_t *al = &ptls2->heap.free_stacks[p];
206206
size_t n_to_free;
207207
if (al->len > MIN_STACK_MAPPINGS_PER_POOL) {
208208
n_to_free = al->len / 2;
@@ -213,12 +213,12 @@ void sweep_stack_pools(void)
213213
n_to_free = 0;
214214
}
215215
for (int n = 0; n < n_to_free; n++) {
216-
void *stk = arraylist_pop(al);
216+
void *stk = small_arraylist_pop(al);
217217
free_stack(stk, pool_sizes[p]);
218218
}
219219
}
220220

221-
arraylist_t *live_tasks = &ptls2->heap.live_tasks;
221+
small_arraylist_t *live_tasks = &ptls2->heap.live_tasks;
222222
size_t n = 0;
223223
size_t ndel = 0;
224224
size_t l = live_tasks->len;
@@ -261,24 +261,52 @@ void sweep_stack_pools(void)
261261

262262
JL_DLLEXPORT jl_array_t *jl_live_tasks(void)
263263
{
264-
jl_task_t *ct = jl_current_task;
265-
jl_ptls_t ptls = ct->ptls;
266-
arraylist_t *live_tasks = &ptls->heap.live_tasks;
267-
size_t i, j, l;
268-
jl_array_t *a;
269-
do {
270-
l = live_tasks->len;
271-
a = jl_alloc_vec_any(l + 1); // may gc, changing the number of tasks
272-
} while (l + 1 < live_tasks->len);
273-
l = live_tasks->len;
274-
void **lst = live_tasks->items;
275-
j = 0;
276-
((void**)jl_array_data(a))[j++] = ptls->root_task;
277-
for (i = 0; i < l; i++) {
278-
if (((jl_task_t*)lst[i])->stkbuf != NULL)
279-
((void**)jl_array_data(a))[j++] = lst[i];
264+
size_t nthreads = jl_atomic_load_acquire(&jl_n_threads);
265+
jl_ptls_t *allstates = jl_atomic_load_relaxed(&jl_all_tls_states);
266+
size_t l = 0; // l is not reset on restart, so we keep getting more aggressive at making a big enough list everything it fails
267+
restart:
268+
for (size_t i = 0; i < nthreads; i++) {
269+
// skip GC threads since they don't have tasks
270+
if (gc_first_tid <= i && i < gc_first_tid + jl_n_gcthreads) {
271+
continue;
272+
}
273+
jl_ptls_t ptls2 = allstates[i];
274+
if (ptls2 == NULL)
275+
continue;
276+
small_arraylist_t *live_tasks = &ptls2->heap.live_tasks;
277+
size_t n = mtarraylist_length(live_tasks);
278+
l += n + (ptls2->root_task->stkbuf != NULL);
279+
}
280+
l += l / 20; // add 5% for margin of estimation error
281+
jl_array_t *a = jl_alloc_vec_any(l); // may gc, changing the number of tasks and forcing us to reload everything
282+
nthreads = jl_atomic_load_acquire(&jl_n_threads);
283+
allstates = jl_atomic_load_relaxed(&jl_all_tls_states);
284+
size_t j = 0;
285+
for (size_t i = 0; i < nthreads; i++) {
286+
// skip GC threads since they don't have tasks
287+
if (gc_first_tid <= i && i < gc_first_tid + jl_n_gcthreads) {
288+
continue;
289+
}
290+
jl_ptls_t ptls2 = allstates[i];
291+
if (ptls2 == NULL)
292+
continue;
293+
jl_task_t *t = ptls2->root_task;
294+
if (t->stkbuf != NULL) {
295+
if (j == l)
296+
goto restart;
297+
((void**)jl_array_data(a))[j++] = t;
298+
}
299+
small_arraylist_t *live_tasks = &ptls2->heap.live_tasks;
300+
size_t n = mtarraylist_length(live_tasks);
301+
for (size_t i = 0; i < n; i++) {
302+
jl_task_t *t = (jl_task_t*)mtarraylist_get(live_tasks, i);
303+
if (t->stkbuf != NULL) {
304+
if (j == l)
305+
goto restart;
306+
((void**)jl_array_data(a))[j++] = t;
307+
}
308+
}
280309
}
281-
l = jl_array_len(a);
282310
if (j < l) {
283311
JL_GC_PUSH1(&a);
284312
jl_array_del_end(a, l - j);

src/gc.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ JL_DLLEXPORT jl_weakref_t *jl_gc_new_weakref_th(jl_ptls_t ptls,
947947
jl_weakref_t *wr = (jl_weakref_t*)jl_gc_alloc(ptls, sizeof(void*),
948948
jl_weakref_type);
949949
wr->value = value; // NOTE: wb not needed here
950-
arraylist_push(&ptls->heap.weak_refs, wr);
950+
small_arraylist_push(&ptls->heap.weak_refs, wr);
951951
return wr;
952952
}
953953

@@ -3510,8 +3510,10 @@ void jl_init_thread_heap(jl_ptls_t ptls)
35103510
p[i].freelist = NULL;
35113511
p[i].newpages = NULL;
35123512
}
3513-
arraylist_new(&heap->weak_refs, 0);
3514-
arraylist_new(&heap->live_tasks, 0);
3513+
small_arraylist_new(&heap->weak_refs, 0);
3514+
small_arraylist_new(&heap->live_tasks, 0);
3515+
for (int i = 0; i < JL_N_STACK_POOLS; i++)
3516+
small_arraylist_new(&heap->free_stacks[i], 0);
35153517
heap->mallocarrays = NULL;
35163518
heap->mafreelist = NULL;
35173519
heap->big_objects = NULL;

src/interpreter.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ extern void JL_GC_ENABLEFRAME(interpreter_state*) JL_NOTSAFEPOINT;
6565
// we define this separately so that we can populate the frame before we add it to the backtrace
6666
// it's recommended to mark the containing function with NOINLINE, though not essential
6767
#define JL_GC_ENABLEFRAME(frame) \
68-
((void**)&frame[1])[0] = __builtin_frame_address(0);
68+
jl_signal_fence(); \
69+
((void**)&frame[1])[0] = __builtin_frame_address(0);
6970

7071
#endif
7172

src/julia.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,11 @@ JL_DLLEXPORT void *jl_gc_managed_realloc(void *d, size_t sz, size_t oldsz,
10501050
int isaligned, jl_value_t *owner);
10511051
JL_DLLEXPORT void jl_gc_safepoint(void);
10521052

1053+
void *mtarraylist_get(small_arraylist_t *_a, size_t idx) JL_NOTSAFEPOINT;
1054+
size_t mtarraylist_length(small_arraylist_t *_a) JL_NOTSAFEPOINT;
1055+
void mtarraylist_add(small_arraylist_t *_a, void *elt, size_t idx) JL_NOTSAFEPOINT;
1056+
void mtarraylist_push(small_arraylist_t *_a, void *elt) JL_NOTSAFEPOINT;
1057+
10531058
// object accessors -----------------------------------------------------------
10541059

10551060
#define jl_svec_len(t) (((jl_svec_t*)(t))->length)

src/julia_internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ JL_DLLEXPORT void jl_lock_profile(void) JL_NOTSAFEPOINT JL_NOTSAFEPOINT_ENTER;
203203
JL_DLLEXPORT void jl_unlock_profile(void) JL_NOTSAFEPOINT JL_NOTSAFEPOINT_LEAVE;
204204
JL_DLLEXPORT void jl_lock_profile_wr(void) JL_NOTSAFEPOINT JL_NOTSAFEPOINT_ENTER;
205205
JL_DLLEXPORT void jl_unlock_profile_wr(void) JL_NOTSAFEPOINT JL_NOTSAFEPOINT_LEAVE;
206+
int jl_lock_stackwalk(void) JL_NOTSAFEPOINT JL_NOTSAFEPOINT_ENTER;
207+
void jl_unlock_stackwalk(int lockret) JL_NOTSAFEPOINT JL_NOTSAFEPOINT_LEAVE;
206208

207209
// number of cycles since power-on
208210
static inline uint64_t cycleclock(void) JL_NOTSAFEPOINT
@@ -1162,6 +1164,9 @@ void jl_print_bt_entry_codeloc(jl_bt_element_t *bt_data) JL_NOTSAFEPOINT;
11621164
#ifdef _OS_WINDOWS_
11631165
JL_DLLEXPORT void jl_refresh_dbg_module_list(void);
11641166
#endif
1167+
int jl_thread_suspend_and_get_state(int tid, int timeout, bt_context_t *ctx) JL_NOTSAFEPOINT;
1168+
void jl_thread_resume(int tid) JL_NOTSAFEPOINT;
1169+
11651170
// *to is NULL or malloc'd pointer, from is allowed to be NULL
11661171
STATIC_INLINE char *jl_copy_str(char **to, const char *from) JL_NOTSAFEPOINT
11671172
{

src/julia_threads.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ typedef struct {
109109

110110
// handle to reference an OS thread
111111
#ifdef _OS_WINDOWS_
112-
typedef DWORD jl_thread_t;
112+
typedef HANDLE jl_thread_t;
113113
#else
114114
typedef pthread_t jl_thread_t;
115115
#endif
@@ -140,10 +140,10 @@ typedef struct {
140140

141141
typedef struct {
142142
// variable for tracking weak references
143-
arraylist_t weak_refs;
143+
small_arraylist_t weak_refs;
144144
// live tasks started on this thread
145145
// that are holding onto a stack from the pool
146-
arraylist_t live_tasks;
146+
small_arraylist_t live_tasks;
147147

148148
// variables for tracking malloc'd arrays
149149
struct _mallocarray_t *mallocarrays;
@@ -170,7 +170,7 @@ typedef struct {
170170
jl_gc_pool_t norm_pools[JL_GC_N_POOLS];
171171

172172
#define JL_N_STACK_POOLS 16
173-
arraylist_t free_stacks[JL_N_STACK_POOLS];
173+
small_arraylist_t free_stacks[JL_N_STACK_POOLS];
174174
} jl_thread_heap_t;
175175

176176
typedef struct {

src/mtarraylist.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// This file is a part of Julia. License is MIT: https://julialang.org/license
2+
3+
#include "julia.h"
4+
#include "julia_internal.h"
5+
#include "julia_assert.h"
6+
7+
#ifdef __cplusplus
8+
extern "C" {
9+
#endif
10+
11+
// this file provides some alternate API functions for small_arraylist (push and add)
12+
// which can be safely observed from other threads concurrently
13+
// there is only permitted to be a single writer thread (or a mutex)
14+
// but there can be any number of observers
15+
16+
typedef struct {
17+
_Atomic(uint32_t) len;
18+
uint32_t max;
19+
_Atomic(_Atomic(void*)*) items;
20+
_Atomic(void*) _space[SMALL_AL_N_INLINE];
21+
} small_mtarraylist_t;
22+
23+
// change capacity to at least newlen
24+
static void mtarraylist_resizeto(small_mtarraylist_t *a, size_t len, size_t newlen) JL_NOTSAFEPOINT
25+
{
26+
size_t max = a->max;
27+
if (newlen > max) {
28+
size_t nm = max * 2;
29+
if (nm == 0)
30+
nm = 1;
31+
while (newlen > nm)
32+
nm *= 2;
33+
void *olditems = (void*)jl_atomic_load_relaxed(&a->items);
34+
void *p = calloc_s(nm * sizeof(void*));
35+
memcpy(p, olditems, len * sizeof(void*));
36+
jl_atomic_store_release(&a->items, (_Atomic(void*)*)p);
37+
a->max = nm;
38+
if (olditems != (void*)&a->_space[0]) {
39+
jl_task_t *ct = jl_current_task;
40+
jl_gc_add_quiescent(ct->ptls, (void**)olditems, free);
41+
}
42+
}
43+
}
44+
45+
// single-threaded
46+
void mtarraylist_push(small_arraylist_t *_a, void *elt)
47+
{
48+
small_mtarraylist_t *a = (small_mtarraylist_t*)_a;
49+
size_t len = jl_atomic_load_relaxed(&a->len);
50+
mtarraylist_resizeto(a, len, len + 1);
51+
jl_atomic_store_release(&jl_atomic_load_relaxed(&a->items)[len], elt);
52+
jl_atomic_store_release(&a->len, len + 1);
53+
}
54+
55+
// single-threaded
56+
void mtarraylist_add(small_arraylist_t *_a, void *elt, size_t idx)
57+
{
58+
small_mtarraylist_t *a = (small_mtarraylist_t*)_a;
59+
size_t len = jl_atomic_load_relaxed(&a->len);
60+
mtarraylist_resizeto(a, len, idx + 1);
61+
jl_atomic_store_release(&jl_atomic_load_relaxed(&a->items)[idx], elt);
62+
if (jl_atomic_load_relaxed(&a->len) < idx + 1)
63+
jl_atomic_store_release(&a->len, idx + 1);
64+
}
65+
66+
// concurrent-safe
67+
size_t mtarraylist_length(small_arraylist_t *_a)
68+
{
69+
small_mtarraylist_t *a = (small_mtarraylist_t*)_a;
70+
return jl_atomic_load_relaxed(&a->len);
71+
}
72+
73+
// concurrent-safe
74+
void *mtarraylist_get(small_arraylist_t *_a, size_t idx)
75+
{
76+
small_mtarraylist_t *a = (small_mtarraylist_t*)_a;
77+
size_t len = jl_atomic_load_acquire(&a->len);
78+
if (idx >= len)
79+
return NULL;
80+
return jl_atomic_load_relaxed(&jl_atomic_load_relaxed(&a->items)[idx]);
81+
}

src/signals-mach.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,12 @@ static void attach_exception_port(thread_port_t thread, int segv_only)
384384
HANDLE_MACH_ERROR("thread_set_exception_ports", ret);
385385
}
386386

387-
static int jl_thread_suspend_and_get_state2(int tid, host_thread_state_t *ctx)
387+
static int jl_thread_suspend_and_get_state2(int tid, host_thread_state_t *ctx) JL_NOTSAFEPOINT
388388
{
389389
jl_ptls_t ptls2 = jl_atomic_load_relaxed(&jl_all_tls_states)[tid];
390390
if (ptls2 == NULL) // this thread is not alive
391391
return 0;
392-
jl_task_t *ct2 = ptls2 ? jl_atomic_load_relaxed(&ptls2->current_task) : NULL;
392+
jl_task_t *ct2 = jl_atomic_load_relaxed(&ptls2->current_task);
393393
if (ct2 == NULL) // this thread is already dead
394394
return 0;
395395

@@ -407,18 +407,18 @@ static int jl_thread_suspend_and_get_state2(int tid, host_thread_state_t *ctx)
407407
return 1;
408408
}
409409

410-
static void jl_thread_suspend_and_get_state(int tid, int timeout, unw_context_t **ctx)
410+
int jl_thread_suspend_and_get_state(int tid, int timeout, bt_context_t *ctx)
411411
{
412412
(void)timeout;
413-
static host_thread_state_t state;
413+
host_thread_state_t state;
414414
if (!jl_thread_suspend_and_get_state2(tid, &state)) {
415-
*ctx = NULL;
416-
return;
415+
return 0;
417416
}
418-
*ctx = (unw_context_t*)&state;
417+
*ctx = *(unw_context_t*)&state;
418+
return 1;
419419
}
420420

421-
static void jl_thread_resume(int tid, int sig)
421+
void jl_thread_resume(int tid)
422422
{
423423
jl_ptls_t ptls2 = jl_atomic_load_relaxed(&jl_all_tls_states)[tid];
424424
mach_port_t thread = pthread_mach_thread_np(ptls2->system_id);
@@ -593,8 +593,15 @@ static void jl_unlock_profile_mach(int dlsymlock, int keymgr_locked)
593593
jl_unlock_profile();
594594
}
595595

596-
#define jl_lock_profile() int keymgr_locked = jl_lock_profile_mach(1)
597-
#define jl_unlock_profile() jl_unlock_profile_mach(1, keymgr_locked)
596+
int jl_lock_stackwalk(void)
597+
{
598+
return jl_lock_profile_mach(1);
599+
}
600+
601+
void jl_unlock_stackwalk(int lockret)
602+
{
603+
jl_unlock_profile_mach(1, lockret);
604+
}
598605

599606
void *mach_profile_listener(void *arg)
600607
{
@@ -691,7 +698,7 @@ void *mach_profile_listener(void *arg)
691698
bt_data_prof[bt_size_cur++].uintptr = 0;
692699
}
693700
// We're done! Resume the thread.
694-
jl_thread_resume(i, 0);
701+
jl_thread_resume(i);
695702
}
696703
jl_unlock_profile_mach(0, keymgr_locked);
697704
if (running) {

0 commit comments

Comments
 (0)