Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate Performance Statistics #23

Merged
merged 2 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions code/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
int msgQID;
process_t *currentProcess = NULL;

perfStats stats;

/**
* main - The main function of the scheduler.
*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -518,6 +525,12 @@ void createLogFile() {
exit(-1);
}

// to initiate performance statistics counters [maybe not the best place to do it]
stats.numFinished = 0;
stats.totalWaitingTime = 0;
stats.totalWorkingTime = 0;
stats.totalWTA = 0;

printf("Started Logging\n");
fclose(logFileptr);
}
Expand All @@ -540,9 +553,42 @@ void logger(char *msg, process_t *p) {

if (strcmp(msg, "finished") == 0) {
fprintf(logFileptr, " TA %i WTA %.2f", p->TA, WTA);
stats.WTAs[stats.numFinished] = WTA;
stats.numFinished += 1;
stats.totalWorkingTime += p->BT;
stats.totalWaitingTime += p->WT;
stats.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();

stats.CPU_utilization = 100.0 * stats.totalWorkingTime / finalTime;
stats.avgWTA = (double) stats.totalWTA / stats.numFinished;
stats.avgWaitingTime = (double) stats.totalWaitingTime / stats.numFinished;

// 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);
}
3 changes: 2 additions & 1 deletion code/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ void sigUsr1Handler(int signum);
//==============================

void createLogFile();
void logger(char * action, process_t* process_pcb);
void logger(char * action, process_t* process_pcb);
void writePerfFile();
64 changes: 16 additions & 48 deletions code/scheduler.log
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,22 @@ 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 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
4 changes: 4 additions & 0 deletions code/scheduler.perf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CPU utilization = 89.26%
Avg WTA = 1.49
Avg Waiting = 10.80
Std WTA = 0.49
19 changes: 19 additions & 0 deletions code/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,22 @@ 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
double WTAs[1000]; // to calculate standard deviation (not best solution)

// 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;