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

count bytes allocated through malloc more precisely #171

Closed
wants to merge 1 commit into from
Closed
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
1 change: 0 additions & 1 deletion src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,6 @@ JL_DLLEXPORT jl_value_t *jl_array_to_string(jl_array_t *a)
return o;
}
}
jl_gc_count_freed(jl_array_nbytes(a));
a->nrows = 0;
a->length = 0;
a->maxsize = 0;
Expand Down
31 changes: 21 additions & 10 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
#include "julia.h"
#include "julia_gcext.h"
#include "julia_assert.h"
#ifdef __GLIBC__

#if defined(_OS_DARWIN_)
#include <malloc/malloc.h>
#else
#include <malloc.h> // for malloc_trim
#endif

Expand Down Expand Up @@ -253,6 +256,7 @@ void jl_gc_wait_for_the_world(jl_ptls_t* gc_all_tls_states, int gc_n_threads)
#if defined(_OS_WINDOWS_)
STATIC_INLINE void *jl_malloc_aligned(size_t sz, size_t align)
{
assert(align == JL_CACHE_BYTE_ALIGNMENT);
return _aligned_malloc(sz ? sz : 1, align);
}
STATIC_INLINE void *jl_realloc_aligned(void *p, size_t sz, size_t oldsz,
Expand All @@ -268,6 +272,7 @@ STATIC_INLINE void jl_free_aligned(void *p) JL_NOTSAFEPOINT
#else
STATIC_INLINE void *jl_malloc_aligned(size_t sz, size_t align)
{
assert(align == JL_CACHE_BYTE_ALIGNMENT);
#if defined(_P64) || defined(__APPLE__)
if (align <= 16)
return malloc(sz);
Expand Down Expand Up @@ -296,6 +301,16 @@ STATIC_INLINE void jl_free_aligned(void *p) JL_NOTSAFEPOINT
free(p);
}
#endif
size_t memory_block_usable_size(void *p) JL_NOTSAFEPOINT
{
#if defined(_OS_WINDOWS_)
return _aligned_msize(p, JL_CACHE_BYTE_ALIGNMENT, 0);
#elif defined(_OS_DARWIN_)
return malloc_size(p);
#else
return malloc_usable_size(p);
#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)

Expand Down Expand Up @@ -1115,13 +1130,6 @@ void jl_gc_count_allocd(size_t sz) JL_NOTSAFEPOINT
jl_atomic_load_relaxed(&ptls->gc_tls.gc_num.allocd) + sz);
}

void jl_gc_count_freed(size_t sz) JL_NOTSAFEPOINT
{
jl_ptls_t ptls = jl_current_task->ptls;
jl_atomic_store_relaxed(&ptls->gc_tls.gc_num.freed,
jl_atomic_load_relaxed(&ptls->gc_tls.gc_num.freed) + sz);
}

static void combine_thread_gc_counts(jl_gc_num_t *dest) JL_NOTSAFEPOINT
{
int gc_n_threads;
Expand Down Expand Up @@ -1196,11 +1204,12 @@ static void jl_gc_free_array(jl_array_t *a) JL_NOTSAFEPOINT
{
if (a->flags.how == 2) {
char *d = (char*)a->data - a->offset*a->elsize;
size_t freed_bytes = memory_block_usable_size(d);
if (a->flags.isaligned)
jl_free_aligned(d);
else
free(d);
gc_num.freed += jl_array_nbytes(a);
gc_num.freed += freed_bytes;
gc_num.freecall++;
}
}
Expand Down Expand Up @@ -4113,8 +4122,10 @@ JL_DLLEXPORT void *jl_gc_managed_malloc(size_t sz)
if (b == NULL)
jl_throw(jl_memory_exception);

size_t allocated_bytes = memory_block_usable_size(b);
assert(allocated_bytes >= allocsz);
jl_atomic_store_relaxed(&ptls->gc_tls.gc_num.allocd,
jl_atomic_load_relaxed(&ptls->gc_tls.gc_num.allocd) + allocsz);
jl_atomic_load_relaxed(&ptls->gc_tls.gc_num.allocd) + allocated_bytes);
jl_atomic_store_relaxed(&ptls->gc_tls.gc_num.malloc,
jl_atomic_load_relaxed(&ptls->gc_tls.gc_num.malloc) + 1);
#ifdef _OS_WINDOWS_
Expand Down
4 changes: 2 additions & 2 deletions src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,9 @@ JL_DLLEXPORT void JL_NORETURN jl_throw_out_of_memory_error(void);
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;
size_t jl_array_nbytes(jl_array_t *a) JL_NOTSAFEPOINT;
void jl_gc_count_allocd(size_t sz) JL_NOTSAFEPOINT;
void jl_gc_count_freed(size_t sz) JL_NOTSAFEPOINT;
size_t jl_array_nbytes(jl_array_t *a) JL_NOTSAFEPOINT;
size_t memory_block_usable_size(void *mem) 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
Loading