-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.cpp
405 lines (325 loc) · 7.03 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
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
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#include<iostream>
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;
enum PriorityType {NP_EDF, P_EDF, NP_RM, P_RM};
class Task
{
protected:
double period;
double execcost;
double relativedeadline;
int tid;
public:
double nexttaskat;
Task(const Task &ntask)
{
nexttaskat = 0;
period = ntask.period;
execcost = ntask.execcost;
relativedeadline = ntask.relativedeadline;
tid = ntask.tid;
}
int gettaskid()
{
return tid;
}
Task (double nperiod, double nexeccost, double nrelativedeadline, int ntid)
{
period = nperiod;
execcost = nexeccost;
relativedeadline = nrelativedeadline;
tid = ntid;
}
double getPeriod()
{
return period;
}
};
class Job: public Task
{
protected:
double executedtime;
double remainingexectime;
double absolutedeadline;
int deadlinemiss;
double cur_priority, original_priority;
int waiting, waitingonid;
int jobid, job_in_taskid;
double begin;
void initjob (double inittime)
{
waiting = 0;
waitingonid = -1;
deadlinemiss = 0;
executedtime = 0;
remainingexectime = execcost;
absolutedeadline = inittime + relativedeadline;
begin = inittime;
}
public:
Job (Task ntask, double inittime, PriorityType ptype, int njobid, int njob_in_taskid): Task(ntask)
{
//Setup IDs
jobid = njobid;
job_in_taskid = njob_in_taskid;
initjob(inittime);
setinitpriority(ptype);
}
int getjobid()
{
return jobid;
}
void setbegin(double b)
{
begin = b;
}
void setinitpriority(PriorityType ptype)
{
switch(ptype)
{
case NP_EDF:
case P_EDF: cur_priority = absolutedeadline;
break;
case NP_RM:
case P_RM: cur_priority = period;
break;
}
original_priority = cur_priority;
}
double getpriority()
{
return cur_priority;
}
int exec(double timeslice, double absolutetime) //1 means done, 0 means not done
{
executedtime += timeslice;
remainingexectime -= timeslice;
cerr << "Remaining Time : "<< remainingexectime << endl;
if (absolutetime > absolutedeadline)
deadlinemiss=1;
if (remainingexectime < 0.00001)
return 1;
return 0;
}
void print(double now, int pno)
{
cout << begin << "\t" << now << "\t" << pno << "\t" << tid << "\t" << jobid << endl;
}
};
class Job_Queue{
protected:
vector <Job *> jobs;
void swap (int i,int j) //swap elements i and j
{
iter_swap(jobs.begin() + i, jobs.begin() + j);
}
public:
Job_Queue ()
{
}
int size()
{
return jobs.size();
}
Job * getTopJob()
{
return jobs.at(0);
}
void removeTopJob()
{
jobs.erase(jobs.begin());
}
void addJob(Job * j)
{
jobs.push_back(j);
}
void sort_queue() //selection sort
{
for(int i = 0; i < jobs.size()-1; i++)
{
double min = jobs.at(i)->getpriority();
int min_at = i;
for (int j = i+1; j < jobs.size(); j++ )
{
if( jobs.at(j)->getpriority() < min)
{
min = jobs.at(j)->getpriority();
min_at = j;
}
}
if (min_at != i)
{
swap(i, min_at);
}
}
}
};
class Processor{
protected:
double granularity, contextswitchtime;
double hp;
vector <Task> taskList;
vector <int> taskListIDcount;
Job_Queue queue;
int ProcNumber;
int nextnewjobID;
double time;
PriorityType ptype;
int justpushnewjobs()
{
int ret = 0;
cerr << time << endl;
for(int i = 0; i < taskList.size(); i++)
{
if((taskList.at(i).nexttaskat - time) < 0.0001)
{
cerr << "New Job!!" << endl;
//add job
//Job (Task ntask, double inittime, PriorityType ptype, int njobid, int njob_in_taskid): Task(ntask)
Job * j = new Job(taskList.at(i), time, ptype, getnewjobID(), taskListIDcount.at(i));
taskListIDcount.at(i)++;
queue.addJob(j);
taskList.at(i).nexttaskat += taskList.at(i).getPeriod();
cerr << "next New Job at"<< taskList.at(i).nexttaskat<< endl;
ret = 1;
}
}
return ret;
}
void getNewJobs()
{
int wasempty = queue.size(); //to avoid context switch on idle
int njob = justpushnewjobs();
if((ptype == P_EDF || ptype == P_RM) && njob == 1)
{
Job * f = queue.getTopJob();
queue.sort_queue();
if(f != queue.getTopJob() && time != 0 && wasempty != 0)
{
f->print(time, ProcNumber);
cout << time << "\t" << time + contextswitchtime << "\t" << "-1" << "\t" << f->gettaskid() << "\t" << f->getjobid() << endl;
int nj = 0;
for(double k = 0; k < contextswitchtime; k+=granularity)
{
time+= granularity;
nj += justpushnewjobs();
}
if(nj > 0)
queue.sort_queue();
queue.getTopJob()->setbegin(time);
}
}
}
public:
Processor(int number, PriorityType p, double grn, double ctxt, double nhp)
{
hp = nhp;
granularity = grn;
contextswitchtime = ctxt;
ptype = p;
time = 0;
ProcNumber = number;
nextnewjobID = number*1000+1;
}
int getnewjobID()
{
nextnewjobID++;
return nextnewjobID-1;
}
void addTask (Task t)
{
taskList.push_back(t);
taskListIDcount.push_back(0);
}
int exec(double slice)
{
if(time >= hp)return -1;
getNewJobs();
if(queue.size() == 0)
{
time+=slice;
return time;
}
if(time == 0)
queue.sort_queue();
//int exec(double timeslice, double absolutetime) //1 means done, 0 means not done
Job * j = queue.getTopJob();
int retval = j->exec(slice,time);
time+=slice;
cerr << "exec with retval " << retval << endl;
if(retval == 1)
{
//done with job, print and remove
j->print(time, ProcNumber);
cout << time << "\t" << time + contextswitchtime << "\t" << "-1" << "\t" << j->gettaskid() << "\t" << j->getjobid() << endl;
queue.removeTopJob();
int nj = 0;
for(double i = 0; i < contextswitchtime; i+=slice)
{
nj += justpushnewjobs();
time+= slice;
}
if(queue.size() == 0)
{
return time;
}
if(ptype == NP_EDF || ptype == NP_RM || nj > 0)
{
queue.sort_queue();
}
queue.getTopJob()->setbegin(time);
}
return time;
}
void finishup()
{
if(queue.size() == 0)
return;
Job * j = queue.getTopJob();
j->print(time-granularity, ProcNumber);
}
};
int main(void)
{
double granularity = 0.1;
double contextswitchtime = 0.1;
int n, hp, pcnt;
cin >> n;
cin >> hp;
cin >> pcnt;
Processor **P;
P = new Processor *[pcnt];
for(int i = 0; i < pcnt; i++)
{
P[i] = new Processor(i, P_RM, granularity, contextswitchtime, hp);
}
cout << hp << "\t" << n <<endl;
for(int i = 0; i < n;i++)
{
double compute, period;
int proc;
cin >> compute;
cin >> period;
cin >> proc;
Task t(period, compute, period, i);
P[proc]->addTask(t);
}
for(double time = 0; time <= hp; time+=granularity)
{
for(int j = 0; j < pcnt; j++)
{
P[j]->exec(granularity);
}
}
for(int i = 0; i < pcnt; i++)
{
P[i]->finishup();
}
cout << "-1";
return 0;
}