Scheduler
taskSetGenerator.cpp
Go to the documentation of this file.
1 
10 #include "taskSetGenerator.h"
11 
12 #include <algorithm>
13 #include <cassert>
14 #include <cmath>
15 #include <stdexcept>
16 
19 
20 #include "freqConstants.h"
21 #include "process.h"
22 #include "schedulerConfiguration.h"
23 #include "schedulingSimulator.h"
24 
25 
26 using namespace Scheduler;
27 
28 
29 
31  SchedulingSimulator *simulator
32  , unsigned int nbOfTasks
33  , double utilization
34  , time_t seed
35  , bool critical
36  , bool integerPeriod
37  , std::shared_ptr<Utils::BoundedRandomDistribution> dist
38  )
39 {
40  if (nbOfTasks <= 0)
41  throw std::runtime_error("need to specify a number of tasks greater or equal than one");
43  gen.seed(seed);
44 
45  std::vector<double> portion = generateCpuUtilizationOfTasks(nbOfTasks, &gen,
46  utilization);
47  std::vector<double> periods = generateTaskPeriods(nbOfTasks, integerPeriod, &gen);
48  std::vector<double> aow = computeAow(periods, portion, nbOfTasks);
49 
50  for (size_t i = 0; i < periods.size(); i++)
51  {
52  std::cerr << "period: "<< periods[i] <<"\n";
53  std::cerr << "aow: "<< aow[i] <<"\n";
54  std::cerr << "portion: "<< portion[i] <<"\n";
55  }
56 
57  bool enablePriority = arePrioritiesEnabled(simulator->conf);
58  generateTasks(
59  periods
60  , aow
61  , &gen
62  , enablePriority
63  , nbOfTasks
64  , critical
65  , simulator
66  , dist
67  );
68 
69 }
70 
71 void TaskSetGenerator::generateTasks(
72  std::vector<double> periods
73  , std::vector<double> aow
75  , bool enablePriority
76  , unsigned int nbOfTasks
77  , bool critical
78  , SchedulingSimulator *simulator
79  , std::shared_ptr<Utils::BoundedRandomDistribution> dist
80  )
81 {
82  int priority = 0;
83 
84  std::shared_ptr<Process> task;
85  for (unsigned int i = 0; i < nbOfTasks; i++)
86  {
87  if (enablePriority)
88  {
89  priority = exp(20*i);
90  }
91  double startTime = 0.1;
92  if (!critical)
93  {
94  startTime += gen->drawUniform(0.0, periods[i]);
95  }
96  std::cerr << "in the generator, aow is "<< aow[i] <<"\n";
97  task = simulator->createRealTimeTask(startTime, periods[i], aow[i],
98  0.0, priority, aow[i]/2.0);
99  double powerSpan = simulator->conf->getDoubleValue("taskSet", "powerSpan");
100  task->setPowerCoeff(1.0 + powerSpan*((double) i)
101  / ((double) nbOfTasks - 1.0));
102  /*
103  task->setDurationDistribution(nullptr);
104  task->setDurationDistribution(std::make_shared<Utils::Bernoulli>(
105  new Utils::RandomGenerator(0), 1.0));
106  */
107  task->setDurationDistribution(dist);
108  }
109 }
110 
111 std::vector<double> TaskSetGenerator::generateCpuUtilizationOfTasks(
112  unsigned int nbOfTasks
114  , double utilization
115  )
116 {
117 
118  /*Let's determine the cpu utilization due to each of the tasks.
119  Imagine a segment going from 0.0 to utilization.
120  put nbOfTasks-1 dots on it.
121  You have now partitioned the segment into nbOfTask subsegments.
122  The length of each of those subsegments is the cpu utilization
123  due to each of the corresponding task.*/
124  std::vector<double> portion(nbOfTasks);
125  std::vector<double> dots(nbOfTasks+1);
126  dots[0] = 0.0;
127  dots[nbOfTasks] = utilization;
128  for (unsigned int i = 0; i < nbOfTasks-1; i++)
129  {
130  dots[i+1] = gen->drawUniform(0.0, utilization);
131  }
132  std::sort(dots.begin(), dots.end());
133  for (unsigned int i = 0; i < nbOfTasks; i++)
134  {
135  portion[i] = dots[i+1] - dots[i];
136  }
137 
138  return portion;
139 }
140 
141 
142 
143 std::vector<double> TaskSetGenerator::computeAow(
144  std::vector<double> periods
145  , std::vector<double> portion
146  , unsigned int nbOfTask
147  )
148 {
149  std::vector<double> aow(nbOfTask);
150  for (unsigned int i = 0; i < nbOfTask; i++)
151  {
152  /*this is not exactly minfreq, but the actual running frequency.
153  it is equal to minfreq if we use the minfreq governor*/
154  aow[i] = periods[i]*portion[i]*MINFREQ;
155  }
156  return aow;
157 }
158 
159 bool TaskSetGenerator::arePrioritiesEnabled(std::shared_ptr<SchedulerConfiguration> conf)
160 {
161  assert(conf != nullptr);
162  std::string str = conf->getStringValue("taskSet", "priorities");
163  if (!str.compare("yes"))
164  return true;
165  return false;
166 }
167 
168 
169 
170 std::vector<double> TaskSetGenerator::generateTaskPeriods(
171  unsigned int nbOfTasks
172  , bool integerPeriod
173  , Utils::RandomGenerator *gen)
174 {
175  std::vector<double> periods(nbOfTasks);
176  for (unsigned int i = 0; i < nbOfTasks; i++)
177  {
178  periods[i] = gen->drawUniform(minT, maxT);
179  if (integerPeriod == true)
180  {
181  periods[i] = (periods[i] < 1.0) ? 1.0 : round(periods[i]);
182  }
183  }
184  return periods;
185 }
186 
187 
188 
189 
190 
191 
192 
193 
194 
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)
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...
#define MINFREQ
Definition: freqConstants.h:15
double drawUniform(double min, double max)