Skip to content

Commit 34dcdf1

Browse files
committed
add a command line option to print out method invalidations to stderr or a file
1 parent 33823ec commit 34dcdf1

File tree

7 files changed

+54
-22
lines changed

7 files changed

+54
-22
lines changed

base/options.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct JLOptions
2828
can_inline::Int8
2929
polly::Int8
3030
trace_compile::Ptr{UInt8}
31+
trace_method_invalidations::Ptr{UInt8}
3132
fast_math::Int8
3233
worker::Int8
3334
cookie::Ptr{UInt8}

src/dump.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,8 +2294,6 @@ static void jl_insert_methods(jl_array_t *list)
22942294
}
22952295
}
22962296

2297-
extern int jl_debug_method_invalidation;
2298-
22992297
// verify that these edges intersect with the same methods as before
23002298
static void jl_verify_edges(jl_array_t *targets, jl_array_t **pvalids)
23012299
{
@@ -2386,9 +2384,9 @@ static void jl_insert_backedges(jl_array_t *list, jl_array_t *targets)
23862384
}
23872385
}
23882386
else {
2389-
if (jl_debug_method_invalidation) {
2390-
jl_static_show(JL_STDOUT, (jl_value_t*)caller);
2391-
jl_uv_puts(JL_STDOUT, "<<<\n", 4);
2387+
if (jl_options.trace_method_invalidations != NULL) {
2388+
jl_static_show(jl_s_method_invalidated, (jl_value_t*)caller);
2389+
jl_uv_puts(jl_s_method_invalidated, "<<<\n", 4);
23922390
}
23932391
}
23942392
}

src/gf.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,17 +1485,15 @@ static void update_max_args(jl_methtable_t *mt, jl_value_t *type)
14851485
mt->max_args = na;
14861486
}
14871487

1488-
JL_DLLEXPORT int jl_debug_method_invalidation = 0;
1489-
14901488
// recursively invalidate cached methods that had an edge to a replaced method
14911489
static void invalidate_method_instance(jl_method_instance_t *replaced, size_t max_world, int depth)
14921490
{
1493-
if (jl_debug_method_invalidation) {
1491+
if (jl_options.trace_method_invalidations != NULL) {
14941492
int d0 = depth;
14951493
while (d0-- > 0)
1496-
jl_uv_puts(JL_STDOUT, " ", 1);
1497-
jl_static_show(JL_STDOUT, (jl_value_t*)replaced);
1498-
jl_uv_puts(JL_STDOUT, "\n", 1);
1494+
jl_uv_puts(jl_s_method_invalidated, " ", 1);
1495+
jl_static_show(jl_s_method_invalidated, (jl_value_t*)replaced);
1496+
jl_uv_puts(jl_s_method_invalidated, "\n", 1);
14991497
}
15001498
if (!jl_is_method(replaced->def.method))
15011499
return; // shouldn't happen, but better to be safe
@@ -1622,10 +1620,10 @@ static int invalidate_mt_cache(jl_typemap_entry_t *oldentry, void *closure0)
16221620
}
16231621
}
16241622
if (intersects) {
1625-
if (jl_debug_method_invalidation) {
1626-
jl_uv_puts(JL_STDOUT, "-- ", 3);
1627-
jl_static_show(JL_STDOUT, (jl_value_t*)mi);
1628-
jl_uv_puts(JL_STDOUT, "\n", 1);
1623+
if (jl_options.trace_method_invalidations != NULL) {
1624+
jl_uv_puts(jl_s_method_invalidated, "-- ", 3);
1625+
jl_static_show(jl_s_method_invalidated, (jl_value_t*)mi);
1626+
jl_uv_puts(jl_s_method_invalidated, "\n", 1);
16291627
}
16301628
oldentry->max_world = env->max_world;
16311629
}
@@ -1772,12 +1770,12 @@ JL_DLLEXPORT void jl_method_table_insert(jl_methtable_t *mt, jl_method_t *method
17721770
}
17731771
}
17741772
}
1775-
if (invalidated && jl_debug_method_invalidation) {
1776-
jl_uv_puts(JL_STDOUT, ">> ", 3);
1777-
jl_static_show(JL_STDOUT, (jl_value_t*)method);
1778-
jl_uv_puts(JL_STDOUT, " ", 1);
1779-
jl_static_show(JL_STDOUT, (jl_value_t*)type);
1780-
jl_uv_puts(JL_STDOUT, "\n", 1);
1773+
if (invalidated && jl_options.trace_method_invalidations != NULL) {
1774+
jl_uv_puts(jl_s_method_invalidated, ">> ", 3);
1775+
jl_static_show(jl_s_method_invalidated, (jl_value_t*)method);
1776+
jl_uv_puts(jl_s_method_invalidated, " ", 1);
1777+
jl_static_show(jl_s_method_invalidated, (jl_value_t*)type);
1778+
jl_uv_puts(jl_s_method_invalidated, "\n", 1);
17811779
}
17821780
update_max_args(mt, type);
17831781
JL_UNLOCK(&mt->writelock);

src/init.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,24 @@ static void jl_resolve_sysimg_location(JL_IMAGE_SEARCH rel)
612612
}
613613
}
614614

615+
ios_t jl_f_method_invalidated;
616+
JL_STREAM* jl_s_method_invalidated;
617+
static void jl_init_method_invalidated_streams(void) {
618+
if (jl_options.trace_method_invalidations != NULL) {
619+
if (jl_s_method_invalidated == NULL) {
620+
const char* t = jl_options.trace_method_invalidations;
621+
if (!strncmp(t, "stderr", 6))
622+
jl_s_method_invalidated = JL_STDERR;
623+
else {
624+
if (ios_file(&jl_f_method_invalidated, t, 1, 1, 1, 1) == NULL)
625+
jl_errorf("cannot open method invalidation file \"%s\" for writing", t);
626+
jl_s_method_invalidated = (JL_STREAM*) &jl_f_method_invalidated;
627+
}
628+
}
629+
}
630+
}
631+
632+
615633
static void jl_set_io_wait(int v)
616634
{
617635
jl_ptls_t ptls = jl_get_ptls_states();
@@ -775,6 +793,8 @@ void _julia_init(JL_IMAGE_SEARCH rel)
775793
JL_GC_POP();
776794
}
777795

796+
jl_init_method_invalidated_streams();
797+
778798
if (jl_options.handle_signals == JL_OPTIONS_HANDLE_SIGNALS_ON)
779799
jl_install_sigint_handler();
780800
}

src/jloptions.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ jl_options_t jl_options = { 0, // quiet
5858
1, // can_inline
5959
JL_OPTIONS_POLLY_ON, // polly
6060
NULL, // trace_compile
61+
NULL, // trace_method_invalidations
6162
JL_OPTIONS_FAST_MATH_DEFAULT,
6263
0, // worker
6364
NULL, // cookie
@@ -161,8 +162,10 @@ static const char opts_hidden[] =
161162
" --output-bc name Generate LLVM bitcode (.bc)\n"
162163
" --output-asm name Generate an assembly file (.s)\n"
163164
" --output-incremental=no Generate an incremental output file (rather than complete)\n"
164-
" --trace-compile={stdout,stderr}\n"
165+
" --trace-compile={name,stderr}\n"
165166
" Print precompile statements for methods compiled during execution.\n\n"
167+
" --trace-method-invalidations={name,stderr}\n"
168+
" Print method invalidations.\n\n"
166169
;
167170

168171
JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
@@ -183,6 +186,7 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
183186
opt_inline,
184187
opt_polly,
185188
opt_trace_compile,
189+
opt_trace_method_invalidations,
186190
opt_math_mode,
187191
opt_worker,
188192
opt_bind_to,
@@ -244,6 +248,7 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
244248
{ "inline", required_argument, 0, opt_inline },
245249
{ "polly", required_argument, 0, opt_polly },
246250
{ "trace-compile", required_argument, 0, opt_trace_compile },
251+
{ "trace-method-invalidations", required_argument, 0, opt_trace_method_invalidations },
247252
{ "math-mode", required_argument, 0, opt_math_mode },
248253
{ "handle-signals", required_argument, 0, opt_handle_signals },
249254
// hidden command line options
@@ -617,6 +622,11 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
617622
if (!jl_options.trace_compile)
618623
jl_errorf("fatal error: failed to allocate memory: %s", strerror(errno));
619624
break;
625+
case opt_trace_method_invalidations:
626+
jl_options.trace_method_invalidations = strdup(optarg);
627+
if (!jl_options.trace_method_invalidations)
628+
jl_errorf("fatal error: failed to allocate memory: %s", strerror(errno));
629+
break;
620630
case opt_math_mode:
621631
if (!strcmp(optarg,"ieee"))
622632
jl_options.fast_math = JL_OPTIONS_FAST_MATH_OFF;

src/julia.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,7 @@ typedef struct {
19211921
int8_t can_inline;
19221922
int8_t polly;
19231923
const char *trace_compile;
1924+
const char *trace_method_invalidations;
19241925
int8_t fast_math;
19251926
int8_t worker;
19261927
const char *cookie;

src/julia_internal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,10 @@ JL_DLLEXPORT jl_value_t *jl_get_cfunction_trampoline(
905905
jl_unionall_t *env, jl_value_t **vals);
906906

907907

908+
// Method invalidation debug output
909+
extern ios_t jl_f_method_invalidated;
910+
extern JL_STREAM* jl_s_method_invalidated;
911+
908912
// Windows only
909913
#define JL_EXE_LIBNAME ((const char*)1)
910914
#define JL_DL_LIBNAME ((const char*)2)

0 commit comments

Comments
 (0)