-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLinux.patch
3389 lines (3338 loc) · 84.9 KB
/
Linux.patch
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/arch/x86/syscalls/syscall_32.tbl b/arch/x86/syscalls/syscall_32.tbl
index 7a35a6e..b5ee1dc 100644
--- a/arch/x86/syscalls/syscall_32.tbl
+++ b/arch/x86/syscalls/syscall_32.tbl
@@ -356,3 +356,6 @@
347 i386 process_vm_readv sys_process_vm_readv compat_sys_process_vm_readv
348 i386 process_vm_writev sys_process_vm_writev compat_sys_process_vm_writev
349 i386 kcmp sys_kcmp
+350 i386 atlas_next sys_atlas_next
+351 i386 atlas_submit sys_atlas_submit
+352 i386 atlas_debug sys_atlas_debug
diff --git a/arch/x86/syscalls/syscall_64.tbl b/arch/x86/syscalls/syscall_64.tbl
index a582bfe..43f9836 100644
--- a/arch/x86/syscalls/syscall_64.tbl
+++ b/arch/x86/syscalls/syscall_64.tbl
@@ -319,6 +319,9 @@
310 64 process_vm_readv sys_process_vm_readv
311 64 process_vm_writev sys_process_vm_writev
312 common kcmp sys_kcmp
+313 common atlas_next sys_atlas_next
+314 64 atlas_submit sys_atlas_submit
+315 common atlas_debug sys_atlas_debug
#
# x32-specific system call numbers start at 512 to avoid cache impact
diff --git a/debian.quantal/config/amd64/config.flavour.atlas b/debian.quantal/config/amd64/config.flavour.atlas
new file mode 100644
index 0000000..06d8b53
--- /dev/null
+++ b/debian.quantal/config/amd64/config.flavour.atlas
@@ -0,0 +1,3 @@
+#
+# Config options for config.flavour.atlas automatically generated by splitconfig.pl
+#
diff --git a/debian.quantal/config/i386/config.flavour.atlas b/debian.quantal/config/i386/config.flavour.atlas
new file mode 100644
index 0000000..06d8b53
--- /dev/null
+++ b/debian.quantal/config/i386/config.flavour.atlas
@@ -0,0 +1,3 @@
+#
+# Config options for config.flavour.atlas automatically generated by splitconfig.pl
+#
diff --git a/debian.quantal/control.d/vars.atlas b/debian.quantal/control.d/vars.atlas
new file mode 100644
index 0000000..83e429e
--- /dev/null
+++ b/debian.quantal/control.d/vars.atlas
@@ -0,0 +1,6 @@
+arch="i386 amd64"
+supported="ATLAS"
+target="Experimental ATLAS scheduler."
+desc="=HUMAN= SMP"
+bootloader="grub-pc | grub-efi-amd64 | grub-efi-ia32 | grub | lilo (>= 19.1)"
+provides="kvm-api-4, redhat-cluster-modules, ivtv-modules, ndiswrapper-modules-1.9"
diff --git a/debian.quantal/rules.d/amd64.mk b/debian.quantal/rules.d/amd64.mk
index 9d00768..2692682 100644
--- a/debian.quantal/rules.d/amd64.mk
+++ b/debian.quantal/rules.d/amd64.mk
@@ -2,7 +2,7 @@ human_arch = 64 bit x86
build_arch = x86_64
header_arch = $(build_arch)
defconfig = defconfig
-flavours = generic
+flavours = atlas
build_image = bzImage
kernel_file = arch/$(build_arch)/boot/bzImage
install_file = vmlinuz
diff --git a/debian.quantal/rules.d/i386.mk b/debian.quantal/rules.d/i386.mk
index 3e82c65..35feef3 100644
--- a/debian.quantal/rules.d/i386.mk
+++ b/debian.quantal/rules.d/i386.mk
@@ -2,7 +2,7 @@ human_arch = 32 bit x86
build_arch = i386
header_arch = x86_64
defconfig = defconfig
-flavours = generic
+flavours = atlas
build_image = bzImage
kernel_file = arch/$(build_arch)/boot/bzImage
install_file = vmlinuz
diff --git a/include/linux/init_task.h b/include/linux/init_task.h
index b806b82..5c0e6b8 100644
--- a/include/linux/init_task.h
+++ b/include/linux/init_task.h
@@ -168,6 +168,14 @@ extern struct task_group root_task_group;
.run_list = LIST_HEAD_INIT(tsk.rt.run_list), \
.time_slice = RR_TIMESLICE, \
}, \
+ .atlas = { \
+ .state = ATLAS_UNDEF, \
+ .flags = 0, \
+ .jobs = LIST_HEAD_INIT(tsk.atlas.jobs), \
+ .job = NULL, \
+ .real_job = NULL, \
+ .jobs_lock = __SPIN_LOCK_UNLOCKED(tsk.atlas.jobs_lock), \
+ }, \
.tasks = LIST_HEAD_INIT(tsk.tasks), \
INIT_PUSHABLE_TASKS(tsk) \
INIT_CGROUP_SCHED(tsk) \
diff --git a/include/linux/sched.h b/include/linux/sched.h
index e63650f..d18a61d 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -39,6 +39,8 @@
#define SCHED_BATCH 3
/* SCHED_ISO: reserved but not implemented yet */
#define SCHED_IDLE 5
+/* SCHED_ATLAS: ATLAS Scheduler*/
+#define SCHED_ATLAS 6
/* Can be ORed in to make sure the process is reverted back to SCHED_NORMAL on fork */
#define SCHED_RESET_ON_FORK 0x40000000
@@ -1084,6 +1086,7 @@ struct sched_domain;
#endif
#define DEQUEUE_SLEEP 1
+#define DEQUEUE_SKIP_UPDATE 2
struct sched_class {
const struct sched_class *next;
@@ -1211,6 +1214,31 @@ struct sched_rt_entity {
#endif
};
+enum atlas_state {
+ ATLAS_UNDEF,
+ ATLAS_BLOCKED,
+ ATLAS_RUNNING,
+};
+
+
+struct sched_atlas_entity {
+ struct rb_node run_node; /*for normal operation*/
+ //struct list_head run_list; ??
+ struct list_head list; /*for initialization*/
+ unsigned int state;
+ unsigned long flags;
+ unsigned int on_rq;
+ unsigned int on_recover_rq;
+ ktime_t start;
+ //struct atlas_rq *atlas_rq; //needed?
+
+ struct atlas_job *job, *real_job;
+ struct list_head jobs;
+ spinlock_t jobs_lock;
+
+ struct hrtimer timer;
+};
+
/*
* default timeslice is 100 msecs (used only for SCHED_RR tasks).
* Timeslices get refilled after they expire.
@@ -1244,6 +1272,7 @@ struct task_struct {
const struct sched_class *sched_class;
struct sched_entity se;
struct sched_rt_entity rt;
+ struct sched_atlas_entity atlas;
#ifdef CONFIG_CGROUP_SCHED
struct task_group *sched_task_group;
#endif
@@ -2005,6 +2034,10 @@ extern unsigned int sysctl_sched_min_granularity;
extern unsigned int sysctl_sched_wakeup_granularity;
extern unsigned int sysctl_sched_child_runs_first;
+extern unsigned int sysctl_sched_atlas_min_slack;
+extern unsigned int sysctl_sched_atlas_advance_in_cfs;
+
+
enum sched_tunable_scaling {
SCHED_TUNABLESCALING_NONE,
SCHED_TUNABLESCALING_LOG,
diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h
index 19439c7..9c26268 100644
--- a/include/linux/syscalls.h
+++ b/include/linux/syscalls.h
@@ -857,6 +857,14 @@ asmlinkage long sys_process_vm_writev(pid_t pid,
const struct iovec __user *rvec,
unsigned long riovcnt,
unsigned long flags);
+asmlinkage long sys_atlas_next(void);
+asmlinkage long sys_atlas_submit(pid_t pid,
+ struct timeval __user *exectime,
+ struct timeval __user *deadline,
+ int time_base);
+asmlinkage long sys_atlas_debug(int operation,
+ int arg1,
+ int arg2);
asmlinkage long sys_kcmp(pid_t pid1, pid_t pid2, int type,
unsigned long idx1, unsigned long idx2);
diff --git a/include/trace/events/sched.h b/include/trace/events/sched.h
index ea7a203..708076a 100644
--- a/include/trace/events/sched.h
+++ b/include/trace/events/sched.h
@@ -150,6 +150,130 @@ TRACE_EVENT(sched_switch,
);
/*
+ * Tracepoint for pick_next_task.
+ */
+DECLARE_EVENT_CLASS(sched_pick_put_template,
+
+ TP_PROTO(struct rq *rq, struct task_struct *p),
+
+ TP_ARGS(rq, p),
+
+ TP_STRUCT__entry(
+ __array( char, p_comm, TASK_COMM_LEN )
+ __field( pid_t, pid )
+ __field( int, policy )
+ __field( unsigned long, pending_work)
+ __field( unsigned long, flags )
+ __field( int, has_sub )
+ __field( void *, job )
+ __field( s64, sdeadline )
+ __field( s64, deadline )
+ __field( s64, sexectime )
+ __field( s64, exectime )
+ __field( s64, now )
+ ),
+
+ TP_fast_assign(
+ memcpy(__entry->p_comm, p->comm, TASK_COMM_LEN);
+ __entry->pid = p->pid;
+ __entry->policy = p->policy;
+ __entry->flags = p->atlas.flags;
+ __entry->job = p->atlas.job;
+ __entry->sdeadline = __entry->job ? ktime_to_ns(p->atlas.job->sdeadline) : 0;
+ __entry->deadline = __entry->job ? ktime_to_ns(p->atlas.job->deadline) : 0;
+ __entry->sexectime = __entry->job ? ktime_to_ns(p->atlas.job->sexectime) : 0;
+ __entry->exectime = __entry->job ? ktime_to_ns(p->atlas.job->exectime) : 0;
+ __entry->now = ktime_to_ns(ktime_get());
+ ),
+
+ TP_printk("pid=%d",
+ __entry->pid)
+);
+
+DEFINE_EVENT(sched_pick_put_template, sched_pick_next_task,
+ TP_PROTO(struct rq *rq, struct task_struct *p),
+ TP_ARGS(rq, p));
+DEFINE_EVENT(sched_pick_put_template, sched_put_prev_task,
+ TP_PROTO(struct rq *rq, struct task_struct *p),
+ TP_ARGS(rq, p));
+
+/*
+ * Tracepoint for queuing:
+ */
+DECLARE_EVENT_CLASS(sched_queue_template,
+
+ TP_PROTO(struct task_struct *p, struct rq *rq),
+
+ TP_ARGS(p, rq),
+
+ TP_STRUCT__entry(
+ __array( char, comm, TASK_COMM_LEN )
+ __field( pid_t, pid )
+ __field( int, policy )
+ __field( int, rq_cpu )
+ __field( s64, now )
+ ),
+
+ TP_fast_assign(
+ memcpy(__entry->comm, p->comm, TASK_COMM_LEN);
+ __entry->pid = p->pid;
+ __entry->policy = p->policy;
+ __entry->rq_cpu = rq->cpu;
+ __entry->now = ktime_to_ns(ktime_get());
+ ),
+
+ TP_printk("pid=%d",
+ __entry->pid)
+);
+
+DEFINE_EVENT(sched_queue_template, sched_enqueue_task,
+ TP_PROTO(struct task_struct *p, struct rq *rq),
+ TP_ARGS(p, rq));
+
+DEFINE_EVENT(sched_queue_template, sched_dequeue_task,
+ TP_PROTO(struct task_struct *p, struct rq *rq),
+ TP_ARGS(p, rq));
+
+TRACE_EVENT(sched_enter,
+
+ TP_PROTO(struct rq *rq),
+
+ TP_ARGS(rq),
+
+ TP_STRUCT__entry(
+ __field( unsigned long, pending_work)
+ __field( s64, now )
+ ),
+
+ TP_fast_assign(
+ __entry->pending_work = rq->atlas.pending_work | rq->atlas_recover.pending_work << 8;
+ __entry->now = ktime_to_ns(ktime_get());
+ ),
+
+ TP_printk("sched_enter (%llu)",
+ __entry->now)
+);
+
+TRACE_EVENT(sched_log,
+ TP_PROTO(const char *msg),
+
+ TP_ARGS(msg),
+
+ TP_STRUCT__entry(
+ __array( char, message, 30 )
+ __field( s64, now )
+ ),
+
+ TP_fast_assign(
+ memcpy(__entry->message, msg, TASK_COMM_LEN);
+ __entry->now = ktime_to_ns(ktime_get());
+ ),
+
+ TP_printk("sched_log: %s",
+ __entry->message)
+);
+
+/*
* Tracepoint for a task being migrated:
*/
TRACE_EVENT(sched_migrate_task,
diff --git a/kernel/exit.c b/kernel/exit.c
index 46ce8da..f438a6a 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -60,6 +60,7 @@
#include <asm/mmu_context.h>
static void exit_mm(struct task_struct * tsk);
+extern void exit_atlas(struct task_struct * tsk);
static void __unhash_process(struct task_struct *p, bool group_dead)
{
@@ -991,6 +992,7 @@ void do_exit(long code)
acct_process();
trace_sched_process_exit(tsk);
+ exit_atlas(tsk);
exit_sem(tsk);
exit_shm(tsk);
exit_files(tsk);
diff --git a/kernel/printk.c b/kernel/printk.c
index 146827f..b827392 100644
--- a/kernel/printk.c
+++ b/kernel/printk.c
@@ -1875,21 +1875,33 @@ int is_console_locked(void)
/*
* Delayed printk version, for scheduler-internal messages:
*/
-#define PRINTK_BUF_SIZE 512
+#define PRINTK_BUF_SIZE 128
+#define PRINTK_BUF_NR 256
#define PRINTK_PENDING_WAKEUP 0x01
#define PRINTK_PENDING_SCHED 0x02
static DEFINE_PER_CPU(int, printk_pending);
-static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf);
+
+typedef struct {
+ int idx;
+ char buf[PRINTK_BUF_NR][PRINTK_BUF_SIZE];
+} printk_sched_buf_t;
+
+static DEFINE_PER_CPU(printk_sched_buf_t, printk_sched_buf) = {.idx = 0 };
void printk_tick(void)
{
if (__this_cpu_read(printk_pending)) {
int pending = __this_cpu_xchg(printk_pending, 0);
if (pending & PRINTK_PENDING_SCHED) {
- char *buf = __get_cpu_var(printk_sched_buf);
- printk(KERN_WARNING "[sched_delayed] %s", buf);
+ printk_sched_buf_t *data = &__get_cpu_var(printk_sched_buf);
+ int i;
+ for (i = 0; i < data->idx; ++i) {
+ char *buf = data->buf[i];
+ printk(KERN_WARNING "[sched_delayed] %s", buf);
+ }
+ data->idx = 0;
}
if (pending & PRINTK_PENDING_WAKEUP)
wake_up_interruptible(&log_wait);
@@ -2362,16 +2374,19 @@ int printk_sched(const char *fmt, ...)
{
unsigned long flags;
va_list args;
+ printk_sched_buf_t *data;
char *buf;
int r;
local_irq_save(flags);
- buf = __get_cpu_var(printk_sched_buf);
-
+ data = &__get_cpu_var(printk_sched_buf);
+ BUG_ON(data->idx >= PRINTK_BUF_NR);
+ buf = data->buf[data->idx++];
+
va_start(args, fmt);
r = vsnprintf(buf, PRINTK_BUF_SIZE, fmt, args);
va_end(args);
-
+
__this_cpu_or(printk_pending, PRINTK_PENDING_SCHED);
local_irq_restore(flags);
diff --git a/kernel/sched/Makefile b/kernel/sched/Makefile
index 173ea52..445a719 100644
--- a/kernel/sched/Makefile
+++ b/kernel/sched/Makefile
@@ -11,7 +11,7 @@ ifneq ($(CONFIG_SCHED_OMIT_FRAME_POINTER),y)
CFLAGS_core.o := $(PROFILING) -fno-omit-frame-pointer
endif
-obj-y += core.o clock.o idle_task.o fair.o rt.o stop_task.o
+obj-y += core.o clock.o idle_task.o fair.o rt.o stop_task.o atlas.o atlas_recover.o
obj-$(CONFIG_SMP) += cpupri.o
obj-$(CONFIG_SCHED_AUTOGROUP) += auto_group.o
obj-$(CONFIG_SCHEDSTATS) += stats.o
diff --git a/kernel/sched/atlas.c b/kernel/sched/atlas.c
new file mode 100644
index 0000000..454f791
--- /dev/null
+++ b/kernel/sched/atlas.c
@@ -0,0 +1,2058 @@
+#include <linux/syscalls.h>
+#include <linux/rbtree.h>
+#include <linux/slab.h>
+#include <linux/hrtimer.h>
+#include <linux/ktime.h>
+#include "sched.h"
+
+
+const struct sched_class atlas_sched_class;
+
+unsigned int sysctl_sched_atlas_min_slack = 1000000ULL;
+unsigned int sysctl_sched_atlas_advance_in_cfs = 0;
+
+
+#define TIMER_EXPIRED 0x01
+
+/* pending work definitions */
+#define PENDING_STOP_CFS_ADVANCED 0x01
+#define PENDING_START_CFS_ADVANCED 0x02
+#define PENDING_JOB_TIMER 0x04
+#define PENDING_MOVE_TO_CFS 0x08
+#define PENDING_MOVE_TO_RECOVER 0x10
+#define PENDING_MOVE_TO_ATLAS 0x20
+
+#define ATLAS_DEBUG
+
+enum {
+ DEBUG_SYS_NEXT = 1UL << 0,
+ DEBUG_SYS_SUBMIT = 1UL << 1,
+ DEBUG_ENQUEUE = 1UL << 2,
+ DEBUG_DEQUEUE = 1UL << 3,
+ DEBUG_PICK_NEXT_TASK = 1UL << 4,
+ DEBUG_SET_CURR_TASK = 1UL << 5,
+ DEBUG_SWITCHED_TO = 1UL << 6,
+ DEBUG_PUT_PREV_TASK = 1UL << 7,
+ DEBUG_CHECK_PREEMPT = 1UL << 8,
+ DEBUG_RBTREE = 1UL << 9,
+ DEBUG_TIMER = 1UL << 10,
+ DEBUG_SUBMISSIONS = 1UL << 11,
+ DEBUG_SWITCH_SCHED = 1UL << 11,
+ DEBUG_ADAPT_SEXEC = 1UL << 13,
+ DEBUG_SLACK_TIME = 1UL << 14,
+};
+
+enum update_exec_time {
+ UPDATE_EXEC_TIME,
+ NO_UPDATE_EXEC_TIME,
+};
+
+#ifdef ATLAS_DEBUG
+//static const unsigned debug_mask = DEBUG_PICK_NEXT_TASK |
+// DEBUG_PUT_PREV_TASK | DEBUG_ENQUEUE | DEBUG_DEQUEUE | DEBUG_TIMER | DEBUG_SWITCH_SCHED |
+// DEBUG_SYS_NEXT;
+
+static const unsigned debug_mask = 0;
+
+static int printk_counter = 0;
+ #define DEBUG(T,STR,...) \
+ do { \
+ if (T & debug_mask) { \
+ preempt_disable(); \
+ printk_sched("%d (%d): "#T ": " STR "\n", (printk_counter++), \
+ smp_processor_id(), ##__VA_ARGS__); \
+ preempt_enable(); \
+ } \
+ } while(0)
+
+ #define DEBUG_ON(T) if (debug_mask & (T))
+
+#else
+ #define DEBUG(...)
+ #define DEBUG_ON(T) if (0)
+#endif /* ATLAS_DEBUG */
+
+
+
+static inline void init_job(struct atlas_job *job) {
+ memset(job, 0, sizeof(struct atlas_job));
+ atomic_set(&job->count, 1);
+}
+
+static inline struct atlas_job *get_job
+ (struct atlas_job *job)
+{
+ if (job)
+ atomic_inc(&job->count);
+ return job;
+}
+
+
+static void put_job(struct atlas_job *job)
+{
+ if (!job)
+ return;
+
+ if (atomic_dec_and_test(&job->count)) {
+ //printk_sched("free job=%p\n", job);
+ put_pid(job->pid);
+ kfree(job);
+ }
+}
+
+static inline int job_before(struct atlas_job *a,
+ struct atlas_job *b)
+{
+ BUG_ON(!a);
+ BUG_ON(!b);
+ return ktime_to_ns(a->deadline) < ktime_to_ns(b->deadline);
+}
+
+static int entity_before(struct sched_atlas_entity *a,
+ struct sched_atlas_entity *b)
+{
+
+ /*
+ * a preemption within sys_next or a wakeup due to a signal can lead
+ * into cases where se->job is null.
+ * Because we also queue this se's into the tree, we have to check
+ * both.
+ *
+ * 4 cases:
+ * new | comparator
+ * ----------------
+ * o | o doesn't matter
+ * o | x new should go to the beginning
+ * x | o the old entry should stay on the left side
+ * x | x compare
+ */
+
+ if (unlikely(!a->job)) //left side if new has no submisson
+ return 1;
+
+ if (unlikely(!b->job)) //right side
+ return 0;
+
+ return job_before(a->job, b->job);
+}
+
+static void enqueue_entity(struct atlas_rq *atlas_rq,
+ struct sched_atlas_entity *se)
+{
+ struct rb_node **link = &atlas_rq->tasks_timeline.rb_node;
+ struct rb_node *parent = NULL;
+ struct sched_atlas_entity *entry;
+ int leftmost = 1;
+
+ //FIXME?
+ rb_init_node(&se->run_node);
+
+ DEBUG(DEBUG_RBTREE, "enqueue_task_rb_tree");
+ while (*link) {
+ parent = *link;
+ entry = rb_entry(parent, struct sched_atlas_entity, run_node);
+
+ if (entity_before(se, entry))
+ link = &parent->rb_left;
+ else {
+ link = &parent->rb_right;
+ leftmost = 0;
+ }
+ }
+
+ if (leftmost)
+ atlas_rq->rb_leftmost_se = &se->run_node;
+
+ rb_link_node(&se->run_node, parent, link);
+ rb_insert_color(&se->run_node, &atlas_rq->tasks_timeline);
+}
+
+static void dequeue_entity(struct atlas_rq *atlas_rq,
+ struct sched_atlas_entity *se)
+{
+ DEBUG(DEBUG_RBTREE, "dequeue_task_rb_tree");
+
+ if (atlas_rq->rb_leftmost_se == &se->run_node) {
+ struct rb_node *next_node;
+
+ next_node = rb_next(&se->run_node);
+ atlas_rq->rb_leftmost_se = next_node;
+ }
+
+ rb_erase(&se->run_node, &atlas_rq->tasks_timeline);
+}
+
+static struct sched_atlas_entity *pick_first_entity(struct atlas_rq *atlas_rq)
+{
+ struct rb_node *left = atlas_rq->rb_leftmost_se;
+
+ if (!left)
+ return NULL;
+
+ return rb_entry(left, struct sched_atlas_entity, run_node);
+}
+
+static struct sched_atlas_entity *pick_next_entity(struct sched_atlas_entity *se)
+{
+ struct rb_node *next = rb_next(&se->run_node);
+
+ if (!next)
+ return NULL;
+
+ return rb_entry(next, struct sched_atlas_entity, run_node);
+}
+
+static struct atlas_job *pick_first_job(struct atlas_rq *atlas_rq) {
+ struct rb_node *first = rb_first(&atlas_rq->jobs);
+
+ if (!first)
+ return NULL;
+
+ return rb_entry(first, struct atlas_job, rb_node);
+}
+
+static struct atlas_job *pick_last_job(struct atlas_rq *atlas_rq) {
+ struct rb_node *last = rb_last(&atlas_rq->jobs);
+
+ if (!last)
+ return NULL;
+
+ return rb_entry(last, struct atlas_job, rb_node);
+}
+
+static struct atlas_job *pick_next_job(struct atlas_job *s) {
+ struct rb_node *next = rb_next(&s->rb_node);
+
+ if (!next)
+ return NULL;
+
+ return rb_entry(next, struct atlas_job, rb_node);
+}
+
+static struct atlas_job *pick_prev_job(struct atlas_job *s) {
+ struct rb_node *prev = rb_prev(&s->rb_node);
+
+ if (!prev)
+ return NULL;
+
+ return rb_entry(prev, struct atlas_job, rb_node);
+}
+
+static inline int job_in_rq(struct atlas_job *s) {
+ return !RB_EMPTY_NODE(&s->rb_node);
+}
+
+/*
+ * remember to call put_task_struct(p) after you are done
+ */
+static inline struct task_struct *task_of_job(struct atlas_job *s) {
+ return get_pid_task(s->pid, PIDTYPE_PID);
+}
+
+static inline int in_slacktime(struct atlas_rq *atlas_rq) {
+ return (atlas_rq->timer_target == ATLAS_SLACK);
+}
+
+static inline ktime_t ktime_min(ktime_t a, ktime_t b) {
+ return ns_to_ktime(min(ktime_to_ns(a), ktime_to_ns(b)));
+}
+
+static inline int ktime_neg(ktime_t a) {
+ return ktime_to_ns(a) < 0;
+}
+
+static inline int ktime_zero(ktime_t a) {
+ return ktime_equal(ktime_set(0,0), a);
+}
+
+static inline int ktime_cmp(ktime_t a, ktime_t b) {
+ s64 tmp = ktime_to_ns(ktime_sub(a, b));
+ if (tmp > 0)
+ return 1;
+ else if (tmp == 0)
+ return 0;
+ else
+ return -1;
+}
+
+static inline ktime_t job_start(struct atlas_job *s) {
+ return ktime_sub(s->sdeadline, s->sexectime);
+}
+
+static inline int job_missed_deadline(struct atlas_job *s, ktime_t now) {
+ return ktime_cmp(s->deadline, now) <= 0;
+}
+
+static inline struct rq *rq_of(struct atlas_rq *atlas_rq)
+{
+ return container_of(atlas_rq, struct rq, atlas);
+}
+
+static inline struct task_struct *task_of(struct sched_atlas_entity *se)
+{
+ return container_of(se, struct task_struct, atlas);
+}
+
+
+
+/*
+ **********************************************************
+ *** timer stuff ***
+ **********************************************************
+ */
+
+static inline int hrtimer_start_nowakeup(struct hrtimer *timer, ktime_t tim,
+ const enum hrtimer_mode mode)
+{
+ return __hrtimer_start_range_ns(timer, tim, 0, mode, 0);
+}
+
+static inline void __setup_rq_timer(struct atlas_rq *atlas_rq, ktime_t ktime) {
+ assert_raw_spin_locked(&rq_of(atlas_rq)->lock);
+ atlas_rq->timer_end = ktime;
+
+ DEBUG(DEBUG_TIMER, "timer up to: %lld",
+ ktime_to_us(atlas_rq->timer_end));
+
+ BUG_ON(atlas_rq->timer_target == ATLAS_NONE);
+ hrtimer_start_nowakeup(&atlas_rq->timer, ktime, HRTIMER_MODE_ABS_PINNED);
+}
+
+static inline void start_slack(struct atlas_rq *atlas_rq, ktime_t slack) {
+ DEBUG(DEBUG_TIMER, "Setup timer for slack");
+ BUG_ON(atlas_rq->timer_target != ATLAS_NONE);
+ slack = ktime_add(slack, ktime_get());
+ atlas_rq->timer_target = ATLAS_SLACK;
+ __setup_rq_timer(atlas_rq, slack);
+}
+
+static inline void start_job(struct atlas_rq *atlas_rq, struct atlas_job *job) {
+ ktime_t tmp = ktime_get();
+ DEBUG(DEBUG_TIMER, "Setup timer for job");
+ BUG_ON(atlas_rq->timer_target != ATLAS_NONE);
+ atlas_rq->timer_target = ATLAS_JOB;
+
+ tmp = ktime_add(tmp, job->sexectime);
+ tmp = ktime_min(tmp, job->deadline);
+
+ __setup_rq_timer(atlas_rq, tmp);
+}
+
+static void reset_slack_time(struct atlas_rq *atlas_rq) {
+ if (!(atlas_rq->timer_target == ATLAS_SLACK))
+ return;
+
+ if (hrtimer_cancel(&atlas_rq->timer)) {
+ atlas_rq->pending_work |= PENDING_STOP_CFS_ADVANCED;
+ resched_task(rq_of(atlas_rq)->curr);
+ atlas_rq->timer_target = ATLAS_NONE;
+ }
+
+ BUG_ON(atlas_rq->timer_target != ATLAS_NONE);
+
+ DEBUG(DEBUG_TIMER, "reset timer programmed for slack time");
+}
+
+static void reset_job_time(struct atlas_rq *atlas_rq) {
+ if (!(atlas_rq->timer_target == ATLAS_JOB))
+ return;
+
+ if (hrtimer_cancel(&atlas_rq->timer)) {
+ atlas_rq->timer_target = ATLAS_NONE;
+ }
+
+ BUG_ON(atlas_rq->timer_target != ATLAS_NONE);
+
+ DEBUG(DEBUG_TIMER, "reset timer programmed for job");
+}
+
+static int update_execution_time(struct atlas_rq *atlas_rq,
+ struct atlas_job *job, ktime_t delta_exec);
+
+
+static inline void reset_timer(struct atlas_rq *atlas_rq) {
+
+ assert_raw_spin_locked(&rq_of(atlas_rq)->lock);
+
+ BUG_ON(atlas_rq->advance_in_cfs && atlas_rq->timer_target != ATLAS_SLACK && !(atlas_rq->pending_work & PENDING_STOP_CFS_ADVANCED));
+
+ switch (atlas_rq->timer_target) {
+ case ATLAS_NONE:
+ break;
+ case ATLAS_SLACK:
+ reset_slack_time(atlas_rq);
+ break;
+ case ATLAS_JOB:
+ reset_job_time(atlas_rq);
+ break;
+ default:
+ BUG();
+ }
+
+ BUG_ON(atlas_rq->advance_in_cfs && !(atlas_rq->pending_work & PENDING_STOP_CFS_ADVANCED));
+
+ BUG_ON(atlas_rq->timer_target != ATLAS_NONE);
+}
+
+static inline void erase_rq_job(struct atlas_rq *, struct atlas_job *);
+void atlas_switch_scheduler(struct rq *, struct task_struct *, const struct sched_class *);
+static void update_curr_atlas(struct rq *);
+
+
+static enum hrtimer_restart timer_rq_func(struct hrtimer *timer)
+{
+ struct atlas_rq *atlas_rq = container_of(timer, struct atlas_rq, timer);
+ struct rq *rq = rq_of(atlas_rq);
+ unsigned long flags;
+
+ raw_spin_lock_irqsave(&rq->lock, flags);
+
+ update_rq_clock(rq);
+ if (atlas_rq->curr)
+ update_curr_atlas(rq);
+
+ BUG_ON(atlas_rq->timer_target == ATLAS_NONE);
+
+ sched_log("Timer: %s", atlas_rq->timer_target == ATLAS_JOB ? "JOB" :
+ atlas_rq->timer_target == ATLAS_SLACK ? "SLACK" : "BUG");
+
+ switch (atlas_rq->timer_target) {
+ case ATLAS_JOB:
+ BUG_ON(rq->curr->sched_class != &atlas_sched_class);
+ atlas_rq->pending_work |= PENDING_JOB_TIMER;
+ break;
+ case ATLAS_SLACK:
+ atlas_rq->pending_work |= PENDING_STOP_CFS_ADVANCED;
+ break;
+ default:
+ BUG();
+ }
+
+ atlas_rq->timer_target = ATLAS_NONE;
+ DEBUG(DEBUG_TIMER, "timer expired: calling resched_task now");
+
+ /* resched curr */
+ if (rq->curr)
+ resched_task(rq->curr);
+
+ BUG_ON(atlas_rq->advance_in_cfs &&
+ !(atlas_rq->pending_work & PENDING_STOP_CFS_ADVANCED));
+
+ raw_spin_unlock_irqrestore(&rq->lock, flags);
+ return HRTIMER_NORESTART;
+}
+
+
+/*
+ * switching between the schedulers
+ */
+
+#ifdef ATLAS_DEBUG
+static const char * sched_name(const struct sched_class *c) {
+ if (c == &rt_sched_class)
+ return "REALTIME";
+ if (c == &atlas_sched_class)
+ return "ATLAS";
+ if (c == &atlas_recover_sched_class)
+ return "ATLAS_RECOVER";
+ if (c == &fair_sched_class)
+ return "CFS";
+ if (c == &idle_sched_class)
+ return "IDLE";
+ return "UNKNOWN";
+}
+#endif /* ATLAS_DEBUG */
+
+/*
+ * scheduler switching stuff
+ */
+
+void atlas_switch_scheduler(struct rq *rq,
+ struct task_struct *p, const struct sched_class *new_sched_class)
+{
+ const struct sched_class *prev_sched_class;
+ int on_rq, running;
+
+ BUG_ON(in_interrupt());
+ assert_raw_spin_locked(&rq->lock);
+
+ //raw_spin_lock(&p->pi_lock);
+
+ prev_sched_class = p->sched_class;
+
+ if (new_sched_class == prev_sched_class) {
+ //raw_spin_unlock(&p->pi_lock);
+ return;
+ }
+ on_rq = p->on_rq;
+ running = rq->curr == p;
+
+ DEBUG(DEBUG_SWITCH_SCHED, "pid=%d from %s to %s, on_rq=%d, running=%d",
+ p->pid, sched_name(prev_sched_class), sched_name(new_sched_class), on_rq, running);
+
+ if (on_rq)
+ prev_sched_class->dequeue_task(rq, p, 0);
+ if (running)
+ prev_sched_class->put_prev_task(rq, p);
+
+ p->sched_class = new_sched_class;
+
+ if (running)
+ new_sched_class->set_curr_task(rq);
+ if (on_rq)
+ new_sched_class->enqueue_task(rq, p, 0);
+
+ if (prev_sched_class->switched_from)
+ prev_sched_class->switched_from(rq, p);
+ new_sched_class->switched_to(rq, p);
+
+ //FIXME: pi-stuff?
+ //raw_spin_unlock(&p->pi_lock);
+ //rt_mutex_adjust_pi(p);
+}
+
+static void advance_thread_in_cfs(struct atlas_rq *atlas_rq) {
+ struct sched_atlas_entity *se;
+ struct task_struct *p;
+
+ BUG_ON(atlas_rq->advance_in_cfs != NULL);
+
+ if (!atlas_rq->nr_runnable) {
+ sched_log("advance: no thread ready");
+ reset_slack_time(atlas_rq);
+ return;
+ }
+
+ se = atlas_rq->curr;
+
+ /*
+ * se can be the blocked entity in cfs (put_prev_task not called yet)
+ * -> select the first entity from rb-tree
+ */
+ if (!se || !task_of(se)->on_rq)
+ se = pick_first_entity(atlas_rq);
+
+ BUG_ON(!se);
+
+ p = task_of(se);
+ BUG_ON(!p->on_rq);
+
+ BUG_ON(atlas_rq->timer_target != ATLAS_SLACK);
+ atlas_rq->advance_in_cfs = p;
+
+ //move p to cfs
+ p->atlas.flags |= ATLAS_CFS_ADVANCED;
+
+ sched_log("advance: next thread p=%d", p->pid);
+ atlas_switch_scheduler(rq_of(atlas_rq), p, &fair_sched_class);
+}
+
+void atlas_cfs_blocked(struct rq *rq, struct task_struct *p) {
+ struct atlas_rq *atlas_rq = &rq->atlas;
+
+ assert_raw_spin_locked(&rq->lock);
+ sched_log("advance_in_cfs: blocked");
+ BUG_ON(p->sched_class != &fair_sched_class);
+ BUG_ON(p->on_rq);
+ BUG_ON(!(p->atlas.flags & ATLAS_CFS_ADVANCED));
+
+ /* switch the scheduling class back to atlas */
+ p->atlas.flags &= ~ATLAS_CFS_ADVANCED;
+ atlas_switch_scheduler(rq, p, &atlas_sched_class);
+ atlas_rq->advance_in_cfs = NULL;
+
+ /* move the next ready task to cfs */
+ if (in_slacktime(atlas_rq))
+ advance_thread_in_cfs(atlas_rq);
+}
+
+
+#ifdef ATLAS_DEBUG