Skip to content

Commit 83b4487

Browse files
don't count time sleeping
1 parent 2659cff commit 83b4487

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

base/Base.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,8 @@ include("weakkeydict.jl")
425425
include("scopedvalues.jl")
426426

427427
# used by task.jl
428-
const Workqueue_sched_times = ScopedValues.ScopedValue{Vector{UInt}}()
428+
const task_times_per_thread = ScopedValues.ScopedValue{Vector{UInt}}()
429+
const sleep_times_per_thread = ScopedValues.ScopedValue{Vector{UInt}}()
429430

430431
# metaprogramming
431432
include("meta.jl")

base/task.jl

+13-2
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ function try_yieldto(undo)
11001100
try
11011101
# when timing time spent in the scheduler we time the time spent in the task
11021102
# then subtract the elapsed time in `@timed` from the total time to get the scheduler time
1103-
t, ts = isassigned(Workqueue_sched_times) ? (time_ns(), Workqueue_sched_times[]) : (nothing, nothing)
1103+
t, ts = isassigned(task_times_per_thread) ? (time_ns(), task_times_per_thread[]) : (nothing, nothing)
11041104
ccall(:jl_switch, Cvoid, ())
11051105
if ts !== nothing
11061106
t = time_ns() - t
@@ -1181,8 +1181,19 @@ end
11811181

11821182
function wait()
11831183
GC.safepoint()
1184-
W = workqueue_for(Threads.threadid())
1184+
tid = Threads.threadid()
1185+
W = workqueue_for(tid)
1186+
# optional sleep timing
1187+
t, ts = isassigned(sleep_times_per_thread) ? (time_ns(), sleep_times_per_thread[]) : (nothing, nothing)
11851188
poptask(W)
1189+
if ts !== nothing # sleep timing is enabled
1190+
t = time_ns() - t
1191+
if tid <= length(ts)
1192+
ts[tid] += t
1193+
end
1194+
# TODO: grow the array if needed i.e. if a thread is added within the `@timed` block
1195+
end
1196+
11861197
result = try_yieldto(ensure_rescheduled)
11871198
process_events()
11881199
# return when we come out of the queue

base/timing.jl

+14-8
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ function format_bytes(bytes; binary=true) # also used by InteractiveUtils
196196
end
197197
end
198198

199-
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,
199+
function time_print(io::IO, elapsedtime, bytes=0, gctime=0, allocs=0, lock_conflicts=0, compile_time=0, recompile_time=0, sched_time_avg=0,
200200
newline=false; msg::Union{String,Nothing}=nothing)
201201
timestr = Ryu.writefixed(Float64(elapsedtime/1e9), 6)
202-
wall_time_sched_perc = wall_time_sched / (elapsedtime / 1e9)
202+
wall_time_sched_perc = sched_time_avg / (elapsedtime / 1e9)
203203
sched_thresh = 0.1
204204
str = sprint() do io
205205
if msg isa String
@@ -354,7 +354,7 @@ macro time(msg, ex)
354354
local ret = @timed $(esc(ex))
355355
local _msg = $(esc(msg))
356356
local _msg_str = _msg === nothing ? _msg : string(_msg)
357-
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)
357+
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.sched_time_avg, true; msg=_msg_str)
358358
ret.value
359359
end
360360
end
@@ -608,7 +608,7 @@ julia> stats.recompile_time
608608
macro timed(ex)
609609
quote
610610
Experimental.@force_compile
611-
ScopedValues.@with Workqueue_sched_times => zeros(UInt, Threads.maxthreadid()) begin
611+
ScopedValues.@with task_times_per_thread => zeros(UInt, Threads.maxthreadid()) sleep_times_per_thread => zeros(UInt, Threads.maxthreadid()) begin
612612
Experimental.@force_compile
613613
Threads.lock_profiling(true)
614614
local lock_conflicts = Threads.LOCK_CONFLICT_COUNT[]
@@ -624,9 +624,15 @@ macro timed(ex)
624624
Threads.lock_profiling(false))
625625
)
626626
local diff = GC_Diff(gc_num(), stats)
627-
# filter out zeros which can only happen if nothing was scheduled
628-
local sched_times = Int.(filter(>(0), Workqueue_sched_times[]))
629-
local wall_time_sched = isempty(sched_times) ? 0 : sum(Int(elapsedtime) .- sched_times) / length(sched_times)
627+
local sched_times = Int[]
628+
for i in 1:Threads.maxthreadid()
629+
# filter out zeros in task timers which can only happen if nothing was scheduled
630+
if task_times_per_thread[][i] != 0
631+
# subtract task and sleep times from global elapsed time to get scheduling time per thread
632+
push!(sched_times, Int(elapsedtime) - Int(task_times_per_thread[][i]) - Int(sleep_times_per_thread[][i]))
633+
end
634+
end
635+
local sched_time_avg = isempty(sched_times) ? 0 : sum(sched_times) / length(sched_times)
630636

631637
(
632638
value=val,
@@ -637,7 +643,7 @@ macro timed(ex)
637643
lock_conflicts=lock_conflicts,
638644
compile_time=compile_elapsedtimes[1]/1e9,
639645
recompile_time=compile_elapsedtimes[2]/1e9,
640-
wall_time_sched=wall_time_sched/1e9
646+
sched_time_avg=sched_time_avg/1e9
641647
)
642648
end
643649
end

0 commit comments

Comments
 (0)