Skip to content

Commit

Permalink
Merge pull request JuliaLang#14923 from JuliaLang/yyc/gc/debug-sleep
Browse files Browse the repository at this point in the history
GC debugging tweaks
  • Loading branch information
vtjnash committed Feb 4, 2016
2 parents 12e127f + b60e730 commit c200b4c
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 16 deletions.
3 changes: 3 additions & 0 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ JL_DLLEXPORT void __stack_chk_fail()
{
/* put your panic function or similar in here */
fprintf(stderr, "fatal error: stack corruption detected\n");
gc_debug_critical_error();
abort(); // end with abort, since the compiler destroyed the stack upon entry to this function, there's no going back now
}
#endif
Expand Down Expand Up @@ -895,6 +896,7 @@ static Function *to_function(jl_lambda_info_t *li, jl_cyclectx_t *cyclectx)
(specf && verifyFunction(*specf, PrintMessageAction))) {
f->dump();
if (specf) specf->dump();
gc_debug_critical_error();
abort();
}
#endif
Expand Down Expand Up @@ -992,6 +994,7 @@ static void writeRecoveryFile(llvm::Module *mod)
raw_fd_ostream OS(fname_ref, err, sys::fs::F_None);
WriteBitcodeToFile(mod,OS);
OS.flush();
gc_debug_critical_error();
abort();
}
#endif
Expand Down
40 changes: 34 additions & 6 deletions src/gc-debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ static void gc_verify(void)
restore();
gc_verify_track();
gc_debug_print_status();
gc_debug_critical_error();
abort();
}

Expand All @@ -286,13 +287,15 @@ typedef struct {

typedef struct {
int sweep_mask;
int wait_for_debugger;
jl_alloc_num_t pool;
jl_alloc_num_t other;
jl_alloc_num_t print;
} jl_gc_debug_env_t;

JL_DLLEXPORT jl_gc_debug_env_t jl_gc_debug_env = {
GC_MARKED_NOESC,
0,
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
Expand All @@ -302,7 +305,7 @@ static void gc_debug_alloc_init(jl_alloc_num_t *num, const char *name)
{
// Not very generic and robust but good enough for a debug option
char buff[128];
sprintf(buff, "JL_GC_ALLOC_%s", name);
sprintf(buff, "JULIA_GC_ALLOC_%s", name);
char *env = getenv(buff);
if (!env)
return;
Expand All @@ -324,10 +327,11 @@ static char *gc_stack_lo;
static void gc_debug_init(void)
{
gc_stack_lo = (char*)gc_get_stack_ptr();
char *env = getenv("JL_GC_NO_GENERATIONAL");
if (env && strcmp(env, "0") != 0) {
char *env = getenv("JULIA_GC_NO_GENERATIONAL");
if (env && strcmp(env, "0") != 0)
jl_gc_debug_env.sweep_mask = GC_MARKED;
}
env = getenv("JULIA_GC_WAIT_FOR_DEBUGGER");
jl_gc_debug_env.wait_for_debugger = env && strcmp(env, "0") != 0;
gc_debug_alloc_init(&jl_gc_debug_env.pool, "POOL");
gc_debug_alloc_init(&jl_gc_debug_env.other, "OTHER");
gc_debug_alloc_init(&jl_gc_debug_env.print, "PRINT");
Expand All @@ -349,8 +353,18 @@ void gc_debug_print_status(void)
uint64_t other_count = jl_gc_debug_env.other.num;
jl_safe_printf("Allocations: %" PRIu64 " "
"(Pool: %" PRIu64 "; Other: %" PRIu64 "); GC: %d\n",
pool_count + other_count, pool_count, other_count,
n_pause);
pool_count + other_count, pool_count, other_count, n_pause);
}

void gc_debug_critical_error(void)
{
gc_debug_print_status();
if (!jl_gc_debug_env.wait_for_debugger)
return;
jl_safe_printf("Waiting for debugger to attach\n");
while (1) {
sleep(1000);
}
}

static inline void gc_debug_print(void)
Expand Down Expand Up @@ -403,6 +417,20 @@ static inline int gc_debug_check_pool(void)
return 0;
}

void gc_debug_print_status(void)
{
// May not be accurate but should be helpful enough
uint64_t pool_count = gc_num.poolalloc;
uint64_t big_count = gc_num.bigalloc;
jl_safe_printf("Allocations: %" PRIu64 " "
"(Pool: %" PRIu64 "; Big: %" PRIu64 "); GC: %d\n",
pool_count + big_count, pool_count, big_count, n_pause);
}

void gc_debug_critical_error(void)
{
}

static inline void gc_debug_print(void)
{
}
Expand Down
4 changes: 4 additions & 0 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ void jl_gc_signal_init(void)
#endif
if (jl_gc_signal_page == NULL) {
jl_printf(JL_STDERR, "could not allocate GC synchronization page\n");
gc_debug_critical_error();
abort();
}
}
Expand Down Expand Up @@ -856,6 +857,7 @@ static NOINLINE void *malloc_page(void)
#endif
if (mem == NULL) {
jl_printf(JL_STDERR, "could not allocate pools\n");
gc_debug_critical_error();
abort();
}
if (GC_PAGE_SZ > jl_page_size) {
Expand Down Expand Up @@ -886,6 +888,7 @@ static NOINLINE void *malloc_page(void)
}
if (region_i >= REGION_COUNT) {
jl_printf(JL_STDERR, "increase REGION_COUNT or allocate less memory\n");
gc_debug_critical_error();
abort();
}
if (regions_lb[region_i] < i)
Expand Down Expand Up @@ -1951,6 +1954,7 @@ static int push_root(jl_value_t *v, int d, int bits)
jl_printf(JL_STDOUT, "GC error (probable corruption) :\n");
gc_debug_print_status();
jl_(vt);
gc_debug_critical_error();
abort();
}

Expand Down
1 change: 1 addition & 0 deletions src/gf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,7 @@ void JL_NORETURN jl_no_method_error_bare(jl_function_t *f, jl_value_t *args)
jl_printf((JL_STREAM*)STDERR_FILENO, "A method error occurred before the base module was defined. Aborting...\n");
jl_static_show((JL_STREAM*)STDERR_FILENO,(jl_value_t*)f); jl_printf((JL_STREAM*)STDERR_FILENO,"\n");
jl_static_show((JL_STREAM*)STDERR_FILENO,args); jl_printf((JL_STREAM*)STDERR_FILENO,"\n");
gc_debug_critical_error();
abort();
}
// not reached
Expand Down
1 change: 1 addition & 0 deletions src/intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ JL_CALLABLE(jl_f_intrinsic_call)
default:
assert(0 && "unexpected number of arguments to an intrinsic function");
}
gc_debug_critical_error();
abort();
}

Expand Down
11 changes: 7 additions & 4 deletions src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

#include <options.h>
#include <uv.h>
#ifndef _MSC_VER
#include <unistd.h>
#include <sched.h>
#else
#define sleep(x) Sleep(1000*x)
#endif

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -97,11 +103,8 @@ static inline void jl_gc_wb_buf(void *parent, void *bufptr) // parent isa jl_val
gc_setmark_buf(bufptr, jl_astaggedvalue(parent)->gc_bits);
}

#ifdef GC_DEBUG_ENV
void gc_debug_print_status(void);
#else
#define gc_debug_print_status()
#endif
void gc_debug_critical_error(void);
#if defined(GC_FINAL_STATS)
void jl_print_gc_stats(JL_STREAM *s);
#else
Expand Down
1 change: 1 addition & 0 deletions src/signal-handling.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ static void jl_critical_error(int sig, bt_context_t context, intptr_t *bt_data,
for(size_t i=0; i < n; i++)
jl_gdblookup(bt_data[i]);
gc_debug_print_status();
gc_debug_critical_error();
}

///////////////////////
Expand Down
1 change: 1 addition & 0 deletions src/signals-win.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ static DWORD WINAPI profile_bt( LPVOID lparam )
bt_size_cur++;
if ((DWORD)-1 == ResumeThread(hMainThread)) {
fputs("failed to resume main thread! aborting.",stderr);
gc_debug_critical_error();
abort();
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ static void JL_NORETURN finish_task(jl_task_t *t, jl_value_t *resultval)
// For now, only thread 0 runs the task scheduler.
// The others return to the thread loop
jl_switchto(jl_root_task, jl_nothing);
gc_debug_critical_error();
abort();
}
if (task_done_hook_func == NULL) {
Expand All @@ -219,6 +220,7 @@ static void JL_NORETURN finish_task(jl_task_t *t, jl_value_t *resultval)
jl_value_t *args[2] = {task_done_hook_func, (jl_value_t*)t};
jl_apply(args, 2);
}
gc_debug_critical_error();
abort();
}

Expand Down Expand Up @@ -253,6 +255,7 @@ static void NOINLINE JL_NORETURN start_task(void)
}
}
finish_task(t, res);
gc_debug_critical_error();
abort();
}

Expand Down
6 changes: 0 additions & 6 deletions src/threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _MSC_VER
#include <unistd.h>
#include <sched.h>
#else
#define sleep(x) Sleep(1000*x)
#endif

#include "julia.h"
#include "julia_internal.h"
Expand Down

0 comments on commit c200b4c

Please sign in to comment.