From 19dff40ee248c563d5586ab7bcde0e52aee9c6b3 Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 25 Sep 2022 17:49:51 +0200 Subject: [PATCH] Scheduler: track total CPU time of tasks --- kernel/include/thread/Task.h | 2 ++ kernel/src/thread/Scheduler.cpp | 7 ++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/kernel/include/thread/Task.h b/kernel/include/thread/Task.h index 71301445..4fb0ccca 100644 --- a/kernel/include/thread/Task.h +++ b/kernel/include/thread/Task.h @@ -23,6 +23,8 @@ struct Task uint64_t allocated_stack = 0; TaskState state; + + uint64_t cpu_time = 0; }; void set_context_from_task(Task& task, Context* ctx); diff --git a/kernel/src/thread/Scheduler.cpp b/kernel/src/thread/Scheduler.cpp index 3eb5f7a3..cb636f86 100644 --- a/kernel/src/thread/Scheduler.cpp +++ b/kernel/src/thread/Scheduler.cpp @@ -25,7 +25,6 @@ static Task* end_task; static void idle_task_function() { - Interrupts::enable(); while (1) asm volatile("hlt"); } @@ -123,7 +122,8 @@ void Scheduler::reap_task(Task* task) void Scheduler::task_exit(Context* context) { ASSERT(Interrupts::is_in_handler()); - kdbgln("exit: task %ld finished running", sched_current_task->id); + kdbgln("exit: task %ld finished running, used %ld ms of cpu time", sched_current_task->id, + sched_current_task->cpu_time); sched_current_task->state = sched_current_task->Exited; task_yield(context); } @@ -131,7 +131,7 @@ void Scheduler::task_exit(Context* context) void Scheduler::task_misbehave(Context* context) { ASSERT(Interrupts::is_in_handler()); - kdbgln("exit: task %ld misbehaved", sched_current_task->id); + kdbgln("exit: task %ld misbehaved, used %ld ms of cpu time", sched_current_task->id, sched_current_task->cpu_time); sched_current_task->state = sched_current_task->Exited; task_yield(context); } @@ -209,6 +209,7 @@ void Scheduler::task_tick(Context* context) sched_decrement_sleep_times(); if (sched_current_task->id == 0) return task_yield(context); sched_current_task->task_time -= frequency; + sched_current_task->cpu_time += frequency; if (sched_current_task->task_time < 0) { sched_current_task->task_time = 0;