Scheduler
process.cpp
Go to the documentation of this file.
1 
10 #include "process.h"
11 
12 #include <iostream>
13 #include <fstream>
14 #include <cassert>
15 #include <exception>
16 #include <stdexcept>
17 #include <thread>
18 
19 #include <utils/randomGenerator.h>
21 
22 #include "time.h"
23 
24 
25 using namespace Scheduler;
26 
27 unsigned long int Process::currentNumber = 0;
28 Utils::RandomGenerator *Process::randomGenerator = nullptr;
29 
30 
31 std::shared_ptr<Process> Process::createNextJob(const Process& task)
32 {
33  assert(task.RT);
34  std::vector<double> burst(1);
35  std::shared_ptr<Process> job = std::make_shared<Process>(task.pid, 1, burst, std::vector<double>(), task.priority);
36 
37  /*TODO: put this in function setRealTimeParams*/
38  job->RT = true;
39  job->deadline = task.deadline;
40  job->deadlineTime = task.deadlineTime;
41  job->period = task.period;
42  job->bcet = task.bcet;
43  job->wcet = task.wcet;
44  job->powerCoeff = task.powerCoeff;
45  job->jobNumber = task.jobNumber + 1;
46  job->randomDist = task.randomDist;
47  job->setDuration();
48  return job;
49 }
50 
51 
52 std::shared_ptr<Process> Process::createProcess(double cpuLambda, double ioLambda)
53 {
54  assert(randomGenerator != nullptr);
55  int nbBursts = 10;
56  std::vector<double> cpuBurst = std::vector<double>(nbBursts);
57  std::vector<double> ioBurst = std::vector<double>(nbBursts);
58  for (int i = 0; i < nbBursts; i++)
59  {
60  cpuBurst[i] = randomGenerator->drawExp(cpuLambda)*100;//TODO: why *100 here but not next line?
61  ioBurst[i] = randomGenerator->drawExp(ioLambda);
62  }
63  std::shared_ptr<Process> p = std::make_shared<Process>(Process::getNewPid(), nbBursts, cpuBurst, ioBurst);
64  return p;
65 }
66 
67 std::shared_ptr<Process> Process::createRealTimeTask(
68  double wcet
69  , double T
70  , double dl
71  , int pid
72  , int priority
73  , double bcet
74  )
75 {
76  int nbBursts = 1;
77  std::vector<double> cpuBurst = std::vector<double>(nbBursts);
78  std::vector<double> ioBurst = std::vector<double>(nbBursts);
79  std::cerr << "in Process, wcet is " << wcet <<"\n";
80  cpuBurst[0] = wcet;
81  ioBurst[0] = 0;
82  std::shared_ptr<Process> p = std::make_shared<Process>(pid, nbBursts, cpuBurst, ioBurst, priority);
83  p->setRtParams(dl, T, wcet, bcet);
84  return p;
85 }
86 
87 
89 {
90  static int pid = 0;
91  return pid++;
92 }
93 
94 unsigned long int Process::getMaxNumber()
95 {
96  return maxNumber;
97 }
98 
100 {
101  randomGenerator = gen;
102 }
104 {
105 }
106 
107 
108 
109 
110 Process::Process(int p, int nb, std::vector<double> cpu, std::vector<double> io, int pri)
111  : pid(p)
112  , cpuBurst(cpu)
113  , ioBurst(io)
114  , currentBurst(0)
115  , nbBursts(nb)
116  , priority(pri)
117 {
118  if(currentNumber++ > maxNumber)
119  throw std::runtime_error("too many processes created");
120 }
121 
123 {
124  if(currentNumber++ > maxNumber)
125  throw "too many processes created";
126  currentBurst = 0;
127  pid = task.pid;
128  powerCoeff = task.powerCoeff;
129  nbBursts = task.nbBursts;
130  cpuBurst = std::vector<double>(nbBursts);
131  ioBurst = std::vector<double>(nbBursts);
132  for (int i = 0; i < nbBursts; i++)
133  {
134  cpuBurst[i] = task.cpuBurst[i];
135  ioBurst[i] = task.ioBurst[i];
136  }
137  priority = task.priority;
138  RT = task.RT;
139  deadline = task.deadline;
140  deadlineTime = 0.0; //this will have to be modified anyway?
141  period = task.period;
142  bcet = task.bcet;
143  wcet = task.wcet;
144  jobNumber = task.jobNumber;
145  randomDist = task.randomDist;
146 }
147 
149 {
150 }
151 
152 
153 void Process::setDurationDistribution(std::shared_ptr<Utils::BoundedRandomDistribution> rd)
154 {
155  randomDist = rd;
156 }
157 
158 int Process::getPid() const
159 {
160  return pid;
161 }
162 
164 {
165  return priority;
166 }
167 
168 void Process::setPriority(int pri)
169 {
170  priority = pri;
171 }
172 
174 {
175  currentBurst++;
176  return currentBurst < nbBursts;
177 }
178 
180 {
181  currentBurst--;
182 }
183 
185 {
186  return cpuBurst[currentBurst];
187 }
188 
190 {
191  if (nbBursts > currentBurst && ioBurst.size() > 0)
192  return ioBurst[currentBurst];
193  else
194  return -1;
195 }
196 
197 double Process::getWcet() const
198 {
199  return wcet;
200 }
201 
202 
203 void Process::setDuration()
204 {
205  assert(nbBursts == 1);
206  assert(bcet <= wcet);
207  double duration = (randomDist == nullptr ? 1.0 : randomDist->draw());
208  duration = bcet + (wcet-bcet)*duration;
209  cpuBurst[0] = duration;
210 }
211 
212 
213 void Process::setRtParams(double dl, double T, double w, double b)
214 {
215  deadline = dl;
216  period = T;
217  RT = true;
218  wcet = w;
219  bcet = b;
220 }
221 
223 {
224  deadlineTime = deadline + startTime;
225 }
226 
227 double Process::getDeadline() const
228 {
229  return deadline;
230 }
231 
232 
234 {
235  return RT;
236 }
237 
238 
239 
240 
242 {
243  cpuBurst[--currentBurst] = aow;
244 }
245 
246 double Process::getPeriod() const
247 {
248  return period;
249 }
250 
252 {
253  return deadlineTime;
254 }
255 
256 void Process::print(std::ostream& stream) const
257 {
258  stream << "Processs "<<pid<<":\n";
259  stream << " priority: "<<priority<<"\n";
260  stream << " Bursts:\n";
261  for (int i = 0; i < nbBursts; i++)
262  {
263  stream << " "<< i <<". CPU: "<< cpuBurst[i] <<"\n";
264  stream << " IO: " << ioBurst[i] <<"\n";
265  }
266  stream << " Power coefficient: "<< powerCoeff<<"\n";
267  if (RT)
268  {
269  stream << " This proces IS real-time\n";
270  stream << " Deadline: "<< deadline <<"\n";
271  //stream << " Absolute deadline: "<< deadlineTime <<"\n";
272  stream << " Period: "<< period <<"\n";
273  }
274  else
275  {
276  stream << " This process IS NOT real-time\n";
277  }
278  stream << "\n";
279 }
280 
281 
283 {
284  jobNumber++;
285 }
286 
287 unsigned int Process::getJobNumber() const
288 {
289  return jobNumber;
290 }
291 
292 
293 void Process::setPowerCoeff(double coeff)
294 {
295  powerCoeff = coeff;
296 }
297 
298 
299 
300 
void setDurationDistribution(std::shared_ptr< Utils::BoundedRandomDistribution > randomDist)
Definition: process.cpp:153
int getPid(void) const
Definition: process.cpp:158
static unsigned long int getMaxNumber()
returns the maximum number of process that can ever be created
Definition: process.cpp:94
void updateCurrentAow(double aow)
Definition: process.cpp:241
double getDeadlineTime() const
Definition: process.cpp:251
void decrementBurst()
Definition: process.cpp:179
int getPriority() const
Definition: process.cpp:163
static std::shared_ptr< Process > createRealTimeTask(double wcet, double T, double dl, int pid, int priority=0, double bcet=0.0)
creates a real time task
Definition: process.cpp:67
double powerCoeff
Definition: process.h:103
static void setRandomGenerator(Utils::RandomGenerator *gen)
specify what random generator to use for interactive task length
Definition: process.cpp:99
static std::shared_ptr< Process > createProcess(double cpuLambda, double ioLambda)
creates an "interactive" process
Definition: process.cpp:52
static int getNewPid()
returns new process identifier. The value returned gets incremented at each call
Definition: process.cpp:88
double getWcet() const
Definition: process.cpp:197
double getDeadline() const
Definition: process.cpp:227
void incrementJobNumber()
Definition: process.cpp:282
void setDeadlineTimeFromStartTime(double startTime)
Definition: process.cpp:222
void setPriority(int pri)
Definition: process.cpp:168
bool isRealTime() const
Definition: process.cpp:233
unsigned int getJobNumber() const
Definition: process.cpp:287
double drawExp(double lambda)
double getCurrentIoTime() const
Definition: process.cpp:189
Process(int pid, int nbBursts, std::vector< double > cpuBursts, std::vector< double > ioBursts, int priority=0)
Definition: process.cpp:110
void setRtParams(double dl, double T, double wcet, double bcet)
Definition: process.cpp:213
static std::shared_ptr< Process > createNextJob(const Process &task)
create the next job for a task
Definition: process.cpp:31
void print(std::ostream &stream) const
Definition: process.cpp:256
void setPowerCoeff(double)
Definition: process.cpp:293
double getPeriod() const
Definition: process.cpp:246
double getCurrentCpuAow() const
Definition: process.cpp:184
static void end()
call this function before any other
Definition: process.cpp:103