From 5178b7237037cf0e7fbad6db8c79d5d447475ac1 Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Tue, 15 Oct 2024 15:34:06 +0100 Subject: [PATCH 1/6] comment fixed point arithmetic header --- src/threads/fixed-point.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/threads/fixed-point.h b/src/threads/fixed-point.h index b192073..79c088b 100644 --- a/src/threads/fixed-point.h +++ b/src/threads/fixed-point.h @@ -7,20 +7,25 @@ #define CONVERSION_CONST (1 << NUM_FRAC_BITS) /* f = 2^q, (2^20) */ /* Fixed Point Arithmetic conversion operations */ +/* Converts an integer n to a fixed point number */ #define INT_TO_FP(n) ((n) * (CONVERSION_CONST)) + +/* Handles conversion of fixed point to integer. First version truncates, second one rounds */ #define FLOOR_FP_TO_INT(x) ((x) / (CONVERSION_CONST)) #define ROUNDING_FP_TO_INT(x) ((x) >= 0 ? ((x) + ((CONVERSION_CONST) / 2)) : ((x) - ((CONVERSION_CONST) / 2)) /* Fixed Point Arithmetic addition operations */ #define FP_ADD(x, y) ((x) + (y)) #define FP_SUBTRACT(x, y) ((x) - (y)) +/* Addition and Subtraction but between a fixed point and an integer */ #define FP_ADD_INT(x, n) ((x) + ((n) * (CONVERSION_CONST))) #define FP_SUBTRACT_INT(x, n) ((x) - ((n) * (CONVERSION_CONST))) /* Fixed Point Arithmetic multiplication operations */ #define FP_MULTIPLY(x, y) ((int64_t)(x)) * (y) / (CONVERSION_CONST) -#define FP_MULTIPLY_INT(x, n) ((x) * (n)) #define FP_DIVIDE(x, y) ((int64_t)(x)) * (CONVERSION_CONST) / (y) +/* Multiplication and division but between a fixed point and an integer */ +#define FP_MULTIPLY_INT(x, n) ((x) * (n)) #define FP_DIVIDE_INT(x, n) ((x) / (n)) #endif //FIXED_POINT_H From df89bda71e47a1f5798d8e8b8f5d2f0770ff9c8f Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Tue, 15 Oct 2024 17:09:06 +0100 Subject: [PATCH 2/6] modify thread struct to track thread niceness and recent_cpu time --- src/threads/thread.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/threads/thread.h b/src/threads/thread.h index f36d7ac..3019926 100644 --- a/src/threads/thread.h +++ b/src/threads/thread.h @@ -93,6 +93,10 @@ struct thread /* Shared between thread.c and synch.c. */ struct list_elem elem; /* List element. */ + /* MLFQS items */ + int nice; /* Nice value for this thread */ + int32_t recent_cpu; /* Amount of time this process received */ + #ifdef USERPROG /* Owned by userprog/process.c. */ uint32_t *pagedir; /* Page directory. */ From 2834af032d0c99396b29e644624f668f3c9710d7 Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Tue, 15 Oct 2024 17:23:03 +0100 Subject: [PATCH 3/6] fix bracketing issue in ROUNDING_FP_TO_INT --- src/threads/fixed-point.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/threads/fixed-point.h b/src/threads/fixed-point.h index 79c088b..9b0140c 100644 --- a/src/threads/fixed-point.h +++ b/src/threads/fixed-point.h @@ -12,7 +12,7 @@ /* Handles conversion of fixed point to integer. First version truncates, second one rounds */ #define FLOOR_FP_TO_INT(x) ((x) / (CONVERSION_CONST)) -#define ROUNDING_FP_TO_INT(x) ((x) >= 0 ? ((x) + ((CONVERSION_CONST) / 2)) : ((x) - ((CONVERSION_CONST) / 2)) +#define ROUNDING_FP_TO_INT(x) ((x) >= 0 ? ((x) + ((CONVERSION_CONST) / 2)) : ((x) - ((CONVERSION_CONST) / 2))) /* Fixed Point Arithmetic addition operations */ #define FP_ADD(x, y) ((x) + (y)) From 3e379acd5e6171fb742157f233ea987e648ac46c Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Tue, 15 Oct 2024 17:27:16 +0100 Subject: [PATCH 4/6] modify gitignore to ignore vscode files --- .gitignore | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index a4fe502..3c4fa65 100644 --- a/.gitignore +++ b/.gitignore @@ -24,5 +24,6 @@ *.nav *.toc -#ignore files from CLion IDE -.idea \ No newline at end of file +#ignore files from CLion/VSCode IDEs +.idea +.vscode \ No newline at end of file From 27d564ab496067d27f645720aee8cb6b860a08e9 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Tue, 15 Oct 2024 19:41:29 +0100 Subject: [PATCH 5/6] inline funcs instead of macros for fixed-point --- src/threads/fixed-point.h | 96 ++++++++++++++++++++++++++++++++------- 1 file changed, 80 insertions(+), 16 deletions(-) diff --git a/src/threads/fixed-point.h b/src/threads/fixed-point.h index 9b0140c..c0a6100 100644 --- a/src/threads/fixed-point.h +++ b/src/threads/fixed-point.h @@ -1,31 +1,95 @@ +#include #ifndef FIXED_POINT_H #define FIXED_POINT_H +typedef struct +{ + int32_t raw; +} fp32_t; + /* Fixed Point Arithmetic bit count constants */ -#define NUM_INT_BITS 11 #define NUM_FRAC_BITS 20 +#define NUM_INT_BITS (31 - NUM_FRAC_BITS) #define CONVERSION_CONST (1 << NUM_FRAC_BITS) /* f = 2^q, (2^20) */ /* Fixed Point Arithmetic conversion operations */ /* Converts an integer n to a fixed point number */ -#define INT_TO_FP(n) ((n) * (CONVERSION_CONST)) +inline fp32_t +int_to_fp (int32_t n) +{ + return { n * CONVERSION_CONST }; +} /* Handles conversion of fixed point to integer. First version truncates, second one rounds */ -#define FLOOR_FP_TO_INT(x) ((x) / (CONVERSION_CONST)) -#define ROUNDING_FP_TO_INT(x) ((x) >= 0 ? ((x) + ((CONVERSION_CONST) / 2)) : ((x) - ((CONVERSION_CONST) / 2))) +inline int32_t +fp_floor (fp32_t x) +{ + return { x.raw / CONVERSION_CONST }; +} -/* Fixed Point Arithmetic addition operations */ -#define FP_ADD(x, y) ((x) + (y)) -#define FP_SUBTRACT(x, y) ((x) - (y)) -/* Addition and Subtraction but between a fixed point and an integer */ -#define FP_ADD_INT(x, n) ((x) + ((n) * (CONVERSION_CONST))) -#define FP_SUBTRACT_INT(x, n) ((x) - ((n) * (CONVERSION_CONST))) +inline int32_t +fp_round (fp32_t x) +{ + if (x.raw >= 0) + return (x.raw + CONVERSION_CONST / 2) / CONVERSION_CONST; + else + return (x.raw - CONVERSION_CONST / 2) / CONVERSION_CONST; +} -/* Fixed Point Arithmetic multiplication operations */ -#define FP_MULTIPLY(x, y) ((int64_t)(x)) * (y) / (CONVERSION_CONST) -#define FP_DIVIDE(x, y) ((int64_t)(x)) * (CONVERSION_CONST) / (y) -/* Multiplication and division but between a fixed point and an integer */ -#define FP_MULTIPLY_INT(x, n) ((x) * (n)) -#define FP_DIVIDE_INT(x, n) ((x) / (n)) +/* Add two fixed points */ +inline fp32_t +fp_add (fp32_t x, fp32_t y) +{ + return { x.raw + y.raw }; +} + +/* Subtract two fixed points */ +inline fp32_t +fp_sub (fp32_t x, fp32_t y) +{ + return { x.raw - y.raw }; +} + +/* Add fixed point to integer */ +inline fp32_t +fp_add_int (fp32_t x, int32_t n) +{ + return { x.raw + n * CONVERSION_CONST }; +} + +/* Subtract integer from fixed point */ +inline fp32_t +fp_sub_int (fp32_t x, int32_t n) +{ + return { x.raw - n * CONVERSION_CONST }; +} + +/* Multiple two fixed points */ +inline fp32_t +fp_mul (fp32_t x, fp32_t y) +{ + return { ((int64_t)x.raw) * y.raw / CONVERSION_CONST }; +} + +/* Divide two fixed points */ +inline fp32_t +fp_div (fp32_t x, fp32_t y) +{ + return { ((int64_t)x.raw) * CONVERSION_CONST / y.raw }; +} + +/* Multiply fixed point and integer */ +inline fp32_t +fp_mul_int (fp32_t x, int32_t n) +{ + return { x.raw * n }; +} + +/* Divide fixed point by integer */ +inline fp32_t +fp_div_int (fp32_t x, int32_t n) +{ + return { x.raw / n }; +} #endif //FIXED_POINT_H From 63742c5717b4c1137d680ec7b4b2a4a0ef9dbb14 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Tue, 15 Oct 2024 19:56:46 +0100 Subject: [PATCH 6/6] implement thread_get_nice & thread_get_recent_cpu --- src/threads/thread.c | 7 +++---- src/threads/thread.h | 3 ++- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/threads/thread.c b/src/threads/thread.c index 30ca2bd..aeec601 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -4,6 +4,7 @@ #include #include #include +#include "threads/fixed-point.h" #include "threads/flags.h" #include "threads/interrupt.h" #include "threads/intr-stubs.h" @@ -375,8 +376,7 @@ thread_set_nice (int nice UNUSED) int thread_get_nice (void) { - /* Not yet implemented. */ - return 0; + return thread_current ()->nice; } /* Returns 100 times the system load average. */ @@ -391,8 +391,7 @@ thread_get_load_avg (void) int thread_get_recent_cpu (void) { - /* Not yet implemented. */ - return 0; + return fp_round (fp_mul_int (thread_current ()->recent_cpu, 100)); } /* Idle thread. Executes when no other thread is ready to run. diff --git a/src/threads/thread.h b/src/threads/thread.h index 3019926..f85b814 100644 --- a/src/threads/thread.h +++ b/src/threads/thread.h @@ -4,6 +4,7 @@ #include #include #include +#include "threads/fixed-point.h" /* States in a thread's life cycle. */ enum thread_status @@ -95,7 +96,7 @@ struct thread /* MLFQS items */ int nice; /* Nice value for this thread */ - int32_t recent_cpu; /* Amount of time this process received */ + fp32_t recent_cpu; /* Amount of time this process received */ #ifdef USERPROG /* Owned by userprog/process.c. */