Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correctly track freed bytes in jl_genericmemory_to_string #54331

Merged
merged 4 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,12 @@ void jl_gc_count_allocd(size_t sz) JL_NOTSAFEPOINT
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + sz);
jl_batch_accum_heap_size(ptls, sz);
}

void jl_gc_count_freed(size_t sz) JL_NOTSAFEPOINT
{
jl_batch_accum_free_size(jl_current_task->ptls, sz);
}

// Only safe to update the heap inside the GC
static void combine_thread_gc_counts(jl_gc_num_t *dest, int update_heap) JL_NOTSAFEPOINT
{
Expand Down
2 changes: 0 additions & 2 deletions src/gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,6 @@ void gc_stats_big_obj(void);
// For debugging
void gc_count_pool(void);

size_t jl_genericmemory_nbytes(jl_genericmemory_t *a) JL_NOTSAFEPOINT;

JL_DLLEXPORT void jl_enable_gc_logging(int enable);
JL_DLLEXPORT int jl_is_gc_logging_enabled(void);
JL_DLLEXPORT uint32_t jl_get_num_stack_mappings(void);
Expand Down
7 changes: 5 additions & 2 deletions src/genericmemory.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ JL_DLLEXPORT jl_genericmemory_t *jl_ptr_to_genericmemory(jl_value_t *mtype, void
m = (jl_genericmemory_t*)jl_gc_alloc(ct->ptls, tsz, mtype);
m->ptr = data;
m->length = nel;
jl_genericmemory_data_owner_field(m) = NULL;
int isaligned = 0; // TODO: allow passing memalign'd buffers
jl_genericmemory_data_owner_field(m) = own_buffer ? (jl_value_t*)m : NULL;
if (own_buffer) {
int isaligned = 0; // TODO: allow passing memalign'd buffers
jl_gc_track_malloced_genericmemory(ct->ptls, m, isaligned);
jl_gc_count_allocd(nel*elsz);
}
Expand Down Expand Up @@ -208,8 +208,11 @@ JL_DLLEXPORT jl_value_t *jl_genericmemory_to_string(jl_genericmemory_t *m, size_
JL_GC_PUSH1(&o);
jl_value_t *str = jl_pchar_to_string((const char*)m->ptr, len);
JL_GC_POP();
if (how == 1) // TODO: we might like to early-call jl_gc_free_memory here instead actually, but hopefully `m` will die soon
jl_gc_count_freed(mlength);
return str;
}
// n.b. how == 0 is always pool-allocated, so the freed bytes are computed from the pool not the object
return jl_pchar_to_string((const char*)m->ptr, len);
}

Expand Down
4 changes: 2 additions & 2 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -1179,8 +1179,8 @@ STATIC_INLINE jl_value_t *jl_svecset(
/*
how - allocation style
0 = data is inlined
1 = owns the gc-managed data, exclusively
2 = malloc-allocated pointer (may or may not own it)
1 = owns the gc-managed data, exclusively (will free it)
2 = malloc-allocated pointer (does not own it)
3 = has a pointer to the object that owns the data pointer
*/
STATIC_INLINE int jl_genericmemory_how(jl_genericmemory_t *m) JL_NOTSAFEPOINT
Expand Down
2 changes: 2 additions & 0 deletions src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,9 @@ JL_DLLEXPORT int64_t jl_gc_diff_total_bytes(void) JL_NOTSAFEPOINT;
JL_DLLEXPORT int64_t jl_gc_sync_total_bytes(int64_t offset) JL_NOTSAFEPOINT;
void jl_gc_track_malloced_array(jl_ptls_t ptls, jl_array_t *a) JL_NOTSAFEPOINT;
void jl_gc_track_malloced_genericmemory(jl_ptls_t ptls, jl_genericmemory_t *m, int isaligned) JL_NOTSAFEPOINT;
size_t jl_genericmemory_nbytes(jl_genericmemory_t *a) JL_NOTSAFEPOINT;
void jl_gc_count_allocd(size_t sz) JL_NOTSAFEPOINT;
void jl_gc_count_freed(size_t sz) JL_NOTSAFEPOINT;
void jl_gc_run_all_finalizers(jl_task_t *ct);
void jl_release_task_stack(jl_ptls_t ptls, jl_task_t *task);
void jl_gc_add_finalizer_(jl_ptls_t ptls, void *v, void *f) JL_NOTSAFEPOINT;
Expand Down