Skip to content

Commit 8e0cba5

Browse files
committed
timing: add envvars for subsystem enable and verbose metadata
This commit adds the following environment variables: - `JULIA_TIMING_SUBSYSTEMS` which can be set to, e.g., "+INFERENCE,-GC,METHOD_MATCH" to enable INFERENCE and METHOD_MATCH and disable GC. - `JULIA_TIMING_METADATA_PRINT_LIMIT` which defaults to 10 and determines how many metadata items to add to a single timing zone before truncating This commit also includes other miscellaneous changes to incorporate review feedback.
1 parent d82a5b5 commit 8e0cba5

File tree

5 files changed

+96
-13
lines changed

5 files changed

+96
-13
lines changed

src/jitlayers.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ static jl_callptr_t _jl_compile_codeinst(
251251
size_t i = 0;
252252
for (auto &def : emitted) {
253253
jl_code_instance_t *this_code = def.first;
254-
if (i < 10)
254+
if (i < jl_timing_print_limit)
255255
jl_timing_show_func_sig(this_code->def->specTypes, JL_TIMING_CURRENT_BLOCK);
256256

257257
jl_llvm_functions_t decls = std::get<1>(def.second);
@@ -297,7 +297,7 @@ static jl_callptr_t _jl_compile_codeinst(
297297
fptr = addr;
298298
i++;
299299
}
300-
if (i > 10)
300+
if (i > jl_timing_print_limit)
301301
jl_timing_printf(JL_TIMING_CURRENT_BLOCK, "... <%d methods truncated>", i - 10);
302302

303303
uint64_t end_time = 0;

src/jlapi.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,8 +685,11 @@ static void rr_detach_teleport(void) {
685685
JL_DLLEXPORT int jl_repl_entrypoint(int argc, char *argv[])
686686
{
687687
#ifdef USE_TRACY
688-
if (getenv("WAIT_FOR_TRACY"))
689-
while (!TracyCIsConnected) ; // Wait for connection
688+
// Apply e.g. JULIA_TIMING_SUBSYSTEMS="+GC,-INFERENCE" and
689+
// JULIA_TIMING_METADATA_PRINT_LIMIT=20
690+
jl_timing_apply_env();
691+
if (getenv("JULIA_WAIT_FOR_TRACY"))
692+
while (!TracyCIsConnected) jl_cpu_pause(); // Wait for connection
690693
#endif
691694

692695
// no-op on Windows, note that the caller must have already converted

src/task.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,8 +1222,6 @@ CFI_NORETURN
12221222
_start_task();
12231223
}
12241224

1225-
const char* fiber = "task";
1226-
12271225
STATIC_OR_JS void NOINLINE JL_NORETURN _start_task(void)
12281226
{
12291227
CFI_NORETURN

src/timing.c

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ JL_DLLEXPORT uint64_t jl_timing_enable_mask = 0xFFFFFFFFFFFFFFFF;
3636
#endif
3737

3838
JL_DLLEXPORT uint64_t jl_timing_counts[(int)JL_TIMING_LAST] = {0};
39+
40+
// Used to as an item limit when several strings of metadata can
41+
// potentially be associated with a single timing zone.
42+
JL_DLLEXPORT uint32_t jl_timing_print_limit = 10;
43+
3944
const char *jl_timing_names[(int)JL_TIMING_LAST] =
4045
{
4146
#define X(name) #name
@@ -100,14 +105,16 @@ void jl_timing_block_enter_task(jl_task_t *ct, jl_ptls_t ptls, jl_timing_block_t
100105
jl_timing_block_t *jl_timing_block_exit_task(jl_task_t *ct, jl_ptls_t ptls)
101106
{
102107
#ifdef USE_TRACY
103-
// Tracy is fairly strict about not leaving a fiber that
104-
// hasn't been entered, which happens often when
105-
// connecting to a running Julia session.
108+
// Tracy is fairly strict about not leaving a fiber that hasn't
109+
// been entered, which happens often when connecting to a running
110+
// Julia session.
111+
//
112+
// Eventually, Tracy will support telling the server which fibers
113+
// are active upon connection, but until then we work around the
114+
// problem by not explicitly leaving the fiber at all.
106115
//
107-
// Eventually, Tracy will support telling the server that
108-
// which fibers are active upon connection, but until then
109-
// work around around the problem by just entering the new
110-
// fiber directly, which implicitly leaves any active fibers.
116+
// Later when we enter the new fiber directly, that will cause the
117+
// the active fiber to be left implicitly.
111118

112119
//TracyCFiberLeave;
113120
#endif
@@ -220,11 +227,71 @@ JL_DLLEXPORT int jl_timing_set_enable(const char *subsystem, uint8_t enabled)
220227
return -1;
221228
}
222229

230+
static void jl_timing_set_enable_from_env(void)
231+
{
232+
const char *env = getenv("JULIA_TIMING_SUBSYSTEMS");
233+
if (!env)
234+
return;
235+
236+
// Copy `env`, so that we can modify it
237+
size_t sz = strlen(env) + 1;
238+
char *env_copy = (char *)malloc(sz);
239+
memcpy(env_copy, env, sz);
240+
241+
char *subsystem = env_copy;
242+
char *ch = subsystem;
243+
uint8_t enable = 1;
244+
while (1) {
245+
// +SUBSYSTEM means enable, -SUBSYSTEM means disable
246+
if (*subsystem == '+' || *subsystem == '-')
247+
enable = (*subsystem++ == '+');
248+
249+
if (*ch == ',') {
250+
*ch++ = '\0';
251+
if ((*subsystem != '\0') && jl_timing_set_enable(subsystem, enable))
252+
fprintf(stderr, "warning: unable to configure timing for non-existent subsystem \"%s\"\n", subsystem);
253+
254+
subsystem = ch;
255+
enable = 1;
256+
}
257+
else if (*ch == '\0') {
258+
if ((*subsystem != '\0') && jl_timing_set_enable(subsystem, enable))
259+
fprintf(stderr, "warning: unable to configure timing for non-existent subsystem \"%s\"\n", subsystem);
260+
261+
break;
262+
}
263+
else ch++;
264+
}
265+
free(env_copy);
266+
}
267+
268+
static void jl_timing_set_print_limit_from_env(void)
269+
{
270+
const char *const env = getenv("JULIA_TIMING_METADATA_PRINT_LIMIT");
271+
if (!env)
272+
return;
273+
274+
char *endp;
275+
long value = strtol(env, &endp, 10);
276+
if (*endp == '\0' && value >= 0 && value <= UINT32_MAX)
277+
jl_timing_print_limit = (uint32_t)value;
278+
}
279+
280+
void jl_timing_apply_env(void)
281+
{
282+
// JULIA_TIMING_SUBSYSTEMS
283+
jl_timing_set_enable_from_env();
284+
285+
// JULIA_TIMING_METADATA_PRINT_LIMIT
286+
jl_timing_set_print_limit_from_env();
287+
}
288+
223289
#else
224290

225291
void jl_init_timing(void) { }
226292
void jl_destroy_timing(void) { }
227293
JL_DLLEXPORT int jl_timing_set_enable(const char *subsystem, uint8_t enabled) { return -1; }
294+
JL_DLLEXPORT uint32_t jl_timing_print_limit = 0;
228295

229296
#endif
230297

src/timing.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ void jl_destroy_timing(void) JL_NOTSAFEPOINT;
1616
// Returns -1 if no matching sub-system was found.
1717
int jl_timing_set_enable(const char *subsystem, uint8_t enabled);
1818

19+
// Check for environment vars "JULIA_TIMING_METADATA_PRINT_LIMIT" and
20+
// "JULIA_TIMING_SUBSYSTEMS" and if present apply these to the metadata
21+
// print limit and the timings enable mask, respectively.
22+
//
23+
// For example, to enable INFERENCE and METHOD_MATCH and disable GC:
24+
// JULIA_TIMING_SUBSYSTEMS="+INFERENCE,-GC,+METHOD_MATCH"
25+
//
26+
// For example, to increase the metadata item print limit from 10 to 20:
27+
// JULIA_TIMING_METADATA_PRINT_LIMIT=20
28+
void jl_timing_apply_env(void);
29+
30+
// Configurable item limit, runtime code should use this to limit printing
31+
// when adding potentially many items of metadata to a single timing zone.
32+
extern uint32_t jl_timing_print_limit;
33+
1934
#ifdef __cplusplus
2035
}
2136
#endif

0 commit comments

Comments
 (0)