refactor thread_mlfqs checks in synch.c

This commit is contained in:
EDiasAlberto
2024-10-24 21:46:39 +01:00
parent a6a0c4ad25
commit 60acc2e58d
2 changed files with 55 additions and 54 deletions

View File

@@ -157,23 +157,26 @@ thread_tick (void)
/* Update system load_avg and all threads recent_cpu every second. */
int64_t ticks = timer_ticks ();
if (thread_mlfqs && (ticks % TIMER_FREQ == 0))
if (thread_mlfqs)
{
size_t ready = threads_ready ();
if (t != idle_thread)
ready++;
fp32_t old_coeff = fp_div_int (fp_mul_int(load_avg, 59), 60);
fp32_t new_coeff = fp_div_int (fp_from_int (ready), 60);
load_avg = fp_add (old_coeff, new_coeff);
{
t->recent_cpu = fp_add_int (t->recent_cpu, 1);
}
thread_foreach (thread_update_recent_cpu, NULL);
}
if (ticks % TIMER_FREQ == 0)
{
size_t ready = threads_ready ();
if (t != idle_thread)
ready++;
fp32_t old_coeff = fp_div_int (fp_mul_int(load_avg, 59), 60);
fp32_t new_coeff = fp_div_int (fp_from_int (ready), 60);
load_avg = fp_add (old_coeff, new_coeff);
/* Update current thread's recent_cpu. */
if (thread_mlfqs && (t != idle_thread))
{
t->recent_cpu = fp_add_int (t->recent_cpu, 1);
if (ticks % 4 == 0) // recent_cpu was updated, update priority.
thread_foreach (thread_update_recent_cpu, NULL);
}
if (ticks % TIME_SLICE == 0) // recent_cpu was updated, update priority.
t->priority = calculate_bsd_priority (t->recent_cpu, t->nice);
}
@@ -470,7 +473,8 @@ thread_recalculate_priority (void)
{
struct thread *t = thread_current ();
ASSERT(!thread_mlfqs)
if (thread_mlfqs)
return;
enum intr_level old_level = intr_disable ();
t->priority = t->base_priority;