From 8194356082ec76655dc4fb14a909e9b721730b79 Mon Sep 17 00:00:00 2001 From: Yi Lin Date: Thu, 25 May 2023 23:48:23 +0000 Subject: [PATCH] Fix stock Julia build --- src/gc-debug.c | 203 ++++++++++++++----------------------------------- src/gc.c | 138 ++++++++++++++++++++++++++++----- src/gc.h | 49 +++++------- src/mmtk-gc.c | 2 + 4 files changed, 201 insertions(+), 191 deletions(-) diff --git a/src/gc-debug.c b/src/gc-debug.c index fc3da5b2ba282..df2e3487506fa 100644 --- a/src/gc-debug.c +++ b/src/gc-debug.c @@ -647,91 +647,6 @@ void jl_gc_debug_print_status(void) } #endif -#ifdef OBJPROFILE -static htable_t obj_counts[3]; -static htable_t obj_sizes[3]; -void objprofile_count(void *ty, int old, int sz) -{ - if (gc_verifying) return; - if ((intptr_t)ty <= 0x10) { - ty = (void*)jl_buff_tag; - } - else if (ty != (void*)jl_buff_tag && ty != jl_malloc_tag && - jl_typeof(ty) == (jl_value_t*)jl_datatype_type && - ((jl_datatype_t*)ty)->instance) { - ty = jl_singleton_tag; - } - void **bp = ptrhash_bp(&obj_counts[old], ty); - if (*bp == HT_NOTFOUND) - *bp = (void*)2; - else - (*((intptr_t*)bp))++; - bp = ptrhash_bp(&obj_sizes[old], ty); - if (*bp == HT_NOTFOUND) - *bp = (void*)(intptr_t)(1 + sz); - else - *((intptr_t*)bp) += sz; -} - -void objprofile_reset(void) -{ - for (int g = 0; g < 3; g++) { - htable_reset(&obj_counts[g], 0); - htable_reset(&obj_sizes[g], 0); - } -} - -static void objprofile_print(htable_t nums, htable_t sizes) -{ - for(int i=0; i < nums.size; i+=2) { - if (nums.table[i+1] != HT_NOTFOUND) { - void *ty = nums.table[i]; - int num = (intptr_t)nums.table[i + 1] - 1; - size_t sz = (uintptr_t)ptrhash_get(&sizes, ty) - 1; - static const int ptr_hex_width = 2 * sizeof(void*); - if (sz > 2e9) { - jl_safe_printf(" %6d : %*.1f GB of (%*p) ", - num, 6, ((double)sz) / 1024 / 1024 / 1024, - ptr_hex_width, ty); - } - else if (sz > 2e6) { - jl_safe_printf(" %6d : %*.1f MB of (%*p) ", - num, 6, ((double)sz) / 1024 / 1024, - ptr_hex_width, ty); - } - else if (sz > 2e3) { - jl_safe_printf(" %6d : %*.1f kB of (%*p) ", - num, 6, ((double)sz) / 1024, - ptr_hex_width, ty); - } - else { - jl_safe_printf(" %6d : %*d B of (%*p) ", - num, 6, (int)sz, ptr_hex_width, ty); - } - if (ty == (void*)jl_buff_tag) - jl_safe_printf("#"); - else if (ty == jl_malloc_tag) - jl_safe_printf("#"); - else if (ty == jl_singleton_tag) - jl_safe_printf("#"); - else - jl_static_show(JL_STDERR, (jl_value_t*)ty); - jl_safe_printf("\n"); - } - } -} - -void objprofile_printall(void) -{ - jl_safe_printf("Transient mark :\n"); - objprofile_print(obj_counts[0], obj_sizes[0]); - jl_safe_printf("Perm mark :\n"); - objprofile_print(obj_counts[1], obj_sizes[1]); - jl_safe_printf("Remset :\n"); - objprofile_print(obj_counts[2], obj_sizes[2]); -} -#endif - #if defined(GC_TIME) || defined(GC_FINAL_STATS) STATIC_INLINE double jl_ns2ms(int64_t t) { @@ -1257,68 +1172,68 @@ void gc_count_pool(void) // `offset` will be added to `mq->current` for convenience in the debugger. NOINLINE void gc_mark_loop_unwind(jl_ptls_t ptls, jl_gc_markqueue_t *mq, int offset) { - jl_jmp_buf *old_buf = jl_get_safe_restore(); - jl_jmp_buf buf; - jl_set_safe_restore(&buf); - if (jl_setjmp(buf, 0) != 0) { - jl_safe_printf("\n!!! ERROR when unwinding gc mark loop -- ABORTING !!!\n"); - jl_set_safe_restore(old_buf); - return; - } - jl_value_t **start = mq->start; - jl_value_t **end = mq->current + offset; - for (; start < end; start++) { - jl_value_t *obj = *start; - jl_taggedvalue_t *o = jl_astaggedvalue(obj); - jl_safe_printf("Queued object: %p :: (tag: %zu) (bits: %zu)\n", obj, - (uintptr_t)o->header, ((uintptr_t)o->header & 3)); - jl_((void*)(jl_datatype_t *)(o->header & ~(uintptr_t)0xf)); - } - jl_set_safe_restore(old_buf); -} - -int gc_slot_to_fieldidx(void *obj, void *slot, jl_datatype_t *vt) JL_NOTSAFEPOINT -{ - int nf = (int)jl_datatype_nfields(vt); - for (int i = 1; i < nf; i++) { - if (slot < (void*)((char*)obj + jl_field_offset(vt, i))) - return i - 1; - } - return nf - 1; -} - -int gc_slot_to_arrayidx(void *obj, void *_slot) JL_NOTSAFEPOINT -{ - char *slot = (char*)_slot; - jl_datatype_t *vt = (jl_datatype_t*)jl_typeof(obj); - char *start = NULL; - size_t len = 0; - size_t elsize = sizeof(void*); - if (vt == jl_module_type) { - jl_module_t *m = (jl_module_t*)obj; - start = (char*)m->usings.items; - len = m->usings.len; - } - else if (vt == jl_simplevector_type) { - start = (char*)jl_svec_data(obj); - len = jl_svec_len(obj); - } - else if (vt->name == jl_array_typename) { - jl_array_t *a = (jl_array_t*)obj; - start = (char*)a->data; - len = jl_array_len(a); - elsize = a->elsize; - } - if (slot < start || slot >= start + elsize * len) - return -1; - return (slot - start) / elsize; -} + // jl_jmp_buf *old_buf = jl_get_safe_restore(); + // jl_jmp_buf buf; + // jl_set_safe_restore(&buf); + // if (jl_setjmp(buf, 0) != 0) { + // jl_safe_printf("\n!!! ERROR when unwinding gc mark loop -- ABORTING !!!\n"); + // jl_set_safe_restore(old_buf); + // return; + // } + // jl_value_t **start = mq->start; + // jl_value_t **end = mq->current + offset; + // for (; start < end; start++) { + // jl_value_t *obj = *start; + // jl_taggedvalue_t *o = jl_astaggedvalue(obj); + // jl_safe_printf("Queued object: %p :: (tag: %zu) (bits: %zu)\n", obj, + // (uintptr_t)o->header, ((uintptr_t)o->header & 3)); + // jl_((void*)(jl_datatype_t *)(o->header & ~(uintptr_t)0xf)); + // } + // jl_set_safe_restore(old_buf); +} + +// int gc_slot_to_fieldidx(void *obj, void *slot, jl_datatype_t *vt) JL_NOTSAFEPOINT +// { +// int nf = (int)jl_datatype_nfields(vt); +// for (int i = 1; i < nf; i++) { +// if (slot < (void*)((char*)obj + jl_field_offset(vt, i))) +// return i - 1; +// } +// return nf - 1; +// } + +// int gc_slot_to_arrayidx(void *obj, void *_slot) JL_NOTSAFEPOINT +// { +// char *slot = (char*)_slot; +// jl_datatype_t *vt = (jl_datatype_t*)jl_typeof(obj); +// char *start = NULL; +// size_t len = 0; +// size_t elsize = sizeof(void*); +// if (vt == jl_module_type) { +// jl_module_t *m = (jl_module_t*)obj; +// start = (char*)m->usings.items; +// len = m->usings.len; +// } +// else if (vt == jl_simplevector_type) { +// start = (char*)jl_svec_data(obj); +// len = jl_svec_len(obj); +// } +// else if (vt->name == jl_array_typename) { +// jl_array_t *a = (jl_array_t*)obj; +// start = (char*)a->data; +// len = jl_array_len(a); +// elsize = a->elsize; +// } +// if (slot < start || slot >= start + elsize * len) +// return -1; +// return (slot - start) / elsize; +// } static int gc_logging_enabled = 0; -JL_DLLEXPORT void jl_enable_gc_logging(int enable) { - gc_logging_enabled = enable; -} +// JL_DLLEXPORT void jl_enable_gc_logging(int enable) { +// gc_logging_enabled = enable; +// } void _report_gc_finished(uint64_t pause, uint64_t freed, int full, int recollect) JL_NOTSAFEPOINT { if (!gc_logging_enabled) { diff --git a/src/gc.c b/src/gc.c index 932bb1d97c6db..ce80597a937f1 100644 --- a/src/gc.c +++ b/src/gc.c @@ -376,10 +376,6 @@ void jl_gc_wait_for_the_world(jl_ptls_t* gc_all_tls_states, int gc_n_threads) } } -<<<<<<< HEAD - -======= ->>>>>>> upstream/master // malloc wrappers, aligned allocation #if defined(_OS_WINDOWS_) @@ -2648,6 +2644,8 @@ JL_EXTENSION NOINLINE void gc_mark_loop_serial(jl_ptls_t ptls) gc_drain_own_chunkqueue(ptls, &ptls->mark_queue); } +extern int gc_first_tid; + void gc_mark_and_steal(jl_ptls_t ptls) { jl_gc_markqueue_t *mq = &ptls->mark_queue; @@ -2799,24 +2797,109 @@ void gc_mark_clean_reclaim_sets(void) } } -static void gc_premark(jl_ptls_t ptls2) +// void gc_premark(jl_ptls_t ptls2) +// { +// arraylist_t *remset = ptls2->heap.remset; +// ptls2->heap.remset = ptls2->heap.last_remset; +// ptls2->heap.last_remset = remset; +// ptls2->heap.remset->len = 0; +// ptls2->heap.remset_nptr = 0; +// // avoid counting remembered objects +// // in `perm_scanned_bytes` +// size_t len = remset->len; +// void **items = remset->items; +// for (size_t i = 0; i < len; i++) { +// jl_value_t *item = (jl_value_t *)items[i]; +// objprofile_count(jl_typeof(item), 2, 0); +// jl_astaggedvalue(item)->bits.gc = GC_OLD_MARKED; +// } +// } + +#ifdef OBJPROFILE +static htable_t obj_counts[3]; +static htable_t obj_sizes[3]; +void objprofile_count(void *ty, int old, int sz) +{ + if (gc_verifying) return; + if ((intptr_t)ty <= 0x10) { + ty = (void*)jl_buff_tag; + } + else if (ty != (void*)jl_buff_tag && ty != jl_malloc_tag && + jl_typeof(ty) == (jl_value_t*)jl_datatype_type && + ((jl_datatype_t*)ty)->instance) { + ty = jl_singleton_tag; + } + void **bp = ptrhash_bp(&obj_counts[old], ty); + if (*bp == HT_NOTFOUND) + *bp = (void*)2; + else + (*((intptr_t*)bp))++; + bp = ptrhash_bp(&obj_sizes[old], ty); + if (*bp == HT_NOTFOUND) + *bp = (void*)(intptr_t)(1 + sz); + else + *((intptr_t*)bp) += sz; +} + +void objprofile_reset(void) { - arraylist_t *remset = ptls2->heap.remset; - ptls2->heap.remset = ptls2->heap.last_remset; - ptls2->heap.last_remset = remset; - ptls2->heap.remset->len = 0; - ptls2->heap.remset_nptr = 0; - // avoid counting remembered objects - // in `perm_scanned_bytes` - size_t len = remset->len; - void **items = remset->items; - for (size_t i = 0; i < len; i++) { - jl_value_t *item = (jl_value_t *)items[i]; - objprofile_count(jl_typeof(item), 2, 0); - jl_astaggedvalue(item)->bits.gc = GC_OLD_MARKED; + for (int g = 0; g < 3; g++) { + htable_reset(&obj_counts[g], 0); + htable_reset(&obj_sizes[g], 0); + } +} + +static void objprofile_print(htable_t nums, htable_t sizes) +{ + for(int i=0; i < nums.size; i+=2) { + if (nums.table[i+1] != HT_NOTFOUND) { + void *ty = nums.table[i]; + int num = (intptr_t)nums.table[i + 1] - 1; + size_t sz = (uintptr_t)ptrhash_get(&sizes, ty) - 1; + static const int ptr_hex_width = 2 * sizeof(void*); + if (sz > 2e9) { + jl_safe_printf(" %6d : %*.1f GB of (%*p) ", + num, 6, ((double)sz) / 1024 / 1024 / 1024, + ptr_hex_width, ty); + } + else if (sz > 2e6) { + jl_safe_printf(" %6d : %*.1f MB of (%*p) ", + num, 6, ((double)sz) / 1024 / 1024, + ptr_hex_width, ty); + } + else if (sz > 2e3) { + jl_safe_printf(" %6d : %*.1f kB of (%*p) ", + num, 6, ((double)sz) / 1024, + ptr_hex_width, ty); + } + else { + jl_safe_printf(" %6d : %*d B of (%*p) ", + num, 6, (int)sz, ptr_hex_width, ty); + } + if (ty == (void*)jl_buff_tag) + jl_safe_printf("#"); + else if (ty == jl_malloc_tag) + jl_safe_printf("#"); + else if (ty == jl_singleton_tag) + jl_safe_printf("#"); + else + jl_static_show(JL_STDERR, (jl_value_t*)ty); + jl_safe_printf("\n"); + } } } +void objprofile_printall(void) +{ + jl_safe_printf("Transient mark :\n"); + objprofile_print(obj_counts[0], obj_sizes[0]); + jl_safe_printf("Perm mark :\n"); + objprofile_print(obj_counts[1], obj_sizes[1]); + jl_safe_printf("Remset :\n"); + objprofile_print(obj_counts[2], obj_sizes[2]); +} +#endif + static void gc_queue_thread_local(jl_gc_markqueue_t *mq, jl_ptls_t ptls2) { jl_task_t *task; @@ -2955,6 +3038,9 @@ static void sweep_finalizer_list(arraylist_t *list) size_t jl_maxrss(void); +extern void objprofile_printall(void); +extern void objprofile_reset(void); + // Only one thread should be running in this function static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection) { @@ -3708,6 +3794,22 @@ void disable_collection(void) { } +JL_DLLEXPORT void jl_gc_wb1_noinline(const void *parent) JL_NOTSAFEPOINT +{ +} + +JL_DLLEXPORT void jl_gc_wb2_noinline(const void *parent, const void *ptr) JL_NOTSAFEPOINT +{ +} + +JL_DLLEXPORT void jl_gc_wb1_slow(const void *parent) JL_NOTSAFEPOINT +{ +} + +JL_DLLEXPORT void jl_gc_wb2_slow(const void *parent, const void* ptr) JL_NOTSAFEPOINT +{ +} + #ifdef __cplusplus } #endif diff --git a/src/gc.h b/src/gc.h index 3def80327ceda..701c2c769e1b4 100644 --- a/src/gc.h +++ b/src/gc.h @@ -47,7 +47,24 @@ extern void gc_premark(jl_ptls_t ptls2); extern void *gc_managed_realloc_(jl_ptls_t ptls, void *d, size_t sz, size_t oldsz, int isaligned, jl_value_t *owner, int8_t can_collect); extern size_t jl_array_nbytes(jl_array_t *a); -extern void objprofile_count(void *ty, int old, int sz); + +#ifdef OBJPROFILE +void objprofile_count(void *ty, int old, int sz) JL_NOTSAFEPOINT; +void objprofile_printall(void); +void objprofile_reset(void); +#else +static inline void objprofile_count(void *ty, int old, int sz) JL_NOTSAFEPOINT +{ +} + +static inline void objprofile_printall(void) +{ +} + +static inline void objprofile_reset(void) +{ +} +#endif #define malloc_cache_align(sz) jl_malloc_aligned(sz, JL_CACHE_BYTE_ALIGNMENT) #define realloc_cache_align(p, sz, oldsz) jl_realloc_aligned(p, sz, oldsz, JL_CACHE_BYTE_ALIGNMENT) @@ -70,7 +87,7 @@ extern uint64_t finalizer_rngState[]; extern int gc_n_threads; extern jl_ptls_t* gc_all_tls_states; -// keep in sync with the Julia type of the same name in base/timing.jl +// This struct must be kept in sync with the Julia type of the same name in base/timing.jl typedef struct { int64_t allocd; int64_t deferred_alloc; @@ -82,7 +99,6 @@ typedef struct { uint64_t freecall; uint64_t total_time; uint64_t total_allocd; - uint64_t since_sweep; size_t interval; int pause; int full_sweep; @@ -90,6 +106,7 @@ typedef struct { uint64_t max_memory; uint64_t time_to_safepoint; uint64_t max_time_to_safepoint; + uint64_t total_time_to_safepoint; uint64_t sweep_time; uint64_t mark_time; uint64_t total_sweep_time; @@ -217,32 +234,6 @@ typedef struct { jl_alloc_num_t print; } jl_gc_debug_env_t; -// This struct must be kept in sync with the Julia type of the same name in base/timing.jl -typedef struct { - int64_t allocd; - int64_t deferred_alloc; - int64_t freed; - uint64_t malloc; - uint64_t realloc; - uint64_t poolalloc; - uint64_t bigalloc; - uint64_t freecall; - uint64_t total_time; - uint64_t total_allocd; - size_t interval; - int pause; - int full_sweep; - uint64_t max_pause; - uint64_t max_memory; - uint64_t time_to_safepoint; - uint64_t max_time_to_safepoint; - uint64_t total_time_to_safepoint; - uint64_t sweep_time; - uint64_t mark_time; - uint64_t total_sweep_time; - uint64_t total_mark_time; -} jl_gc_num_t; - // Array chunks (work items representing suffixes of // large arrays of pointers left to be marked) diff --git a/src/mmtk-gc.c b/src/mmtk-gc.c index 8b4d1f2c22397..5e868ef11c1d2 100644 --- a/src/mmtk-gc.c +++ b/src/mmtk-gc.c @@ -473,6 +473,7 @@ void jl_print_gc_stats(JL_STREAM *s) { } +#ifdef OBJPROFILE void objprofile_count(void *ty, int old, int sz) JL_NOTSAFEPOINT { } @@ -484,6 +485,7 @@ void objprofile_printall(void) void objprofile_reset(void) { } +#endif // gc thread function void jl_gc_threadfun(void *arg)