From 4a5de13d1e68bc241ab507aaedec26330a3f84be Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Wed, 16 Oct 2024 18:45:28 +0100 Subject: [PATCH] implement recent_cpu calculations on every second --- src/devices/timer.c | 4 ++++ src/threads/thread.c | 10 +++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/devices/timer.c b/src/devices/timer.c index 63e15f6..3e481cc 100644 --- a/src/devices/timer.c +++ b/src/devices/timer.c @@ -199,6 +199,10 @@ static void timer_interrupt (struct intr_frame *args UNUSED) { ticks++; + if (ticks % TIMER_FREQ == 0) + { + calculate_recent_cpu (); + } for (struct list_elem *e = list_begin (&sleeping_threads); e != list_end (&sleeping_threads); e = list_next (e)) { diff --git a/src/threads/thread.c b/src/threads/thread.c index 47cd5c4..62a8c57 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -375,11 +375,15 @@ calculate_priority (void) return 0; } -fp32_t +void calculate_recent_cpu (void) { - /* Not yet implemented */ - return { 0 }; + struct thread *t = thread_current (); + fp32_t curr_recent_cpu = t->recent_cpu; + fp32_t curr_load_avg = fp_mul_int(thread_get_load_avg (), 2); + fp32_t recent_cpu_coeff = fp_div(curr_load_avg, fp_add_int(curr_load_avg, 1)); + fp32_t new_recent_cpu = fp_add_int(fp_mul(recent_cpu_coeff, curr_recent_cpu), thread_get_nice ()); + t->recent_cpu = new_recent_cpu; } void