Skip to content

Commit a6abd18

Browse files
committed
count bytes allocated through malloc more precisely
1 parent 40ecf69 commit a6abd18

File tree

5 files changed

+31
-15
lines changed

5 files changed

+31
-15
lines changed

src/gc-common.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ JL_DLLEXPORT void jl_gc_set_cb_notify_gc_pressure(jl_gc_cb_notify_gc_pressure_t
115115
jl_gc_deregister_callback(&gc_cblist_notify_gc_pressure, (jl_gc_cb_func_t)cb);
116116
}
117117

118+
// =========================================================================== //
119+
// malloc wrappers, aligned allocation
120+
// =========================================================================== //
121+
122+
size_t memory_block_usable_size(void *p) JL_NOTSAFEPOINT
123+
{
124+
#if defined(_OS_WINDOWS_)
125+
return _aligned_msize(p, JL_CACHE_BYTE_ALIGNMENT, 0);
126+
#elif defined(_OS_DARWIN_)
127+
return malloc_size(p);
128+
#else
129+
return malloc_usable_size(p);
130+
#endif
131+
}
132+
118133
// =========================================================================== //
119134
// Finalization
120135
// =========================================================================== //

src/gc-common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
#endif
1313
#endif
1414

15+
#if defined(_OS_DARWIN_)
16+
#include <malloc/malloc.h>
17+
#else
18+
#include <malloc.h> // for malloc_trim
19+
#endif
20+
1521
#ifdef __cplusplus
1622
extern "C" {
1723
#endif

src/gc-stock.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
#include "julia_atomics.h"
1010
#include "julia_gcext.h"
1111
#include "julia_assert.h"
12-
#ifdef __GLIBC__
13-
#include <malloc.h> // for malloc_trim
14-
#endif
1512

1613
#ifdef __cplusplus
1714
extern "C" {
@@ -579,11 +576,6 @@ void jl_gc_count_allocd(size_t sz) JL_NOTSAFEPOINT
579576
jl_batch_accum_heap_size(ptls, sz);
580577
}
581578

582-
void jl_gc_count_freed(size_t sz) JL_NOTSAFEPOINT
583-
{
584-
jl_batch_accum_free_size(jl_current_task->ptls, sz);
585-
}
586-
587579
// Only safe to update the heap inside the GC
588580
static void combine_thread_gc_counts(jl_gc_num_t *dest, int update_heap) JL_NOTSAFEPOINT
589581
{
@@ -664,13 +656,15 @@ static void jl_gc_free_memory(jl_value_t *v, int isaligned) JL_NOTSAFEPOINT
664656
jl_genericmemory_t *m = (jl_genericmemory_t*)v;
665657
assert(jl_genericmemory_how(m) == 1 || jl_genericmemory_how(m) == 2);
666658
char *d = (char*)m->ptr;
659+
size_t freed_bytes = memory_block_usable_size(d);
660+
assert(freed_bytes != 0);
667661
if (isaligned)
668662
jl_free_aligned(d);
669663
else
670664
free(d);
671665
jl_atomic_store_relaxed(&gc_heap_stats.heap_size,
672-
jl_atomic_load_relaxed(&gc_heap_stats.heap_size) - jl_genericmemory_nbytes(m));
673-
gc_num.freed += jl_genericmemory_nbytes(m);
666+
jl_atomic_load_relaxed(&gc_heap_stats.heap_size) - freed_bytes);
667+
gc_num.freed += freed_bytes;
674668
gc_num.freecall++;
675669
}
676670

@@ -3739,8 +3733,10 @@ JL_DLLEXPORT void *jl_gc_managed_malloc(size_t sz)
37393733
if (b == NULL)
37403734
jl_throw(jl_memory_exception);
37413735

3736+
size_t allocated_bytes = memory_block_usable_size(b);
3737+
assert(allocated_bytes >= allocsz);
37423738
jl_atomic_store_relaxed(&ptls->gc_tls.gc_num.allocd,
3743-
jl_atomic_load_relaxed(&ptls->gc_tls.gc_num.allocd) + allocsz);
3739+
jl_atomic_load_relaxed(&ptls->gc_tls.gc_num.allocd) + allocated_bytes);
37443740
jl_atomic_store_relaxed(&ptls->gc_tls.gc_num.malloc,
37453741
jl_atomic_load_relaxed(&ptls->gc_tls.gc_num.malloc) + 1);
37463742
jl_batch_accum_heap_size(ptls, allocsz);

src/genericmemory.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ JL_DLLEXPORT jl_genericmemory_t *jl_ptr_to_genericmemory(jl_value_t *mtype, void
165165
if (own_buffer) {
166166
int isaligned = 0; // TODO: allow passing memalign'd buffers
167167
jl_gc_track_malloced_genericmemory(ct->ptls, m, isaligned);
168-
jl_gc_count_allocd(nel*elsz);
168+
size_t allocated_bytes = memory_block_usable_size(data);
169+
jl_gc_count_allocd(allocated_bytes);
169170
}
170171
return m;
171172
}
@@ -208,8 +209,6 @@ JL_DLLEXPORT jl_value_t *jl_genericmemory_to_string(jl_genericmemory_t *m, size_
208209
JL_GC_PUSH1(&o);
209210
jl_value_t *str = jl_pchar_to_string((const char*)m->ptr, len);
210211
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);
213212
return str;
214213
}
215214
// n.b. how == 0 is always pool-allocated, so the freed bytes are computed from the pool not the object

src/julia_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,8 @@ jl_svec_t *jl_perm_symsvec(size_t n, ...);
601601

602602
void jl_gc_track_malloced_genericmemory(jl_ptls_t ptls, jl_genericmemory_t *m, int isaligned) JL_NOTSAFEPOINT;
603603
size_t jl_genericmemory_nbytes(jl_genericmemory_t *a) JL_NOTSAFEPOINT;
604+
size_t memory_block_usable_size(void *mem) JL_NOTSAFEPOINT;
604605
void jl_gc_count_allocd(size_t sz) JL_NOTSAFEPOINT;
605-
void jl_gc_count_freed(size_t sz) JL_NOTSAFEPOINT;
606606
void jl_gc_run_all_finalizers(jl_task_t *ct);
607607
void jl_release_task_stack(jl_ptls_t ptls, jl_task_t *task);
608608
void jl_gc_add_finalizer_(jl_ptls_t ptls, void *v, void *f) JL_NOTSAFEPOINT;

0 commit comments

Comments
 (0)