-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskSet.cpp
More file actions
350 lines (308 loc) · 8.7 KB
/
TaskSet.cpp
File metadata and controls
350 lines (308 loc) · 8.7 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/************************************************************************************************
* File: TaskSet.cpp
* Author: Stephen Thomson
* Date: 2/8/2024
* Description: This file contains the implementation of the TaskSet class. This class is used to
* store a list of tasks and perform operations on them.
*************************************************************************************************/
#include "TaskSet.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <numeric>
#include "Task.h"
#include "TaskSet.h"
int m_numTasks = 0;
int m_numProcessors = 0;
bool m_preemptive = false;
std::vector<Task> m_tasks;
TaskSet::TaskSet()
{
m_numTasks = 0;
m_numProcessors = 0;
m_tasks.clear();
m_preemptive = false;
}
TaskSet::~TaskSet()
{
m_numTasks = 0;
m_numProcessors = 0;
m_tasks.clear();
}
TaskSet::TaskSet(int numTasks, int numProcessors, bool preemptive)
{
m_numTasks = numTasks;
m_numProcessors = numProcessors;
m_preemptive = preemptive;
}
TaskSet& TaskSet::operator=(const TaskSet& taskSet)
{
if (this == &taskSet)
{
return *this;
}
m_numTasks = taskSet.m_numTasks;
m_numProcessors = taskSet.m_numProcessors;
m_preemptive = taskSet.m_preemptive;
m_tasks = taskSet.m_tasks;
return *this;
}
void TaskSet::addTask(Task task)
{
m_tasks.push_back(task);
if (m_numTasks < m_tasks.size()) {
m_numTasks++;
}
}
std::vector<Task> TaskSet::getTasks()
{
return m_tasks;
}
void TaskSet::removeTask(int index) {
m_tasks.erase(m_tasks.begin() + index);
}
Task TaskSet::getTask(int index) {
return m_tasks[index];
}
void TaskSet::printTasks()
{
std::cout << "Tasks: " << m_numTasks << std::endl;
std::cout << "Processors: " << m_numProcessors << std::endl;
std::cout << "Preemptive: " << (m_preemptive ? "True":"False") << std::endl;
for (int i = 0; i < m_numTasks; i++)
{
// Print Task: i, then call m_tasks[i].printTask() all on the same line.
std::cout << m_tasks[i].getName() << ", "; m_tasks[i].printTask();
}
}
// this is caculateUtilizationRate: calculateUtilizationRate(m_tasks[i], m_tasks[i].getComputationTime());
double TaskSet::calculateUtilizationRate(Task task)
{
return task.getComputationTime() / task.getHardDeadline();
}
double TaskSet::calculateTotalUtilizationRate()
{
double totalUtilizationRate = 0;
for (int i = 0; i < m_numTasks; i++)
{
totalUtilizationRate += calculateUtilizationRate(m_tasks[i]);
}
return totalUtilizationRate;
}
int TaskSet::getNumTasks()
{
return m_numTasks;
}
void TaskSet::setNumProcessors(int numProcessors)
{
m_numProcessors = numProcessors;
}
int TaskSet::getNumProcessors() {
return m_numProcessors;
}
void TaskSet::setNumTasks(int numTasks)
{
m_numTasks = numTasks;
}
void TaskSet::setPreemptive(bool preemptive)
{
m_preemptive = preemptive;
}
bool TaskSet::getPreemptive()
{
return m_preemptive;
}
int TaskSet::EUSI()
{
//s = start time
//d = hard deadline
//c = computation time
// Step 1, calculate Requested Execution Time (Ri) for each task
// Create an int vector to store the Ri for each task
vector<int> Ri;
// Make computations for each task
// Start Loop i to numtasks
// int Ri1Value = 0
// Start Loop j to less than equal to i
// Ri1Value += cj
// End Loop j
// Set Ri[i] to Ri1Value;
// End Loop i
for (int i = 0; i < m_numTasks; i++)
{
int Ri1Value = 0;
for (int j = 0; j <= i; j++)
{
Ri1Value += m_tasks[j].getComputationTime();
}
Ri.push_back(Ri1Value);
}
// Start Loop i to numtasks
// Create int riValue and set to Ri[i]
// Start Loop j at i+1 to numtasks
// if dj<(di+cj)
// Add to riValue(cj+di-dj)
// End Loop j
// Update Ri[i] to riValue
for (int i = 0; i < m_numTasks; i++)
{
int riValue = Ri[i];
for (int j = i + 1; j < m_numTasks; j++)
{
if (m_tasks[j].getHardDeadline() < (m_tasks[i].getHardDeadline() + m_tasks[j].getComputationTime()))
{
riValue += (m_tasks[j].getComputationTime() + m_tasks[i].getHardDeadline() - m_tasks[j].getHardDeadline());
}
}
Ri[i] = riValue;
}
// Step 2, calculate Available Execution Time (Ai) for each task
// Create an int vector to store the Ai for each task
vector<int> Ai;
// Make computations for each task
// Start Loop i to numTasks
// int Ai1Value = 0
// Start Loop j start 0 to numtasks
// if dj<=si
// Ai1Value = Ai[i] + cj
// End Loop j
// Set Ai[i] to Ai1Value
for (int i = 0; i < m_numTasks; i++)
{
int Ai1Value = 0;
Ai.push_back(Ai1Value);
for (int j = 0; j < m_numTasks; j++)
{
if (m_tasks[j].getHardDeadline() <= m_tasks[i].getStartTime())
{
Ai1Value = Ai[i] + m_tasks[j].getComputationTime();
}
}
Ai[i] = Ai1Value;
}
// Start Loop i to numtasks
// Create int aiValue and set to Ai[i]
// Start Loop j at 0 to numtasks
// if dj>si and sj<si
// aiValue += (minimum value of (cj or (si-sj)))
// End Loop j
// Set Ai[i] to aiValue
// End Loop i
for (int i = 0; i < m_numTasks; i++)
{
int aiValue = Ai[i];
for (int j = 0; j < m_numTasks; j++)
{
if (m_tasks[j].getHardDeadline() > m_tasks[i].getStartTime() && m_tasks[j].getStartTime() < m_tasks[i].getStartTime())
{
aiValue += min(m_tasks[j].getComputationTime(), (m_tasks[i].getStartTime() - m_tasks[j].getStartTime()));
}
}
Ai[i] = aiValue;
}
// Step 3, calculate the EUSI for each task
// Create an int vector to store the EUSI for each task
vector<int> EUSI;
// Make computations for each task
//Start Loop i start 0 to numtasks
// Create vector to hold value of each loop
//vector<int> EUSIValue;
// Start Loop j start 0 to numtasks
// Calculate (Ri[i] - Ai[j])/(di-sj) and push absolute value into EUSIValue
// End Loop j
// Insert into EUSI[i] the Maximum value (Highest Value) in EUSIValue)
// End Loop i
for (int i = 0; i < m_numTasks; i++)
{
int EUSIValue = 0;
for (int j = 0; j < m_numTasks; j++)
{
if ((m_tasks[i].getHardDeadline() - m_tasks[j].getStartTime()) != 0)
{
int calcVal = (ceil((Ri[i] - Ai[j]) / static_cast<double>(m_tasks[i].getHardDeadline() - m_tasks[j].getStartTime())));
if (EUSIValue < calcVal)
{
EUSIValue = calcVal;
}
}
}
EUSI.push_back(EUSIValue);
}
// Step 4, find the maximum EUSI value
// Got through vector to find the maximum value
int maxEUSI = EUSI[0];
for (int i = 1; i < m_numTasks; i++)
{
if (EUSI[i] > maxEUSI)
{
maxEUSI = EUSI[i];
}
}
// Step 5, return the maximum EUSI value
return maxEUSI;
}
int TaskSet::USI()
{
// int vector to hold USI calculations for each task
vector<int> USICalculations;
// int USI
int USI = 0;
// Begin Loop i from 0 to m_numTasks
for (int i = 0; i < m_numTasks; i++)
{
// int to hold total
int total = 0;
// Begin Loop j from 0 to i
for (int j = 0; j <= i; j++)
{
// total += task j computation time
total += m_tasks[j].getComputationTime();
}
// USI = total/task i hard deadline rounded up to nearest integer
USI = ceil(static_cast<double>(total) / m_tasks[i].getHardDeadline());
// push USI into int vector
USICalculations.push_back(USI);
// set USI to 0
USI = 0;
}
int finalUSI = 0;
// Go through vector of USI calculations and find biggest number
for (int i = 0; i < USICalculations.size(); i++)
{
if (USICalculations[i] > finalUSI)
{
finalUSI = USICalculations[i];
}
}
// Return the highest int in vector.
return finalUSI;
}
bool TaskSet::checkProcessors()
{
int USIValue = USI();
int EUSIValue = EUSI();
if (USIValue <= m_numProcessors && EUSIValue <= m_numProcessors)
{
return true;
}
else
{
return false;
}
}
unsigned long long TaskSet::getLCM()
{
unsigned long long leastcm = m_tasks[0].getPeriod();
for (int i = 1; i < m_numTasks; i++)
{
leastcm = lcm(leastcm, m_tasks[i].getPeriod());
}
if (leastcm > 500 || leastcm < 0)
{
leastcm = 500;
}
return leastcm;
}