-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsched-adv.cpp
More file actions
171 lines (149 loc) · 4.99 KB
/
sched-adv.cpp
File metadata and controls
171 lines (149 loc) · 4.99 KB
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
168
169
170
171
/*
* The Priority Task Scheduler
*/
#include <infos/kernel/sched.h>
#include <infos/kernel/thread.h>
#include <infos/kernel/log.h>
#include <infos/util/list.h>
#include <infos/util/lock.h>
using namespace infos::kernel;
using namespace infos::util;
/**
* A Multiple Queue priority scheduling algorithm
*/
class AdvancedSchedulingAlgorithm : public SchedulingAlgorithm
{
public:
/**
* Returns the friendly name of the algorithm, for debugging and selection purposes.
*/
const char* name() const override { return "adv"; }
/**
* Called during scheduler initialisation.
*/
void init()
{
}
/**
* Called when a scheduling entity becomes eligible for running.
* @param entity
*/
void add_to_runqueue(SchedulingEntity& entity) override
{
UniqueIRQLock l;
runqueue.enqueue(&entity);
}
/**
* Called when a scheduling entity is no longer eligible for running.
* @param entity
*/
void remove_from_runqueue(SchedulingEntity& entity) override
{
UniqueIRQLock l;
runqueue.remove(&entity);
}
/**
* determines if a scheduling entity is present within a queue.
* @param queue: queue where entity is checked for within
* @param entity: entity we are checking is contained within the queue
*/
bool inQueue(SchedulingEntity* entity, List<SchedulingEntity *> queue)
{
for (const auto& entityInQueue : queue) {
if (entity == entityInQueue) {
return true;
}
}
return false;
}
/**
* Called to determine if an object is in a queue and not runnable.
* @param queue : Queue of scheduling entities
*/
bool stopped(List<SchedulingEntity *> queue)
{
if (!queue.empty()) {
while (!inQueue(queue.first(),runqueue)) {
return true;
if (highPriority.empty()) {
break;
}
}
}
return false;
}
/**
* Called every time a scheduling event occurs, to cause the next eligible entity
* to be chosen. The next eligible entity might actually be the same entity, if
* e.g. its timeslice has not expired.
*/
SchedulingEntity *pick_next_entity() override {
if (runqueue.count() == 0) {
return NULL;
}
if (runqueue.count() == 1) {
return runqueue.first();
}
SchedulingEntity *RunningEntity = NULL;
//Sorting Scheduling entities of runqueue into seperate queues based off of priority level, Low priority or High priority
for (const auto& entity : runqueue) {
if (entity->priority() == 0) {
if (!inQueue(entity, highPriority)){
highPriority.push(entity);
}
}
else if (entity->priority() == 1) {
if (!inQueue(entity, highPriority)){
highPriority.append(entity);
}
}
else if (entity->priority() == 2) {
if (!inQueue(entity, lowPriority)) {
lowPriority.append(entity);
}
}
else if (entity->priority() == 3){
if (!inQueue(entity, lowPriority)){
lowPriority.append(entity);
}
}
else if (entity->priority() > 3 || entity->priority()< 0){
syslog.messagef(LogLevel::ERROR, "Entity is not of any known priority level");
}
}
if ((!lowPriority.empty() && stopped(highPriority)) ||highPriority.empty() || (!stopped(lowPriority) && lowPriority.count()==1)) {
if (!lowPriority.empty()) {
while (!inQueue(lowPriority.first(),runqueue)) {
lowPriority.remove(lowPriority.first());
if (lowPriority.empty()) {
break;
}
}
}
if (!highPriority.empty()) {
while (!inQueue(highPriority.first(),runqueue)) {
highPriority.remove(highPriority.first());
if (highPriority.empty()) {
break;
}
}
}
RunningEntity = lowPriority.first();
lowPriority.enqueue(lowPriority.dequeue());
return RunningEntity;
}
if (!highPriority.empty()) {
if (!highPriority.empty()) {
RunningEntity = highPriority.first();
highPriority.enqueue(highPriority.dequeue());
return RunningEntity;
}
}
syslog.messagef(LogLevel::ERROR,"Queue Empty");
return NULL;
}
private: List<SchedulingEntity *> runqueue;
private: List<SchedulingEntity *> highPriority;
private: List<SchedulingEntity *> lowPriority;
};
RegisterScheduler(AdvancedSchedulingAlgorithm);