From 7268354f7c747aacec310373a64f881e459de571 Mon Sep 17 00:00:00 2001 From: Nick Robinson Date: Thu, 31 Oct 2024 18:53:34 +0000 Subject: [PATCH] Consolidate names again --- base/experimental.jl | 35 +++++++---------------------------- test/threads_exec.jl | 8 ++++---- 2 files changed, 11 insertions(+), 32 deletions(-) diff --git a/base/experimental.jl b/base/experimental.jl index abf5adeabe391..bf2b86a650f79 100644 --- a/base/experimental.jl +++ b/base/experimental.jl @@ -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. @@ -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} @@ -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 @@ -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 diff --git a/test/threads_exec.jl b/test/threads_exec.jl index 57abafbf982d7..2ceb4b92abee3 100644 --- a/test/threads_exec.jl +++ b/test/threads_exec.jl @@ -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