Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

per-Task CPU time counter #4

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/jltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2626,7 +2626,7 @@ void jl_init_types(void) JL_GC_DISABLED
NULL,
jl_any_type,
jl_emptysvec,
jl_perm_symsvec(15,
jl_perm_symsvec(16,
"next",
"queue",
"storage",
Expand All @@ -2641,8 +2641,9 @@ void jl_init_types(void) JL_GC_DISABLED
"_state",
"sticky",
"_isexception",
"priority"),
jl_svec(15,
"priority",
"cpu_time_ns"),
jl_svec(16,
jl_any_type,
jl_any_type,
jl_any_type,
Expand All @@ -2657,7 +2658,8 @@ void jl_init_types(void) JL_GC_DISABLED
jl_uint8_type,
jl_bool_type,
jl_bool_type,
jl_uint16_type),
jl_uint16_type,
jl_uint64_type),
jl_emptysvec,
0, 1, 6);
jl_value_t *listt = jl_new_struct(jl_uniontype_type, jl_task_type, jl_nothing_type);
Expand Down
4 changes: 4 additions & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -1890,8 +1890,12 @@ typedef struct _jl_task_t {
_Atomic(uint8_t) _isexception; // set if `result` is an exception to throw or that we exited with
// multiqueue priority
uint16_t priority;
// TODO: int32 of ms instead?
uint64_t cpu_time_ns; // time this task has spent running; updated when it yields

// hidden state:
// timestamp this task was last scheduled (TODO: int32 of ms instead?)
uint64_t last_scheduled_ns;
// id of owning thread - does not need to be defined until the task runs
_Atomic(int16_t) tid;
// threadpool id
Expand Down
10 changes: 10 additions & 0 deletions src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ void JL_NORETURN jl_finish_task(jl_task_t *t)
{
jl_task_t *ct = jl_current_task;
JL_PROBE_RT_FINISH_TASK(ct);
ct->cpu_time_ns += jl_hrtime() - ct->last_scheduled_ns;
JL_SIGATOMIC_BEGIN();
if (jl_atomic_load_relaxed(&t->_isexception))
jl_atomic_store_release(&t->_state, JL_TASK_STATE_FAILED);
Expand Down Expand Up @@ -534,6 +535,7 @@ JL_DLLEXPORT void jl_switch(void)
jl_error("cannot switch to task running on another thread");

JL_PROBE_RT_PAUSE_TASK(ct);
ct->cpu_time_ns += jl_hrtime() - ct->last_scheduled_ns;

// Store old values on the stack and reset
sig_atomic_t defer_signal = ptls->defer_signal;
Expand Down Expand Up @@ -584,6 +586,9 @@ JL_DLLEXPORT void jl_switch(void)
jl_sigint_safepoint(ptls);

JL_PROBE_RT_RUN_TASK(ct);
ct->last_scheduled_ns = jl_hrtime();

jl_gc_unsafe_leave(ptls, gc_state);
}

JL_DLLEXPORT void jl_switchto(jl_task_t **pt)
Expand Down Expand Up @@ -803,6 +808,8 @@ JL_DLLEXPORT jl_task_t *jl_new_task(jl_function_t *start, jl_value_t *completion
t->threadpoolid = ct->threadpoolid;
t->ptls = NULL;
t->world_age = ct->world_age;
t->last_scheduled_ns = 0;
t->cpu_time_ns = 0;

#ifdef COPY_STACKS
if (!t->copy_stack) {
Expand Down Expand Up @@ -916,6 +923,7 @@ CFI_NORETURN

ct->started = 1;
JL_PROBE_RT_START_TASK(ct);
ct->last_scheduled_ns = jl_hrtime();
if (jl_atomic_load_relaxed(&ct->_isexception)) {
record_backtrace(ptls, 0);
jl_push_excstack(&ct->excstack, ct->result,
Expand Down Expand Up @@ -1366,6 +1374,8 @@ jl_task_t *jl_init_root_task(jl_ptls_t ptls, void *stack_lo, void *stack_hi)
ct->sticky = 1;
ct->ptls = ptls;
ct->world_age = 1; // OK to run Julia code on this task
ct->last_scheduled_ns = 0;
ct->cpu_time_ns = 0;
ptls->root_task = ct;
jl_atomic_store_relaxed(&ptls->current_task, ct);
JL_GC_PROMISE_ROOTED(ct);
Expand Down
9 changes: 9 additions & 0 deletions test/threads_exec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Test
using Base.Threads
using Base.Threads: SpinLock
using LinearAlgebra: peakflops

# for cfunction_closure
include("testenv.jl")
Expand Down Expand Up @@ -1062,3 +1063,11 @@ end
popfirst!(LOAD_PATH)
end
end

@testset "CPU time counter" begin
t = Threads.@spawn begin
peakflops()
end
wait(t)
@test t.cpu_time_ns > 0
end