Skip to content

Commit

Permalink
[gc] decruft -DMEMORY_DEBUG and DETAIL_MEMORY_DEBUG, add -D100
Browse files Browse the repository at this point in the history
Check -D100 instead of DETAIL_MEMORY_DEBUG when -DMEMORY_DEBUG is enabled.
This way we can run -D101 and -D1 to get verbose and non-verbose GC tracings.

Add new MEMORY_DEBUG_DETAIL_2 macro for fprintf helpers all over.
Move common panic_failed_allocation into gc_private.h, maybe the other
common gc memory funcs also to save space.
Print avail sysmem on startup with -D1.
GH #1108
  • Loading branch information
Reini Urban committed Oct 26, 2014
1 parent ca7dff2 commit d70c95b
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 213 deletions.
1 change: 1 addition & 0 deletions include/parrot/interpreter.h
Expand Up @@ -38,6 +38,7 @@ typedef enum {
PARROT_EVAL_DEBUG_FLAG = 0x20, /* create EVAL_n file */
PARROT_REG_DEBUG_FLAG = 0x40, /* fill I,N with garbage */
PARROT_CTX_DESTROY_DEBUG_FLAG = 0x80, /* ctx of a sub is gone */
PARROT_GC_DETAIL_DEBUG_FLAG = 0x100,
PARROT_ALL_DEBUG_FLAGS = 0xffff
} Parrot_debug_flags;
/* &end_gen */
Expand Down
37 changes: 15 additions & 22 deletions src/gc/alloc_memory.c
@@ -1,5 +1,5 @@
/*
Copyright (C) 2001-2011, Parrot Foundation.
Copyright (C) 2001-2014, Parrot Foundation.
=head1 NAME
Expand Down Expand Up @@ -27,6 +27,13 @@ setup function to initialize the memory pools.
#define PANIC_OUT_OF_MEM(size) panic_failed_allocation(__LINE__, (size))
#define PANIC_ZERO_ALLOCATION(func) panic_zero_byte_allocation(__LINE__, (func))

#ifndef DETAIL_MEMORY_DEBUG
# define MEMORY_DEBUG_DETAIL_2(s, a1, a2)
#else
# define MEMORY_DEBUG_DETAIL_2(s, a1, a2) \
fprintf(stderr, (s), (a1), (a2))
#endif

/* HEADERIZER HFILE: include/parrot/memory.h */
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
Expand Down Expand Up @@ -68,9 +75,7 @@ mem_sys_allocate(size_t size)
void * const ptr = malloc(size);
if (size==0)
PANIC_ZERO_ALLOCATION("mem_sys_allocate");
#ifdef DETAIL_MEMORY_DEBUG
fprintf(stderr, "Allocated %i at %p\n", size, ptr);
#endif
MEMORY_DEBUG_DETAIL_2("Allocated %i at %p\n", size, ptr);
if (!ptr)
PANIC_OUT_OF_MEM(size);
return ptr;
Expand Down Expand Up @@ -98,9 +103,7 @@ mem_sys_allocate_zeroed(size_t size)

if (size==0)
PANIC_ZERO_ALLOCATION("mem_sys_allocate_zeroed");
#ifdef DETAIL_MEMORY_DEBUG
fprintf(stderr, "Allocated %i at %p\n", size, ptr);
#endif
MEMORY_DEBUG_DETAIL_2("Allocated %i at %p\n", size, ptr);
if (!ptr)
PANIC_OUT_OF_MEM(size);
return ptr;
Expand Down Expand Up @@ -128,16 +131,12 @@ mem_sys_realloc(ARGFREE(void *from), size_t size)
void *ptr;
if (size==0)
PANIC_ZERO_ALLOCATION("mem_sys_realloc");
#ifdef DETAIL_MEMORY_DEBUG
fprintf(stderr, "Freed %p (realloc -- %i bytes)\n", from, size);
#endif
MEMORY_DEBUG_DETAIL_2("Freed %p (realloc -- %i bytes)\n", from, size);
if (from)
ptr = realloc(from, size);
else
ptr = calloc(1, size);
#ifdef DETAIL_MEMORY_DEBUG
fprintf(stderr, "Allocated %i at %p\n", size, ptr);
#endif
MEMORY_DEBUG_DETAIL_2("Allocated %i at %p\n", size, ptr);
if (!ptr)
PANIC_OUT_OF_MEM(size);
return ptr;
Expand Down Expand Up @@ -166,13 +165,9 @@ mem_sys_realloc_zeroed(ARGFREE(void *from), size_t size, size_t old_size)
void *ptr;
if (size==0)
PANIC_ZERO_ALLOCATION("mem_sys_realloc_zeroed");
#ifdef DETAIL_MEMORY_DEBUG
fprintf(stderr, "Freed %p (realloc -- %i bytes)\n", from, size);
#endif
MEMORY_DEBUG_DETAIL_2("Freed %p (realloc -- %i bytes)\n", from, size);
ptr = from ? realloc(from, size) : malloc(size);
#ifdef DETAIL_MEMORY_DEBUG
fprintf(stderr, "Allocated %i at %p\n", size, ptr);
#endif
MEMORY_DEBUG_DETAIL_2("Allocated %i at %p\n", size, ptr);
if (!ptr)
PANIC_OUT_OF_MEM(size);

Expand All @@ -197,9 +192,7 @@ void
mem_sys_free(ARGFREE(void *from))
{
ASSERT_ARGS(mem_sys_free)
#ifdef DETAIL_MEMORY_DEBUG
fprintf(stderr, "Freed %p\n", from);
#endif
MEMORY_DEBUG_DETAIL_2("Freed %p%s\n", from, "");
if (from)
free(from);
}
Expand Down
97 changes: 37 additions & 60 deletions src/gc/gc_gms.c
Expand Up @@ -110,8 +110,6 @@ TBD
#include "gc_private.h"
#include "fixed_allocator.h"

#define PANIC_OUT_OF_MEM(size) failed_allocation(__LINE__, (size))

#ifdef THREAD_DEBUG
# define PARROT_GC_ASSERT_INTERP(pmc, interp) \
PARROT_ASSERT((pmc) == NULL || (pmc)->orig_interp == (interp))
Expand Down Expand Up @@ -217,9 +215,6 @@ typedef void (*sweep_cb)(PARROT_INTERP, PObj *obj);
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */

PARROT_DOES_NOT_RETURN
static void failed_allocation(unsigned int line, size_t size);

PARROT_MALLOC
PARROT_CAN_RETURN_NULL
static Parrot_Buffer* gc_gms_allocate_buffer_header(PARROT_INTERP,
Expand All @@ -238,12 +233,14 @@ static void* gc_gms_allocate_fixed_size_storage(PARROT_INTERP, size_t size)

PARROT_MALLOC
PARROT_CAN_RETURN_NULL
static void * gc_gms_allocate_memory_chunk(PARROT_INTERP, size_t size);
static void * gc_gms_allocate_memory_chunk(PARROT_INTERP, size_t size)
__attribute__nonnull__(1);

PARROT_MALLOC
PARROT_CAN_RETURN_NULL
static void * gc_gms_allocate_memory_chunk_zeroed(PARROT_INTERP,
size_t size);
size_t size)
__attribute__nonnull__(1);

PARROT_MALLOC
PARROT_CAN_RETURN_NULL
Expand Down Expand Up @@ -315,7 +312,9 @@ static void gc_gms_free_fixed_size_storage(PARROT_INTERP,
__attribute__nonnull__(3)
FUNC_MODIFIES(*data);

static void gc_gms_free_memory_chunk(PARROT_INTERP, ARGFREE(void *data));
static void gc_gms_free_memory_chunk(PARROT_INTERP, ARGFREE(void *data))
__attribute__nonnull__(1);

static void gc_gms_free_pmc_attributes(PARROT_INTERP, ARGMOD(PMC *pmc))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
Expand Down Expand Up @@ -389,6 +388,7 @@ static void gc_gms_pmc_get_youngest_generation(PARROT_INTERP,
static void gc_gms_pmc_needs_early_collection(PARROT_INTERP, PMC *pmc)
__attribute__nonnull__(1);

PARROT_INLINE
static void gc_gms_print_stats(PARROT_INTERP, ARGIN(const char* header))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
Expand Down Expand Up @@ -416,7 +416,8 @@ PARROT_MALLOC
PARROT_CAN_RETURN_NULL
static void * gc_gms_reallocate_memory_chunk(PARROT_INTERP,
ARGFREE(void *from),
size_t size);
size_t size)
__attribute__nonnull__(1);

PARROT_MALLOC
PARROT_CAN_RETURN_NULL
Expand Down Expand Up @@ -475,7 +476,6 @@ static void gc_gms_write_barrier(PARROT_INTERP, ARGMOD(PMC *pmc))
FUNC_MODIFIES(*pmc);

static int gen2flags(int gen);
#define ASSERT_ARGS_failed_allocation __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_gc_gms_allocate_buffer_header __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_allocate_buffer_storage \
Expand All @@ -485,9 +485,11 @@ static int gen2flags(int gen);
#define ASSERT_ARGS_gc_gms_allocate_fixed_size_storage \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_allocate_memory_chunk __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_gc_gms_allocate_memory_chunk __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_allocate_memory_chunk_zeroed \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_allocate_pmc_attributes \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
Expand Down Expand Up @@ -529,7 +531,8 @@ static int gen2flags(int gen);
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(data))
#define ASSERT_ARGS_gc_gms_free_memory_chunk __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_gc_gms_free_memory_chunk __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_free_pmc_attributes __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(pmc))
Expand Down Expand Up @@ -590,7 +593,8 @@ static int gen2flags(int gen);
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(str))
#define ASSERT_ARGS_gc_gms_reallocate_memory_chunk \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_gc_gms_reallocate_memory_chunk_zeroed \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_gc_gms_reallocate_string_storage \
Expand Down Expand Up @@ -1935,22 +1939,18 @@ size)>
=item C<static void gc_gms_free_memory_chunk(PARROT_INTERP, void *data)>
=item C<static void failed_allocation(unsigned int line, size_t size)>
TODO Write docu.
*/

PARROT_MALLOC
PARROT_CAN_RETURN_NULL
static void *
gc_gms_allocate_memory_chunk(SHIM_INTERP, size_t size)
gc_gms_allocate_memory_chunk(PARROT_INTERP, size_t size)
{
ASSERT_ARGS(gc_gms_allocate_memory_chunk)
void * const ptr = malloc(size);
#if defined(DETAIL_MEMORY_DEBUG)
fprintf(stderr, "Allocated %i at %p\n", size, ptr);
#endif
MEMORY_DEBUG_DETAIL_2("Allocated %ld at %p\n", size, ptr);
if (!ptr && size)
PANIC_OUT_OF_MEM(size);
return ptr;
Expand All @@ -1959,20 +1959,16 @@ gc_gms_allocate_memory_chunk(SHIM_INTERP, size_t size)
PARROT_MALLOC
PARROT_CAN_RETURN_NULL
static void *
gc_gms_reallocate_memory_chunk(SHIM_INTERP, ARGFREE(void *from), size_t size)
gc_gms_reallocate_memory_chunk(PARROT_INTERP, ARGFREE(void *from), size_t size)
{
ASSERT_ARGS(gc_gms_reallocate_memory_chunk)
void *ptr;
#if defined(DETAIL_MEMORY_DEBUG)
fprintf(stderr, "Freed %p (realloc -- %i bytes)\n", from, size);
#endif
MEMORY_DEBUG_DETAIL_2("Freed %p (realloc -- %ld bytes)\n", from, size);
if (from)
ptr = realloc(from, size);
else
ptr = calloc(1, size);
#if defined(DETAIL_MEMORY_DEBUG)
fprintf(stderr, "Allocated %i at %p\n", size, ptr);
#endif
MEMORY_DEBUG_DETAIL_2("Allocated %ld at %p\n", size, ptr);
if (!ptr && size)
PANIC_OUT_OF_MEM(size);
return ptr;
Expand All @@ -1981,13 +1977,11 @@ gc_gms_reallocate_memory_chunk(SHIM_INTERP, ARGFREE(void *from), size_t size)
PARROT_MALLOC
PARROT_CAN_RETURN_NULL
static void *
gc_gms_allocate_memory_chunk_zeroed(SHIM_INTERP, size_t size)
gc_gms_allocate_memory_chunk_zeroed(PARROT_INTERP, size_t size)
{
ASSERT_ARGS(gc_gms_allocate_memory_chunk_zeroed)
void * const ptr = calloc(1, size);
#if defined(DETAIL_MEMORY_DEBUG)
fprintf(stderr, "Allocated %i at %p\n", size, ptr);
#endif
MEMORY_DEBUG_DETAIL_2("Allocated %ld at %p\n", size, ptr);
if (!ptr && size)
PANIC_OUT_OF_MEM(size);
return ptr;
Expand All @@ -2007,26 +2001,14 @@ gc_gms_reallocate_memory_chunk_zeroed(SHIM_INTERP, ARGFREE(void *data),
}

static void
gc_gms_free_memory_chunk(SHIM_INTERP, ARGFREE(void *data))
gc_gms_free_memory_chunk(PARROT_INTERP, ARGFREE(void *data))
{
ASSERT_ARGS(gc_gms_free_memory_chunk)
#if defined(DETAIL_MEMORY_DEBUG)
fprintf(stderr, "Freed %p\n", data);
#endif
MEMORY_DEBUG_DETAIL_2("Freed %p%s\n", data, "");
if (data)
free(data);
}

PARROT_DOES_NOT_RETURN
static void
failed_allocation(unsigned int line, size_t size)
{
ASSERT_ARGS(failed_allocation)
fprintf(stderr, "Failed allocation of %lu bytes\n", (unsigned long)size);
Parrot_x_panic_and_exit(NULL, "Out of mem", __FILE__, line);
}


/*
=item C<static void gc_gms_pmc_needs_early_collection(PARROT_INTERP, PMC *pmc)>
Expand Down Expand Up @@ -2086,8 +2068,9 @@ gc_gms_write_barrier(PARROT_INTERP, ARGMOD(PMC *pmc))
PARROT_GC_ASSERT_INTERP(pmc, interp);

#ifdef MEMORY_DEBUG
fprintf(stderr, "GC WB pmc %-21s gen %ld at %p - %p\n",
pmc->vtable->whoami->strstart, gen, pmc, item->ptr);
if (Interp_debug_TEST(interp, PARROT_MEM_STAT_DEBUG_FLAG))
fprintf(stderr, "GC WB pmc %-21s gen %ld at %p - %p\n",
pmc->vtable->whoami->strstart, gen, pmc, item->ptr);
#endif
Parrot_pa_remove(interp, self->objects[gen], item->ptr);
item->ptr = Parrot_pa_insert(self->dirty_list, item);
Expand Down Expand Up @@ -2275,12 +2258,8 @@ gc_gms_check_sanity(PARROT_INTERP)
PMC *pmc = &(((pmc_alloc_struct*)ptr)->pmc);
const size_t gen = POBJ2GEN(pmc);
PARROT_GC_ASSERT_INTERP(pmc, interp);

# ifdef DETAIL_MEMORY_DEBUG
fprintf(stderr, "GC live pmc %-21s gen %ld at %p\n",
pmc->vtable->whoami->strstart, gen, pmc);
# endif

MEMORY_DEBUG_DETAIL_3("GC live pmc %-21s gen %ld at %p\n",
pmc->vtable->whoami->strstart, gen, pmc);
PARROT_ASSERT((gen == i)
|| !"Object from wrong generation");

Expand All @@ -2297,20 +2276,16 @@ gc_gms_check_sanity(PARROT_INTERP)
POINTER_ARRAY_ITER(self->dirty_list,
PMC *pmc = &(((pmc_alloc_struct*)ptr)->pmc);
PARROT_GC_ASSERT_INTERP(pmc, interp);
# ifdef DETAIL_MEMORY_DEBUG
fprintf(stderr, "GC dirty pmc %-21s at %p\n",
MEMORY_DEBUG_DETAIL_2("GC dirty pmc %-21s at %p\n",
pmc->vtable->whoami->strstart, pmc);
# endif
PARROT_ASSERT(PObj_GC_on_dirty_list_TEST(pmc)
|| !"Object in dirty_list without dirty_flag"););

POINTER_ARRAY_ITER(self->work_list,
PMC *pmc = &(((pmc_alloc_struct*)ptr)->pmc);
PARROT_GC_ASSERT_INTERP(pmc, interp);
# ifdef DETAIL_MEMORY_DEBUG
fprintf(stderr, "GC work pmc %-21s at %p\n",
MEMORY_DEBUG_DETAIL_2("GC work pmc %-21s at %p\n",
pmc->vtable->whoami->strstart, pmc);
# endif
PARROT_ASSERT(!PObj_GC_on_dirty_list_TEST(pmc)
|| !"Dirty object in work_list"););
#endif
Expand Down Expand Up @@ -2374,13 +2349,15 @@ gc_gms_print_stats_always(PARROT_INTERP, ARGIN(const char* header))

}

PARROT_INLINE
static void
gc_gms_print_stats(PARROT_INTERP, ARGIN(const char* header))
{
ASSERT_ARGS(gc_gms_print_stats)

#ifdef MEMORY_DEBUG
gc_gms_print_stats_always(interp, header);
if (Interp_debug_TEST(interp, PARROT_MEM_STAT_DEBUG_FLAG))
gc_gms_print_stats_always(interp, header);
#else
UNUSED(interp);
UNUSED(header);
Expand Down

0 comments on commit d70c95b

Please sign in to comment.