Skip to content

Commit

Permalink
add excessive scheduling time to time macro etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Oct 3, 2024
1 parent 77c5875 commit d371291
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
3 changes: 3 additions & 0 deletions base/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ include("weakkeydict.jl")
# ScopedValues
include("scopedvalues.jl")

# used by task.jl
const Workqueue_sched_times = ScopedValues.ScopedValue{Vector{UInt}}()

# metaprogramming
include("meta.jl")

Expand Down
16 changes: 14 additions & 2 deletions base/task.jl
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,8 @@ function workqueue_for(tid::Int)
if length(qs) < tid
nt = Threads.maxthreadid()
@assert tid <= nt
global Workqueues = qs = copyto!(typeof(qs)(undef, length(qs) + nt - 1), qs)
new_length = length(qs) + nt - 1
global Workqueues = qs = copyto!(typeof(qs)(undef, new_length), qs)
end
if !isassigned(qs, tid)
@inbounds qs[tid] = StickyWorkqueue()
Expand Down Expand Up @@ -1098,7 +1099,9 @@ end

function try_yieldto(undo)
try
# stop timer - add to global timer
ccall(:jl_switch, Cvoid, ())
# start timer
catch
undo(ccall(:jl_get_next_task, Ref{Task}, ()))
rethrow()
Expand Down Expand Up @@ -1170,8 +1173,17 @@ end

function wait()
GC.safepoint()
W = workqueue_for(Threads.threadid())
tid = Threads.threadid()
W = workqueue_for(tid)
t = time_ns()
poptask(W)
t = time_ns() - t
if isassigned(Workqueue_sched_times)
ts = Workqueue_sched_times[]
if tid <= length(ts) # TODO: Grow Workqueue_sched_times if needed, but needs to be threadsafe
ts[tid] += t
end
end
result = try_yieldto(ensure_rescheduled)
process_events()
# return when we come out of the queue
Expand Down
41 changes: 29 additions & 12 deletions base/timing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ function format_bytes(bytes; binary=true) # also used by InteractiveUtils
end
end

function time_print(io::IO, elapsedtime, bytes=0, gctime=0, allocs=0, lock_conflicts=0, compile_time=0, recompile_time=0, newline=false;
msg::Union{String,Nothing}=nothing)
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,
newline=false; msg::Union{String,Nothing}=nothing)
timestr = Ryu.writefixed(Float64(elapsedtime/1e9), 6)
str = sprint() do io
if msg isa String
Expand All @@ -223,12 +223,22 @@ function time_print(io::IO, elapsedtime, bytes=0, gctime=0, allocs=0, lock_confl
end
print(io, Ryu.writefixed(Float64(100*gctime/elapsedtime), 2), "% gc time")
end
wall_time_sched_perc = wall_time_sched / (elapsedtime / 1e9)
if wall_time_sched_perc > 0.1
if bytes != 0 || allocs != 0 || gctime > 0
print(io, ", ")
end
print(io, Ryu.writefixed(Float64(100*wall_time_sched_perc), 2), "% wall time scheduling")
end
if lock_conflicts > 0
if bytes != 0 || allocs != 0 || gctime > 0 || wall_time_sched_perc > 0.1
print(io, ", ")
end
plural = lock_conflicts == 1 ? "" : "s"
print(io, ", ", lock_conflicts, " lock conflict$plural")
end
if compile_time > 0
if bytes != 0 || allocs != 0 || gctime > 0
if bytes != 0 || allocs != 0 || gctime > 0 || wall_time_sched_perc > 0.1 || lock_conflicts > 0
print(io, ", ")
end
print(io, Ryu.writefixed(Float64(100*compile_time/elapsedtime), 2), "% compilation time")
Expand Down Expand Up @@ -343,7 +353,7 @@ macro time(msg, ex)
local ret = @timed $(esc(ex))
local _msg = $(esc(msg))
local _msg_str = _msg === nothing ? _msg : string(_msg)
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_str)
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_str)
ret.value
end
end
Expand Down Expand Up @@ -603,13 +613,19 @@ macro timed(ex)
local elapsedtime = time_ns()
cumulative_compile_timing(true)
local compile_elapsedtimes = cumulative_compile_time_ns()
local val = @__tryfinally($(esc(ex)),
(elapsedtime = time_ns() - elapsedtime;
cumulative_compile_timing(false);
compile_elapsedtimes = cumulative_compile_time_ns() .- compile_elapsedtimes;
lock_conflicts = Threads.LOCK_CONFLICT_COUNT[] - lock_conflicts;
Threads.lock_profiling(false))
)
local wall_time_sched = 0.0
ScopedValues.@with Workqueue_sched_times => zeros(UInt, Threads.maxthreadid()) begin
sched_ts = copy(Base.Workqueue_sched_times[])
local val = @__tryfinally($(esc(ex)),
(elapsedtime = time_ns() - elapsedtime;
cumulative_compile_timing(false);
compile_elapsedtimes = cumulative_compile_time_ns() .- compile_elapsedtimes;
lock_conflicts = Threads.LOCK_CONFLICT_COUNT[] - lock_conflicts;
Threads.lock_profiling(false))
)
wall_time_sched = sum(Base.Workqueue_sched_times[] .- sched_ts) / (Threads.nthreads(:interactive) + Threads.nthreads(:default))
end

local diff = GC_Diff(gc_num(), stats)
(
value=val,
Expand All @@ -619,7 +635,8 @@ macro timed(ex)
gcstats=diff,
lock_conflicts=lock_conflicts,
compile_time=compile_elapsedtimes[1]/1e9,
recompile_time=compile_elapsedtimes[2]/1e9
recompile_time=compile_elapsedtimes[2]/1e9,
wall_time_sched=wall_time_sched/1e9
)
end
end

0 comments on commit d371291

Please sign in to comment.