2 * Copyright (c) 2012 NVIDIA CORPORATION. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include <linux/kernel.h>
20 #include <linux/cpuquiet.h>
21 #include <linux/cpumask.h>
22 #include <linux/module.h>
23 #include <linux/pm_qos.h>
24 #include <linux/jiffies.h>
25 #include <linux/slab.h>
26 #include <linux/cpu.h>
27 #include <linux/sched.h>
36 static struct work_struct runnables_work;
37 static struct kobject *runnables_kobject;
38 static struct timer_list runnables_timer;
40 static RUNNABLES_STATE runnables_state;
41 /* configurable parameters */
42 static unsigned int sample_rate = 20; /* msec */
44 static RUNNABLES_STATE runnables_state;
46 #define NR_FSHIFT_EXP 3
47 #define NR_FSHIFT (1 << NR_FSHIFT_EXP)
48 /* avg run threads * 8 (e.g., 11 = 1.375 threads) */
49 static unsigned int default_thresholds[] = {
53 static unsigned int nr_run_last;
54 static unsigned int nr_run_hysteresis = 2; /* 1 / 2 thread */
55 static unsigned int default_threshold_level = 4; /* 1 / 4 thread */
56 static unsigned int nr_run_thresholds[NR_CPUS];
58 DEFINE_MUTEX(runnables_work_lock);
60 struct runnables_avg_sample {
61 u64 previous_integral;
63 bool integral_sampled;
67 static DEFINE_PER_CPU(struct runnables_avg_sample, avg_nr_sample);
69 /* EXP = alpha in the exponential moving average.
70 * Alpha = e ^ (-sample_rate / window_size) * FIXED_1
71 * Calculated for sample_rate of 20ms, window size of 100ms
75 static unsigned int get_avg_nr_runnables(void)
77 unsigned int i, sum = 0;
78 static unsigned int avg;
79 struct runnables_avg_sample *sample;
80 u64 integral, old_integral, delta_integral, delta_time, cur_time;
82 for_each_online_cpu(i) {
83 sample = &per_cpu(avg_nr_sample, i);
84 integral = nr_running_integral(i);
85 old_integral = sample->previous_integral;
86 sample->previous_integral = integral;
87 cur_time = ktime_to_ns(ktime_get());
88 delta_time = cur_time - sample->prev_timestamp;
89 sample->prev_timestamp = cur_time;
91 if (!sample->integral_sampled) {
92 sample->integral_sampled = true;
93 /* First sample to initialize prev_integral, skip
99 if (integral < old_integral) {
101 delta_integral = (ULLONG_MAX - old_integral) + integral;
103 delta_integral = integral - old_integral;
106 /* Calculate average for the previous sample window */
107 do_div(delta_integral, delta_time);
108 sample->avg = delta_integral;
112 /* Exponential moving average
113 * Avgn = Avgn-1 * alpha + new_avg * (1 - alpha)
116 avg += sum * (FIXED_1 - EXP);
122 static void update_runnables_state(unsigned int nr_run)
124 unsigned int nr_cpus = num_online_cpus();
125 int max_cpus = pm_qos_request(PM_QOS_MAX_ONLINE_CPUS) ? : 4;
126 int min_cpus = pm_qos_request(PM_QOS_MIN_ONLINE_CPUS);
128 if ((nr_cpus > max_cpus || nr_run < nr_cpus) && nr_cpus >= min_cpus) {
129 runnables_state = DOWN;
130 } else if (nr_cpus < min_cpus || nr_run > nr_cpus) {
131 runnables_state = UP;
133 runnables_state = IDLE;
137 static void runnables_avg_sampler(unsigned long data)
139 unsigned int nr_run, avg_nr_run;
141 if (runnables_state == DISABLED)
144 avg_nr_run = get_avg_nr_runnables();
145 mod_timer(&runnables_timer, jiffies + msecs_to_jiffies(sample_rate));
147 for (nr_run = 1; nr_run < ARRAY_SIZE(nr_run_thresholds); nr_run++) {
148 unsigned int nr_threshold = nr_run_thresholds[nr_run - 1];
149 if (nr_run_last <= nr_run)
150 nr_threshold += NR_FSHIFT / nr_run_hysteresis;
151 if (avg_nr_run <= (nr_threshold << (FSHIFT - NR_FSHIFT_EXP)))
155 nr_run_last = nr_run;
156 update_runnables_state(nr_run);
158 if (runnables_state != DISABLED && runnables_state != IDLE)
159 schedule_work(&runnables_work);
162 static unsigned int get_lightest_loaded_cpu_n(void)
164 unsigned long min_avg_runnables = ULONG_MAX;
165 unsigned int cpu = nr_cpu_ids;
168 for_each_online_cpu(i) {
169 struct runnables_avg_sample *s = &per_cpu(avg_nr_sample, i);
170 unsigned int nr_runnables = s->avg;
171 if (i > 0 && min_avg_runnables > nr_runnables) {
173 min_avg_runnables = nr_runnables;
180 static void runnables_work_func(struct work_struct *work)
183 unsigned int cpu = nr_cpu_ids;
185 mutex_lock(&runnables_work_lock);
187 /* Update state to avoid duplicate operations */
188 update_runnables_state(nr_run_last);
190 switch (runnables_state) {
195 cpu = cpumask_next_zero(0, cpu_online_mask);
199 cpu = get_lightest_loaded_cpu_n();
202 pr_err("%s: invalid cpuquiet runnable governor state %d\n",
203 __func__, runnables_state);
207 if (cpu < nr_cpu_ids) {
209 cpuquiet_wake_cpu(cpu);
211 cpuquiet_quiesence_cpu(cpu);
214 mutex_unlock(&runnables_work_lock);
217 CPQ_BASIC_ATTRIBUTE(sample_rate, 0644, uint);
218 CPQ_BASIC_ATTRIBUTE(nr_run_hysteresis, 0644, uint);
220 static struct attribute *runnables_attributes[] = {
221 &sample_rate_attr.attr,
222 &nr_run_hysteresis_attr.attr,
226 static const struct sysfs_ops runnables_sysfs_ops = {
227 .show = cpuquiet_auto_sysfs_show,
228 .store = cpuquiet_auto_sysfs_store,
231 static struct kobj_type ktype_runnables = {
232 .sysfs_ops = &runnables_sysfs_ops,
233 .default_attrs = runnables_attributes,
236 static int runnables_sysfs(void)
240 runnables_kobject = kzalloc(sizeof(*runnables_kobject),
243 if (!runnables_kobject)
246 err = cpuquiet_kobject_init(runnables_kobject, &ktype_runnables,
250 kfree(runnables_kobject);
255 static void runnables_device_busy(void)
257 if (runnables_state != DISABLED) {
258 runnables_state = DISABLED;
259 cancel_work_sync(&runnables_work);
263 static void runnables_device_free(void)
265 if (runnables_state == DISABLED) {
266 runnables_state = IDLE;
267 mod_timer(&runnables_timer, jiffies + 1);
271 static void runnables_stop(void)
273 runnables_state = DISABLED;
274 cancel_work_sync(&runnables_work);
275 kobject_put(runnables_kobject);
278 static int runnables_start(void)
282 err = runnables_sysfs();
286 INIT_WORK(&runnables_work, runnables_work_func);
288 init_timer(&runnables_timer);
289 runnables_timer.function = runnables_avg_sampler;
291 for(i = 0; i < ARRAY_SIZE(nr_run_thresholds); ++i) {
292 if (i < ARRAY_SIZE(default_thresholds))
293 nr_run_thresholds[i] = default_thresholds[i];
294 else if (i == (ARRAY_SIZE(nr_run_thresholds) - 1))
295 nr_run_thresholds[i] = UINT_MAX;
297 nr_run_thresholds[i] = i + 1 +
298 NR_FSHIFT / default_threshold_level;
301 runnables_state = IDLE;
302 runnables_avg_sampler(0);
307 struct cpuquiet_governor runnables_governor = {
309 .start = runnables_start,
310 .device_free_notification = runnables_device_free,
311 .device_busy_notification = runnables_device_busy,
312 .stop = runnables_stop,
313 .owner = THIS_MODULE,
316 static int __init init_runnables(void)
318 return cpuquiet_register_governor(&runnables_governor);
321 static void __exit exit_runnables(void)
323 cpuquiet_unregister_governor(&runnables_governor);
326 MODULE_LICENSE("GPL");
327 #ifdef CONFIG_CPUQUIET_DEFAULT_GOV_RUNNABLE
328 fs_initcall(init_runnables);
330 module_init(init_runnables);
332 module_exit(exit_runnables);