-
Notifications
You must be signed in to change notification settings - Fork 0
/
edf-sim.cpp
504 lines (411 loc) · 12.6 KB
/
edf-sim.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string>
using namespace std;
bool DEBUG;
class task_set
{
public:
int p[10]; //periods in milli-seconds
double c[10]; // worst-case execution times in micro-seconds
//double e_c[10];
long hyper_period;
};
bool preemptive_edf_schedule(task_set set, int n, int c_time = 0, int w = 1)
{
//cout << endl;
long hyper_period = set.hyper_period * 1000; //microseconds
//cout << "hyper_period: " <<hyper_period << endl;
bool active_jobs[10];
double remaining_execution[10];
for(int i = 0; i < n; i++)
{
active_jobs[i] = false;
remaining_execution[i] = 0;
}
int current_task = -1; // task index of highest priority job
int previous_task = 0;
long last_empty_slot = -1;
int halt_execution = 0;
long time_elapsed;
for(time_elapsed = 1; time_elapsed <= hyper_period; time_elapsed++)
{
//find job released
for(int i = 0; i < n; i++)
{
if(((time_elapsed - 1) % (set.p[i] * 1000)) == 0) // new job has been released of a task
{
//cout << "at t= " << time_elapsed << " : " ;
//cout << "J(" << i + 1 << "," << (time_elapsed / (set.p[i] * 1000)) +1 <<") " << endl;
//check if the previous job still needs time
if(remaining_execution[i] != 0)
{
if(DEBUG) cout << "preemptive: J(" << i + 1 << "," << (time_elapsed / (set.p[i] * 1000)) +1 << ") has missed deadline at t= " << time_elapsed - 1 << " us, exe_remain: " << remaining_execution[i] << " us" << endl;
return false;
}
// activate the job, set the execution time
active_jobs[i] = true;
remaining_execution[i] = set.c[i] * w;
//cout << "Acitve job: " << set.p[i]*1000 << " Ex_remaining: " << remaining_execution[i] << endl;
}
}
// check for highest priority
current_task = -1;
for(int i = 0; i < n; i++)
{
if(active_jobs[i])
{
current_task = i;
/*if ((current_task != previous_task) && (remaining_execution[previous_task] != 0))
{
cout << "at t= " << time_elapsed-1 << ", J(" << previous_task+1 << "," << (time_elapsed / (set.p[previous_task] * 1000)) +1 << ") with remaining_execution: " <<remaining_execution[previous_task] << " of us, has been preempted by J(" << current_task+1 << "," << (time_elapsed / (set.p[current_task] * 1000)) +1 << ") !" << endl;
}*/
//cout << "at t = " << time_elapsed << " active job: " << "J(" << i+1 <<"," << (time_elapsed / (set.p[i] * 1000)) +1 << ")" << endl;
break;
}
}
if (current_task == -1) // no jobs to execute
{
if(last_empty_slot == -1)
{
last_empty_slot = time_elapsed - 1;
//cout << "last_empty_slot: " << last_empty_slot << endl;
}
continue;
}
// check for the context switch time
if(previous_task != current_task)
{
if(last_empty_slot == -1)
{
halt_execution = c_time;
}
else if(time_elapsed - 1 - last_empty_slot < c_time)
{
halt_execution = time_elapsed - 1 - last_empty_slot;
}
}
if(halt_execution == 0)
{
remaining_execution[current_task]--; //execute one time unit
last_empty_slot = -1;
if(remaining_execution[current_task] == 0) //inactivate job if all execution done
{
active_jobs[current_task] = false;
remaining_execution[current_task] = 0;
//cout << "at t = " << time_elapsed << ", J(" << current_task+1 <<"," << (time_elapsed / (set.p[current_task] * 1000)) +1 << ") finished!" <<endl;
//time_elapsed += c_time; // add switch time since starting a new job
}
}
//check at any particular time what jobs are active
/*if (time_elapsed == 1)
{
cout << "at t = " << time_elapsed << endl;
for(int i = 0; i < n; i++)
{
if(active_jobs[i]) cout << "J(" << i+1 <<"," << (time_elapsed / (set.p[i] * 1000)) +1 << ") , remaining_execution: " << remaining_execution[i] << endl;
}
}*/
if(halt_execution > 0) halt_execution--;
previous_task = current_task;
}
return true;
}
bool non_preemptive_edf_schedule(task_set set, int n, int c_time = 0, int w = 1)
{
//cout << endl;
long hyper_period = set.hyper_period * 1000; //microseconds
//cout << "hyper_period: " <<hyper_period << endl;
bool active_jobs[10];
double remaining_execution[10];
for(int i = 0; i < n; i++)
{
active_jobs[i] = false;
remaining_execution[i] = 0;
}
int current_task = -1; // task index of highest priority job
int previous_task = 0;
long last_empty_slot = -1;
int halt_execution = 0;
long time_elapsed;
for(time_elapsed = 1; time_elapsed <= hyper_period; time_elapsed++)
{
//find job released
for(int i = 0; i < n; i++)
{
if(((time_elapsed - 1) % (set.p[i] * 1000)) == 0) // new job has been released of a task
{
//cout << "at t= " << time_elapsed << " : " ;
//cout << "J(" << i + 1 << "," << (time_elapsed / (set.p[i] * 1000)) +1 <<") " << endl;
//check if the previous job still needs time
if(remaining_execution[i] != 0)
{
if(DEBUG) cout << "non-preemptive: J(" << i + 1 << "," << (time_elapsed / (set.p[i] * 1000)) +1 << ") has missed deadline at t= " << time_elapsed - 1 << " us, exe_remain: " << remaining_execution[i] << " us" << endl;
return false;
}
// activate the job, set the execution time
active_jobs[i] = true;
remaining_execution[i] = set.c[i] * w;
//cout << "Acitve job: " << set.p[i]*1000 << " Ex_remaining: " << remaining_execution[i] << endl;
}
}
// check if the current task is still running
if(remaining_execution[previous_task] != 0)
{
current_task = previous_task;
}
else
{
// previous task finished, check for highest priority task now
current_task = -1;
for(int i = 0; i < n; i++)
{
if(active_jobs[i])
{
current_task = i;
//cout << "at t = " << time_elapsed << " active job: " << "J(" << i+1 <<"," << (time_elapsed / (set.p[i] * 1000)) +1 << ")" << endl;
break;
}
}
}
if (current_task == -1) // no jobs to execute
{
if(last_empty_slot == -1)
{
last_empty_slot = time_elapsed - 1;
//cout << "last_empty_slot: " << last_empty_slot << endl;
}
continue;
}
// check for the context switch time
if(previous_task != current_task)
{
if(last_empty_slot == -1)
{
halt_execution = c_time;
}
else if(time_elapsed - 1 - last_empty_slot < c_time)
{
halt_execution = time_elapsed - 1 - last_empty_slot;
}
}
if(halt_execution == 0)
{
remaining_execution[current_task]--; //execute one time unit
last_empty_slot = -1;
if(remaining_execution[current_task] == 0) //inactivate job if all execution done
{
active_jobs[current_task] = false;
remaining_execution[current_task] = 0;
//cout << "at t = " << time_elapsed << ", J(" << current_task+1 <<"," << (time_elapsed / (set.p[current_task] * 1000)) +1 << ") finished!" <<endl;
}
}
//check at any particular time what jobs are active
/*if (time_elapsed == 1)
{
cout << "at t = " << time_elapsed << endl;
for(int i = 0; i < n; i++)
{
if(active_jobs[i]) cout << "J(" << i+1 <<"," << (time_elapsed / (set.p[i] * 1000)) +1 << ") , remaining_execution: " << remaining_execution[i] << endl;
}
}*/
if(halt_execution > 0) halt_execution--;
previous_task = current_task;
}
return true;
}
int gcd(int a, int b)
{
if (b == 0) return a;
return gcd(b, a%b);
}
int lcm(int a [], int n)
{
int res = 1;
for (int i = 0; i < n; i++)
{
res = (res*a[i])/gcd(res, a[i]);
}
return res;
}
double cal_utilization(task_set set, int n, double w)
{
double utilization = 0;
for (int i = 0; i < n; i++)
{
double temp_uti = (w * set.c[i]) / (set.p[i] * 1000);
utilization += temp_uti;
}
if (utilization > 1) return -100;
return utilization;
}
bool utilizaiton_condtion(task_set set, int n, int w = 1)
{
double utilization = 0;
for (int i = 0; i < n; i++)
{
double temp_uti = (w * set.c[i]) / (set.p[i] * 1000);
utilization += temp_uti;
}
return (utilization <=1)? true : false;
//return utilization;
}
//jeffay condition
bool non_preemptive_condition(task_set set, int n)
{
int p_1 = set.p[0] * 1000;
//cout << "P_1 : " << p_1 << endl;
for(int i = 1; i < n; i++)
{
int p_i = set.p[i] * 1000;
double c_i = set.c[i];
//cout << "p_i: " << p_i << " c_i: " << c_i << endl;
//int L = p_1 + (p_i - p_1);
for(int x = p_1 +1; x < p_i; x++)
{
double temp_cost = c_i;
//cout << "Temp cost: " <<temp_cost << endl;
for(int j = 0; j < i-1; j++)
{
temp_cost += (floor(((x - 1) * 1.0)/ (set.p[j] * 1000)) * set.c[j]);
}
if(x < temp_cost)
{
cout << "Failed Jeffay test : L = " << x << " cost = " << temp_cost << endl;
return false;
}
}
}
return true;
}
void quick_sort(task_set & arr, int left, int right)
{
int i = left, j = right;
int pivot = arr.p[left];
while (i <= j)
{
while (arr.p[i] < pivot)
i++;
while (arr.p[j] > pivot)
j--;
if (i <= j) {
int tmp, tmc;
tmp = arr.p[i]; tmc = arr.c[i];
arr.p[i] = arr.p[j]; arr.c[i] = arr.c[j];
arr.p[j] = tmp; arr.c[j] = tmc;
i++;
j--;
}
};
if (left < j)
quick_sort(arr, left, j);
if (i < right)
quick_sort(arr, i, right);
}
int main(int argc, char* argv[])
{
if(argc > 2)
{
cout << endl;
cout << "Usage: g++ hw3_q1.cpp -o out && ./out arg1" << endl;
cout << "arg1: true or false (DEBUG activate or deactivate)" << endl;
cout << endl;
exit(1);
}
if(argc ==2)
{
string str(argv[1]);
if(str == "true"){DEBUG = true;}
else{DEBUG = false;}
}
else{
DEBUG = false;
}
srand(time(NULL));
//task tasks [25][10]; // 25 task sets each having 10 periodic tasks
int context_switch_time[5] = {6, 206, 406, 806, 1006};
task_set task_system[25];
int no_of_task_set = 0;
do{
task_set candidate_set;
for (int j = 0; j < 10; j++)
{
int period = rand() % 8 + 3; // generate random periods between 3 - 10 milli-seconds
int exe_time = rand() % 501 + 1; // execution time between 1 - 500 micro seconds
if(exe_time <= 0) exe_time = 1;
candidate_set.p[j]= period;
candidate_set.c[j] = exe_time;
}
quick_sort(candidate_set, 0, 9);
if (utilizaiton_condtion(candidate_set, 10) && non_preemptive_condition(candidate_set, 10))
{
candidate_set.hyper_period = lcm(candidate_set.p, 10);
task_system[no_of_task_set] = candidate_set;
no_of_task_set++;
/*for(int j=0; j < 10; j++)
{
cout << "T"<< j+1 << ": " <<candidate_set.p[j] << " " << candidate_set.c[j] / 1000 << endl;
}
cout << endl;*/
}
}while(no_of_task_set < 25);
/*
* ***************************************************Context switch time***********************************
*/
for(int c=0; c < 5; c++)
{
cout << "================ context switch time: " << context_switch_time[c] <<" us =======================" << endl;
if(!DEBUG) cout << "Scheduling tasks ..." << endl;
int preemptive_w = 0;
int non_preemptive_w = 0;
double preemptive_uti = 0;
double non_preemptive_uti = 0;
for(int i = 0; i < no_of_task_set; i++)
{
//task system
if(DEBUG){
cout << "Tasks (p_i, c_i), (time in ms): ";
for(int l = 0; l < 10; l++)
{
cout << "(" << task_system[i].p[l] << "," << task_system[i].c[l] / 1000 << ") ";
}
cout << endl;
}
//preemptive test
int j;
for(j = 2;; j++)
{
if(!preemptive_edf_schedule(task_system[i], 10, context_switch_time[c], j)) // context switch time 6
{
preemptive_w += (j -1);
preemptive_uti += cal_utilization(task_system[i], 10, j-1);
break;
}
}
//non-preemptive test
int k;
for(k = 2;; k++)
{
if(!non_preemptive_edf_schedule(task_system[i], 10, context_switch_time[c], k)) // context switch time 6
{
non_preemptive_w += (k -1);
non_preemptive_uti += cal_utilization(task_system[i], 10, k-1);
break;
}
}
if(DEBUG) cout << "Task System: " << i+1 << ", preemptive_breakdown_w: " << j-1 << ", non-preemptive_breakdown_w: " << k-1 << endl;
if(DEBUG) cout << "--------------------------------------------------------------------"<< endl;
}
cout << "preemptive_w_average: " << preemptive_w / (no_of_task_set * 1.0) << endl;
cout << "non-preemptive_w_average: " << non_preemptive_w / (no_of_task_set * 1.0) << endl;
cout << "preemptive_avg_breakdown_uti: " << preemptive_uti / (no_of_task_set * 1.0) << endl;
cout << "non-preemptive_avg_breakdown_uti:: " << non_preemptive_uti / (no_of_task_set * 1.0) << endl;
cout << "For context switch time: " << context_switch_time[c] << " us" << endl;
cout << "=====================================================================" << endl;
}
// test random task set
return 0;
}