/
sched_other_rr.c
260 lines (211 loc) · 6.29 KB
/
sched_other_rr.c
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* SCHED_OTHER_RR scheduling class. Implements a round robin scheduler with no
* priority mechanism.
*/
/*
* Update the current task's runtime statistics. Skip current tasks that
* are not in our scheduling class.
*/
#include <linux/list.h>
static void update_curr_other_rr(struct rq *rq)
{
struct task_struct *curr = rq->curr;
u64 delta_exec;
if (!task_has_other_rr_policy(curr))
return;
delta_exec = rq->clock - curr->se.exec_start;
if (unlikely((s64)delta_exec < 0))
delta_exec = 0;
schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
curr->se.sum_exec_runtime += delta_exec;
curr->se.exec_start = rq->clock;
cpuacct_charge(curr, delta_exec);
}
/*
* Adding/removing a task to/from a priority array:
*/
static void enqueue_task_other_rr(struct rq *rq, struct task_struct *p, int wakeup, bool b)
{
list_add_tail(&p->other_rr_run_list, &rq->other_rr.queue);
rq->other_rr.nr_running++;
}
static void dequeue_task_other_rr(struct rq *rq, struct task_struct *p, int sleep)
{
// first update the task's runtime statistics
update_curr_other_rr(rq);
list_del(&p->other_rr_run_list);
rq->other_rr.nr_running--;
}
/*
* Put task to the end of the run list without the overhead of dequeue
* followed by enqueue.
*/
static void requeue_task_other_rr(struct rq *rq, struct task_struct *p)
{
list_move_tail(&p->other_rr_run_list, &rq->other_rr.queue);
}
/*
* current process is relinquishing control of the CPU
*/
static void
yield_task_other_rr(struct rq *rq)
{
requeue_task_other_rr(rq, rq->curr);
}
/*
* Preempt the current task with a newly woken task if needed:
* int wakeflags added to match function signature of other schedulers
*/
static void check_preempt_curr_other_rr(struct rq *rq, struct task_struct *p, int wakeflags)
{
}
/*
* select the next task to run
*/
static struct task_struct *pick_next_task_other_rr(struct rq *rq)
{
struct task_struct *next;
struct list_head *queue;
queue = &rq->other_rr.queue;
if(list_empty(queue))
return NULL;
next = list_entry(queue->next, struct task_struct, other_rr_run_list);
/* after selecting a task, we need to set a timer to maintain correct
* runtime statistics. You can uncomment this line after you have
* written the code to select the appropriate task.
*/
next->se.exec_start = rq->clock;
/* you need to return the selected task here */
return next;
}
static void put_prev_task_other_rr(struct rq *rq, struct task_struct *p)
{
update_curr_other_rr(rq);
p->se.exec_start = 0;
}
#ifdef CONFIG_SMP
/*
* Load-balancing iterator. Note: while the runqueue stays locked
* during the whole iteration, the current task might be
* dequeued so the iterator has to be dequeue-safe. Here we
* achieve that by always pre-iterating before returning
* the current task:
*/
static struct task_struct *load_balance_start_other_rr(void *arg)
{
struct rq *rq = arg;
struct list_head *head, *curr;
struct task_struct *p;
head = &rq->other_rr.queue;
curr = head->prev;
p = list_entry(curr, struct task_struct, other_rr_run_list);
curr = curr->prev;
rq->other_rr.other_rr_load_balance_head = head;
rq->other_rr.other_rr_load_balance_curr = curr;
return p;
}
static struct task_struct *load_balance_next_other_rr(void *arg)
{
struct rq *rq = arg;
struct list_head *curr;
struct task_struct *p;
curr = rq->other_rr.other_rr_load_balance_curr;
p = list_entry(curr, struct task_struct, other_rr_run_list);
curr = curr->prev;
rq->other_rr.other_rr_load_balance_curr = curr;
return p;
}
static unsigned long
load_balance_other_rr(struct rq *this_rq, int this_cpu, struct rq *busiest,
unsigned long max_load_move,
struct sched_domain *sd, enum cpu_idle_type idle,
int *all_pinned, int *this_best_prio)
{
struct rq_iterator other_rr_rq_iterator;
other_rr_rq_iterator.start = load_balance_start_other_rr;
other_rr_rq_iterator.next = load_balance_next_other_rr;
/* pass 'busiest' rq argument into
* load_balance_[start|next]_other_rr iterators
*/
other_rr_rq_iterator.arg = busiest;
return balance_tasks(this_rq, this_cpu, busiest, max_load_move, sd,
idle, all_pinned, this_best_prio, &other_rr_rq_iterator);
}
static int
move_one_task_other_rr(struct rq *this_rq, int this_cpu, struct rq *busiest,
struct sched_domain *sd, enum cpu_idle_type idle)
{
struct rq_iterator other_rr_rq_iterator;
other_rr_rq_iterator.start = load_balance_start_other_rr;
other_rr_rq_iterator.next = load_balance_next_other_rr;
other_rr_rq_iterator.arg = busiest;
return iter_move_one_task(this_rq, this_cpu, busiest, sd, idle,
&other_rr_rq_iterator);
}
#endif
/*
* task_tick_other_rr is invoked on each scheduler timer tick.
*/
static void task_tick_other_rr(struct rq *rq, struct task_struct *p, int queued)
{
// first update the task's runtime statistics
update_curr_other_rr(rq);
if(other_rr_time_slice != 0)
{
p->task_time_slice--;
if(p->task_time_slice == 0)
{
p->task_time_slice = other_rr_time_slice;
set_tsk_need_resched(p);
requeue_task_other_rr(rq, rq->curr);
}
}
}
/*
* scheduling policy has changed -- update the current task's scheduling
* statistics
*/
static void set_curr_task_other_rr(struct rq *rq)
{
struct task_struct *p = rq->curr;
p->se.exec_start = rq->clock;
}
/*
* We switched to the sched_other_rr class.
*/
static void switched_to_other_rr(struct rq *rq, struct task_struct *p,
int running)
{
/*
* Kick off the schedule if running, otherwise just see
* if we can still preempt the current task.
*/
if (running)
resched_task(rq->curr);
else
check_preempt_curr(rq, p, 0);
}
static int
select_task_rq_other_rr(struct rq *rq, struct task_struct *p, int sd_flag, int flags)
{
if (sd_flag != SD_BALANCE_WAKE)
return smp_processor_id();
return task_cpu(p);
}
const struct sched_class other_rr_sched_class = {
.next = &idle_sched_class,
.enqueue_task = enqueue_task_other_rr,
.dequeue_task = dequeue_task_other_rr,
.yield_task = yield_task_other_rr,
.check_preempt_curr = check_preempt_curr_other_rr,
.pick_next_task = pick_next_task_other_rr,
.put_prev_task = put_prev_task_other_rr,
#ifdef CONFIG_SMP
.load_balance = load_balance_other_rr,
.move_one_task = move_one_task_other_rr,
#endif
.switched_to = switched_to_other_rr,
.select_task_rq = select_task_rq_other_rr,
.set_curr_task = set_curr_task_other_rr,
.task_tick = (void *)task_tick_other_rr,
};