-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilityFunctions.c
147 lines (124 loc) · 2.5 KB
/
utilityFunctions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Author: Akhil Tarikere
* Date: 9/3/20
*
* This module contains a few utility functions.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "configuration.h"
#include "functionPeriodic.h"
#include "functionNonPeriodic.h"
extern FILE *outputFile;
/*
* Utility function to find GCD of 'a' and 'b'.
*/
int
gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
/*
* Utility function to find absolute value of 'x'.
*/
float
floatAbs(float x)
{
if (x > 0)
return x;
else
return -x;
}
/*
* Finds and returns the CPU utilisation.
* CPU utilisation = sum(ei/pi)
*/
float
calculateCpuUtilisation(Task *tasks, int numTasks)
{
float sum = 0;
for (int i = 0; i < numTasks; ++i)
{
sum += ((float)tasks[i].wcet / tasks[i].period);
}
return sum;
}
/*
* Finding hyperperiod is the same as LCM for in-phase jobs.
* This method is essentially to find the LCM.
*/
int
findHyperPeriod(Task *tasks, int n)
{
int ans = tasks[0].period;
for (int i = 1; i < n; i++)
ans = (((tasks[i].period * ans)) / (gcd((int)tasks[i].period, ans)));
return ans;
}
/*
* Finds the total number of task-instances that are to be created
* in the case of periodic tasks.
*/
int
findNumJobs(Task *tasks, int numTasks, int hyperperiod)
{
int numJobs = 0;
// Calculating the number of total jobs.
for (int i = 0; i < numTasks; ++i)
{
numJobs += hyperperiod / tasks[i].period;
}
return numJobs;
}
/*
* Checks to see if the number of arguments given to the main driver
* is equal to two. If not, it exits the program.
*/
void
firstCheck(int argc, char *argv[])
{
// Test to see if user has entered the file name or not.
if (argc != 2)
{
fprintf(stderr, "Please enter valid arguments. Program exiting.\n");
exit(0);
}
}
/*
* Tries to open any files given to it.
* Returns the file if it opened it successfully, else exits.
*/
FILE
*inputFileCheck(char *fileName)
{
FILE *file = fopen(fileName, "r");
// Checking if there is an error.
if (!file)
{
fprintf(stderr, "Could not open file: %s\nProgram exiting.\n", fileName);
exit(0);
}
return file;
}
/*
* Finds the total CPU utilisation of the periodic tasks, if > 1
* it exits the program.
*/
void
checkCpuUtilisation(Task *tasks, int numTasks)
{
float cpuUtilisation = calculateCpuUtilisation(tasks, numTasks);
if (cpuUtilisation <= 1.0f)
{
fprintf(outputFile, "CPU Utilisation <= 1: might be schedulable.\n");
}
else
{
fprintf(stderr, "CPU Utilisation > 1: cannot be scheduled\n");
exit(0);
}
}