-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathlinux_linsched.c
More file actions
372 lines (318 loc) · 12 KB
/
linux_linsched.c
File metadata and controls
372 lines (318 loc) · 12 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* LinSched -- The Linux Scheduler Simulator
* Copyright (C) 2008 John M. Calandrino
* E-mail: jmc@cs.unc.edu
*
* This file contains Linux variables and functions that have been "defined
* away" or exist here in a modified form to avoid including an entire Linux
* source file that might otherwise lead to a "cascade" of dependency issues.
* It also includes certain LinSched variables to which some Linux functions
* and definitions now map.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (see COPYING); if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* To support printing debugging and other messages to the console. */
#include <stdio.h>
#include <assert.h>
/* linsched variables and functions */
int __linsched_curr_cpu = 0;
struct task_struct *__linsched_tasks[LINSCHED_MAX_TASKS];
int curr_task_id = 0;
void linsched_change_cpu(int cpu) { __linsched_curr_cpu = cpu; }
void linsched_init_cpus(void) { /* Look at Tong's work to init. */ }
void linsched_init(void)
{
/* Initialize platform. For now, this does nothing, since
* the number of CPUs is known, and we do not yet support
* MC/SMT/NUMA. Scheduling decisions are not dependent on
* asymmetries yet, either, but that could change.
*/
linsched_init_cpus();
/* Initialize random number generator. */
linsched_random_init(LINSCHED_RAND_SEED);
/* Change context to "boot" cpu and boot kernel. */
linsched_change_cpu(0);
start_kernel();
}
void linsched_default_callback(void) { }
void linsched_exit_callback(void) { do_exit(0); }
void linsched_announce_callback(void)
{
printf("CPU %d / t = %u: Task 0x%x scheduled.\n", smp_processor_id(),
(unsigned int)jiffies, (unsigned int)current);
}
void linsched_disable_migrations(void)
{
int i;
for (i = 0; i < curr_task_id; i++)
set_cpus_allowed(__linsched_tasks[i],
cpumask_of_cpu(
task_cpu(__linsched_tasks[i])));
}
void linsched_enable_migrations(void)
{
int i;
for (i = 0; i < curr_task_id; i++)
set_cpus_allowed(__linsched_tasks[i], CPU_MASK_ALL);
}
/* Needed for migration to work correctly. */
void linsched_run_sim(int sim_ticks);
/* Force a migration of task to the dest_cpu.
* If migr is set, allow migrations after the forced migration... otherwise,
* do not allow them. (We need to disable migrations so that the forced
* migration takes place correctly.)
* Returns old cpu of task.
*/
int linsched_force_migration(struct task_struct *task, int dest_cpu, int migr)
{
int old_cpu = task_cpu(task);
linsched_disable_migrations();
set_cpus_allowed(task, cpumask_of_cpu(dest_cpu));
linsched_change_cpu(old_cpu);
schedule();
linsched_change_cpu(dest_cpu);
schedule();
if (migr)
linsched_enable_migrations();
return old_cpu;
}
/* Return the task in position task_id in the task array.
* No error checking, so be careful!
*/
struct task_struct *linsched_get_task(int task_id)
{
return __linsched_tasks[task_id];
}
struct task_struct *__linsched_create_task(void (*callback)(void))
{
struct task_struct *newtask =
(struct task_struct *)do_fork(0, 0, 0, 0, 0, 0, callback);
/* Allow task to run on any CPU. */
set_cpus_allowed(newtask, CPU_MASK_ALL);
return newtask;
}
/* Create a normal task with the specified callback and
* nice value of niceval, which determines its priority.
*/
void linsched_create_normal_task(void (*callback)(void), int niceval)
{
struct sched_param params = {};
/* If we cannot support any more tasks, return. */
if (curr_task_id >= LINSCHED_MAX_TASKS)
return;
/* Create "normal" task and set its nice value. */
__linsched_tasks[curr_task_id] = __linsched_create_task(callback);
params.sched_priority = 0;
sched_setscheduler(__linsched_tasks[curr_task_id], SCHED_NORMAL,
¶ms);
set_user_nice(__linsched_tasks[curr_task_id], niceval);
/* Print message. */
printf("Created normal task 0x%x with nice value %d.\n",
(unsigned int)__linsched_tasks[curr_task_id], niceval);
/* Increment task id. */
curr_task_id++;
}
/* Create a batch task with the specified callback and
* nice value of niceval, which determines its priority.
*/
void linsched_create_batch_task(void (*callback)(void), int niceval)
{
struct sched_param params = {};
/* If we cannot support any more tasks, return. */
if (curr_task_id >= LINSCHED_MAX_TASKS)
return;
/* Create "batch" task and set its nice value. */
__linsched_tasks[curr_task_id] = __linsched_create_task(callback);
params.sched_priority = 0;
sched_setscheduler(__linsched_tasks[curr_task_id], SCHED_BATCH,
¶ms);
set_user_nice(__linsched_tasks[curr_task_id], niceval);
/* Print message. */
printf("Created batch task 0x%x with nice value %d.\n",
(unsigned int)__linsched_tasks[curr_task_id], niceval);
/* Increment task id. */
curr_task_id++;
}
/* Create a FIFO real-time task with the specified callback and priority. */
void linsched_create_RTfifo_task(void (*callback)(void), int prio)
{
struct sched_param params = {};
/* If we cannot support any more tasks, return. */
if (curr_task_id >= LINSCHED_MAX_TASKS)
return;
/* Create FIFO real-time task and set its priority. */
__linsched_tasks[curr_task_id] = __linsched_create_task(callback);
params.sched_priority = prio;
sched_setscheduler(__linsched_tasks[curr_task_id], SCHED_FIFO,
¶ms);
/* Print message. */
printf("Created FIFO real-time task 0x%x with priority %d.\n",
(unsigned int)__linsched_tasks[curr_task_id], prio);
/* Increment task id. */
curr_task_id++;
}
/* Create a RR real-time task with the specified callback and priority. */
void linsched_create_RTrr_task(void (*callback)(void), int prio)
{
struct sched_param params = {};
/* If we cannot support any more tasks, return. */
if (curr_task_id >= LINSCHED_MAX_TASKS)
return;
/* Create RR real-time task and set its priority. */
__linsched_tasks[curr_task_id] = __linsched_create_task(callback);
params.sched_priority = prio;
sched_setscheduler(__linsched_tasks[curr_task_id], SCHED_RR, ¶ms);
/* Print message. */
printf("Created RR real-time task 0x%x with priority %d.\n",
(unsigned int)__linsched_tasks[curr_task_id], prio);
/* Increment task id. */
curr_task_id++;
}
void linsched_yield(void)
{
/* If the current task is not the idle task, yield. */
if (current != idle_task(smp_processor_id()))
yield();
}
/* Borrowed from drivers/net/hamradio/dmascc.c. */
static unsigned long __linsched_rand;
void linsched_random_init(int seed) { __linsched_rand = seed; }
unsigned long linsched_random(void)
{
/* See "Numerical Recipes in C", second edition, p. 284 */
__linsched_rand = __linsched_rand * 1664525L + 1013904223L;
return (unsigned long)(__linsched_rand >> 24);
}
void linsched_run_sim(int sim_ticks)
{
/* Run a simulation for some number of ticks. Each tick,
* scheduling and load balancing decisions are made. The
* order in which CPUs make their scheduler_tick calls
* is randomized. Obviously, we could create tasks,
* change priorities, etc., at certain ticks if we desired,
* rather than just running a simple simulation.
* (Tasks can also be removed by having them exit.)
*/
/* NOTE: The per-CPU "tick" is never disabled, like it might be in a
* real system, when a CPU goes idle. Since even the most current
* version of Linux maintains a periodic tick when there is
* actual work to do, and disabling the tick when idle would
* not change anything about how the scheduler behaves
* (it only conserves energy, which is not going to help us here),
* there is no need.
*/
int initial_jiffies = jiffies;
for (jiffies = initial_jiffies;
jiffies < initial_jiffies + sim_ticks;
jiffies++) {
cpumask_t cpu_processed_map = CPU_MASK_NONE;
while (!cpumask_full(&(cpu_processed_map))) {
int active_cpu;
/* Determine next active CPU, and set as processed. */
do {
active_cpu = linsched_random() % NR_CPUS;
} while (cpu_isset(active_cpu, cpu_processed_map));
cpu_set(active_cpu, cpu_processed_map);
/* Call scheduler_tick for that CPU. */
linsched_change_cpu(active_cpu);
scheduler_tick(); /* may trigger a schedule() call */
/* First time executing a task? Do not need to
* call schedule_tail, since we are not actually
* performing a "real" context switch.
*/
}
}
}
/* Linux variables and functions */
struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task;
unsigned long volatile __jiffy_data jiffies = INITIAL_JIFFIES;
const struct cpumask *const cpu_online_mask = cpu_all_mask;
const struct cpumask *const cpu_possible_mask = cpu_all_mask;
const struct cpumask *const cpu_active_mask = cpu_all_mask;
const struct cpumask *const cpu_present_mask = cpu_all_mask;
unsigned int cpu_khz; // needs correct initialization depending on core speeds
__cacheline_aligned DEFINE_RWLOCK(tasklist_lock);
struct task_struct *kthreadd_task;
void __lockfunc _spin_lock(spinlock_t *lock) { }
void __lockfunc _spin_unlock(spinlock_t *lock) { }
void __lockfunc _spin_lock_irq(spinlock_t *lock) { }
void __lockfunc _spin_unlock_irq(spinlock_t *lock) { }
unsigned long __lockfunc _spin_lock_irqsave(spinlock_t *lock) { return 0; }
void __lockfunc _spin_unlock_irqrestore(spinlock_t *lock,
unsigned long flags) { }
void __lockfunc _read_lock(rwlock_t *lock) { }
void __lockfunc _read_unlock(rwlock_t *lock) { }
int __lockfunc _spin_trylock(spinlock_t *lock) { return 1; }
int __lockfunc __reacquire_kernel_lock(void) { return 0; }
void __lockfunc __release_kernel_lock(void) { }
extern void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
int trylock, int read, int check,
struct lockdep_map *nest_lock, unsigned long ip);
void lock_release(struct lockdep_map *lock, int nested, unsigned long ip) { }
void __sched mutex_unlock(struct mutex *lock) { }
void rt_mutex_adjust_pi(struct task_struct *task) { }
int rt_mutex_getprio(struct task_struct *task) { return task->normal_prio; }
void trace_hardirqs_on(void) { }
void trace_hardirqs_off(void) { }
void add_preempt_count(int val) { }
void sub_preempt_count(int val) { }
asmlinkage int printk(const char *fmt, ...) { return 0; }
void dump_stack(void) { }
struct sighand_struct *lock_task_sighand(struct task_struct *tsk,
unsigned long *flags) { return tsk->sighand; }
unsigned int debug_smp_processor_id(void) { return __linsched_curr_cpu; }
int capable(int cap) { return 1; }
void fire_sched_out_preempt_notifiers(struct task_struct *curr,
struct task_struct *next) { }
void fire_sched_in_preempt_notifiers(struct task_struct *curr) { }
/*Kernel memory operations, subsitutes*/
void *kmalloc(size_t size, gfp_t flags) {
return malloc(size);
}
void kfree(const void *block) {
free((void *)block);
}
/* These functions do not copy to and from user space anymore, so
* they are just memory copy functions now.
*/
unsigned long
copy_from_user(void *to, const void __user *from, unsigned long n)
{
memcpy(to, from, n);
return 0;
}
unsigned long
copy_to_user(void __user *to, const void *from, unsigned long n)
{
memcpy(to, from, n);
return 0;
}
/* find_task_by_pid_vpid: just a typecast is performed,
* no actual mapping/hashing.
*/
struct task_struct *find_task_by_pid_vpid(pid_t nr)
{
return (struct task_struct*)nr;
}
void ktime_get_ts(struct timespec *ts)
{
unsigned long long curr_ns = sched_clock();
ts->tv_sec = curr_ns / NSEC_PER_SEC;
ts->tv_nsec = curr_ns % NSEC_PER_SEC;
if (ts->tv_nsec < 0) {
ts->tv_sec--;
ts->tv_nsec += NSEC_PER_SEC;
}
}