Skip to content

Commit a6f76be

Browse files
function tidy & change handling to two separate categories
1 parent 7abaab3 commit a6f76be

File tree

4 files changed

+34
-24
lines changed

4 files changed

+34
-24
lines changed

base/timing.jl

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,20 @@ function gc_alloc_count(diff::GC_Diff)
5555
diff.malloc + diff.realloc + diff.poolalloc + diff.bigalloc
5656
end
5757

58-
# cumulative total time spent on compilation, in nanoseconds
59-
function cumulative_compile_time_ns_before()
60-
comp = ccall(:jl_cumulative_compile_time_ns_before, UInt64, ())
61-
recomp = ccall(:jl_cumulative_recompile_time_ns_before, UInt64, ())
58+
# cumulative total time spent on compilation and recompilation, in nanoseconds
59+
function cumulative_compile_time_ns()
60+
comp = ccall(:jl_cumulative_compile_time_ns, UInt64, ())
61+
recomp = ccall(:jl_cumulative_recompile_time_ns, UInt64, ())
6262
return comp, recomp
6363
end
64-
function cumulative_compile_time_ns_after()
65-
comp = ccall(:jl_cumulative_compile_time_ns_after, UInt64, ())
66-
recomp = ccall(:jl_cumulative_recompile_time_ns_before, UInt64, ())
67-
return comp, recomp
64+
65+
function cumulative_compile_timing(b::Bool)
66+
if b
67+
ccall(:jl_cumulative_compile_timing_enable, Cvoid, ())
68+
else
69+
ccall(:jl_cumulative_compile_timing_disable, Cvoid, ())
70+
end
71+
return
6872
end
6973

7074
# total time spend in garbage collection, in nanoseconds
@@ -127,7 +131,7 @@ function time_print(elapsedtime, bytes=0, gctime=0, allocs=0, compile_time=0, re
127131
str = sprint() do io
128132
_lpad && print(io, length(timestr) < 10 ? (" "^(10 - length(timestr))) : "")
129133
print(io, timestr, " seconds")
130-
parens = bytes != 0 || allocs != 0 || gctime > 0 || compile_time > 0
134+
parens = bytes != 0 || allocs != 0 || gctime > 0 || compile_time > 0 || recompile_time > 0
131135
parens && print(io, " (")
132136
if bytes != 0 || allocs != 0
133137
allocs, ma = prettyprint_getunits(allocs, length(_cnt_units), Int64(1000))
@@ -151,7 +155,10 @@ function time_print(elapsedtime, bytes=0, gctime=0, allocs=0, compile_time=0, re
151155
print(io, Ryu.writefixed(Float64(100*compile_time/elapsedtime), 2), "% compilation time")
152156
end
153157
if recompile_time > 0
154-
print(io, " of which ", Ryu.writefixed(Float64(100*recompile_time/compile_time), 0), "% was recompilation")
158+
if bytes != 0 || allocs != 0 || gctime > 0 || compile_time > 0
159+
print(io, ", ")
160+
end
161+
print(io, Ryu.writefixed(Float64(100*recompile_time/elapsedtime), 2), "% recompilation time")
155162
end
156163
parens && print(io, ")")
157164
end
@@ -251,10 +258,12 @@ macro time(msg, ex)
251258
Experimental.@force_compile
252259
local stats = gc_num()
253260
local elapsedtime = time_ns()
254-
local compile_elapsedtimes = cumulative_compile_time_ns_before()
261+
cumulative_compile_timing(true)
262+
local compile_elapsedtimes = cumulative_compile_time_ns()
255263
local val = @__tryfinally($(esc(ex)),
256264
(elapsedtime = time_ns() - elapsedtime;
257-
compile_elapsedtimes = cumulative_compile_time_ns_after() .- compile_elapsedtimes)
265+
cumulative_compile_timing(false);
266+
compile_elapsedtimes = cumulative_compile_time_ns() .- compile_elapsedtimes)
258267
)
259268
local diff = GC_Diff(gc_num(), stats)
260269
local _msg = $(esc(msg))
@@ -333,10 +342,10 @@ macro timev(msg, ex)
333342
Experimental.@force_compile
334343
local stats = gc_num()
335344
local elapsedtime = time_ns()
336-
local compile_elapsedtimes = cumulative_compile_time_ns_before()
345+
local compile_elapsedtimes = cumulative_compile_time_ns()
337346
local val = @__tryfinally($(esc(ex)),
338347
(elapsedtime = time_ns() - elapsedtime;
339-
compile_elapsedtimes = cumulative_compile_time_ns_after() .- compile_elapsedtimes)
348+
compile_elapsedtimes = cumulative_compile_time_ns() .- compile_elapsedtimes)
340349
)
341350
local diff = GC_Diff(gc_num(), stats)
342351
local _msg = $(esc(msg))

src/jitlayers.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,8 @@ jl_code_instance_t *jl_generate_fptr_impl(jl_method_instance_t *mi JL_PROPAGATES
365365
uint64_t t_comp = jl_hrtime() - compiler_start_time;
366366
if (is_recompile)
367367
jl_atomic_fetch_add_relaxed(&jl_cumulative_recompile_time, t_comp);
368-
jl_atomic_fetch_add_relaxed(&jl_cumulative_compile_time, t_comp);
368+
else
369+
jl_atomic_fetch_add_relaxed(&jl_cumulative_compile_time, t_comp);
369370
}
370371
JL_UNLOCK(&jl_codegen_lock);
371372
JL_GC_POP();

src/jlapi.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -471,26 +471,24 @@ JL_DLLEXPORT void (jl_cpu_wake)(void)
471471
jl_cpu_wake();
472472
}
473473

474-
JL_DLLEXPORT uint64_t jl_cumulative_compile_time_ns_before(void)
474+
JL_DLLEXPORT void jl_cumulative_compile_timing_enable(void)
475475
{
476476
// Increment the flag to allow reentrant callers to `@time`.
477477
jl_atomic_fetch_add(&jl_measure_compile_time_enabled, 1);
478-
return jl_atomic_load_relaxed(&jl_cumulative_compile_time);
479478
}
480479

481-
JL_DLLEXPORT uint64_t jl_cumulative_compile_time_ns_after(void)
480+
JL_DLLEXPORT void jl_cumulative_compile_timing_disable(void)
482481
{
483482
// Decrement the flag when done measuring, allowing other callers to continue measuring.
484483
jl_atomic_fetch_add(&jl_measure_compile_time_enabled, -1);
485-
return jl_atomic_load_relaxed(&jl_cumulative_compile_time);
486484
}
487485

488-
JL_DLLEXPORT uint64_t jl_cumulative_recompile_time_ns_before(void)
486+
JL_DLLEXPORT uint64_t jl_cumulative_compile_time_ns(void)
489487
{
490-
return jl_atomic_load_relaxed(&jl_cumulative_recompile_time);
488+
return jl_atomic_load_relaxed(&jl_cumulative_compile_time);
491489
}
492490

493-
JL_DLLEXPORT uint64_t jl_cumulative_recompile_time_ns_after(void)
491+
JL_DLLEXPORT uint64_t jl_cumulative_recompile_time_ns(void)
494492
{
495493
return jl_atomic_load_relaxed(&jl_cumulative_recompile_time);
496494
}

test/misc.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ function timev_macro_scope()
334334
end
335335
@test timev_macro_scope() == 1
336336

337-
before_comp, before_recomp = Base.cumulative_compile_time_ns_before()
337+
cumulative_compile_timing(true)
338+
before_comp, before_recomp = Base.cumulative_compile_time_ns()
338339

339340
# exercise concurrent calls to `@time` for reentrant compilation time measurement.
340341
t1 = @async @time begin
@@ -347,7 +348,8 @@ t2 = @async begin
347348
@time 2 + 2
348349
end
349350

350-
after_comp, after_recomp = Base.cumulative_compile_time_ns_after()
351+
cumulative_compile_timing(false)
352+
after_comp, after_recomp = Base.cumulative_compile_time_ns()
351353
@test after_comp >= before_comp;
352354

353355
# wait for completion of these tasks before restoring stdout, to suppress their @time prints.

0 commit comments

Comments
 (0)