Merge branch 'merged-complete' into 'master'

Merge code refactors into main branch

See merge request lab2425_autumn/pintos_22!16
This commit is contained in:
sb3923
2024-10-24 22:15:24 +00:00
4 changed files with 100 additions and 100 deletions

View File

@@ -10,30 +10,33 @@ typedef struct
/* Fixed Point Arithmetic bit count constants */ /* Fixed Point Arithmetic bit count constants */
#define NUM_FRAC_BITS 14 #define NUM_FRAC_BITS 14
#define NUM_INT_BITS (31 - NUM_FRAC_BITS) #define NUM_INT_BITS (31 - NUM_FRAC_BITS)
#define CONVERSION_CONST (1 << NUM_FRAC_BITS) /* f = 2^q, (2^20) */ #define CONVERSION_FACTOR (1 << NUM_FRAC_BITS) /* f = 2^q, (2^14) */
/* Fixed Point Arithmetic conversion operations */ /* Fixed Point Arithmetic conversion operations */
/* Converts an integer n to a fixed point number */ /* Converts an integer n to a fixed point number */
inline fp32_t inline fp32_t
int_to_fp (int32_t n) fp_from_int (int32_t n)
{ {
return (fp32_t){ n * CONVERSION_CONST }; return (fp32_t){ n * CONVERSION_FACTOR };
} }
/* Handles conversion of fixed point to integer. First version truncates, second one rounds */ /* Handles conversion of fixed point to integer,
with truncation */
inline int32_t inline int32_t
fp_floor (fp32_t x) fp_floor (fp32_t x)
{ {
return x.raw / CONVERSION_CONST; return x.raw / CONVERSION_FACTOR;
} }
/* Handles conversion of fixed point to integer,
with rounding */
inline int32_t inline int32_t
fp_round (fp32_t x) fp_round (fp32_t x)
{ {
if (x.raw >= 0) if (x.raw >= 0)
return (x.raw + CONVERSION_CONST / 2) / CONVERSION_CONST; return (x.raw + CONVERSION_FACTOR / 2) / CONVERSION_FACTOR;
else else
return (x.raw - CONVERSION_CONST / 2) / CONVERSION_CONST; return (x.raw - CONVERSION_FACTOR / 2) / CONVERSION_FACTOR;
} }
/* Add two fixed points */ /* Add two fixed points */
@@ -50,32 +53,20 @@ fp_sub (fp32_t x, fp32_t y)
return (fp32_t){ x.raw - y.raw }; return (fp32_t){ x.raw - y.raw };
} }
/* Add fixed point to integer */
inline fp32_t
fp_add_int (fp32_t x, int32_t n)
{
return (fp32_t){ x.raw + n * CONVERSION_CONST };
}
/* Subtract integer from fixed point */
inline fp32_t
fp_sub_int (fp32_t x, int32_t n)
{
return (fp32_t){ x.raw - n * CONVERSION_CONST };
}
/* Multiple two fixed points */ /* Multiple two fixed points */
inline fp32_t inline fp32_t
fp_mul (fp32_t x, fp32_t y) fp_mul (fp32_t x, fp32_t y)
{ {
return (fp32_t){ ((int64_t)x.raw) * y.raw / CONVERSION_CONST }; return (fp32_t){ ((int64_t)x.raw) * y.raw / CONVERSION_FACTOR };
} }
/* Divide two fixed points */ /* Divide two fixed points */
inline fp32_t inline fp32_t
fp_div (fp32_t x, fp32_t y) fp_div (fp32_t x, fp32_t y)
{ {
return (fp32_t){ ((int64_t)x.raw) * CONVERSION_CONST / y.raw }; return (fp32_t){ ((int64_t)x.raw) * CONVERSION_FACTOR / y.raw };
} }
/* Multiply fixed point and integer */ /* Multiply fixed point and integer */
@@ -92,4 +83,18 @@ fp_div_int (fp32_t x, int32_t n)
return (fp32_t){ x.raw / n }; return (fp32_t){ x.raw / n };
} }
/* Add fixed point to integer */
inline fp32_t
fp_add_int (fp32_t x, int32_t n)
{
return (fp32_t){ x.raw + n * CONVERSION_FACTOR };
}
/* Subtract integer from fixed point */
inline fp32_t
fp_sub_int (fp32_t x, int32_t n)
{
return (fp32_t){ x.raw - n * CONVERSION_FACTOR };
}
#endif //FIXED_POINT_H #endif //FIXED_POINT_H

View File

@@ -305,8 +305,6 @@ lock_release (struct lock *lock)
ASSERT (lock != NULL); ASSERT (lock != NULL);
ASSERT (lock_held_by_current_thread (lock)); ASSERT (lock_held_by_current_thread (lock));
if (!thread_mlfqs)
{
struct thread *current_thread = thread_current (); struct thread *current_thread = thread_current ();
struct thread *max_donor = NULL; struct thread *max_donor = NULL;
@@ -351,7 +349,6 @@ lock_release (struct lock *lock)
/* Removal of donors to this thread may change its effective priority, /* Removal of donors to this thread may change its effective priority,
so recalculate. */ so recalculate. */
thread_recalculate_priority (); thread_recalculate_priority ();
}
lock->holder = NULL; lock->holder = NULL;
sema_up (&lock->semaphore); sema_up (&lock->semaphore);

View File

@@ -72,7 +72,7 @@ static void init_thread (struct thread *, const char *name, int nice,
static bool is_thread (struct thread *) UNUSED; static bool is_thread (struct thread *) UNUSED;
static void *alloc_frame (struct thread *, size_t size); static void *alloc_frame (struct thread *, size_t size);
static int calculate_bsd_priority (fp32_t recent_cpu, int nice); static int calculate_bsd_priority (fp32_t recent_cpu, int nice);
static void thread_update_recent_cpu (struct thread *t, void *aux UNUSED); static void update_recent_cpu (struct thread *t, void *aux UNUSED);
static void schedule (void); static void schedule (void);
void thread_schedule_tail (struct thread *prev); void thread_schedule_tail (struct thread *prev);
static tid_t allocate_tid (void); static tid_t allocate_tid (void);
@@ -157,23 +157,24 @@ thread_tick (void)
/* Update system load_avg and all threads recent_cpu every second. */ /* Update system load_avg and all threads recent_cpu every second. */
int64_t ticks = timer_ticks (); int64_t ticks = timer_ticks ();
if (thread_mlfqs && (ticks % TIMER_FREQ == 0)) if (thread_mlfqs)
{
if (t != idle_thread)
t->recent_cpu = fp_add_int (t->recent_cpu, 1);
if (ticks % TIMER_FREQ == 0)
{ {
size_t ready = threads_ready (); size_t ready = threads_ready ();
if (t != idle_thread) if (t != idle_thread)
ready++; ready++;
fp32_t old_coeff = fp_div_int (fp_mul_int (load_avg, 59), 60); fp32_t old_coeff = fp_div_int (fp_mul_int (load_avg, 59), 60);
fp32_t new_coeff = fp_div_int (int_to_fp (ready), 60); fp32_t new_coeff = fp_div_int (fp_from_int (ready), 60);
load_avg = fp_add (old_coeff, new_coeff); load_avg = fp_add (old_coeff, new_coeff);
thread_foreach (thread_update_recent_cpu, NULL); thread_foreach (update_recent_cpu, NULL);
} }
/* Update current thread's recent_cpu. */ if (ticks % TIME_SLICE == 0) // recent_cpu was updated, update priority.
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.
t->priority = calculate_bsd_priority (t->recent_cpu, t->nice); t->priority = calculate_bsd_priority (t->recent_cpu, t->nice);
} }
@@ -224,8 +225,8 @@ thread_create (const char *name, int priority,
return TID_ERROR; return TID_ERROR;
/* Initialize thread. */ /* Initialize thread. */
struct thread *pt = thread_current (); struct thread *parent_thread = thread_current ();
init_thread (t, name, pt->nice, priority, pt->recent_cpu); init_thread (t, name, parent_thread->nice, priority, parent_thread->recent_cpu);
tid = t->tid = allocate_tid (); tid = t->tid = allocate_tid ();
/* Prepare thread for first run by initializing its stack. /* Prepare thread for first run by initializing its stack.
@@ -291,7 +292,6 @@ thread_unblock (struct thread *t)
old_level = intr_disable (); old_level = intr_disable ();
ASSERT (t->status == THREAD_BLOCKED); ASSERT (t->status == THREAD_BLOCKED);
/* Insert the thread back into the ready list in priority order. */ /* Insert the thread back into the ready list in priority order. */
list_insert_ordered (&ready_list, &t->elem, priority_more, NULL); list_insert_ordered (&ready_list, &t->elem, priority_more, NULL);
@@ -367,10 +367,8 @@ thread_yield (void)
if (cur != idle_thread) if (cur != idle_thread)
{ {
/* Insert the thread back into the ready list in priority order. */ /* Insert the thread back into the ready list in priority order. */
list_insert_ordered (&ready_list, &cur->elem, priority_more, NULL); list_insert_ordered (&ready_list, &cur->elem, priority_more, NULL);
} }
cur->status = THREAD_READY; cur->status = THREAD_READY;
@@ -402,7 +400,6 @@ bool
priority_more (const struct list_elem *a_, const struct list_elem *b_, priority_more (const struct list_elem *a_, const struct list_elem *b_,
void *aux UNUSED) void *aux UNUSED)
{ {
struct thread *a = list_entry (a_, struct thread, elem); struct thread *a = list_entry (a_, struct thread, elem);
struct thread *b = list_entry (b_, struct thread, elem); struct thread *b = list_entry (b_, struct thread, elem);
@@ -457,7 +454,7 @@ thread_get_priority (void)
/* Updates recent_cpu for a thread. */ /* Updates recent_cpu for a thread. */
static void static void
thread_update_recent_cpu (struct thread *t, void *aux UNUSED) update_recent_cpu (struct thread *t, void *aux UNUSED)
{ {
fp32_t curr_recent_cpu = t->recent_cpu; fp32_t curr_recent_cpu = t->recent_cpu;
fp32_t dbl_load_avg = fp_mul_int (load_avg, 2); fp32_t dbl_load_avg = fp_mul_int (load_avg, 2);
@@ -474,7 +471,8 @@ thread_recalculate_priority (void)
{ {
struct thread *t = thread_current (); struct thread *t = thread_current ();
ASSERT(!thread_mlfqs) if (thread_mlfqs)
return;
enum intr_level old_level = intr_disable (); enum intr_level old_level = intr_disable ();
t->priority = t->base_priority; t->priority = t->base_priority;
@@ -634,17 +632,16 @@ init_thread (struct thread *t, const char *name, int nice, int priority,
t->status = THREAD_BLOCKED; t->status = THREAD_BLOCKED;
strlcpy (t->name, name, sizeof t->name); strlcpy (t->name, name, sizeof t->name);
t->stack = (uint8_t *) t + PGSIZE; t->stack = (uint8_t *) t + PGSIZE;
t->magic = THREAD_MAGIC;
t->base_priority = priority;
list_init (&t->donors_list);
t->waiting_lock = NULL;
t->nice = nice; t->nice = nice;
t->recent_cpu = recent_cpu; t->recent_cpu = recent_cpu;
t->priority = thread_mlfqs ? calculate_bsd_priority (recent_cpu, nice)
t->base_priority = priority; : t->base_priority;
t->magic = THREAD_MAGIC;
list_init (&t->donors_list);
t->priority = thread_mlfqs ? calculate_bsd_priority (recent_cpu, nice) : t->base_priority;
t->waiting_lock = NULL;
old_level = intr_disable (); old_level = intr_disable ();
list_push_back (&all_list, &t->allelem); list_push_back (&all_list, &t->allelem);

View File

@@ -104,13 +104,14 @@ struct thread
struct list_elem donor_elem; /* List element so that thread can be struct list_elem donor_elem; /* List element so that thread can be
enlisted in other donors list. */ enlisted in other donors list. */
/* Shared between thread.c and synch.c. */
struct list_elem elem; /* List element. */
/* MLFQS items */ /* MLFQS items */
int nice; /* Nice value for this thread */ int nice; /* Nice value for this thread */
fp32_t recent_cpu; /* Amount of time this process received */ fp32_t recent_cpu; /* Amount of time this process received */
/* Shared between thread.c and synch.c. */
struct list_elem elem; /* List element. */
#ifdef USERPROG #ifdef USERPROG
/* Owned by userprog/process.c. */ /* Owned by userprog/process.c. */
uint32_t *pagedir; /* Page directory. */ uint32_t *pagedir; /* Page directory. */