-
Notifications
You must be signed in to change notification settings - Fork 0
/
simEDFkClass.cpp
167 lines (139 loc) · 3.48 KB
/
simEDFkClass.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
#include "simEDFkClass.hpp"
bool taskSortCriteria (Task t1,Task t2) { return (t1.getUtilisation() > t2.getUtilisation()); }
simEDFk::simEDFk() :
_k(0),
_numberCPU(0),
_initialTasks(deque<Task>())
{ }
/**
* \details Upload a set of tasks from a file
*/
void simEDFk::uploadTask(char* file){
ifstream taskFile(file);
string tasks_text = "";
while (!taskFile.eof())
{
// this does not check for input correctness
string temp;
for (int i = 0; i < 4; ++i)
{
taskFile >> temp;
tasks_text += temp + "\t";
}
tasks_text += "\n";
}
_initialTasks = Task::generateFromString(tasks_text);
}
/**
* \details Compute the number of CPU and the k, needed by EDF-k
*/
void simEDFk::computeCPUandK(){
//sort tasks by utilisation
sort(_initialTasks.begin(),_initialTasks.end(), taskSortCriteria);
std::vector<int> testNumberCPU;
// we compute minCPU
for(unsigned int k = 1; k <= _initialTasks.size(); ++k)
{
float Utot = 0;
int minCPU = 0;
for(unsigned int i=k; i < _initialTasks.size(); ++i)
{
Utot += _initialTasks.at(i).getUtilisation() ;
}
if(_initialTasks.at(k-1).getUtilisation()!=1)
{
minCPU = (k-1) + ceil(Utot/(1-_initialTasks.at(k-1).getUtilisation()));
}
testNumberCPU.push_back(minCPU);
}
// we initialise tne number of CPU and k before we can search for the min.
unsigned int cnt=0;
while(_numberCPU==0 && cnt < testNumberCPU.size())
{
if(testNumberCPU.at(cnt) != 0)
{
_numberCPU = testNumberCPU.at(cnt);
_k = cnt+1;
}
cnt ++;
}
// we search the minimal number of CPU and k
if(_numberCPU==0)
{
// if every tasks have an utilisation of 1, then we need a core for every tasks
_numberCPU = _initialTasks.size();
}
else
{
// we search for the minimal number of CPU
for(unsigned int i = 0; i< testNumberCPU.size(); ++i)
{
if(testNumberCPU.at(i) < _numberCPU && testNumberCPU.at(i) != 0)
{
_numberCPU = testNumberCPU.at(i);
_k = i+1;
}
}
}
if(_numberCPU < _k)
{
// We can't have less core than priority tasks
_numberCPU = _k;
}
}
/**
* \details Modify the priority of the k first tasks
*/
void simEDFk::modifyPriority(){
for(int i=0; i<_k; ++i)
{
_initialTasks.at(i).setPriority(true);
}
}
/**
* \details Run EDF-k from a file and display the result on the terminal
*/
void simEDFk::run(char* file)
{
_numberCPU=0;
uploadTask(file);
computeCPUandK();
modifyPriority();
Simulation s(_numberCPU, _initialTasks);
vector<int> result = s.runGlobal();
if(result.size()>0)
{
cout << "statistics of the simulation :"<<endl;
cout << "Number of preemption = " << result.at(0) << endl;
cout << "Number of migration = " << result.at(1) << endl;
cout << "idle time = " << result.at(2) << endl;
cout << "studyInterval = " << result.at(4) << endl;
cout << "Core used = " << _numberCPU << endl;
cout << "Core necessary = " << result.at(3) << endl;
}
else
{
cout<<"This system is not schedulable"<<endl;
}
}
/**
* \details Run EDF-k from a deque of tasks and return the result.
* \return A vector containing the statistics of the system
* vector[0]= number of preemption
* vector[1]= number of migration
* vector[2]= idle time
* vector[3]= number of core necessary
* vector[4]= studyInterval
* vector[5]= number of core used
*/
vector<int> simEDFk::run(deque<Task> t)
{
_numberCPU=0;
_initialTasks = t;
computeCPUandK();
modifyPriority();
Simulation s(_numberCPU, _initialTasks);
vector<int> result = s.runGlobal();
result.push_back(_numberCPU);
return result;
}