Skip to content

Commit 2260ddf

Browse files
committed
Apply suggestions from code review
1 parent 015cf96 commit 2260ddf

File tree

5 files changed

+31
-16
lines changed

5 files changed

+31
-16
lines changed

src/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ $(BUILDDIR)/debuginfo.o $(BUILDDIR)/debuginfo.dbg.obj: $(addprefix $(SRCDIR)/,de
318318
$(BUILDDIR)/disasm.o $(BUILDDIR)/disasm.dbg.obj: $(SRCDIR)/debuginfo.h $(SRCDIR)/processor.h
319319
$(BUILDDIR)/gc-debug.o $(BUILDDIR)/gc-debug.dbg.obj: $(SRCDIR)/gc-common.h $(SRCDIR)/gc-stock.h
320320
$(BUILDDIR)/gc-pages.o $(BUILDDIR)/gc-pages.dbg.obj: $(SRCDIR)/gc-common.h $(SRCDIR)/gc-stock.h
321+
$(BUILDDIR)/gc-stacks.o $(BUILDDIR)/gc-stacks.dbg.obj: $(SRCDIR)/gc-common.h $(SRCDIR)/gc-stock.h
321322
$(BUILDDIR)/gc-stock.o $(BUILDDIR)/gc.dbg.obj: $(SRCDIR)/gc-common.h $(SRCDIR)/gc-stock.h $(SRCDIR)/gc-heap-snapshot.h $(SRCDIR)/gc-alloc-profiler.h $(SRCDIR)/gc-page-profiler.h
322323
$(BUILDDIR)/gc-heap-snapshot.o $(BUILDDIR)/gc-heap-snapshot.dbg.obj: $(SRCDIR)/gc-heap-snapshot.h
323324
$(BUILDDIR)/gc-alloc-profiler.o $(BUILDDIR)/gc-alloc-profiler.dbg.obj: $(SRCDIR)/gc-alloc-profiler.h

src/gc-stacks.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// This file is a part of Julia. License is MIT: https://julialang.org/license
22

33
#include "gc-common.h"
4+
#include "gc-stock.h"
45
#include "threading.h"
56
#ifndef _OS_WINDOWS_
67
# include <sys/resource.h>
@@ -202,11 +203,8 @@ JL_DLLEXPORT void *jl_malloc_stack(size_t *bufsz, jl_task_t *owner) JL_NOTSAFEPO
202203
return stk;
203204
}
204205

205-
extern _Atomic(int) gc_ptls_sweep_idx;
206-
extern _Atomic(int) gc_n_threads_sweeping;
207-
extern _Atomic(int) gc_stack_free_idx;
208206

209-
void sweep_stack_pools(void) JL_NOTSAFEPOINT
207+
void sweep_stack_pool_loop(void) JL_NOTSAFEPOINT
210208
{
211209
// Stack sweeping algorithm:
212210
// // deallocate stacks if we have too many sitting around unused

src/gc-stock.c

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,20 @@ void gc_sweep_wait_for_all_stacks(void)
10271027
}
10281028
}
10291029

1030+
void sweep_stack_pools(jl_ptls_t ptls) JL_NOTSAFEPOINT
1031+
{
1032+
// initialize ptls index for parallel sweeping of stack pools
1033+
int stack_free_idx = jl_atomic_load_relaxed(&gc_stack_free_idx);
1034+
if (stack_free_idx + 1 == gc_n_threads)
1035+
jl_atomic_store_relaxed(&gc_stack_free_idx, 0);
1036+
else
1037+
jl_atomic_store_relaxed(&gc_stack_free_idx, stack_free_idx + 1);
1038+
jl_atomic_store_release(&gc_ptls_sweep_idx, gc_n_threads - 1); // idx == gc_n_threads = release stacks to the OS so it's serial
1039+
gc_sweep_wake_all_stacks(ptls);
1040+
sweep_stack_pool_loop();
1041+
gc_sweep_wait_for_all_stacks();
1042+
}
1043+
10301044
static void gc_pool_sync_nfree(jl_gc_pagemeta_t *pg, jl_taggedvalue_t *last) JL_NOTSAFEPOINT
10311045
{
10321046
assert(pg->fl_begin_offset != UINT16_MAX);
@@ -3095,16 +3109,7 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
30953109
#endif
30963110
current_sweep_full = sweep_full;
30973111
sweep_weak_refs();
3098-
// initialize ptls index for parallel sweeping of stack pools
3099-
int stack_free_idx = jl_atomic_load_relaxed(&gc_stack_free_idx);
3100-
if (stack_free_idx + 1 == gc_n_threads)
3101-
jl_atomic_store_relaxed(&gc_stack_free_idx, 0);
3102-
else
3103-
jl_atomic_store_relaxed(&gc_stack_free_idx, stack_free_idx + 1);
3104-
jl_atomic_store_release(&gc_ptls_sweep_idx, gc_n_threads - 1); // idx == gc_n_threads = release stacks to the OS so it's serial
3105-
gc_sweep_wake_all_stacks(ptls);
3106-
sweep_stack_pools();
3107-
gc_sweep_wait_for_all_stacks();
3112+
sweep_stack_pools(ptls);
31083113
gc_sweep_other(ptls, sweep_full);
31093114
gc_scrub();
31103115
gc_verify_tags();
@@ -3516,6 +3521,10 @@ STATIC_INLINE int may_sweep(jl_ptls_t ptls) JL_NOTSAFEPOINT
35163521
return (jl_atomic_load(&ptls->gc_tls.gc_sweeps_requested) > 0);
35173522
}
35183523

3524+
STATIC_INLINE int may_sweep_stack(jl_ptls_t ptls) JL_NOTSAFEPOINT
3525+
{
3526+
return (jl_atomic_load(&ptls->gc_tls.gc_stack_sweep_requested) > 0);
3527+
}
35193528
// parallel gc thread function
35203529
void jl_parallel_gc_threadfun(void *arg)
35213530
{
@@ -3544,10 +3553,14 @@ void jl_parallel_gc_threadfun(void *arg)
35443553
uv_mutex_unlock(&gc_threads_lock);
35453554
assert(jl_atomic_load_relaxed(&ptls->gc_state) == JL_GC_PARALLEL_COLLECTOR_THREAD);
35463555
gc_mark_loop_parallel(ptls, 0);
3556+
if (may_sweep_stack(ptls)) {
3557+
assert(jl_atomic_load_relaxed(&ptls->gc_state) == JL_GC_PARALLEL_COLLECTOR_THREAD);
3558+
sweep_stack_pool_loop();
3559+
jl_atomic_fetch_add(&ptls->gc_tls.gc_stack_sweep_requested, -1);
3560+
}
35473561
if (may_sweep(ptls)) {
35483562
assert(jl_atomic_load_relaxed(&ptls->gc_state) == JL_GC_PARALLEL_COLLECTOR_THREAD);
35493563
gc_sweep_pool_parallel(ptls);
3550-
sweep_stack_pools();
35513564
jl_atomic_fetch_add(&ptls->gc_tls.gc_sweeps_requested, -1);
35523565
}
35533566
}

src/gc-stock.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,8 @@ extern uv_cond_t gc_threads_cond;
511511
extern uv_sem_t gc_sweep_assists_needed;
512512
extern _Atomic(int) gc_n_threads_marking;
513513
extern _Atomic(int) gc_n_threads_sweeping;
514+
extern _Atomic(int) gc_ptls_sweep_idx;
515+
extern _Atomic(int) gc_stack_free_idx;
514516
extern _Atomic(int) n_threads_running;
515517
extern uv_barrier_t thread_init_done;
516518
void gc_mark_queue_all_roots(jl_ptls_t ptls, jl_gc_markqueue_t *mq);
@@ -521,7 +523,7 @@ void gc_mark_loop_serial(jl_ptls_t ptls);
521523
void gc_mark_loop_parallel(jl_ptls_t ptls, int master);
522524
void gc_sweep_pool_parallel(jl_ptls_t ptls);
523525
void gc_free_pages(void);
524-
void sweep_stack_pools(void) JL_NOTSAFEPOINT;
526+
void sweep_stack_pool_loop(void) JL_NOTSAFEPOINT;
525527
void jl_gc_debug_init(void);
526528

527529
// GC pages

src/gc-tls.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ typedef struct {
8282
jl_gc_markqueue_t mark_queue;
8383
jl_gc_mark_cache_t gc_cache;
8484
_Atomic(size_t) gc_sweeps_requested;
85+
_Atomic(uint8_t) gc_stack_sweep_requested;
8586
arraylist_t sweep_objs;
8687
} jl_gc_tls_states_t;
8788

0 commit comments

Comments
 (0)