Scheduler
schedulingSimulator.cpp
Go to the documentation of this file.
1 
10 #include "schedulingSimulator.h"
11 
12 #include <algorithm>
13 #include <cassert>
14 #include <cmath>
15 #include <ctime>
16 
17 #include <utils/log.h>
18 #include <utils/randomGenerator.h>
19 
20 #include "events/coreEvents.h"
21 #include "events/eventList.h"
22 #include "freqConstants.h"
23 #include "process.h"
24 #include "schedulerConfiguration.h"
25 #include "system.h"
26 #include "taskSetGenerator.h"
27 #include "time.h"
28 #include "xmlTaskSet.h"
29 
30 using namespace Scheduler;
31 
33 {
34  conf = std::make_shared<SchedulerConfiguration>("configuration.conf");
35  System::buildSystem(conf);
36 }
37 
38 SchedulingSimulator::SchedulingSimulator(std::shared_ptr<SchedulerConfiguration> c) : conf(c)
39 {
40  NewJob::init();
42 }
43 
45 {
46  Process::end();
47 }
48 
50 {
51  std::string value = conf->getStringValue("taskSet", "tasksetSource");
52  if (!value.compare("xml"))
53  initializeTaskSetFromXml();
54  else if (!value.compare("random"))
55  initializeTaskSetRandomly();
56  else if (!value.compare("compiled"))
57  initializeTaskSetAtCompileTime();
58  else initializeTaskSetAtCompileTime();
59 }
60 
61 void SchedulingSimulator::initializeTaskSetFromXml()
62 {
63  XmlTaskSet xml;
64  std::shared_ptr<std::vector<std::shared_ptr<Process>>>
65  taskset = xml.getRealTimeTaskSetFromXml("xml/taskset.xml");
66  for (size_t i = 0; i < taskset->size(); i++)
67  {
68  System::getInstance()->addRealTimeTask((*taskset)[i]);
69  double startTime = 0.1; //TODO HARDCODED !!!
70  std::shared_ptr<Event> rt = std::make_shared<NewJob>(startTime);
71  rt->setTask((*taskset)[i]);
73  //std::cerr << "added real-time task. The event list is:\n";
74  //EventList::getInstance()->print();
75  }
76 }
77 
78 void SchedulingSimulator::initializeTaskSetRandomly()
79 {
80  int nbOfTasks = conf->getIntValue("taskSet","nbOfTasks");
81  double utilization = conf->getDoubleValue("taskSet", "utilization");
82  time_t tasksetSeed = conf->getIntValue("taskSet","seed");
83  std::shared_ptr<Utils::BoundedRandomDistribution> dist;
84  dist = conf->getDistributionFromFile(&randomGenerator);
85  TaskSetGenerator::generate(this, nbOfTasks, utilization,
86  tasksetSeed, true, true, dist);
87 }
88 
89 void SchedulingSimulator::initializeTaskSetAtCompileTime()
90 {
91 #if 0
92  /*Utilization for this task set is 0.792*/
93  createRealTimeTask(0.1, 35.0, 10.0*100, 25.0);
94  createRealTimeTask(0.1, 25.0, 6.0*100, 6.0);
95  createRealTimeTask(0.1, 15.0, 4.0*100, 14.9);
96 #endif
97 #if 0
98  /*Utilization for this task set is 0.972*/
99  createRealTimeTask(0.1, 35.0, 10.0*100);
100  createRealTimeTask(0.1, 25.0, 8.0*100);
101  createRealTimeTask(0.1, 15.0, 3.0*100);
102  createRealTimeTask(0.1, 30.0, 5.0*100);
103 #endif
104 #if 0
105  /*Utilization for this task set is 0.967*/
106  createRealTimeTask(0.1, 30.0, 6.0*100);
107  createRealTimeTask(0.1, 20.0, 8.0*100);
108  createRealTimeTask(0.1, 15.0, 3.0*100);
109  createRealTimeTask(0.1, 30.0, 5.0*100);
110 #endif
111  //simulator.createRealTimeTask(0.0, 15.0, 7.0, 15.0);
112 #if 0
118 #endif
119 
120 #if 1
121  createRealTimeTask(0.1, 200, 6000, 200, 2, 6000, 1.5);
122  createRealTimeTask(0.1, 100, 3000, 200, 1, 3000, 1.0);
123 #endif
124 
125 #if 0
126  createRealTimeTask(0.1, 30.0, 5.0*100.0, 20.0, 0, 499.0, 1.0);
127  createRealTimeTask(0.1, 20.0, 5.0*100.0, 20.0, 0, 499.0, 1.5);
128 #endif
129 }
130 
131 
132 std::shared_ptr<Process> SchedulingSimulator::createRealTimeTask(double startTime,
133  double period, double wcet, double deadline, int priority, double bcet,
134  double powerCoeff)
135 {
136  if (deadline <= 0.0)
137  deadline = period;
138  std::shared_ptr<Process> p = Process::createRealTimeTask(wcet, period, deadline,
139  Process::getNewPid(), priority, bcet);
140  p->setPowerCoeff(powerCoeff);
141  std::shared_ptr<Event> rt = std::make_shared<NewJob>(startTime);
142  rt->setTask(p);
145  return p;
146 }
147 
149 {
150  EventList::getInstance()->insert(std::make_shared<NewInteractiveProcess>(startTime));
151 }
152 
154 {
155  randomGenerator.seed(seed);
156 }
157 
158 
159 
161 {
162  EventList *eventList = EventList::getInstance();;
163  std::shared_ptr<Event> stopEvent = std::make_shared<StopSimulation>(time);
164  eventList->insert(stopEvent);
165 }
166 
167 void SchedulingSimulator::setUsageCalculationTimeout(double start, double interval)
168 {
169  std::shared_ptr<UsageUpdate> cpuUsageUpdate = std::make_shared<UsageUpdate>(start);
170  cpuUsageUpdate->setInterval(interval);
171  EventList::getInstance()->insert(cpuUsageUpdate);
172 }
173 
174 void SchedulingSimulator::setStatsTick(double start, double interval)
175 {
176  std::shared_ptr<StatsTick> tick = std::make_shared<StatsTick>(start);
177  tick->setInterval(interval);
179 }
180 
181 void SchedulingSimulator::setFreqUpdate(double start, double interval)
182 {
183  std::shared_ptr<FreqUpdate> freqTO = std::make_shared<FreqUpdate>(start);
184  freqTO->setInterval(interval);
185  EventList::getInstance()->insert(freqTO);
186 }
187 
188 void SchedulingSimulator::setSchedulerTimeout(double start, double interval)
189 {
190  std::shared_ptr<SchedTimeOut> timeout = std::make_shared<SchedTimeOut>(start);
191  timeout->setInterval(interval);
192  EventList::getInstance()->insert(timeout);
193 }
194 
195 
196 void SchedulingSimulator::setDummyEvent(double start, double interval)
197 {
198  std::shared_ptr<DummyEvent> dummy = std::make_shared<DummyEvent>(start);
199  dummy->setInterval(interval);
200  EventList::getInstance()->insert(dummy);
201 }
202 
203 
205 {
207  //std::cerr << "list of events:\n";
208  //list->print();
209  list->setRandomGenerator(&randomGenerator);
210  Process::setRandomGenerator(&randomGenerator);
211  while(!list->isEmpty())
212  {
213  std::shared_ptr<Event> e = list->getHead();
214  Utils::Log log;
215  Time::updateTime(e->getTime());
216  double timeInterval = e->getTime() - previousTime;
217  previousTime = e->getTime();
218  System::getInstance()->updateTemperature(timeInterval);
219  e->process();
220  list->pop();
221 
222  static double previousTimeTrack = 0.0;
223  double timeTrack = e->getTime();
224 
225  if (timeTrack - previousTimeTrack > 100000.0)
226  {
227  std::cerr << "time: "<< timeTrack <<"\n";
228  previousTimeTrack = timeTrack;
229  }
230  }
231  std::cerr << "after the startScheduler\n";
232 }
233 
234 
236 {
237  Utils::Log log;
238  std::shared_ptr<StartProc> startProc = std::make_shared<StartProc>(start);
239  EventList::getInstance()->insert(startProc);
240 }
241 
242 
243 
244 
245 
246 
247 
248 
249 
250 
251 
252 
253 
static void buildSystem(std::shared_ptr< SchedulerConfiguration > conf)
Definition: system.cpp:34
static void updateTime(double t)
Definition: time.cpp:21
std::shared_ptr< Process > createRealTimeTask(double startTime, double period, double wcet, double deadline=0.0, int priority=0, double bcet=0.0, double powerCoeff=1.0)
creates a new real-time task event and add it to the event scheduling queue
void seed(time_t seed)
void setFreqUpdate(double start, double interval)
Sets a timer at the end of which the frequency governor updates the frequency of processor.
void startScheduler()
call this function to launch the simulation
void updateTemperature(double timeInterval)
updates and logs the temperature of the processor. This function has to be called at least every time...
Definition: system.cpp:71
void addRealTimeTask(std::shared_ptr< Process > p)
add a real-time task to the system&#39;s list
Definition: system.cpp:127
std::shared_ptr< std::vector< std::shared_ptr< Process > > > getRealTimeTaskSetFromXml(std::string filename)
Definition: xmlTaskSet.cpp:28
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
void setRandomGenerator(Utils::RandomGenerator *gen)
set a random generator TODO: shouldn&#39;t that be part of the constructor?
Definition: eventList.cpp:37
static System * getInstance()
Definition: system.cpp:28
static void setRandomGenerator(Utils::RandomGenerator *gen)
specify what random generator to use for interactive task length
Definition: process.cpp:99
std::shared_ptr< Event > getHead()
get the element at the head of the list
Definition: eventList.cpp:124
static int getNewPid()
returns new process identifier. The value returned gets incremented at each call
Definition: process.cpp:88
std::shared_ptr< Event > pop()
Definition: eventList.cpp:47
void seedRandomGenerator(time_t seed)
seeds the random generator
list time
Definition: aging.py:11
static void init()
Definition: newJob.cpp:28
std::shared_ptr< Event > insert(std::shared_ptr< Event > e)
Definition: eventList.cpp:56
Definition: log.h:18
static void generate(SchedulingSimulator *simulator, unsigned int nbOfTasks, double utilization, time_t seed, bool critical=true, bool integerPeriod=false, std::shared_ptr< Utils::BoundedRandomDistribution > dist=nullptr)
generates a random task set. The deadline will be equal to the period. The start time will be 0...
void turnOnProcessor(double start)
turns on the processor
void createInteractiveProcess(double startTime)
creates a new interactive process event and adds it to the event scheduling queue ...
void setStatsTick(double start, double interval)
void setDummyEvent(double start, double interval)
void initializeTaskSet()
Builds the task set using a variety of methods This function is an alternative to using createRealTim...
void setSchedulerTimeout(double start, double interval)
sets the timeout at which the task scheduler is invoked
void endSimulation(double time)
set the end of the simulation
void setUsageCalculationTimeout(double start, double interval)
Sets a UsageUpdate timeout.
static void end()
call this function before any other
Definition: process.cpp:103
static EventList * getInstance()
Singleton pattern.
Definition: eventList.cpp:22