Skip to content

Commit

Permalink
added HL_GC_PROFILE_MEM
Browse files Browse the repository at this point in the history
  • Loading branch information
ncannasse committed Oct 18, 2023
1 parent 2bad7f4 commit b02120b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,24 @@ static void gc_flush_empty_pages() {
}
}

static int64 gc_allocator_private_memory() {
return free_lists_size;
}

static int gc_free_memory( gc_pheader *ph ) {
gc_allocator_page_data *p = &ph->alloc;
if( p->need_flush )
flush_free_list(ph);
gc_freelist *fl = &p->free;
int k;
int free = 0;
for(k=fl->current;k<fl->count;k++) {
gc_fl *c = GET_FL(fl,k);
free += c->count * p->block_size;
}
return free;
}

#ifdef GC_DEBUG
static void gc_clear_unmarked_mem() {
int i;
Expand Down
38 changes: 38 additions & 0 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ struct _gc_pheader {
#define GC_DUMP_MEM 2
#define GC_NO_THREADS 4
#define GC_FORCE_MAJOR 8
#define GC_PROFILE_MEM 16

static int gc_flags = 0;
static gc_pheader *gc_level1_null[1<<GC_LEVEL1_BITS] = {NULL};
Expand All @@ -186,6 +187,7 @@ static struct {
int64 last_mark_allocs;
int64 pages_total_memory;
int64 allocation_count;
int64 free_memory;
int pages_count;
int pages_allocated;
int pages_blocks;
Expand Down Expand Up @@ -846,7 +848,41 @@ static void gc_mark() {
gc_allocator_after_mark();
}

static void count_free_memory( gc_pheader *page, int size ) {
gc_stats.free_memory += gc_free_memory(page);
}

static void gc_major() {

if( gc_flags & GC_PROFILE_MEM ) {
double gc_mem = gc_stats.mark_bytes;
int i;
gc_mem += gc_allocator_private_memory();
gc_mem += global_mark_stack.size * sizeof(void*);
for(i=0;i<gc_mark_threads;i++) {
gc_mthread *t = &mark_threads[i];
gc_mem += t->stack.size * sizeof(void*);
}
int pages = gc_stats.pages_count;
gc_pheader *p = gc_free_pheaders;
while( p ) {
pages++;
p = p->next_page;
}
gc_mem += sizeof(gc_pheader) * pages;
gc_mem += sizeof(void*) * gc_roots_max;
gc_mem += (sizeof(void*) + sizeof(hl_thread_info)) * gc_threads.count;
for(i=0;i<(1<<GC_LEVEL0_BITS);i++) {
void *v = hl_gc_page_map[i];
if( v != gc_level1_null )
gc_mem += sizeof(void*) * (1<<GC_LEVEL1_BITS);
}
gc_mem += gc_stats.pages_total_memory;
gc_stats.free_memory = 0;
gc_iter_pages(count_free_memory);
printf("GC-PROFILE-MEM %.2fMB total, %.2f%% free %.2f%% gc\n", gc_mem / (1024.0 * 1024.0), (gc_stats.free_memory * 100.0 / gc_mem), (gc_mem - gc_stats.pages_total_memory) * 100.0 / gc_mem);
}

int time = TIMESTAMP(), dt;
gc_stats.last_mark = gc_stats.total_allocated;
gc_stats.last_mark_allocs = gc_stats.allocation_count;
Expand Down Expand Up @@ -931,6 +967,8 @@ static void hl_gc_init() {
# ifndef HL_CONSOLE
if( getenv("HL_GC_PROFILE") )
gc_flags |= GC_PROFILE;
if( getenv("HL_GC_PROFILE_MEM") )
gc_flags |= GC_PROFILE_MEM;
if( getenv("HL_DUMP_MEMORY") )
gc_flags |= GC_DUMP_MEM;
# endif
Expand Down

0 comments on commit b02120b

Please sign in to comment.