Skip to content

Commit 6ca5297

Browse files
d-nettovtjnash
authored andcommitted
correctly track freed bytes in jl_genericmemory_to_string (#54331)
Master version of #54309. Should fix #54275. --------- Co-authored-by: Jameson Nash <[email protected]> (cherry picked from commit 92ccc74)
1 parent 5326a2c commit 6ca5297

File tree

5 files changed

+15
-6
lines changed

5 files changed

+15
-6
lines changed

src/gc.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,12 @@ void jl_gc_count_allocd(size_t sz) JL_NOTSAFEPOINT
11391139
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + sz);
11401140
jl_batch_accum_heap_size(ptls, sz);
11411141
}
1142+
1143+
void jl_gc_count_freed(size_t sz) JL_NOTSAFEPOINT
1144+
{
1145+
jl_batch_accum_free_size(jl_current_task->ptls, sz);
1146+
}
1147+
11421148
// Only safe to update the heap inside the GC
11431149
static void combine_thread_gc_counts(jl_gc_num_t *dest, int update_heap) JL_NOTSAFEPOINT
11441150
{

src/gc.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,8 +723,6 @@ void gc_stats_big_obj(void);
723723
// For debugging
724724
void gc_count_pool(void);
725725

726-
size_t jl_genericmemory_nbytes(jl_genericmemory_t *a) JL_NOTSAFEPOINT;
727-
728726
JL_DLLEXPORT void jl_enable_gc_logging(int enable);
729727
JL_DLLEXPORT int jl_is_gc_logging_enabled(void);
730728
JL_DLLEXPORT uint32_t jl_get_num_stack_mappings(void);

src/genericmemory.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ JL_DLLEXPORT jl_genericmemory_t *jl_ptr_to_genericmemory(jl_value_t *mtype, void
161161
m = (jl_genericmemory_t*)jl_gc_alloc(ct->ptls, tsz, mtype);
162162
m->ptr = data;
163163
m->length = nel;
164-
jl_genericmemory_data_owner_field(m) = NULL;
165-
int isaligned = 0; // TODO: allow passing memalign'd buffers
164+
jl_genericmemory_data_owner_field(m) = own_buffer ? (jl_value_t*)m : NULL;
166165
if (own_buffer) {
166+
int isaligned = 0; // TODO: allow passing memalign'd buffers
167167
jl_gc_track_malloced_genericmemory(ct->ptls, m, isaligned);
168168
jl_gc_count_allocd(nel*elsz);
169169
}
@@ -208,8 +208,11 @@ JL_DLLEXPORT jl_value_t *jl_genericmemory_to_string(jl_genericmemory_t *m, size_
208208
JL_GC_PUSH1(&o);
209209
jl_value_t *str = jl_pchar_to_string((const char*)m->ptr, len);
210210
JL_GC_POP();
211+
if (how == 1) // TODO: we might like to early-call jl_gc_free_memory here instead actually, but hopefully `m` will die soon
212+
jl_gc_count_freed(mlength);
211213
return str;
212214
}
215+
// n.b. how == 0 is always pool-allocated, so the freed bytes are computed from the pool not the object
213216
return jl_pchar_to_string((const char*)m->ptr, len);
214217
}
215218

src/julia.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,8 +1148,8 @@ STATIC_INLINE jl_value_t *jl_svecset(
11481148
/*
11491149
how - allocation style
11501150
0 = data is inlined
1151-
1 = owns the gc-managed data, exclusively
1152-
2 = malloc-allocated pointer (may or may not own it)
1151+
1 = owns the gc-managed data, exclusively (will free it)
1152+
2 = malloc-allocated pointer (does not own it)
11531153
3 = has a pointer to the object that owns the data pointer
11541154
*/
11551155
STATIC_INLINE int jl_genericmemory_how(jl_genericmemory_t *m) JL_NOTSAFEPOINT

src/julia_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,9 @@ JL_DLLEXPORT int64_t jl_gc_diff_total_bytes(void) JL_NOTSAFEPOINT;
616616
JL_DLLEXPORT int64_t jl_gc_sync_total_bytes(int64_t offset) JL_NOTSAFEPOINT;
617617
void jl_gc_track_malloced_array(jl_ptls_t ptls, jl_array_t *a) JL_NOTSAFEPOINT;
618618
void jl_gc_track_malloced_genericmemory(jl_ptls_t ptls, jl_genericmemory_t *m, int isaligned) JL_NOTSAFEPOINT;
619+
size_t jl_genericmemory_nbytes(jl_genericmemory_t *a) JL_NOTSAFEPOINT;
619620
void jl_gc_count_allocd(size_t sz) JL_NOTSAFEPOINT;
621+
void jl_gc_count_freed(size_t sz) JL_NOTSAFEPOINT;
620622
void jl_gc_run_all_finalizers(jl_task_t *ct);
621623
void jl_release_task_stack(jl_ptls_t ptls, jl_task_t *task);
622624
void jl_gc_add_finalizer_(jl_ptls_t ptls, void *v, void *f) JL_NOTSAFEPOINT;

0 commit comments

Comments
 (0)