Skip to content

Commit 13c2d17

Browse files
add excessive scheduling time to time macro etc.
1 parent ad407a6 commit 13c2d17

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

base/Base.jl

+3
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,9 @@ include("weakkeydict.jl")
423423
include("scopedvalues.jl")
424424
using .ScopedValues
425425

426+
# used by task.jl
427+
const Workqueue_sched_times = ScopedValue{Vector{UInt}}()
428+
426429
# metaprogramming
427430
include("meta.jl")
428431

base/task.jl

+12-2
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,8 @@ function workqueue_for(tid::Int)
931931
if length(qs) < tid
932932
nt = Threads.maxthreadid()
933933
@assert tid <= nt
934-
global Workqueues = qs = copyto!(typeof(qs)(undef, length(qs) + nt - 1), qs)
934+
new_length = length(qs) + nt - 1
935+
global Workqueues = qs = copyto!(typeof(qs)(undef, new_length), qs)
935936
end
936937
if !isassigned(qs, tid)
937938
@inbounds qs[tid] = StickyWorkqueue()
@@ -1173,8 +1174,17 @@ end
11731174

11741175
function wait()
11751176
GC.safepoint()
1176-
W = workqueue_for(Threads.threadid())
1177+
tid = Threads.threadid()
1178+
W = workqueue_for(tid)
1179+
t = time_ns()
11771180
poptask(W)
1181+
t = time_ns() - t
1182+
if isassigned(Workqueue_sched_times)
1183+
ts = Workqueue_sched_times[]
1184+
if tid <= length(ts) # TODO: Grow Workqueue_sched_times if needed, but needs to be threadsafe
1185+
ts[tid] += t
1186+
end
1187+
end
11781188
result = try_yieldto(ensure_rescheduled)
11791189
process_events()
11801190
# return when we come out of the queue

base/timing.jl

+29-12
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ function format_bytes(bytes; binary=true) # also used by InteractiveUtils
169169
end
170170
end
171171

172-
function time_print(io::IO, elapsedtime, bytes=0, gctime=0, allocs=0, lock_conflicts=0, compile_time=0, recompile_time=0, newline=false;
173-
msg::Union{String,Nothing}=nothing)
172+
function time_print(io::IO, elapsedtime, bytes=0, gctime=0, allocs=0, lock_conflicts=0, compile_time=0, recompile_time=0, wall_time_sched=0,
173+
newline=false; msg::Union{String,Nothing}=nothing)
174174
timestr = Ryu.writefixed(Float64(elapsedtime/1e9), 6)
175175
str = sprint() do io
176176
if msg isa String
@@ -196,12 +196,22 @@ function time_print(io::IO, elapsedtime, bytes=0, gctime=0, allocs=0, lock_confl
196196
end
197197
print(io, Ryu.writefixed(Float64(100*gctime/elapsedtime), 2), "% gc time")
198198
end
199+
wall_time_sched_perc = wall_time_sched / (elapsedtime / 1e9)
200+
if wall_time_sched_perc > 0.1
201+
if bytes != 0 || allocs != 0 || gctime > 0
202+
print(io, ", ")
203+
end
204+
print(io, Ryu.writefixed(Float64(100*wall_time_sched_perc), 2), "% wall time scheduling")
205+
end
199206
if lock_conflicts > 0
207+
if bytes != 0 || allocs != 0 || gctime > 0 || wall_time_sched_perc > 0.1
208+
print(io, ", ")
209+
end
200210
plural = lock_conflicts == 1 ? "" : "s"
201211
print(io, ", ", lock_conflicts, " lock conflict$plural")
202212
end
203213
if compile_time > 0
204-
if bytes != 0 || allocs != 0 || gctime > 0
214+
if bytes != 0 || allocs != 0 || gctime > 0 || wall_time_sched_perc > 0.1 || lock_conflicts > 0
205215
print(io, ", ")
206216
end
207217
print(io, Ryu.writefixed(Float64(100*compile_time/elapsedtime), 2), "% compilation time")
@@ -315,7 +325,7 @@ macro time(msg, ex)
315325
quote
316326
local ret = @timed $(esc(ex))
317327
local _msg = $(esc(msg))
318-
time_print(stdout, ret.time*1e9, ret.gcstats.allocd, ret.gcstats.total_time, gc_alloc_count(ret.gcstats), ret.lock_conflicts, ret.compile_time*1e9, ret.recompile_time*1e9, true; msg=_msg)
328+
time_print(stdout, ret.time*1e9, ret.gcstats.allocd, ret.gcstats.total_time, gc_alloc_count(ret.gcstats), ret.lock_conflicts, ret.compile_time*1e9, ret.recompile_time*1e9, ret.wall_time_sched, true; msg=_msg)
319329
ret.value
320330
end
321331
end
@@ -574,13 +584,19 @@ macro timed(ex)
574584
local elapsedtime = time_ns()
575585
cumulative_compile_timing(true)
576586
local compile_elapsedtimes = cumulative_compile_time_ns()
577-
local val = @__tryfinally($(esc(ex)),
578-
(elapsedtime = time_ns() - elapsedtime;
579-
cumulative_compile_timing(false);
580-
compile_elapsedtimes = cumulative_compile_time_ns() .- compile_elapsedtimes;
581-
lock_conflicts = Threads.LOCK_CONFLICT_COUNT[] - lock_conflicts;
582-
Threads.lock_profiling(false))
583-
)
587+
local wall_time_sched = 0.0
588+
@with Workqueue_sched_times => zeros(UInt, Threads.maxthreadid()) begin
589+
sched_ts = copy(Base.Workqueue_sched_times[])
590+
local val = @__tryfinally($(esc(ex)),
591+
(elapsedtime = time_ns() - elapsedtime;
592+
cumulative_compile_timing(false);
593+
compile_elapsedtimes = cumulative_compile_time_ns() .- compile_elapsedtimes;
594+
lock_conflicts = Threads.LOCK_CONFLICT_COUNT[] - lock_conflicts;
595+
Threads.lock_profiling(false))
596+
)
597+
wall_time_sched = sum(Base.Workqueue_sched_times[] .- sched_ts) / (Threads.nthreads(:interactive) + Threads.nthreads(:default))
598+
end
599+
584600
local diff = GC_Diff(gc_num(), stats)
585601
(
586602
value=val,
@@ -590,7 +606,8 @@ macro timed(ex)
590606
gcstats=diff,
591607
lock_conflicts=lock_conflicts,
592608
compile_time=compile_elapsedtimes[1]/1e9,
593-
recompile_time=compile_elapsedtimes[2]/1e9
609+
recompile_time=compile_elapsedtimes[2]/1e9,
610+
wall_time_sched=wall_time_sched/1e9
594611
)
595612
end
596613
end

0 commit comments

Comments
 (0)