Skip to content

Commit b033c0a

Browse files
committed
Add batching for counted functions
1 parent 01d6e39 commit b033c0a

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

src/gc.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,12 +1130,13 @@ static void combine_thread_gc_counts(jl_gc_num_t *dest) JL_NOTSAFEPOINT
11301130
jl_ptls_t ptls = gc_all_tls_states[i];
11311131
if (ptls) {
11321132
dest->allocd += (jl_atomic_load_relaxed(&ptls->gc_num.allocd) + gc_num.interval);
1133-
dest->freed += jl_atomic_load_relaxed(&ptls->gc_num.freed);
11341133
dest->malloc += jl_atomic_load_relaxed(&ptls->gc_num.malloc);
11351134
dest->realloc += jl_atomic_load_relaxed(&ptls->gc_num.realloc);
11361135
dest->poolalloc += jl_atomic_load_relaxed(&ptls->gc_num.poolalloc);
11371136
dest->bigalloc += jl_atomic_load_relaxed(&ptls->gc_num.bigalloc);
1138-
dest->freecall += jl_atomic_load_relaxed(&ptls->gc_num.freecall);
1137+
uint64_t alloc_acc = jl_atomic_load_relaxed(&ptls->gc_num.alloc_acc);
1138+
uint64_t free_acc = jl_atomic_load_relaxed(&ptls->gc_num.free_acc);
1139+
jl_atomic_store_relaxed(&gc_heap_stats.heap_size, alloc_acc - free_acc + jl_atomic_load_relaxed(&gc_heap_stats.heap_size));
11391140
}
11401141
}
11411142
}
@@ -3601,7 +3602,13 @@ JL_DLLEXPORT void *jl_gc_counted_malloc(size_t sz)
36013602
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + sz);
36023603
jl_atomic_store_relaxed(&ptls->gc_num.malloc,
36033604
jl_atomic_load_relaxed(&ptls->gc_num.malloc) + 1);
3604-
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, sz);
3605+
uint64_t alloc_acc = jl_atomic_load_relaxed(&ptls->gc_num.alloc_acc);
3606+
if (alloc_acc + sz < 16*1024)
3607+
jl_atomic_store_relaxed(&ptls->gc_num.alloc_acc, alloc_acc + sz);
3608+
else {
3609+
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, alloc_acc + sz);
3610+
jl_atomic_store_relaxed(&ptls->gc_num.alloc_acc, 0);
3611+
}
36053612
}
36063613
return malloc(sz);
36073614
}
@@ -3617,7 +3624,13 @@ JL_DLLEXPORT void *jl_gc_counted_calloc(size_t nm, size_t sz)
36173624
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + nm*sz);
36183625
jl_atomic_store_relaxed(&ptls->gc_num.malloc,
36193626
jl_atomic_load_relaxed(&ptls->gc_num.malloc) + 1);
3620-
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, nm*sz);
3627+
uint64_t alloc_acc = jl_atomic_load_relaxed(&ptls->gc_num.alloc_acc);
3628+
if (alloc_acc + sz < 16*1024)
3629+
jl_atomic_store_relaxed(&ptls->gc_num.alloc_acc, alloc_acc + sz * nm);
3630+
else {
3631+
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, alloc_acc + sz * nm);
3632+
jl_atomic_store_relaxed(&ptls->gc_num.alloc_acc, 0);
3633+
}
36213634
}
36223635
return calloc(nm, sz);
36233636
}
@@ -3629,11 +3642,13 @@ JL_DLLEXPORT void jl_gc_counted_free_with_size(void *p, size_t sz)
36293642
free(p);
36303643
if (pgcstack != NULL && ct->world_age) {
36313644
jl_ptls_t ptls = ct->ptls;
3632-
jl_atomic_store_relaxed(&ptls->gc_num.freed,
3633-
jl_atomic_load_relaxed(&ptls->gc_num.freed) + sz);
3634-
jl_atomic_store_relaxed(&ptls->gc_num.freecall,
3635-
jl_atomic_load_relaxed(&ptls->gc_num.freecall) + 1);
3636-
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, -sz);
3645+
uint64_t free_acc = jl_atomic_load_relaxed(&ptls->gc_num.free_acc);
3646+
if (free_acc + sz < 16*1024)
3647+
jl_atomic_store_relaxed(&ptls->gc_num.free_acc, free_acc + sz);
3648+
else {
3649+
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, -(free_acc + sz));
3650+
jl_atomic_store_relaxed(&ptls->gc_num.free_acc, 0);
3651+
}
36373652
}
36383653
}
36393654

src/julia_threads.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,12 @@ typedef struct {
130130

131131
typedef struct {
132132
_Atomic(int64_t) allocd;
133-
_Atomic(int64_t) freed;
134133
_Atomic(uint64_t) malloc;
135134
_Atomic(uint64_t) realloc;
136135
_Atomic(uint64_t) poolalloc;
137136
_Atomic(uint64_t) bigalloc;
138-
_Atomic(uint64_t) freecall;
137+
_Atomic(int64_t) free_acc;
138+
_Atomic(uint64_t) alloc_acc;
139139
} jl_thread_gc_num_t;
140140

141141
typedef struct {

0 commit comments

Comments
 (0)