Skip to content

Commit

Permalink
Consolidate names again
Browse files Browse the repository at this point in the history
  • Loading branch information
nickrobinson251 committed Oct 31, 2024
1 parent aceab7b commit 7268354
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 32 deletions.
35 changes: 7 additions & 28 deletions base/experimental.jl
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ end
Base.Experimental.task_cpu_time_ns(t::Task) -> Union{Int, Nothing}
Return the total nanoseconds that the task `t` has spent running.
This metric only updates when `t` yields or completes, unless `t` is the current task
in which it will update continuously.
See also [`Base.Experimental.task_wall_time_ns`](@ref).
Will be `nothing` if task timings are not enabled.
Expand All @@ -406,32 +408,17 @@ See [`Base.Experimental.task_metrics`](@ref).
!!! compat "Julia 1.12"
This method was added in Julia 1.12.
"""
function task_cpu_time_ns(t::Task)
function task_cpu_time_ns(t::Task=current_task())
t.metrics_enabled || return nothing
if t == current_task()
current_task_cpu_time_ns()
# These metrics fields can't update while we're running.
# But since we're running we need to include the time since we last started running!
return Int(t.cpu_time_ns + (time_ns() - t.last_started_running_at))
else
return Int(t.cpu_time_ns)
end
end

"""
Base.Experimental.current_task_cpu_time_ns() -> Union{Int, Nothing}
Return the total nanoseconds that the current task `t` has spent running.
Like [`Base.Experimental.task_cpu_time_ns`](@ref), but returns an up-to-date value for the
currently running task, whereas `task_cpu_time_ns(t)` for another task `t` only updates when
`t` yields or completes.
"""
function current_task_cpu_time_ns()
t = current_task()
t.metrics_enabled || return nothing
# These metrics fields can't update while we're running.
# But since we're running we need to include the time since we last started running!
return Int(t.cpu_time_ns + (time_ns() - t.last_started_running_at))
end

"""
Base.Experimental.task_wall_time_ns(t::Task) -> Union{Int, Nothing}
Expand All @@ -446,7 +433,7 @@ See [`Base.task_metrics`](@ref).
!!! compat "Julia 1.12"
This method was added in Julia 1.12.
"""
function task_wall_time_ns(t::Task)
function task_wall_time_ns(t::Task=current_task())
t.metrics_enabled || return nothing
start_at = t.first_enqueued_at
start_at == 0 && return 0
Expand All @@ -455,12 +442,4 @@ function task_wall_time_ns(t::Task)
return Int(end_at - start_at)
end

"""
Base.Experimental.current_task_wall_time_ns() -> Union{Int, Nothing}
Report the total wall time that the current task has been running.
See [`Base.Experimental.task_wall_time_ns`](@ref) for more details.
"""
current_task_wall_time_ns() = task_wall_time_ns(current_task())

end # module
8 changes: 4 additions & 4 deletions test/threads_exec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1231,14 +1231,14 @@ end
last_cpu_time = Ref(typemax(Int))
last_wall_time = Ref(typemax(Int))
t = Threads.@spawn begin
cpu_time = Base.Experimental.current_task_cpu_time_ns()
wall_time = Base.Experimental.current_task_wall_time_ns()
cpu_time = Base.Experimental.task_cpu_time_ns()
wall_time = Base.Experimental.task_wall_time_ns()
for _ in 1:5
x = time_ns()
while time_ns() < x + 100
end
new_cpu_time = Base.Experimental.current_task_cpu_time_ns()
new_wall_time = Base.Experimental.current_task_wall_time_ns()
new_cpu_time = Base.Experimental.task_cpu_time_ns()
new_wall_time = Base.Experimental.task_wall_time_ns()
@test new_cpu_time > cpu_time
@test new_wall_time > wall_time
cpu_time = new_cpu_time
Expand Down

0 comments on commit 7268354

Please sign in to comment.