-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation.cpp
412 lines (368 loc) · 9.42 KB
/
simulation.cpp
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "simulation.hpp"
Simulation::Simulation() :
_t(0),
_deltaT(1),
_tasks(),
_jobs(),
_CPUs(),
_readyJobs(),
_runningJobs(),
preemption_counter(0),
migration_counter(0),
number_of_core_used(0),
number_of_core_necessary(0),
idle_time_counter(0)
{ }
Simulation::Simulation(int nCPU, deque<Task> t) :
_t(0),
_deltaT(1),
_tasks(t),
_jobs(),
_CPUs(nCPU, NULL),
_readyJobs(),
_runningJobs(),
preemption_counter(0),
migration_counter(0),
number_of_core_used(0),
number_of_core_necessary(0),
idle_time_counter(0)
{ }
Simulation::~Simulation()
{ }
/**
* \details Generate the new jobs that appears at the time t
*/
void Simulation::generateNewJobs(int t)
{
for (unsigned int i = 0; i < _tasks.size(); ++i)
{
Task* task = &_tasks[i];
if (t % task->getPeriod() == task->getOffset())
{
_jobs.push_back(Job(&_tasks[i], t));
}
}
}
/**
* \details Remove all jobs whose deadline is in the past and check that they were completed successfully.
* \return true if all jobs whose deadline is in the past are completed successfully, false otherwise.
*/
bool Simulation::cleanAndCheckJobs(int t)
{
if (_jobs.empty()) return true;
// save CPUs
deque<Job> savedCPUs(_CPUs.size());
for (unsigned int i = 0; i < _CPUs.size(); ++i)
{
if (_CPUs[i] != NULL)
{
savedCPUs[i] = (*_CPUs[i]);
}
}
for (deque<Job>::iterator it = _jobs.begin(); it != _jobs.end() and not _jobs.empty(); it++)
{
if (it->getAbsoluteDeadline() < t)
{
if (it->getComputationLeft() != 0)
{
return false;
}
else
{
assert(findInDeque((*it), savedCPUs) == -1);
it = _jobs.erase(it);
}
}
if (it == _jobs.end())
break;
}
// restore CPU
for (unsigned int i = 0; i < _CPUs.size(); ++i)
{
if (savedCPUs[i].getStartTime() != -1)
{ int posJob = findInDeque<Job>(savedCPUs[i], _jobs);
assert(posJob != -1);
_CPUs[i] = &_jobs[posJob];
}
}
return true;
}
/**
* \details Compute the lowest common multiple
* \return The lowest common multiple
*/
long ppcm(long A, long B)
{
if (A == 1) return B;
if (B == 1) return A;
if (A == B) return A;
deque<long> divisorsA;
deque<long> divisorsB;
for (long d = 2; d <= max(A, B); ++d)
{
if (A > 1 and d <= A and A % d == 0)
{
divisorsA.push_back(d);
A /= d;
--d;
}
else if (B > 1 and d <= B and B % d == 0)
{
divisorsB.push_back(d);
B /= d;
--d;
}
}
if (divisorsA.empty()) divisorsA.push_back(A);
if (divisorsB.empty()) divisorsB.push_back(B);
long current_ppcm = 1;
while(not divisorsA.empty())
{
long divisor = divisorsA[0];
divisorsA.erase(divisorsA.begin());
current_ppcm *= divisor;
if (not divisorsB.empty())
{
int positionB = findInDeque<long> (divisor, divisorsB);
if (positionB != -1) divisorsB.erase(divisorsB.begin() + positionB);
}
}
while (not divisorsB.empty())
{
current_ppcm *= (*divisorsB.begin());
divisorsB.erase(divisorsB.begin());
}
return current_ppcm;
}
/**
* \details Compute the study interval
* \return The study interval
*/
long Simulation::computeStudyInterval()
{
long current_ppcm = 1;
for (deque<Task>::iterator it = _tasks.begin(); it != _tasks.end(); ++it)
{
current_ppcm = ppcm(current_ppcm, it->getPeriod());
}
return (2*current_ppcm + maxOffsetOf(_tasks));
}
/**
* \details Compute the largest offset from a deque of tasks
* \return this offset
*/
int Simulation::maxOffsetOf(std::deque<Task> tasks)
{
int maxOffset = 0;
for (deque<Task>::iterator it = tasks.begin(); it != tasks.end(); ++it)
{
maxOffset = max(maxOffset, (*it).getOffset());
}
return maxOffset;
}
/**
* \details Compute the jobs that are actives but not running on the CPU
* \return priority_queue of thoses jobs (sort by deadline)
*/
priority_queue<Job*, std::vector<Job*>, EDFComp<false> > Simulation::getReadyJobs()
{
priority_queue<Job*, std::vector<Job*>, EDFComp<false> > readyJobs;
int currentTime = _t;
for (unsigned int i = 0; i < _jobs.size(); ++i)
{
if (currentTime >= _jobs[i].getStartTime()
and currentTime < _jobs[i].getAbsoluteDeadline()
and _jobs[i].getComputationLeft() > 0
and not isInCPUs(&_jobs[i]))
{
readyJobs.push(&_jobs[i]);
}
}
return readyJobs;
}
/**
* \details Compute the jobs that are running on the CPU
* \return priority_queue of thoses jobs (sort by deadline)
*/
priority_queue<Job*, std::vector<Job*>, EDFComp<true> > Simulation::getRunningJobs()
{
priority_queue<Job*, std::vector<Job*>, EDFComp<true> > runningJobs;
for (deque<Job*>::iterator it = _CPUs.begin(); it != _CPUs.end(); it++)
{
if (*it != NULL)
{
runningJobs.push(*it);
}
}
return runningJobs;
}
/**
* \details Check if a job is running on a CPU
* \return true if this job is running on a CPU, false otherwise
*/
bool Simulation::isInCPUs(Job* j)
{
return (findInDeque<Job*>(j, _CPUs) != -1);
}
/**
* \details Compute the position of the first idle CPU
* \return This position or -1 if all CPUs are busy
*/
int Simulation::positionOfFirstIdleCPU()
{
return findInDeque<Job*>(NULL, _CPUs);
}
/**
* \details Compute the position of the CPU where the job j is running
* \return This position or -1 if j is not in CPUs
*/
int Simulation::findInCPUs(Job* j)
{
return findInDeque<Job*> (j, _CPUs);
}
/**
* \details Check if a ready job need to be preempted with a job running on a CPU
* \return True if a ready job need to be preempted, false otherwise
*/
bool Simulation::JobNeedToBePreempted()
{
if(_readyJobs.top()->getPriority() and not _runningJobs.top()->getPriority())
{
return true;
}
else if(_runningJobs.top()->getPriority() and not _readyJobs.top()->getPriority())
{
return false;
}
else // pure Global EDF
{
return _readyJobs.top()->getAbsoluteDeadline() < _runningJobs.top()->getAbsoluteDeadline();
}
}
/**
* \details Run global EDF (adapted to "pure" global EDF and to EDF-k).
* \return A vector containing the statistics of the system
* vector[0]= average number of preemption
* vector[1]= average number of migration
* vector[2]= average idle time
* vector[3]= number_of_core_necessary
* vector[4]= studyInterval
*/
vector<int> Simulation::runGlobal()
{
long studyInterval = computeStudyInterval();
bool isSchedulable = true;
_jobs.clear();
_t = 0;
while (_t < studyInterval && isSchedulable) // main loop
{
if (DEBUG)
{
cout << endl << "t: " << _t << endl;
cout << "initial CPUs" << endl;
for (unsigned int i = 0; i < _CPUs.size(); ++i)
{
cout << "\tCPU[" << i << "]: ";
if (_CPUs[i] != NULL)
cout << _CPUs[i]->asString();
cout << endl;
}
}
generateNewJobs(_t);
_readyJobs = getReadyJobs();
_runningJobs = getRunningJobs();
int firstIdleCPU = positionOfFirstIdleCPU();
bool availableCPUs = (firstIdleCPU != -1);
// scheduling : assign which jobs goes to which CPUs
while (not _readyJobs.empty() and (availableCPUs or JobNeedToBePreempted() ))
{
// If we get in this loop, we know that the earliest-deadline active job should get a CPU
assert(_readyJobs.top()->getComputationLeft() > 0);
if (availableCPUs)
{
assert(_CPUs[firstIdleCPU] == NULL);
Job* newJob = _readyJobs.top(); _readyJobs.pop();
_CPUs[firstIdleCPU] = newJob;
_runningJobs.push(newJob);
if (DEBUG) cout << "hi to\t" << newJob->asString() << "\tat CPU " << firstIdleCPU << endl;
}
else // all CPUs are busy, preempt the one with the latest deadline
{
Job* oldJob = _runningJobs.top(); _runningJobs.pop();
Job* newJob = _readyJobs.top(); _readyJobs.pop();
int posOldJob = findInDeque<Job*>(oldJob, _CPUs);
assert (posOldJob != -1);
_CPUs[posOldJob] = newJob;
_readyJobs.push(oldJob);
_runningJobs.push(newJob);
preemption_counter++;
if (DEBUG) cout << "bye to\t" << oldJob->asString() << "\tat CPU " << firstIdleCPU << endl;
if (DEBUG) cout << "hi to\t" << newJob->asString() << "\tat CPU " << firstIdleCPU << endl;
}
_readyJobs = getReadyJobs();
_runningJobs = getRunningJobs();
firstIdleCPU = positionOfFirstIdleCPU();
availableCPUs = (firstIdleCPU != -1);
}
// CPUs
if (DEBUG) cout << "CPUS" << endl;
int idle_cpus_count = 0;
for (unsigned int i = 0; i < _CPUs.size(); ++i)
{
if (DEBUG) cout << "\tCPU[" << i << "]: ";
if (_CPUs[i] != NULL)
{
if (DEBUG) cout << _CPUs[i]->asString();
// verifications
assert(findInDeque(_CPUs[i], _CPUs) == (int)i);
assert(_CPUs[i]->getComputationLeft() > 0);
// compute jobs in CPUs
if (_CPUs[i]->getLastCPU_Id() != -1 and _CPUs[i]->getLastCPU_Id() != (int) i)
++migration_counter;
_CPUs[i]->giveCPU(_deltaT, i);
// check if a job is done
if (_CPUs[i]->getComputationLeft() == 0)
{
if (DEBUG) cout << "\tbye!";
_CPUs[i] = NULL;
}
}
else
{
++idle_cpus_count;
++idle_time_counter;
}
if (DEBUG) cout << endl;
}
if ((int)_CPUs.size() - idle_cpus_count > number_of_core_necessary)
{
number_of_core_necessary = _CPUs.size() - idle_cpus_count;
}
// advance time
_t += _deltaT;
isSchedulable = cleanAndCheckJobs(_t);
}
vector<int> result;
if(isSchedulable)
{
result.push_back(preemption_counter);
result.push_back(migration_counter);
result.push_back(idle_time_counter);
result.push_back(number_of_core_necessary);
result.push_back(studyInterval);
}
return result;
}
template <class T>
int findInDeque (T t, deque<T> aDeque)
// return -1 if T is not in aDeque
{
if (aDeque.empty())
throw std::logic_error("findInDeque : Empty Deque !");
for (unsigned int i = 0; i < aDeque.size(); ++i)
if (aDeque[i] == t)
{
return (int)i;
}
return -1;
}