-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scheduler.cpp
372 lines (311 loc) · 10.8 KB
/
Scheduler.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
#include "classes.hpp"
using namespace std;
// Helper function to calculate time diff in milliseconds.
static time_t getDiffMillisec(timeval end, timeval start)
{
return double(end.tv_sec-start.tv_sec) * 1000.0 + double(end.tv_usec-start.tv_usec) / 1000.0 + 0.5;
}
Scheduler::Scheduler() :
shortTermTime(NULL),
projectHandler(NULL),
currentTimeSlot(NULL),
currentTime(0L),
nextSBTime(0L),
nextSBCount(0U),
executeTime(0L),
executeCount(0U),
nextTimeSlotTime(0L),
nextTimeSlotCount(0U)
{
//Nothing to do.
}
Scheduler::~Scheduler()
{
if (shortTermTime != NULL)
delete shortTermTime;
}
void Scheduler::initializePeriod()
{
cout << "SCHEDULER: Initializing current period." << endl;
// Initialize short term time
shortTermTime = new ShortTermTime();
shortTermTime->initializePeriod();
//Get a reference to ProjectHandler instance
projectHandler = shortTermTime->getProjectHandler();
}
void Scheduler::runSchedule()
{
cout << "SCHEDULER: Starting scheduling process." << endl;
currentTime = projectHandler->getStartDate();
// srand(currentTime);
while (currentTime < projectHandler->getEndDate()) {
// //Toggling random time slot availabilities. TODO: Define as optional
// if (currentTimeSlot != NULL) {
// int toggleIndex = currentTimeSlot->getIndex() + rand()%5;
// TimeSlot* toggleSlot = shortTermTime->getTimeSlot(toggleIndex);
// if (toggleSlot != NULL) {
// cout << "SCHEDULER: Toggling availability of time slot " << toggleSlot->getIndex() << endl;
// toggleSlot->setAvailability(!toggleSlot->isAvailable());
// } else {
// cerr << "Current time slot: " << currentTimeSlot->getIndex() << endl;
// }
// }
if (!nextTimeSlot()) {
// No new time slots available
break;
}
while (currentTime < currentTimeSlot->getEndDate()) {
SchedBlock* schedBlock = nextSB();
if (schedBlock != NULL) {
if (!execute(schedBlock))
// Some sort of error while executing SB
break;
} else {
// No other SB available for current timeSlot
cout << "SCHEDULER: No more SchedBlocks available for current time slot." << endl;
break;
}
}
if (currentTime < currentTimeSlot->getEndDate()) {
waitUntil(currentTimeSlot->getEndDate());
}
}
if (currentTime < (projectHandler->getEndDate() - 1)) {
cout << "SCHEDULER: Remaining period time:"
<< difftime(projectHandler->getEndDate(), currentTime) << endl;
waitUntil(projectHandler->getEndDate());
}
cout << "SCHEDULER: Scheduling process completed." << endl;
}
double Scheduler::calculateTotalThroughput()
{
double throughput(0.0);
list<Project*> projects = projectHandler->getCompletedProjects();
list<Project*>::iterator it;
for (it=projects.begin(); it!=projects.end(); it++)
throughput += (*it)->getSciGrade();
double possibleThroughput = throughput;
list<Project*> pendingProjects = projectHandler->getPendingProjects();
list<Project*>::iterator it2;
for (it2=pendingProjects.begin(); it2!=pendingProjects.end(); it2++)
possibleThroughput += (*it2)->getSciGrade();
return throughput / possibleThroughput;
}
double Scheduler::calculateTotalTimeUsage()
{
time_t execTime(0L);
time_t availableTime = shortTermTime->getTotalAvailableTime();
map<time_t,SchedBlock*>::iterator it;
for (it=executionOrder.begin(); it!=executionOrder.end(); it++)
execTime += it->second->getExecTime();
return double(execTime) / double(availableTime);
}
double Scheduler::calculateExecutedSchedBlocks()
{
unsigned int numSchedBlocks(0U);
list<Project*> projects = projectHandler->getProjects();
list<Project*>::iterator it;
for (it=projects.begin(); it != projects.end(); it++)
numSchedBlocks += (*it)->getSchedBlocks().size();
return double(executionOrder.size()) / double(numSchedBlocks);
}
map<time_t,SchedBlock*> Scheduler::getExecutionOrder()
{
return executionOrder;
}
list<Project*> Scheduler::getPendingProjects()
{
return projectHandler->getPendingProjects();
}
double Scheduler::getAvgeNextSBTime()
{
return double(nextSBTime) / double(nextSBCount);
}
double Scheduler::getAvgeExecuteTime()
{
return double(executeTime) / double(executeCount);
}
double Scheduler::getAvgeNextTimeSlotTime()
{
return double(nextTimeSlotTime) / double(nextTimeSlotCount);
}
unsigned int Scheduler::getAvgeShortTermSBListSize()
{
return shortTermTime->getAvgeShortTermSBListSize();
}
unsigned int Scheduler::getAvgeLongTermSBListSize()
{
return shortTermTime->getAvgeLongTermSBListSize();
}
/*Private methods*/
SchedBlock* Scheduler::nextSB()
{
timeval start, end;
gettimeofday(&start, NULL);
SchedBlock* topPriority = currentTimeSlot->getTopPriority();
gettimeofday(&end, NULL);
time_t diff = getDiffMillisec(end, start);
nextSBTime += diff;
nextSBCount++;
return topPriority;
}
bool Scheduler::execute(SchedBlock* schedBlock)
{
if (schedBlock->isExecuted()) {
cerr << "ERROR: SchedBlock " << schedBlock->getProject()->getIndex()
<< "," << schedBlock->getIndex() << " was already executed. Skipping to next TimeSlot."
<< endl;
return false;
}
cout << ">> Executing SchedBlock: " << schedBlock->getProject()->getIndex()
<< "," << schedBlock->getIndex()
<< " " << double(schedBlock->getExecTime())/60.0 << "[min] "
<< currentTime << endl;
timeval start, end;
gettimeofday(&start, NULL);
schedBlock->setExecuted();
executionOrder.insert(make_pair(currentTime,schedBlock));
waitUntil(currentTime + schedBlock->getExecTime());
gettimeofday(&end, NULL);
time_t diff = getDiffMillisec(end, start);
executeTime += diff;
executeCount++;
return true;
}
bool Scheduler::nextTimeSlot()
{
cout << "SCHEDULER: Moving to next time slot." << endl;
timeval start, end;
gettimeofday(&start, NULL);
currentTimeSlot = shortTermTime->nextTimeSlot();
// If currentTimeSlot is NULL, period has ended.
if (currentTimeSlot == NULL) {
cout << "SCHEDULER: No more time slots available." << endl;
return false;
}
gettimeofday(&end, NULL);
time_t diff = getDiffMillisec(end, start);
nextTimeSlotTime += diff;
nextTimeSlotCount++;
cout << "SCHEDULER: Current time slot: " << currentTimeSlot->getIndex()
<< " (" << currentTimeSlot->getLongTermTimeSlot()->getIndex() << ") "
<< currentTime << endl;
return true;
}
void Scheduler::setTimeSlotAvailability(unsigned int slotIndex, bool availability)
{
TimeSlot* timeSlot = shortTermTime->getTimeSlot(slotIndex);
if (timeSlot == NULL or timeSlot == currentTimeSlot)
//Skip if empty reference or time slot currently under execution
return;
if (timeSlot->isAvailable() != availability) {
cout << "SCHEDULER: Setting availability of time slot " << slotIndex
<< " to " << availability << endl;
timeSlot->setAvailability(availability);
}
}
void Scheduler::waitUntil(time_t time)
{
//long diff = long(difftime(time, currentTime));
// cout << "SCHEDULER: Sleeping for " << diff << endl;
//usleep(diff); //Sleep one micro-second for each real second
currentTime = time;
}
/* MAIN */
int main(int argc, const char* argv[])
{
// Getting input parameter
if (argc > 2) {
cerr << "Usage: ./aia-dpm [output_file]" << endl;
return 1;
}
string filename;
if (argc == 2)
filename = argv[1];
else
filename = "output.txt";
Scheduler* scheduler = NULL;
try {
// Initializing and executing the observation period
timeval start, end;
gettimeofday(&start, NULL);
scheduler = new Scheduler();
scheduler->initializePeriod();
gettimeofday(&end, NULL);
time_t initTime = getDiffMillisec(end, start);
gettimeofday(&start, NULL);
scheduler->runSchedule();
gettimeofday(&end, NULL);
time_t execTime = getDiffMillisec(end, start);
// Getting execution data
gettimeofday(&start, NULL);
double throughput = scheduler->calculateTotalThroughput();
double timeUsage = scheduler->calculateTotalTimeUsage();
double totalSbs = scheduler->calculateExecutedSchedBlocks();
double avgeNextSBTime = scheduler->getAvgeNextSBTime();
double avgeExecuteTime = scheduler->getAvgeExecuteTime();
double avgeNextTimeSlotTime = scheduler->getAvgeNextTimeSlotTime();
unsigned int avgeShortTermSBListSize = scheduler->getAvgeShortTermSBListSize();
unsigned int avgeLongTermSBListSize = scheduler->getAvgeLongTermSBListSize();
map<time_t,SchedBlock*> executionOrder = scheduler->getExecutionOrder();
gettimeofday(&end, NULL);
time_t finalTime = getDiffMillisec(end, start);
// Prepare output file
ofstream outputFile;
outputFile.open(filename.c_str(), ios::out);
if (!outputFile.is_open()) {
cerr << "Error opening output file " << filename << endl;
if (scheduler != NULL)
delete scheduler;
return 1;
}
// Print statistics
outputFile << "-------------------------------------------------------" << endl;
outputFile << "Total scientific throughput rate: " << throughput << endl;
outputFile << "Total time usage rate: " << timeUsage << endl;
outputFile << "Total SBs execution rate: " << totalSbs << endl;
outputFile << endl;
outputFile << "Average initial short term SB list size: " << avgeShortTermSBListSize << endl;
outputFile << "Average initial long term SB list size: " << avgeLongTermSBListSize << endl;
outputFile << endl;
outputFile << "Average next SB processing time: " << avgeNextSBTime << "[msec]" << endl;
outputFile << "Average execute SB processing time: " << avgeExecuteTime << "[msec]" << endl;
outputFile << "Average next time slot processing time: " << avgeNextTimeSlotTime << "[msec]" << endl;
outputFile << endl;
outputFile << "Initialization time: " << initTime/1000.0 << "[sec]" << endl;
outputFile << "Period execution time: " << execTime/1000.0 << "[sec]" << endl;
outputFile << "Finalization time: " << finalTime/1000.0 << "[sec]" << endl;
outputFile << "-------------------------------------------------------" << endl;
// Print execution order
char buffer[80];
map<time_t,SchedBlock*>::iterator it;
for (it=executionOrder.begin(); it!=executionOrder.end(); it++) {
//outputFile << it->first << " "
strftime(buffer,80,"%Y-%m-%dT%H:%M:%S ",localtime(&(it->first)));
outputFile << buffer
<< it->second->getProject()->getIndex() << ","
<< it->second->getIndex() << endl;
}
outputFile << "-------------------------------------------------------" << endl;
// Print pending SBs
list<Project*> pendingProj = scheduler->getPendingProjects();
list<Project*>::iterator itProj;
for (itProj=pendingProj.begin(); itProj!=pendingProj.end(); itProj++) {
list<SchedBlock*> pendingSB = (*itProj)->getPendingSchedBlocks();
list<SchedBlock*>::iterator itSB;
for (itSB=pendingSB.begin(); itSB!=pendingSB.end(); itSB++)
outputFile << (*itProj)->getIndex() << "," << (*itSB)->getIndex()
<< " (" << (*itProj)->getSciGrade() << ")" << endl;
}
outputFile << "-------------------------------------------------------" << endl;
// Cleanup
outputFile.close();
delete scheduler;
} catch (...) {
cerr << "ERROR: Something went wrong! Aborting." << endl;
if (scheduler != NULL)
delete scheduler;
return 1;
}
return 0;
}