From f471ea87c0cab5df836a675ff4b65f0f57560e42 Mon Sep 17 00:00:00 2001 From: g-magdy Date: Wed, 1 May 2024 20:25:02 +0300 Subject: [PATCH 1/2] feat: calculate utilization, avg WTA, avg Waiting --- code/scheduler.c | 37 ++++++++++++++++++++++++++++ code/scheduler.h | 3 ++- code/scheduler.log | 60 +++++++++------------------------------------ code/scheduler.perf | 3 +++ code/structs.h | 18 ++++++++++++++ 5 files changed, 72 insertions(+), 49 deletions(-) create mode 100644 code/scheduler.perf diff --git a/code/scheduler.c b/code/scheduler.c index a7d93c3..cb02b4d 100644 --- a/code/scheduler.c +++ b/code/scheduler.c @@ -17,6 +17,8 @@ int msgQID; process_t *currentProcess = NULL; +perfStats schedulerStats; + /** * main - The main function of the scheduler. * @@ -48,6 +50,8 @@ int main(int argc, char *argv[]) { schedule(schedulerType, quantem, gen_msgQID); printf(ANSI_BLUE "==>SCH: Scheduler Finished\n" ANSI_RESET); + // writePerfFile(); // moved it to the end of schedule() function + // TODO Initialize Scheduler // Create Wait queue ?? // Create log file @@ -148,6 +152,9 @@ void schedule(scheduler_type schType, int quantem, int gen_msgQID) { } printf(ANSI_BLUE "==>SCH: " ANSI_RED ANSI_BOLD "All processes are done\n" ANSI_RESET); + + writePerfFile(); + // FIXME: If I exit here it's all sunshines and rainbows // if I got back to main it gets angry // something about the stack needs to be fixed @@ -518,6 +525,12 @@ void createLogFile() { exit(-1); } + // to initiate performance statistics counters [maybe not the best place to do it] + schedulerStats.numFinished = 0; + schedulerStats.totalWaitingTime = 0; + schedulerStats.totalWorkingTime = 0; + schedulerStats.totalWTA = 0; + printf("Started Logging\n"); fclose(logFileptr); } @@ -540,9 +553,33 @@ void logger(char *msg, process_t *p) { if (strcmp(msg, "finished") == 0) { fprintf(logFileptr, " TA %i WTA %.2f", p->TA, WTA); + schedulerStats.numFinished += 1; + schedulerStats.totalWorkingTime += p->BT; + schedulerStats.totalWaitingTime += p->WT; + schedulerStats.totalWTA += WTA; } fprintf(logFileptr, "\n"); fclose(logFileptr); } + +void writePerfFile() { + FILE * perfFile = fopen("scheduler.perf", "w"); + + if (perfFile == NULL) { + perror("Can't open perf file"); + exit(-1); + } + + int finalTime = getClk(); + + schedulerStats.CPU_utilization = 100.0 * schedulerStats.totalWorkingTime / finalTime; + schedulerStats.avgWTA = (double) schedulerStats.totalWTA / schedulerStats.numFinished; + schedulerStats.avgWaitingTime = (double) schedulerStats.totalWaitingTime / schedulerStats.numFinished; + + fprintf(perfFile, "CPU utilization = %.2f%%\n", schedulerStats.CPU_utilization); + fprintf(perfFile, "Avg WTA = %.2f\n", schedulerStats.avgWTA); + fprintf(perfFile, "Avg Waiting = %.2f\n", schedulerStats.avgWaitingTime); + // TODO calculate std deviation +} diff --git a/code/scheduler.h b/code/scheduler.h index 27b7810..b8fb515 100644 --- a/code/scheduler.h +++ b/code/scheduler.h @@ -48,4 +48,5 @@ void sigUsr1Handler(int signum); //============================== void createLogFile(); -void logger(char * action, process_t* process_pcb); \ No newline at end of file +void logger(char * action, process_t* process_pcb); +void writePerfFile(); \ No newline at end of file diff --git a/code/scheduler.log b/code/scheduler.log index dbec7af..b13f3fa 100644 --- a/code/scheduler.log +++ b/code/scheduler.log @@ -3,54 +3,18 @@ At time 7 process 1 finished arr 6 total 1 remain 0 wait 0 TA 1 WTA 1.00 At time 16 process 2 started arr 16 total 2 remain 2 wait 0 At time 18 process 2 finished arr 16 total 2 remain 0 wait 0 TA 2 WTA 1.00 At time 18 process 3 started arr 17 total 11 remain 11 wait 1 -At time 21 process 3 stopped arr 17 total 11 remain 8 wait 1 -At time 21 process 3 resumed arr 17 total 11 remain 8 wait 4 -At time 26 process 3 stopped arr 17 total 11 remain 3 wait 4 -At time 26 process 3 resumed arr 17 total 11 remain 3 wait 9 -At time 29 process 3 finished arr 17 total 11 remain 0 wait 9 TA 12 WTA 1.09 +At time 29 process 3 finished arr 17 total 11 remain 0 wait 1 TA 12 WTA 1.09 At time 29 process 4 started arr 28 total 1 remain 1 wait 1 At time 30 process 4 finished arr 28 total 1 remain 0 wait 1 TA 2 WTA 2.00 At time 30 process 5 started arr 29 total 23 remain 23 wait 1 -At time 31 process 5 stopped arr 29 total 23 remain 22 wait 1 -At time 31 process 5 resumed arr 29 total 23 remain 22 wait 2 -At time 36 process 5 stopped arr 29 total 23 remain 17 wait 2 -At time 36 process 6 started arr 34 total 4 remain 4 wait 2 -At time 40 process 6 finished arr 34 total 4 remain 0 wait 2 TA 6 WTA 1.50 -At time 40 process 5 resumed arr 29 total 23 remain 17 wait 11 -At time 41 process 5 stopped arr 29 total 23 remain 15 wait 11 -At time 41 process 5 resumed arr 29 total 23 remain 15 wait 12 -At time 46 process 5 stopped arr 29 total 23 remain 10 wait 12 -At time 46 process 7 started arr 44 total 24 remain 24 wait 2 -At time 51 process 7 stopped arr 44 total 24 remain 19 wait 2 -At time 51 process 5 resumed arr 29 total 23 remain 10 wait 22 -At time 56 process 5 stopped arr 29 total 23 remain 4 wait 22 -At time 56 process 7 resumed arr 44 total 24 remain 19 wait 12 -At time 61 process 7 stopped arr 44 total 24 remain 13 wait 12 -At time 61 process 8 started arr 51 total 4 remain 4 wait 10 -At time 65 process 8 finished arr 51 total 4 remain 0 wait 10 TA 14 WTA 3.50 -At time 65 process 9 started arr 52 total 21 remain 21 wait 13 -At time 66 process 9 stopped arr 52 total 21 remain 20 wait 13 -At time 66 process 10 started arr 56 total 17 remain 17 wait 10 -At time 71 process 10 stopped arr 56 total 17 remain 12 wait 10 -At time 71 process 5 resumed arr 29 total 23 remain 4 wait 42 -At time 74 process 5 finished arr 29 total 23 remain 0 wait 42 TA 45 WTA 1.96 -At time 74 process 7 resumed arr 44 total 24 remain 13 wait 30 -At time 76 process 7 stopped arr 44 total 24 remain 10 wait 30 -At time 76 process 9 resumed arr 52 total 21 remain 20 wait 24 -At time 81 process 9 stopped arr 52 total 21 remain 14 wait 24 -At time 81 process 10 resumed arr 56 total 17 remain 12 wait 25 -At time 86 process 10 stopped arr 56 total 17 remain 6 wait 25 -At time 86 process 7 resumed arr 44 total 24 remain 10 wait 42 -At time 91 process 7 stopped arr 44 total 24 remain 4 wait 42 -At time 91 process 9 resumed arr 52 total 21 remain 14 wait 39 -At time 96 process 9 stopped arr 52 total 21 remain 8 wait 39 -At time 96 process 10 resumed arr 56 total 17 remain 6 wait 40 -At time 101 process 10 finished arr 56 total 17 remain 0 wait 40 TA 45 WTA 2.65 -At time 101 process 7 resumed arr 44 total 24 remain 4 wait 57 -At time 101 process 7 stopped arr 44 total 24 remain 4 wait 57 -At time 101 process 9 resumed arr 52 total 21 remain 8 wait 49 -At time 106 process 9 stopped arr 52 total 21 remain 2 wait 49 -At time 106 process 7 resumed arr 44 total 24 remain 4 wait 62 -At time 109 process 7 finished arr 44 total 24 remain 0 wait 62 TA 65 WTA 2.71 -At time 109 process 9 resumed arr 52 total 21 remain 2 wait 57 -At time 110 process 9 finished arr 52 total 21 remain 0 wait 57 TA 58 WTA 2.76 +At time 53 process 5 finished arr 29 total 23 remain 0 wait 1 TA 24 WTA 1.04 +At time 53 process 8 started arr 51 total 4 remain 4 wait 2 +At time 57 process 8 finished arr 51 total 4 remain 0 wait 2 TA 6 WTA 1.50 +At time 57 process 6 started arr 34 total 4 remain 4 wait 23 +At time 61 process 6 finished arr 34 total 4 remain 0 wait 23 TA 27 WTA 6.75 +At time 61 process 10 started arr 56 total 17 remain 17 wait 5 +At time 78 process 10 finished arr 56 total 17 remain 0 wait 5 TA 22 WTA 1.29 +At time 78 process 9 started arr 52 total 21 remain 21 wait 26 +At time 99 process 9 finished arr 52 total 21 remain 0 wait 26 TA 47 WTA 2.24 +At time 99 process 7 started arr 44 total 24 remain 24 wait 55 +At time 123 process 7 finished arr 44 total 24 remain 0 wait 55 TA 79 WTA 3.29 diff --git a/code/scheduler.perf b/code/scheduler.perf new file mode 100644 index 0000000..8ffd1ed --- /dev/null +++ b/code/scheduler.perf @@ -0,0 +1,3 @@ +CPU utilization = 87.80% +Avg WTA = 2.12 +Avg Waiting = 11.40 diff --git a/code/structs.h b/code/structs.h index 1e5e139..19a4c26 100644 --- a/code/structs.h +++ b/code/structs.h @@ -29,3 +29,21 @@ union SemUn { unsigned short *array; /* Array for GETALL, SETALL */ struct seminfo *__buf; /* Buffer for IPC_INFO (Linux-specific) */ }; + +// To create scheduler.perf. +// The scheduler will create only one instant from this struct +typedef struct perfStats { + + // these are incrementally updated each time a process finishes + int totalWorkingTime; // sum of burst times + int totalWaitingTime; // sum of waiting times + double totalWTA; // sum of weighted turnaround times + int numFinished; // number of finished processes + + // and these are calculated when the last process finishes + double CPU_utilization; // = totalWorkingTime / totalTime + double avgWaitingTime; // totalWaitingTime / N + double avgWTA; // totalWTA / N + double stdWTA; // Standard deviation of weighted turnaround time + //= SUM( (WTA[i] - avgWTA)^2 ) / N +} perfStats; \ No newline at end of file From 0cf09f1d625dbbe4ed59cc87e64b110d7dc780e1 Mon Sep 17 00:00:00 2001 From: g-magdy Date: Wed, 1 May 2024 21:07:40 +0300 Subject: [PATCH 2/2] feat: calculate standard deviation on WTA --- code/scheduler.c | 41 +++++++++++++++++++++++++---------------- code/scheduler.log | 26 +++++++++++++++----------- code/scheduler.perf | 7 ++++--- code/structs.h | 1 + 4 files changed, 45 insertions(+), 30 deletions(-) diff --git a/code/scheduler.c b/code/scheduler.c index cb02b4d..292c2ce 100644 --- a/code/scheduler.c +++ b/code/scheduler.c @@ -17,7 +17,7 @@ int msgQID; process_t *currentProcess = NULL; -perfStats schedulerStats; +perfStats stats; /** * main - The main function of the scheduler. @@ -526,10 +526,10 @@ void createLogFile() { } // to initiate performance statistics counters [maybe not the best place to do it] - schedulerStats.numFinished = 0; - schedulerStats.totalWaitingTime = 0; - schedulerStats.totalWorkingTime = 0; - schedulerStats.totalWTA = 0; + stats.numFinished = 0; + stats.totalWaitingTime = 0; + stats.totalWorkingTime = 0; + stats.totalWTA = 0; printf("Started Logging\n"); fclose(logFileptr); @@ -553,10 +553,11 @@ void logger(char *msg, process_t *p) { if (strcmp(msg, "finished") == 0) { fprintf(logFileptr, " TA %i WTA %.2f", p->TA, WTA); - schedulerStats.numFinished += 1; - schedulerStats.totalWorkingTime += p->BT; - schedulerStats.totalWaitingTime += p->WT; - schedulerStats.totalWTA += WTA; + stats.WTAs[stats.numFinished] = WTA; + stats.numFinished += 1; + stats.totalWorkingTime += p->BT; + stats.totalWaitingTime += p->WT; + stats.totalWTA += WTA; } fprintf(logFileptr, "\n"); @@ -574,12 +575,20 @@ void writePerfFile() { int finalTime = getClk(); - schedulerStats.CPU_utilization = 100.0 * schedulerStats.totalWorkingTime / finalTime; - schedulerStats.avgWTA = (double) schedulerStats.totalWTA / schedulerStats.numFinished; - schedulerStats.avgWaitingTime = (double) schedulerStats.totalWaitingTime / schedulerStats.numFinished; + stats.CPU_utilization = 100.0 * stats.totalWorkingTime / finalTime; + stats.avgWTA = (double) stats.totalWTA / stats.numFinished; + stats.avgWaitingTime = (double) stats.totalWaitingTime / stats.numFinished; - fprintf(perfFile, "CPU utilization = %.2f%%\n", schedulerStats.CPU_utilization); - fprintf(perfFile, "Avg WTA = %.2f\n", schedulerStats.avgWTA); - fprintf(perfFile, "Avg Waiting = %.2f\n", schedulerStats.avgWaitingTime); - // TODO calculate std deviation + // calculate STD Deviation of Weighted Turn around time + double sumSquaresErr = 0; + for (int i = 0; i < stats.numFinished; i++) { + sumSquaresErr += (stats.WTAs[i] - stats.avgWTA) * (stats.WTAs[i] - stats.avgWTA); + } + + stats.stdWTA = sumSquaresErr / stats.numFinished; + + fprintf(perfFile, "CPU utilization = %.2f%%\n", stats.CPU_utilization); + fprintf(perfFile, "Avg WTA = %.2f\n", stats.avgWTA); + fprintf(perfFile, "Avg Waiting = %.2f\n", stats.avgWaitingTime); + fprintf(perfFile, "Std WTA = %.2f\n", stats.stdWTA); } diff --git a/code/scheduler.log b/code/scheduler.log index b13f3fa..c37e91c 100644 --- a/code/scheduler.log +++ b/code/scheduler.log @@ -7,14 +7,18 @@ At time 29 process 3 finished arr 17 total 11 remain 0 wait 1 TA 12 WTA 1.09 At time 29 process 4 started arr 28 total 1 remain 1 wait 1 At time 30 process 4 finished arr 28 total 1 remain 0 wait 1 TA 2 WTA 2.00 At time 30 process 5 started arr 29 total 23 remain 23 wait 1 -At time 53 process 5 finished arr 29 total 23 remain 0 wait 1 TA 24 WTA 1.04 -At time 53 process 8 started arr 51 total 4 remain 4 wait 2 -At time 57 process 8 finished arr 51 total 4 remain 0 wait 2 TA 6 WTA 1.50 -At time 57 process 6 started arr 34 total 4 remain 4 wait 23 -At time 61 process 6 finished arr 34 total 4 remain 0 wait 23 TA 27 WTA 6.75 -At time 61 process 10 started arr 56 total 17 remain 17 wait 5 -At time 78 process 10 finished arr 56 total 17 remain 0 wait 5 TA 22 WTA 1.29 -At time 78 process 9 started arr 52 total 21 remain 21 wait 26 -At time 99 process 9 finished arr 52 total 21 remain 0 wait 26 TA 47 WTA 2.24 -At time 99 process 7 started arr 44 total 24 remain 24 wait 55 -At time 123 process 7 finished arr 44 total 24 remain 0 wait 55 TA 79 WTA 3.29 +At time 34 process 5 stopped arr 29 total 23 remain 19 wait 1 +At time 34 process 6 started arr 34 total 4 remain 4 wait 0 +At time 38 process 6 finished arr 34 total 4 remain 0 wait 0 TA 4 WTA 1.00 +At time 38 process 5 resumed arr 29 total 23 remain 19 wait 9 +At time 51 process 5 stopped arr 29 total 23 remain 5 wait 9 +At time 51 process 8 started arr 51 total 4 remain 4 wait 0 +At time 55 process 8 finished arr 51 total 4 remain 0 wait 0 TA 4 WTA 1.00 +At time 55 process 5 resumed arr 29 total 23 remain 5 wait 26 +At time 59 process 5 finished arr 29 total 23 remain 0 wait 26 TA 30 WTA 1.30 +At time 59 process 10 started arr 56 total 17 remain 17 wait 3 +At time 76 process 10 finished arr 56 total 17 remain 0 wait 3 TA 20 WTA 1.18 +At time 76 process 9 started arr 52 total 21 remain 21 wait 24 +At time 97 process 9 finished arr 52 total 21 remain 0 wait 24 TA 45 WTA 2.14 +At time 97 process 7 started arr 44 total 24 remain 24 wait 53 +At time 121 process 7 finished arr 44 total 24 remain 0 wait 53 TA 77 WTA 3.21 diff --git a/code/scheduler.perf b/code/scheduler.perf index 8ffd1ed..0510479 100644 --- a/code/scheduler.perf +++ b/code/scheduler.perf @@ -1,3 +1,4 @@ -CPU utilization = 87.80% -Avg WTA = 2.12 -Avg Waiting = 11.40 +CPU utilization = 89.26% +Avg WTA = 1.49 +Avg Waiting = 10.80 +Std WTA = 0.49 diff --git a/code/structs.h b/code/structs.h index 19a4c26..4f57d41 100644 --- a/code/structs.h +++ b/code/structs.h @@ -39,6 +39,7 @@ typedef struct perfStats { int totalWaitingTime; // sum of waiting times double totalWTA; // sum of weighted turnaround times int numFinished; // number of finished processes + double WTAs[1000]; // to calculate standard deviation (not best solution) // and these are calculated when the last process finishes double CPU_utilization; // = totalWorkingTime / totalTime