1 #include <linux/cgroup.h>
3 #include <linux/kernel.h>
4 #include <linux/percpu.h>
5 #include <linux/printk.h>
6 #include <linux/rcupdate.h>
7 #include <linux/slab.h>
9 #include <trace/events/sched.h>
14 unsigned int sysctl_sched_cfs_boost __read_mostly;
16 extern struct target_nrg schedtune_target_nrg;
18 /* Performance Boost region (B) threshold params */
19 static int perf_boost_idx;
21 /* Performance Constraint region (C) threshold params */
22 static int perf_constrain_idx;
25 * Performance-Energy (P-E) Space thresholds constants
27 struct threshold_params {
33 * System specific P-E space thresholds constants
35 static struct threshold_params
38 { 0, 4 }, /* >= 10% */
39 { 1, 4 }, /* >= 20% */
40 { 2, 4 }, /* >= 30% */
41 { 3, 4 }, /* >= 40% */
42 { 4, 3 }, /* >= 50% */
43 { 4, 2 }, /* >= 60% */
44 { 4, 1 }, /* >= 70% */
45 { 4, 0 }, /* >= 80% */
50 __schedtune_accept_deltas(int nrg_delta, int cap_delta,
51 int perf_boost_idx, int perf_constrain_idx)
53 int payoff = -INT_MAX;
56 /* Performance Boost (B) region */
57 if (nrg_delta >= 0 && cap_delta > 0)
58 gain_idx = perf_boost_idx;
59 /* Performance Constraint (C) region */
60 else if (nrg_delta < 0 && cap_delta <= 0)
61 gain_idx = perf_constrain_idx;
63 /* Default: reject schedule candidate */
68 * Evaluate "Performance Boost" vs "Energy Increase"
70 * - Performance Boost (B) region
72 * Condition: nrg_delta > 0 && cap_delta > 0
74 * cap_gain / nrg_gain < cap_delta / nrg_delta =
75 * cap_gain * nrg_delta < cap_delta * nrg_gain
76 * Note that since both nrg_gain and nrg_delta are positive, the
77 * inequality does not change. Thus:
79 * payoff = (cap_delta * nrg_gain) - (cap_gain * nrg_delta)
81 * - Performance Constraint (C) region
83 * Condition: nrg_delta < 0 && cap_delta < 0
85 * cap_gain / nrg_gain > cap_delta / nrg_delta =
86 * cap_gain * nrg_delta < cap_delta * nrg_gain
87 * Note that since nrg_gain > 0 while nrg_delta < 0, the
88 * inequality change. Thus:
90 * payoff = (cap_delta * nrg_gain) - (cap_gain * nrg_delta)
92 * This means that, in case of same positive defined {cap,nrg}_gain
93 * for both the B and C regions, we can use the same payoff formula
94 * where a positive value represents the accept condition.
96 payoff = cap_delta * threshold_gains[gain_idx].nrg_gain;
97 payoff -= nrg_delta * threshold_gains[gain_idx].cap_gain;
102 #ifdef CONFIG_CGROUP_SCHEDTUNE
105 * EAS scheduler tunables for task groups.
108 /* SchdTune tunables for a group of tasks */
110 /* SchedTune CGroup subsystem */
111 struct cgroup_subsys_state css;
113 /* Boost group allocated ID */
116 /* Boost value for tasks on that SchedTune CGroup */
119 /* Performance Boost (B) region threshold params */
122 /* Performance Constraint (C) region threshold params */
123 int perf_constrain_idx;
126 static inline struct schedtune *css_st(struct cgroup_subsys_state *css)
128 return css ? container_of(css, struct schedtune, css) : NULL;
131 static inline struct schedtune *task_schedtune(struct task_struct *tsk)
133 return css_st(task_css(tsk, schedtune_cgrp_id));
136 static inline struct schedtune *parent_st(struct schedtune *st)
138 return css_st(st->css.parent);
142 * SchedTune root control group
143 * The root control group is used to defined a system-wide boosting tuning,
144 * which is applied to all tasks in the system.
145 * Task specific boost tuning could be specified by creating and
146 * configuring a child control group under the root one.
147 * By default, system-wide boosting is disabled, i.e. no boosting is applied
148 * to tasks which are not into a child control group.
150 static struct schedtune
154 .perf_constrain_idx = 0,
158 schedtune_accept_deltas(int nrg_delta, int cap_delta,
159 struct task_struct *task)
161 struct schedtune *ct;
163 int perf_constrain_idx;
165 /* Optimal (O) region */
166 if (nrg_delta < 0 && cap_delta > 0) {
167 trace_sched_tune_filter(nrg_delta, cap_delta, 0, 0, 1, 0);
171 /* Suboptimal (S) region */
172 if (nrg_delta > 0 && cap_delta < 0) {
173 trace_sched_tune_filter(nrg_delta, cap_delta, 0, 0, -1, 5);
177 /* Get task specific perf Boost/Constraints indexes */
179 ct = task_schedtune(task);
180 perf_boost_idx = ct->perf_boost_idx;
181 perf_constrain_idx = ct->perf_constrain_idx;
184 return __schedtune_accept_deltas(nrg_delta, cap_delta,
185 perf_boost_idx, perf_constrain_idx);
189 * Maximum number of boost groups to support
190 * When per-task boosting is used we still allow only limited number of
191 * boost groups for two main reasons:
192 * 1. on a real system we usually have only few classes of workloads which
193 * make sense to boost with different values (e.g. background vs foreground
194 * tasks, interactive vs low-priority tasks)
195 * 2. a limited number allows for a simpler and more memory/time efficient
196 * implementation especially for the computation of the per-CPU boost
199 #define BOOSTGROUPS_COUNT 4
201 /* Array of configured boostgroups */
202 static struct schedtune *allocated_group[BOOSTGROUPS_COUNT] = {
207 /* SchedTune boost groups
208 * Keep track of all the boost groups which impact on CPU, for example when a
209 * CPU has two RUNNABLE tasks belonging to two different boost groups and thus
210 * likely with different boost values.
211 * Since on each system we expect only a limited number of boost groups, here
212 * we use a simple array to keep track of the metrics required to compute the
213 * maximum per-CPU boosting value.
215 struct boost_groups {
216 /* Maximum boost value for all RUNNABLE tasks on a CPU */
220 /* The boost for tasks on that boost group */
222 /* Count of RUNNABLE tasks on that boost group */
224 } group[BOOSTGROUPS_COUNT];
227 /* Boost groups affecting each CPU in the system */
228 DEFINE_PER_CPU(struct boost_groups, cpu_boost_groups);
231 schedtune_cpu_update(int cpu)
233 struct boost_groups *bg;
237 bg = &per_cpu(cpu_boost_groups, cpu);
239 /* The root boost group is always active */
240 boost_max = bg->group[0].boost;
241 for (idx = 1; idx < BOOSTGROUPS_COUNT; ++idx) {
243 * A boost group affects a CPU only if it has
244 * RUNNABLE tasks on that CPU
246 if (bg->group[idx].tasks == 0)
249 boost_max = max(boost_max, bg->group[idx].boost);
251 /* Ensures boost_max is non-negative when all cgroup boost values
252 * are neagtive. Avoids under-accounting of cpu capacity which may cause
253 * task stacking and frequency spikes.*/
254 boost_max = max(boost_max, 0);
255 bg->boost_max = boost_max;
259 schedtune_boostgroup_update(int idx, int boost)
261 struct boost_groups *bg;
266 /* Update per CPU boost groups */
267 for_each_possible_cpu(cpu) {
268 bg = &per_cpu(cpu_boost_groups, cpu);
271 * Keep track of current boost values to compute the per CPU
272 * maximum only when it has been affected by the new value of
273 * the updated boost group
275 cur_boost_max = bg->boost_max;
276 old_boost = bg->group[idx].boost;
278 /* Update the boost value of this boost group */
279 bg->group[idx].boost = boost;
281 /* Check if this update increase current max */
282 if (boost > cur_boost_max && bg->group[idx].tasks) {
283 bg->boost_max = boost;
284 trace_sched_tune_boostgroup_update(cpu, 1, bg->boost_max);
288 /* Check if this update has decreased current max */
289 if (cur_boost_max == old_boost && old_boost > boost) {
290 schedtune_cpu_update(cpu);
291 trace_sched_tune_boostgroup_update(cpu, -1, bg->boost_max);
295 trace_sched_tune_boostgroup_update(cpu, 0, bg->boost_max);
302 schedtune_tasks_update(struct task_struct *p, int cpu, int idx, int task_count)
304 struct boost_groups *bg;
307 bg = &per_cpu(cpu_boost_groups, cpu);
309 /* Update boosted tasks count while avoiding to make it negative */
310 if (task_count < 0 && bg->group[idx].tasks <= -task_count)
311 bg->group[idx].tasks = 0;
313 bg->group[idx].tasks += task_count;
315 /* Boost group activation or deactivation on that RQ */
316 tasks = bg->group[idx].tasks;
317 if (tasks == 1 || tasks == 0)
318 schedtune_cpu_update(cpu);
320 trace_sched_tune_tasks_update(p, cpu, tasks, idx,
321 bg->group[idx].boost, bg->boost_max);
326 * NOTE: This function must be called while holding the lock on the CPU RQ
328 void schedtune_enqueue_task(struct task_struct *p, int cpu)
330 struct schedtune *st;
334 * When a task is marked PF_EXITING by do_exit() it's going to be
335 * dequeued and enqueued multiple times in the exit path.
336 * Thus we avoid any further update, since we do not want to change
337 * CPU boosting while the task is exiting.
339 if (p->flags & PF_EXITING)
342 /* Get task boost group */
344 st = task_schedtune(p);
348 schedtune_tasks_update(p, cpu, idx, 1);
352 * NOTE: This function must be called while holding the lock on the CPU RQ
354 void schedtune_dequeue_task(struct task_struct *p, int cpu)
356 struct schedtune *st;
360 * When a task is marked PF_EXITING by do_exit() it's going to be
361 * dequeued and enqueued multiple times in the exit path.
362 * Thus we avoid any further update, since we do not want to change
363 * CPU boosting while the task is exiting.
364 * The last dequeue will be done by cgroup exit() callback.
366 if (p->flags & PF_EXITING)
369 /* Get task boost group */
371 st = task_schedtune(p);
375 schedtune_tasks_update(p, cpu, idx, -1);
378 int schedtune_cpu_boost(int cpu)
380 struct boost_groups *bg;
382 bg = &per_cpu(cpu_boost_groups, cpu);
383 return bg->boost_max;
386 int schedtune_task_boost(struct task_struct *p)
388 struct schedtune *st;
391 /* Get task boost value */
393 st = task_schedtune(p);
394 task_boost = st->boost;
401 boost_read(struct cgroup_subsys_state *css, struct cftype *cft)
403 struct schedtune *st = css_st(css);
409 boost_write(struct cgroup_subsys_state *css, struct cftype *cft,
412 struct schedtune *st = css_st(css);
413 unsigned threshold_idx;
416 if (boost < -100 || boost > 100)
420 if (css == &root_schedtune.css)
421 sysctl_sched_cfs_boost = boost;
423 /* Update CPU boost */
424 schedtune_boostgroup_update(st->idx, st->boost);
426 trace_sched_tune_config(st->boost);
431 static struct cftype files[] = {
434 .read_s64 = boost_read,
435 .write_s64 = boost_write,
441 schedtune_boostgroup_init(struct schedtune *st)
443 struct boost_groups *bg;
446 /* Keep track of allocated boost groups */
447 allocated_group[st->idx] = st;
449 /* Initialize the per CPU boost groups */
450 for_each_possible_cpu(cpu) {
451 bg = &per_cpu(cpu_boost_groups, cpu);
452 bg->group[st->idx].boost = 0;
453 bg->group[st->idx].tasks = 0;
462 struct boost_groups *bg;
465 /* Initialize the per CPU boost groups */
466 for_each_possible_cpu(cpu) {
467 bg = &per_cpu(cpu_boost_groups, cpu);
468 memset(bg, 0, sizeof(struct boost_groups));
471 pr_info(" schedtune configured to support %d boost groups\n",
476 static struct cgroup_subsys_state *
477 schedtune_css_alloc(struct cgroup_subsys_state *parent_css)
479 struct schedtune *st;
484 return &root_schedtune.css;
487 /* Allow only single level hierachies */
488 if (parent_css != &root_schedtune.css) {
489 pr_err("Nested SchedTune boosting groups not allowed\n");
490 return ERR_PTR(-ENOMEM);
493 /* Allow only a limited number of boosting groups */
494 for (idx = 1; idx < BOOSTGROUPS_COUNT; ++idx)
495 if (!allocated_group[idx])
497 if (idx == BOOSTGROUPS_COUNT) {
498 pr_err("Trying to create more than %d SchedTune boosting groups\n",
500 return ERR_PTR(-ENOSPC);
503 st = kzalloc(sizeof(*st), GFP_KERNEL);
507 /* Initialize per CPUs boost group support */
509 if (schedtune_boostgroup_init(st))
517 return ERR_PTR(-ENOMEM);
521 schedtune_boostgroup_release(struct schedtune *st)
523 /* Reset this boost group */
524 schedtune_boostgroup_update(st->idx, 0);
526 /* Keep track of allocated boost groups */
527 allocated_group[st->idx] = NULL;
531 schedtune_css_free(struct cgroup_subsys_state *css)
533 struct schedtune *st = css_st(css);
535 schedtune_boostgroup_release(st);
539 struct cgroup_subsys schedtune_cgrp_subsys = {
540 .css_alloc = schedtune_css_alloc,
541 .css_free = schedtune_css_free,
542 .legacy_cftypes = files,
546 #else /* CONFIG_CGROUP_SCHEDTUNE */
549 schedtune_accept_deltas(int nrg_delta, int cap_delta,
550 struct task_struct *task)
552 /* Optimal (O) region */
553 if (nrg_delta < 0 && cap_delta > 0) {
554 trace_sched_tune_filter(nrg_delta, cap_delta, 0, 0, 1, 0);
558 /* Suboptimal (S) region */
559 if (nrg_delta > 0 && cap_delta < 0) {
560 trace_sched_tune_filter(nrg_delta, cap_delta, 0, 0, -1, 5);
564 return __schedtune_accept_deltas(nrg_delta, cap_delta,
565 perf_boost_idx, perf_constrain_idx);
568 #endif /* CONFIG_CGROUP_SCHEDTUNE */
571 sysctl_sched_cfs_boost_handler(struct ctl_table *table, int write,
572 void __user *buffer, size_t *lenp,
575 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
580 /* Performance Boost (B) region threshold params */
581 perf_boost_idx = sysctl_sched_cfs_boost;
582 perf_boost_idx /= 10;
584 /* Performance Constraint (C) region threshold params */
585 perf_constrain_idx = 100 - sysctl_sched_cfs_boost;
586 perf_constrain_idx /= 10;
591 #ifdef CONFIG_SCHED_DEBUG
593 schedtune_test_nrg(unsigned long delta_pwr)
595 unsigned long test_delta_pwr;
596 unsigned long test_norm_pwr;
600 * Check normalization constants using some constant system
603 pr_info("schedtune: verify normalization constants...\n");
604 for (idx = 0; idx < 6; ++idx) {
605 test_delta_pwr = delta_pwr >> idx;
607 /* Normalize on max energy for target platform */
608 test_norm_pwr = reciprocal_divide(
609 test_delta_pwr << SCHED_LOAD_SHIFT,
610 schedtune_target_nrg.rdiv);
612 pr_info("schedtune: max_pwr/2^%d: %4lu => norm_pwr: %5lu\n",
613 idx, test_delta_pwr, test_norm_pwr);
617 #define schedtune_test_nrg(delta_pwr)
621 * Compute the min/max power consumption of a cluster and all its CPUs
624 schedtune_add_cluster_nrg(
625 struct sched_domain *sd,
626 struct sched_group *sg,
627 struct target_nrg *ste)
629 struct sched_domain *sd2;
630 struct sched_group *sg2;
632 struct cpumask *cluster_cpus;
635 unsigned long min_pwr;
636 unsigned long max_pwr;
639 /* Get Cluster energy using EM data for the first CPU */
640 cluster_cpus = sched_group_cpus(sg);
641 snprintf(str, 32, "CLUSTER[%*pbl]",
642 cpumask_pr_args(cluster_cpus));
644 min_pwr = sg->sge->idle_states[sg->sge->nr_idle_states - 1].power;
645 max_pwr = sg->sge->cap_states[sg->sge->nr_cap_states - 1].power;
646 pr_info("schedtune: %-17s min_pwr: %5lu max_pwr: %5lu\n",
647 str, min_pwr, max_pwr);
650 * Keep track of this cluster's energy in the computation of the
651 * overall system energy
653 ste->min_power += min_pwr;
654 ste->max_power += max_pwr;
656 /* Get CPU energy using EM data for each CPU in the group */
657 for_each_cpu(cpu, cluster_cpus) {
658 /* Get a SD view for the specific CPU */
659 for_each_domain(cpu, sd2) {
660 /* Get the CPU group */
662 min_pwr = sg2->sge->idle_states[sg2->sge->nr_idle_states - 1].power;
663 max_pwr = sg2->sge->cap_states[sg2->sge->nr_cap_states - 1].power;
665 ste->min_power += min_pwr;
666 ste->max_power += max_pwr;
668 snprintf(str, 32, "CPU[%d]", cpu);
669 pr_info("schedtune: %-17s min_pwr: %5lu max_pwr: %5lu\n",
670 str, min_pwr, max_pwr);
673 * Assume we have EM data only at the CPU and
674 * the upper CLUSTER level
676 BUG_ON(!cpumask_equal(
677 sched_group_cpus(sg),
678 sched_group_cpus(sd2->parent->groups)
686 * Initialize the constants required to compute normalized energy.
687 * The values of these constants depends on the EM data for the specific
688 * target system and topology.
689 * Thus, this function is expected to be called by the code
690 * that bind the EM to the topology information.
693 schedtune_init_late(void)
695 struct target_nrg *ste = &schedtune_target_nrg;
696 unsigned long delta_pwr = 0;
697 struct sched_domain *sd;
698 struct sched_group *sg;
700 pr_info("schedtune: init normalization constants...\n");
707 * When EAS is in use, we always have a pointer to the highest SD
708 * which provides EM data.
710 sd = rcu_dereference(per_cpu(sd_ea, cpumask_first(cpu_online_mask)));
712 pr_info("schedtune: no energy model data\n");
718 schedtune_add_cluster_nrg(sd, sg, ste);
719 } while (sg = sg->next, sg != sd->groups);
723 pr_info("schedtune: %-17s min_pwr: %5lu max_pwr: %5lu\n",
724 "SYSTEM", ste->min_power, ste->max_power);
726 /* Compute normalization constants */
727 delta_pwr = ste->max_power - ste->min_power;
728 ste->rdiv = reciprocal_value(delta_pwr);
729 pr_info("schedtune: using normalization constants mul: %u sh1: %u sh2: %u\n",
730 ste->rdiv.m, ste->rdiv.sh1, ste->rdiv.sh2);
732 schedtune_test_nrg(delta_pwr);
739 late_initcall(schedtune_init_late);