This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
tt-6.0.patch
2433 lines (2315 loc) · 61 KB
/
tt-6.0.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/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index 021e83fe831f..6e432fcc8583 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -29,7 +29,12 @@ static const struct file_operations proc_sys_dir_file_operations;
static const struct inode_operations proc_sys_dir_operations;
/* shared constants to be used in various sysctls */
+#ifdef CONFIG_TT_SCHED
+const int sysctl_vals[] = { 0, 1, 2, 3, 4, 100, 200, 1000, 3000, INT_MAX, 65535, -1, -20, 19 };
+#else
const int sysctl_vals[] = { 0, 1, 2, 3, 4, 100, 200, 1000, 3000, INT_MAX, 65535, -1 };
+#endif
+
EXPORT_SYMBOL(sysctl_vals);
const unsigned long sysctl_long_vals[] = { 0, 1, LONG_MAX };
diff --git a/include/linux/sched.h b/include/linux/sched.h
index e7b2f8a5c711..3c9856266345 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -534,6 +534,23 @@ struct sched_statistics {
#endif /* CONFIG_SCHEDSTATS */
} ____cacheline_aligned;
+#ifdef CONFIG_TT_SCHED
+struct tt_node {
+ struct tt_node* next;
+ struct tt_node* prev;
+ unsigned int task_type;
+ u64 vruntime;
+ u64 start_time;
+
+ u64 prev_wait_time;
+ u64 wait_time;
+ u64 prev_burst;
+ u64 curr_burst;
+ u64 burst;
+ unsigned int rt_sticky;
+};
+#endif
+
struct sched_entity {
/* For load-balancing: */
struct load_weight load;
@@ -543,9 +560,14 @@ struct sched_entity {
u64 exec_start;
u64 sum_exec_runtime;
- u64 vruntime;
u64 prev_sum_exec_runtime;
+#ifdef CONFIG_TT_SCHED
+ struct tt_node tt_node;
+#endif
+
+ u64 vruntime;
+
u64 nr_migrations;
#ifdef CONFIG_FAIR_GROUP_SCHED
diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h
index 780690dc08cd..81645f7d767a 100644
--- a/include/linux/sysctl.h
+++ b/include/linux/sysctl.h
@@ -53,6 +53,11 @@ struct ctl_dir;
#define SYSCTL_MAXOLDUID ((void *)&sysctl_vals[10])
#define SYSCTL_NEG_ONE ((void *)&sysctl_vals[11])
+#ifdef CONFIG_TT_SCHED
+#define SYSCTL_NEG_TWENTY ((void *)&sysctl_vals[12])
+#define SYSCTL_NINETEEN ((void *)&sysctl_vals[13])
+#endif
+
extern const int sysctl_vals[];
#define SYSCTL_LONG_ZERO ((void *)&sysctl_long_vals[0])
diff --git a/init/Kconfig b/init/Kconfig
index 532362fcfe31..45fed3911f10 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -110,6 +110,10 @@ config THREAD_INFO_IN_TASK
One subtle change that will be needed is to use try_get_task_stack()
and put_task_stack() in save_thread_stack_tsk() and get_wchan().
+config TT_SCHED
+ bool "TT Scheduler"
+ default y
+
menu "General setup"
config BROKEN
@@ -997,6 +1001,7 @@ config CGROUP_WRITEBACK
menuconfig CGROUP_SCHED
bool "CPU controller"
+ depends on !TT_SCHED
default n
help
This feature lets CPU scheduler recognize task groups and control CPU
@@ -1274,6 +1279,8 @@ config CHECKPOINT_RESTORE
config SCHED_AUTOGROUP
bool "Automatic process group scheduling"
+ default n
+ depends on !TT_SCHED
select CGROUPS
select CGROUP_SCHED
select FAIR_GROUP_SCHED
diff --git a/kernel/Kconfig.preempt b/kernel/Kconfig.preempt
index c2f1fd95a821..92d9bbbd78b2 100644
--- a/kernel/Kconfig.preempt
+++ b/kernel/Kconfig.preempt
@@ -118,6 +118,7 @@ config PREEMPT_DYNAMIC
config SCHED_CORE
bool "Core Scheduling for SMT"
depends on SCHED_SMT
+ depends on !TT_SCHED
help
This option permits Core Scheduling, a means of coordinated task
selection across SMT siblings. When enabled -- see
diff --git a/kernel/reboot.c b/kernel/reboot.c
index 3c35445bf5ad..3c4f92639008 100644
--- a/kernel/reboot.c
+++ b/kernel/reboot.c
@@ -23,6 +23,10 @@
* this indicates whether you can reboot with ctrl-alt-del: the default is yes
*/
+#ifdef CONFIG_TT_SCHED
+extern int tt_dedicated_cpu_bound_enabled;
+#endif
+
static int C_A_D = 1;
struct pid *cad_pid;
EXPORT_SYMBOL(cad_pid);
@@ -230,6 +234,10 @@ void migrate_to_reboot_cpu(void)
/* The boot cpu is always logical cpu 0 */
int cpu = reboot_cpu;
+#ifdef CONFIG_TT_SCHED
+ tt_dedicated_cpu_bound_enabled = 0;
+#endif
+
cpu_hotplug_disable();
/* Make certain the cpu I'm about to reboot on is online */
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index ee28253c9ac0..f8eac7c1422f 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2938,6 +2938,12 @@ static int __set_cpus_allowed_ptr(struct task_struct *p,
struct rq *rq;
rq = task_rq_lock(p, &rf);
+
+#ifdef CONFIG_TT_SCHED
+ if (rq->cfs.dedicated_cpu_bound == &p->se.tt_node)
+ rq->cfs.dedicated_cpu_bound = NULL;
+#endif
+
return __set_cpus_allowed_ptr_locked(p, new_mask, flags, rq, &rf);
}
@@ -3079,6 +3085,20 @@ void relax_compatible_cpus_allowed_ptr(struct task_struct *p)
kfree(user_mask);
}
+#ifdef CONFIG_TT_SCHED
+inline void inc_nr_lat_sensitive(unsigned int cpu)
+{
+ if (tt_lat_sens_enabled)
+ per_cpu(nr_lat_sensitive, cpu)++;
+}
+
+inline void dec_nr_lat_sensitive(unsigned int cpu)
+{
+ if (per_cpu(nr_lat_sensitive, cpu))
+ per_cpu(nr_lat_sensitive, cpu) >>= 1;
+}
+#endif
+
void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
{
#ifdef CONFIG_SCHED_DEBUG
@@ -3124,6 +3144,12 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
trace_sched_migrate_task(p, new_cpu);
if (task_cpu(p) != new_cpu) {
+#ifdef CONFIG_TT_SCHED
+ if (task_is_lat_sensitive(p)) {
+ dec_nr_lat_sensitive(task_cpu(p));
+ inc_nr_lat_sensitive(new_cpu);
+ }
+#endif
if (p->sched_class->migrate_task_rq)
p->sched_class->migrate_task_rq(p, new_cpu);
p->se.nr_migrations++;
@@ -4486,6 +4512,78 @@ static int sysctl_schedstats(struct ctl_table *table, int write, void *buffer,
#ifdef CONFIG_SYSCTL
static struct ctl_table sched_core_sysctls[] = {
+#ifdef CONFIG_TT_SCHED
+ {
+ .procname = "sched_tt_balancer_opt",
+ .data = &tt_balancer_opt,
+ .maxlen = sizeof(int),
+ .mode = 0644,
+ .proc_handler = proc_dointvec_minmax,
+ .extra1 = SYSCTL_ZERO,
+ .extra2 = SYSCTL_THREE,
+ },
+ {
+ .procname = "sched_tt_max_lifetime",
+ .data = &tt_max_lifetime,
+ .maxlen = sizeof(unsigned int),
+ .mode = 0644,
+ .proc_handler = proc_dointvec,
+ },
+ {
+ .procname = "sched_tt_rt_prio",
+ .data = &tt_rt_prio,
+ .maxlen = sizeof(int),
+ .mode = 0644,
+ .proc_handler = proc_dointvec_minmax,
+ .extra1 = SYSCTL_NEG_TWENTY,
+ .extra2 = SYSCTL_NINETEEN,
+ },
+ {
+ .procname = "sched_tt_interactive_prio",
+ .data = &tt_interactive_prio,
+ .maxlen = sizeof(int),
+ .mode = 0644,
+ .proc_handler = proc_dointvec_minmax,
+ .extra1 = SYSCTL_NEG_TWENTY,
+ .extra2 = SYSCTL_NINETEEN,
+ },
+ {
+ .procname = "sched_tt_cpu_bound_prio",
+ .data = &tt_cpu_bound_prio,
+ .maxlen = sizeof(int),
+ .mode = 0644,
+ .proc_handler = proc_dointvec_minmax,
+ .extra1 = SYSCTL_NEG_TWENTY,
+ .extra2 = SYSCTL_NINETEEN,
+ },
+ {
+ .procname = "sched_tt_batch_prio",
+ .data = &tt_batch_prio,
+ .maxlen = sizeof(int),
+ .mode = 0644,
+ .proc_handler = proc_dointvec_minmax,
+ .extra1 = SYSCTL_NEG_TWENTY,
+ .extra2 = SYSCTL_NINETEEN,
+ },
+ {
+ .procname = "sched_tt_lat_sens_enabled",
+ .data = &tt_lat_sens_enabled,
+ .maxlen = sizeof(int),
+ .mode = 0644,
+ .proc_handler = proc_dointvec_minmax,
+ .extra1 = SYSCTL_ZERO,
+ .extra2 = SYSCTL_ONE,
+ },
+ {
+ .procname = "sched_tt_dedicated_cpu_bound_enabled",
+ .data = &tt_dedicated_cpu_bound_enabled,
+ .maxlen = sizeof(int),
+ .mode = 0644,
+ .proc_handler = proc_dointvec_minmax,
+ .extra1 = SYSCTL_ZERO,
+ .extra2 = SYSCTL_ONE,
+ },
+#endif
#ifdef CONFIG_SCHEDSTATS
{
.procname = "sched_schedstats",
@@ -4657,7 +4755,9 @@ void wake_up_new_task(struct task_struct *p)
{
struct rq_flags rf;
struct rq *rq;
-
+#ifdef CONFIG_TT_SCHED
+ int target_cpu = 0;
+#endif
raw_spin_lock_irqsave(&p->pi_lock, rf.flags);
WRITE_ONCE(p->__state, TASK_RUNNING);
#ifdef CONFIG_SMP
@@ -4671,12 +4771,25 @@ void wake_up_new_task(struct task_struct *p)
*/
p->recent_used_cpu = task_cpu(p);
rseq_migrate(p);
+#ifdef CONFIG_TT_SCHED
+ target_cpu = select_task_rq(p, task_cpu(p), WF_FORK);
+ __set_task_cpu(p, target_cpu);
+#else
__set_task_cpu(p, select_task_rq(p, task_cpu(p), WF_FORK));
+#endif
#endif
rq = __task_rq_lock(p, &rf);
+
+#ifdef CONFIG_TT_SCHED
+ if (task_is_lat_sensitive(p))
+ inc_nr_lat_sensitive(target_cpu);
+#endif
+
update_rq_clock(rq);
post_init_entity_util_avg(p);
-
+#ifdef CONFIG_TT_SCHED
+ p->se.tt_node.start_time = sched_clock();
+#endif
activate_task(rq, p, ENQUEUE_NOCLOCK);
trace_sched_wakeup_new(p);
check_preempt_curr(rq, p, WF_FORK);
@@ -5463,6 +5576,10 @@ void scheduler_tick(void)
perf_event_task_tick();
+#ifdef CONFIG_TT_SCHED
+ dec_nr_lat_sensitive(rq->cpu);
+#endif
+
#ifdef CONFIG_SMP
rq->idle_balance = idle_cpu(cpu);
trigger_load_balance(rq);
@@ -5514,7 +5631,9 @@ static void sched_tick_remote(struct work_struct *work)
struct rq *rq = cpu_rq(cpu);
struct task_struct *curr;
struct rq_flags rf;
+#ifndef CONFIG_TT_SCHED
u64 delta;
+#endif
int os;
/*
@@ -5533,7 +5652,7 @@ static void sched_tick_remote(struct work_struct *work)
goto out_unlock;
update_rq_clock(rq);
-
+#ifndef CONFIG_TT_SCHED
if (!is_idle_task(curr)) {
/*
* Make sure the next tick runs within a reasonable
@@ -5542,6 +5661,7 @@ static void sched_tick_remote(struct work_struct *work)
delta = rq_clock_task(rq) - curr->se.exec_start;
WARN_ON_ONCE(delta > (u64)NSEC_PER_SEC * 3);
}
+#endif
curr->sched_class->task_tick(rq, curr, 0);
calc_load_nohz_remote(rq);
@@ -9603,6 +9723,9 @@ static struct kmem_cache *task_group_cache __read_mostly;
DECLARE_PER_CPU(cpumask_var_t, load_balance_mask);
DECLARE_PER_CPU(cpumask_var_t, select_rq_mask);
+#ifdef CONFIG_TT_SCHED
+DEFINE_PER_CPU(int, nr_lat_sensitive);
+#endif
void __init sched_init(void)
{
@@ -9619,6 +9742,10 @@ void __init sched_init(void)
wait_bit_init();
+#ifdef CONFIG_TT_SCHED
+ printk(KERN_INFO "TT CPU scheduler v6.0 by Hamad Al Marri.");
+#endif
+
#ifdef CONFIG_FAIR_GROUP_SCHED
ptr += 2 * nr_cpu_ids * sizeof(void **);
#endif
@@ -9747,6 +9874,9 @@ void __init sched_init(void)
#endif /* CONFIG_SMP */
hrtick_rq_init(rq);
atomic_set(&rq->nr_iowait, 0);
+#ifdef CONFIG_TT_SCHED
+ per_cpu(nr_lat_sensitive, i) = 0;
+#endif
#ifdef CONFIG_SCHED_CORE
rq->core = rq;
diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c
index 667876da8382..f549b4c53f98 100644
--- a/kernel/sched/debug.c
+++ b/kernel/sched/debug.c
@@ -956,6 +956,22 @@ void proc_sched_show_task(struct task_struct *p, struct pid_namespace *ns,
#define P_SCHEDSTAT(F) __PS(#F, schedstat_val(p->stats.F))
#define PN_SCHEDSTAT(F) __PSN(#F, schedstat_val(p->stats.F))
+
+#ifdef CONFIG_TT_SCHED
+#define PN_TT(F, S) SEQ_printf(m, "%-45s: %20s\n", #F, #S)
+
+ if (p->se.tt_node.task_type == TT_NO_TYPE)
+ PN_TT(task_type, NO_TYPE);
+ else if (p->se.tt_node.task_type == TT_INTERACTIVE)
+ PN_TT(task_type, INTERACTIVE);
+ else if (p->se.tt_node.task_type == TT_REALTIME)
+ PN_TT(task_type, REALTIME);
+ else if (p->se.tt_node.task_type == TT_CPU_BOUND)
+ PN_TT(task_type, CPU_BOUND);
+ else if (p->se.tt_node.task_type == TT_BATCH)
+ PN_TT(task_type, BATCH);
+#endif
+
PN(se.exec_start);
PN(se.vruntime);
PN(se.sum_exec_runtime);
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 914096c5b1ae..95774ec52c6b 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -105,6 +105,66 @@ unsigned int sysctl_sched_idle_min_granularity = 750000ULL;
*/
static unsigned int sched_nr_latency = 8;
+#ifdef CONFIG_TT_SCHED
+unsigned int __read_mostly tt_balancer_opt = TT_BL_CFS;
+unsigned int __read_mostly tt_max_lifetime = 22000; // in ms
+int __read_mostly tt_rt_prio = -20;
+int __read_mostly tt_interactive_prio = -10;
+int __read_mostly tt_cpu_bound_prio = -15;
+int __read_mostly tt_batch_prio = 19;
+int __read_mostly tt_lat_sens_enabled = 1;
+int __read_mostly tt_dedicated_cpu_bound_enabled = 1;
+
+#define MAX_HRRN 0xFFFFFFFFFFFFFFFULL
+#define YIELD_MARK(ttn) ((ttn)->vruntime |= 0x8000000000000000ULL)
+#define YIELD_UNMARK(ttn) ((ttn)->vruntime &= 0x7FFFFFFFFFFFFFFFULL)
+
+struct global_candidate {
+ struct rq *rq;
+ struct tt_node *candidate;
+ u64 hrrn;
+
+ // for update
+ raw_spinlock_t lock;
+};
+
+struct global_candidate global_candidate = {0, 0, MAX_HRRN};
+
+#define LOCK_GRQ(grf) ({ \
+ rq_lock_irqsave(grq, &(grf)); \
+ update_rq_clock(grq); \
+})
+
+#define UNLOCK_GRQ(grf) ({ \
+ rq_unlock(grq, &(grf)); \
+ local_irq_restore((grf).flags); \
+})
+
+#define INTERACTIVE_HRRN 2U
+#define RT_WAIT_DELTA 800000U
+#define RT_BURST_DELTA 2000000U
+#define RT_BURST_MAX 4000000U
+
+#define HZ_PERIOD (1000000000 / HZ)
+#define RACE_TIME 40000000
+#define FACTOR (RACE_TIME / HZ_PERIOD)
+
+#define IS_REALTIME(ttn) ((ttn)->task_type == TT_REALTIME)
+#define IS_INTERACTIVE(ttn) ((ttn)->task_type == TT_INTERACTIVE)
+#define IS_NO_TYPE(ttn) ((ttn)->task_type == TT_NO_TYPE)
+#define IS_CPU_BOUND(ttn) ((ttn)->task_type == TT_CPU_BOUND)
+#define IS_BATCH(ttn) ((ttn)->task_type == TT_BATCH)
+
+#define GEQ(a, b) ((s64)((a) - (b)) >= 0) // is a >= b
+#define LEQ(a, b) ((s64)((a) - (b)) <= 0) // is a <= b
+#define LES(a, b) ((s64)((a) - (b)) < 0) // is a < b
+#define EQ_D(a, b, d) (LEQ(a, b + d) && GEQ(a, b - d))
+
+#define HRRN_PERCENT(ttn, now) \
+ (((ttn)->vruntime * 1000ULL) / (((now) - (ttn)->start_time) | 1))
+
+#endif
+
/*
* After fork, child runs first. If set to 0 (default) then
* parent will (try to) run first.
@@ -125,6 +185,13 @@ static unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
+#ifdef CONFIG_TT_SCHED
+static inline struct sched_entity *se_of(struct tt_node *ttn)
+{
+ return container_of(ttn, struct sched_entity, tt_node);
+}
+#endif
+
int sched_thermal_decay_shift;
static int __init setup_sched_thermal_decay_shift(char *str)
{
@@ -579,11 +646,24 @@ static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
return min_vruntime;
}
+#ifdef CONFIG_TT_SCHED
+/**
+ * Should `a` preempts `b`?
+ */
+static inline bool
+entity_before(struct tt_node *a, struct tt_node *b)
+{
+ u64 now = sched_clock();
+
+ return (s64)(HRRN_PERCENT(a, now) - HRRN_PERCENT(b, now)) < 0;
+}
+#else
static inline bool entity_before(struct sched_entity *a,
struct sched_entity *b)
{
return (s64)(a->vruntime - b->vruntime) < 0;
}
+#endif
#define __node_2_se(node) \
rb_entry((node), struct sched_entity, run_node)
@@ -618,12 +698,73 @@ static void update_min_vruntime(struct cfs_rq *cfs_rq)
static inline bool __entity_less(struct rb_node *a, const struct rb_node *b)
{
+#ifdef CONFIG_TT_SCHED
+ return entity_before(&(__node_2_se(a)->tt_node), &(__node_2_se(b)->tt_node));
+#else
return entity_before(__node_2_se(a), __node_2_se(b));
+#endif
}
+#ifdef CONFIG_TT_SCHED
/*
* Enqueue an entity into the rb-tree:
*/
+static void __enqueue_entity_cfs(struct cfs_rq *cfs_rq, struct sched_entity *se)
+{
+ rb_add_cached(&se->run_node, &cfs_rq->tasks_timeline, __entity_less);
+}
+
+static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
+{
+ struct tt_node *ttn = &se->tt_node;
+
+ ttn->next = ttn->prev = NULL;
+
+ // if empty
+ if (!cfs_rq->head) {
+ cfs_rq->head = ttn;
+ }
+ else {
+ ttn->next = cfs_rq->head;
+ cfs_rq->head->prev = ttn;
+ cfs_rq->head = ttn;
+ }
+
+ __enqueue_entity_cfs(cfs_rq, se);
+}
+
+static void __dequeue_entity_cfs(struct cfs_rq *cfs_rq, struct sched_entity *se)
+{
+ rb_erase_cached(&se->run_node, &cfs_rq->tasks_timeline);
+}
+
+static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
+{
+ struct tt_node *ttn = &se->tt_node;
+ struct tt_node *prev, *next;
+
+ // if only one se in rq
+ if (cfs_rq->head->next == NULL) {
+ cfs_rq->head = NULL;
+ }
+ // if it is the head
+ else if (ttn == cfs_rq->head) {
+ cfs_rq->head = cfs_rq->head->next;
+ cfs_rq->head->prev = NULL;
+ }
+ // if in the middle
+ else {
+ prev = ttn->prev;
+ next = ttn->next;
+
+ prev->next = next;
+ if (next)
+ next->prev = prev;
+ }
+
+ __dequeue_entity_cfs(cfs_rq, se);
+}
+#else
static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
{
rb_add_cached(&se->run_node, &cfs_rq->tasks_timeline, __entity_less);
@@ -633,7 +774,58 @@ static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
{
rb_erase_cached(&se->run_node, &cfs_rq->tasks_timeline);
}
+#endif
+
+#ifdef CONFIG_TT_SCHED
+struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
+{
+ struct tt_node *ttn = cfs_rq->head;
+ struct tt_node *next;
+
+ if (!ttn)
+ return NULL;
+
+ next = ttn->next;
+ while (next) {
+ if (entity_before(next, ttn))
+ ttn = next;
+
+ next = next->next;
+ }
+
+ return se_of(ttn);
+}
+
+static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
+{
+ struct tt_node *ttn = cfs_rq->head;
+ struct tt_node *se_ttn;
+ struct tt_node *next;
+
+ if (!ttn)
+ return NULL;
+
+ if (!se)
+ return __pick_first_entity(cfs_rq);
+
+ se_ttn = &se->tt_node;
+ next = ttn->next;
+
+ while (next) {
+ if (next != se_ttn) {
+ if (ttn == se_ttn || entity_before(next, ttn))
+ ttn = next;
+ }
+
+ next = next->next;
+ }
+
+ if (ttn == se_ttn)
+ return NULL;
+ return se_of(ttn);
+}
+#else
struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
{
struct rb_node *left = rb_first_cached(&cfs_rq->tasks_timeline);
@@ -653,6 +845,7 @@ static struct sched_entity *__pick_next_entity(struct sched_entity *se)
return __node_2_se(next);
}
+#endif
#ifdef CONFIG_SCHED_DEBUG
struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
@@ -879,13 +1072,244 @@ static void update_tg_load_avg(struct cfs_rq *cfs_rq)
}
#endif /* CONFIG_SMP */
+#ifdef CONFIG_TT_SCHED
+static inline bool is_interactive(struct tt_node *ttn, u64 now, u64 _hrrn)
+{
+ u64 wait;
+
+ if (LES(_hrrn, (u64) INTERACTIVE_HRRN))
+ return false;
+
+ wait = now - se_of(ttn)->exec_start;
+ if (wait && EQ_D(wait, ttn->prev_wait_time, RT_WAIT_DELTA))
+ return false;
+
+ return true;
+}
+
+static inline bool is_realtime(struct tt_node *ttn, u64 now, int flags)
+{
+ u64 life_time, wait;
+
+ // it has slept at least once
+ if (!ttn->wait_time)
+ return false;
+
+ // life time >= 0.5s
+ life_time = now - task_of(se_of(ttn))->start_time;
+ if (LES(life_time, 500000000ULL))
+ return false;
+
+ // don't check wait time for migrated tasks
+ if (!(flags & ENQUEUE_MIGRATED)) {
+ /* it has relatively equal sleeping/waiting times
+ * (ex. it sleeps for ~10ms and run repeatedly)
+ */
+ wait = now - se_of(ttn)->exec_start;
+ if (wait && !EQ_D(wait, ttn->prev_wait_time, RT_WAIT_DELTA))
+ return false;
+ }
+
+ // bursts before sleep are relatively equal (delta 2ms)
+ if (!EQ_D(ttn->burst, ttn->prev_burst, RT_BURST_DELTA))
+ return false;
+
+ // burst before sleep is <= 4ms
+ if (LEQ(ttn->burst, RT_BURST_MAX) &&
+ LEQ(ttn->curr_burst, RT_BURST_MAX))
+ return true;
+
+ return false;
+}
+
+static inline bool is_cpu_bound(struct tt_node *ttn)
+{
+ u64 _hrrn_percent;
+
+ _hrrn_percent = ttn->vruntime * 100ULL;
+ _hrrn_percent /= ttn->wait_time + ttn->vruntime;
+
+ // HRRN >= 90%
+ return (GEQ(_hrrn_percent, 90ULL));
+}
+
+static inline bool is_batch(struct tt_node *ttn, u64 _hrrn)
+{
+ // HRRN > 50%
+ return (LES(_hrrn, 2ULL));
+}
+
+static inline void set_dedicated_cpu_bound(struct cfs_rq *cfs_rq, struct tt_node *ttn)
+{
+ if (tt_dedicated_cpu_bound_enabled)
+ cfs_rq->dedicated_cpu_bound = ttn;
+}
+
+static inline void unset_dedicated_cpu_bound(struct cfs_rq *cfs_rq)
+{
+ cfs_rq->dedicated_cpu_bound = NULL;
+}
+
+static void check_dedicated_cpu_bound(struct cfs_rq *cfs_rq)
+{
+ struct tt_node *ttn = cfs_rq->head;
+ struct tt_node *curr_ttn = &cfs_rq->curr->tt_node;
+ struct tt_node *d_ttn = cfs_rq->dedicated_cpu_bound;
+
+ if (!tt_dedicated_cpu_bound_enabled)
+ return;
+
+ if (!d_ttn)
+ goto out;
+
+ if (curr_ttn == d_ttn)
+ return;
+
+ while (ttn) {
+ if (ttn == d_ttn)
+ return;
+
+ ttn = ttn->next;
+ }
+
+out:
+ unset_dedicated_cpu_bound(cfs_rq);
+}
+
+static void detect_type(struct cfs_rq *cfs_rq, struct tt_node *ttn, u64 now, int flags)
+{
+ unsigned int new_type = TT_NO_TYPE;
+ unsigned int old_type = ttn->task_type;
+ u64 _hrrn;
+ unsigned int cpu;
+
+ if (ttn->vruntime == 1) {
+ ttn->task_type = TT_NO_TYPE;
+ return;
+ }
+
+ _hrrn = (ttn->wait_time + ttn->vruntime) / ttn->vruntime;
+
+ if (is_realtime(ttn, now, flags))
+ new_type = TT_REALTIME;
+ else if (is_interactive(ttn, now, _hrrn))
+ new_type = TT_INTERACTIVE;
+ else if (is_cpu_bound(ttn))
+ new_type = TT_CPU_BOUND;
+ else if (is_batch(ttn, _hrrn))
+ new_type = TT_BATCH;
+
+ if (new_type == TT_REALTIME) {
+ ttn->rt_sticky = 4;
+ } else if (IS_REALTIME(ttn) && ttn->rt_sticky) {
+ ttn->rt_sticky--;
+ return;
+ }
+
+ if (new_type != old_type) {
+ cpu = task_cpu(task_of(se_of(ttn)));
+
+ /*
+ * Recall:
+ * TT_REALTIME 0
+ * TT_INTERACTIVE 1
+ * TT_NO_TYPE 2
+ * TT_CPU_BOUND 3
+ * TT_BATCH 4
+ */
+
+ /* set latency sensitive */
+ if (new_type <= TT_INTERACTIVE && old_type > TT_INTERACTIVE)
+ inc_nr_lat_sensitive(cpu);
+ else if (old_type <= TT_INTERACTIVE && new_type > TT_INTERACTIVE)
+ dec_nr_lat_sensitive(cpu);
+
+ /* set dedicated cpu bound */
+ if (!cfs_rq->dedicated_cpu_bound && new_type == TT_CPU_BOUND)
+ set_dedicated_cpu_bound(cfs_rq, ttn);
+ else if (cfs_rq->dedicated_cpu_bound == ttn && old_type == TT_CPU_BOUND)
+ unset_dedicated_cpu_bound(cfs_rq);
+ }
+
+ ttn->task_type = new_type;
+}
+
+static void normalize_lifetime(u64 now, struct tt_node *ttn)
+{
+ u64 max_life_ns, life_time, old_hrrn_x;
+ s64 diff;
+
+ /*
+ * left shift 20 bits is approximately = * 1000000
+ * we don't need the precision of life time
+ * Ex. for 22s, with left shift (20bits) == 23.06s
+ */
+ max_life_ns = ((u64) tt_max_lifetime) << 20;
+ life_time = now - ttn->start_time;
+ diff = life_time - max_life_ns;
+
+ if (likely(diff < 0))
+ return;
+
+ // unmark YIELD. No need to check or remark since
+ // this normalize action doesn't happen very often
+ YIELD_UNMARK(ttn);
+
+ // multiply life_time by 1024 for more precision
+ old_hrrn_x = (life_time << 7) / ((ttn->vruntime >> 3) | 1);
+
+ // reset life to half max_life (i.e ~15s)
+ ttn->start_time = now - (max_life_ns >> 1);
+
+ // avoid division by zero
+ if (old_hrrn_x == 0) old_hrrn_x = 1;
+
+ // reset vruntime based on old hrrn ratio
+ ttn->vruntime = ((max_life_ns << 9) / old_hrrn_x) | 1;
+}
+
+static u64 convert_to_vruntime(u64 delta, struct sched_entity *se)
+{
+ struct task_struct *p = task_of(se);
+ s64 prio_diff;
+ int prio = PRIO_TO_NICE(p->prio);
+
+ if (IS_REALTIME(&se->tt_node))
+ prio = tt_rt_prio;
+ else if (IS_INTERACTIVE(&se->tt_node))
+ prio = tt_interactive_prio;
+ else if (IS_CPU_BOUND(&se->tt_node))
+ prio = tt_cpu_bound_prio;
+ else if (IS_BATCH(&se->tt_node))
+ prio = tt_batch_prio;
+
+ if (prio == 0)
+ return delta;
+
+ prio_diff = prio * 1000000;
+ prio_diff /= FACTOR;
+
+ if ((s64)(delta + prio_diff) < 0)
+ return 1;
+
+ return delta + prio_diff;
+}
+
+static void update_candidate(struct cfs_rq *cfs_rq);
+#endif
+
/*
* Update the current task's runtime statistics.
*/
static void update_curr(struct cfs_rq *cfs_rq)
{
struct sched_entity *curr = cfs_rq->curr;
+#ifdef CONFIG_TT_SCHED
+ struct tt_node *ttn = &curr->tt_node;
+ u64 now = sched_clock();
+#else
u64 now = rq_clock_task(rq_of(cfs_rq));
+#endif
u64 delta_exec;
if (unlikely(!curr))
@@ -911,6 +1335,18 @@ static void update_curr(struct cfs_rq *cfs_rq)
curr->vruntime += calc_delta_fair(delta_exec, curr);
update_min_vruntime(cfs_rq);
+#ifdef CONFIG_TT_SCHED
+ ttn->curr_burst += delta_exec;
+ ttn->vruntime += convert_to_vruntime(delta_exec, curr);
+ detect_type(cfs_rq, ttn, now, 0);
+ normalize_lifetime(now, &curr->tt_node);
+
+ if (IS_CAND_BL_ENABLED) {
+ cfs_rq->local_cand_hrrn = HRRN_PERCENT(&curr->tt_node, now);
+ update_candidate(cfs_rq);
+ }
+#endif
+
if (entity_is_task(curr)) {
struct task_struct *curtask = task_of(curr);
@@ -1045,7 +1481,11 @@ update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
/*
* We are starting a new run period:
*/
+#ifdef CONFIG_TT_SCHED
+ se->exec_start = sched_clock();
+#else
se->exec_start = rq_clock_task(rq_of(cfs_rq));
+#endif
}
/**************************************************
@@ -4413,6 +4853,22 @@ enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
bool renorm = !(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_MIGRATED);
bool curr = cfs_rq->curr == se;
+#ifdef CONFIG_TT_SCHED
+ struct tt_node *ttn = &se->tt_node;
+ bool wakeup = (flags & ENQUEUE_WAKEUP);
+ u64 now = sched_clock();
+ u64 wait;
+
+ if (wakeup) {
+ wait = now - se->exec_start;
+ ttn->wait_time += wait;
+ detect_type(cfs_rq, ttn, now, flags);
+
+ ttn->prev_wait_time = wait;
+ } else {
+ detect_type(cfs_rq, ttn, now, flags);
+ }
+#endif
/*
* If we're the current task, we must renormalise before calling
* update_curr().
@@ -4508,9 +4964,135 @@ static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
+#ifdef CONFIG_TT_SCHED
+static inline int clear_this_candidate(struct sched_entity *se)
+{
+ struct tt_node *ttn = &se->tt_node;
+ struct tt_node *curr_can = READ_ONCE(global_candidate.candidate);
+
+ if (ttn != curr_can)
+ return 0;
+
+ WRITE_ONCE(global_candidate.candidate, NULL);
+ WRITE_ONCE(global_candidate.rq, NULL);
+ WRITE_ONCE(global_candidate.hrrn, MAX_HRRN);
+
+ return 1;
+}
+
+static inline void clear_rq_candidate(struct cfs_rq *cfs_rq)
+{
+ struct rq *rq = READ_ONCE(global_candidate.rq);
+
+ if (rq != rq_of(cfs_rq))
+ return;
+
+ WRITE_ONCE(global_candidate.candidate, NULL);
+ WRITE_ONCE(global_candidate.rq, NULL);
+ WRITE_ONCE(global_candidate.hrrn, MAX_HRRN);
+}
+
+static inline bool
+can_be_candidate(struct tt_node *ttn, int this_cpu)
+{
+ struct task_struct *p = task_of(se_of(ttn));
+
+ if (kthread_is_per_cpu(p))
+ return 0;