Scheduler
rlDiscipline.cpp
Go to the documentation of this file.
1 
10 #include "rlDiscipline.h"
11 
12 #include <iostream>
13 #include <cassert>
14 #include <memory>
15 
16 #include <utils/log.h>
17 #include <utils/math.h>
18 
19 #include <scheduler/process.h>
20 #include <scheduler/queue.h>
21 #include <scheduler/system.h>
22 #include <scheduler/time.h>
24 
25 #include <mdp/mdpModel.h>
26 #include <mdp/mdpConfiguration.h>
27 
28 #include "rlDisciplineBuilder.h"
29 #include "actions.h"
30 #include "configuration.h"
31 #include "domainModel.h"
32 
33 #define MISSED_DEADLINE_REWARD -1.0
34 
35 using namespace RlScheduler;
36 
37 RlDiscipline::RlDiscipline(std::shared_ptr<Configuration> c) : SchedulingDiscipline(c)
38 {
39  construct();
40 }
41 
43 {
44  if (mdpModel != nullptr)
45  delete mdpModel;
46 }
47 
48 
49 void RlDiscipline::construct()
50 {
52  mdpModel->init();
53 }
54 
55 
56 
57 std::shared_ptr<Scheduler::Process>
59  std::shared_ptr<Scheduler::Process> running,
60  unsigned int deadlineMisses)
61 {
62  currentDeadlineMisses = deadlineMisses;
63  model->running = Scheduler::System::getInstance()->getProc()->getRunningTask();
64  bool processNotReady = true;
65  std::shared_ptr<Scheduler::Process> process = nullptr;
66 
67  updateReward();
68  Action *action = nullptr;
69 
70 
71  int counter = 0; /*FIXME: not pretty*/
72  do
73  {
74  counter++;
75  action = static_cast<Action*>(mdpModel->selectAction()); //FIXME: should use dynamic_cast, but static_cast is faster
76  if (readyQueue->isEmpty())
77  return running;
78  assert(action != nullptr);
79  process = action->getProcessToRun();
80  if (process == nullptr)
81  model->reward = -HUGE_VAL;
82  else
83  processNotReady = false;
84  } while (processNotReady && counter < 1000);
85  if (counter >= 1000)
86  {
87  if (Scheduler::Queue::getReadyQueue()->isEmpty())
88  return nullptr;
90  }
91  action->performAction();
92  return process;
93 }
94 
96 {
97  return ((trigger == Scheduler::TriggeringEvent::newprocess)
98  || (trigger == Scheduler::TriggeringEvent::ready));
99  return true;
100 }
101 
102 
103 void RlDiscipline::updateReward()
104 {
105  /*We look at the ready queue and the running process.
106  If one of them has crossed its deadline in the last iteration,
107  a bad cost is incurred.
108  TODO: an improvement would be to measure the time at which a job completes,
109  and incur a reward proportional to the time between completion and deadline
110  (positive reward if before deadline, negative if after.)
111  UPDATE: partially done...*/
112  model->reward = (currentDeadlineMisses - previousDeadlineMisses);
113  model->reward *= MISSED_DEADLINE_REWARD;
114  previousDeadlineMisses = currentDeadlineMisses;
116  static bool softDeadline = conf->getBoolValue("rlDiscipline", "softDeadline", false);
117 
118  if (softDeadline)
119  {
120  if (mailbox->isValid())
121  {
122  //std::cerr << "before: "<< model->reward<<"\n";
123  model->reward += mailbox->get();
124  //std::cerr << "after: "<< model->reward<<"\n";
125  mailbox->invalidate();
126  }
127  }
128 }
129 
130 
131 
132 
134 {
135  model->end();
136  mdpModel->end();
137 }
138 
139 
140 
141 
142 
TriggeringEvent
Definition: eventType.h:16
#define MISSED_DEADLINE_REWARD
This class implements the ready queue and the wait queue. Those queues contain processes ready to run...
Definition: queue.h:28
std::shared_ptr< Scheduler::Process > selectNextTask(Scheduler::Queue *readyQueue, std::shared_ptr< Scheduler::Process > running, unsigned int deadlineMisses)
select the best task to run at this point.
static System * getInstance()
Definition: system.cpp:28
static void buildRlDisciplineModels(RlDiscipline *discipline)
Action * selectAction(bool updateModel=true)
Returns the optimal action for the current timestep.
Definition: mdpModel.cpp:89
static Queue * getReadyQueue()
get a pointer to the system&#39;s ready queue
Definition: queue.cpp:30
RlDiscipline(std::shared_ptr< Configuration > conf)
iterator begin()
Definition: queue.cpp:65
std::shared_ptr< SchedulerConfiguration > conf
Processor * getProc()
return the processor
Definition: system.cpp:76
static SpecialMailbox * getInstance()
void init()
call this function first
Definition: mdpModel.cpp:72
bool preempts(Scheduler::TriggeringEvent trigger)
returns true if the argument is a scheduling trigger for this specific discipline ...
void end()
call this function at the end
Definition: mdpModel.cpp:79
bool isEmpty() const
Definition: queue.cpp:80
void performAction() override
Definition: actions.cpp:27
def deadlineMisses(reportsFolder, configFilename, reportFilename, runningTime)
Definition: shortReport.py:36
std::shared_ptr< Process > getRunningTask()
Definition: processor.cpp:92
virtual std::shared_ptr< Scheduler::Process > getProcessToRun()
returns the process to run according to the RL algorithm
Definition: actions.cpp:32