[CPUFREQ] ondemand governor automatic downscaling

[PATCH] [4/5] ondemand governor automatic downscaling

Here is a change of policy for the ondemand governor. The modification
concerns the frequency downscaling. Instead of decreasing to a lower
frequency when the CPU usage is under 20%, this new policy automatically
scales to the optimal frequency. The optimal frequency being the lowest
frequency which provides enough power to not trigger the upscaling policy.

Signed-off-by: Eric Piel <eric.piel@tremplin-utc.net>
Signed-off-by: Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
Signed-off-by: Dave Jones <davej@redhat.com>

+25 -92
+25 -92
drivers/cpufreq/cpufreq_ondemand.c
··· 34 34 */ 35 35 36 36 #define DEF_FREQUENCY_UP_THRESHOLD (80) 37 - #define MIN_FREQUENCY_UP_THRESHOLD (0) 37 + #define MIN_FREQUENCY_UP_THRESHOLD (11) 38 38 #define MAX_FREQUENCY_UP_THRESHOLD (100) 39 - 40 - #define DEF_FREQUENCY_DOWN_THRESHOLD (20) 41 - #define MIN_FREQUENCY_DOWN_THRESHOLD (0) 42 - #define MAX_FREQUENCY_DOWN_THRESHOLD (100) 43 39 44 40 /* 45 41 * The polling frequency of this governor depends on the capability of ··· 73 77 unsigned int sampling_rate; 74 78 unsigned int sampling_down_factor; 75 79 unsigned int up_threshold; 76 - unsigned int down_threshold; 77 80 unsigned int ignore_nice; 78 - unsigned int freq_step; 79 81 }; 80 82 81 83 static struct dbs_tuners dbs_tuners_ins = { 82 84 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD, 83 - .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD, 84 85 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR, 85 86 }; 86 87 ··· 118 125 show_one(sampling_rate, sampling_rate); 119 126 show_one(sampling_down_factor, sampling_down_factor); 120 127 show_one(up_threshold, up_threshold); 121 - show_one(down_threshold, down_threshold); 122 128 show_one(ignore_nice, ignore_nice); 123 - show_one(freq_step, freq_step); 124 129 125 130 static ssize_t store_sampling_down_factor(struct cpufreq_policy *unused, 126 131 const char *buf, size_t count) ··· 164 173 165 174 down(&dbs_sem); 166 175 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD || 167 - input < MIN_FREQUENCY_UP_THRESHOLD || 168 - input <= dbs_tuners_ins.down_threshold) { 176 + input < MIN_FREQUENCY_UP_THRESHOLD) { 169 177 up(&dbs_sem); 170 178 return -EINVAL; 171 179 } 172 180 173 181 dbs_tuners_ins.up_threshold = input; 174 - up(&dbs_sem); 175 - 176 - return count; 177 - } 178 - 179 - static ssize_t store_down_threshold(struct cpufreq_policy *unused, 180 - const char *buf, size_t count) 181 - { 182 - unsigned int input; 183 - int ret; 184 - ret = sscanf (buf, "%u", &input); 185 - 186 - down(&dbs_sem); 187 - if (ret != 1 || input > MAX_FREQUENCY_DOWN_THRESHOLD || 188 - input < MIN_FREQUENCY_DOWN_THRESHOLD || 189 - input >= dbs_tuners_ins.up_threshold) { 190 - up(&dbs_sem); 191 - return -EINVAL; 192 - } 193 - 194 - dbs_tuners_ins.down_threshold = input; 195 182 up(&dbs_sem); 196 183 197 184 return count; ··· 209 240 return count; 210 241 } 211 242 212 - static ssize_t store_freq_step(struct cpufreq_policy *policy, 213 - const char *buf, size_t count) 214 - { 215 - unsigned int input; 216 - int ret; 217 - 218 - ret = sscanf (buf, "%u", &input); 219 - 220 - if ( ret != 1 ) 221 - return -EINVAL; 222 - 223 - if ( input > 100 ) 224 - input = 100; 225 - 226 - /* no need to test here if freq_step is zero as the user might actually 227 - * want this, they would be crazy though :) */ 228 - down(&dbs_sem); 229 - dbs_tuners_ins.freq_step = input; 230 - up(&dbs_sem); 231 - 232 - return count; 233 - } 234 - 235 243 #define define_one_rw(_name) \ 236 244 static struct freq_attr _name = \ 237 245 __ATTR(_name, 0644, show_##_name, store_##_name) ··· 216 270 define_one_rw(sampling_rate); 217 271 define_one_rw(sampling_down_factor); 218 272 define_one_rw(up_threshold); 219 - define_one_rw(down_threshold); 220 273 define_one_rw(ignore_nice); 221 - define_one_rw(freq_step); 222 274 223 275 static struct attribute * dbs_attributes[] = { 224 276 &sampling_rate_max.attr, ··· 224 280 &sampling_rate.attr, 225 281 &sampling_down_factor.attr, 226 282 &up_threshold.attr, 227 - &down_threshold.attr, 228 283 &ignore_nice.attr, 229 - &freq_step.attr, 230 284 NULL 231 285 }; 232 286 ··· 237 295 238 296 static void dbs_check_cpu(int cpu) 239 297 { 240 - unsigned int idle_ticks, up_idle_ticks, down_idle_ticks; 241 - unsigned int freq_down_step; 298 + unsigned int idle_ticks, up_idle_ticks, total_ticks; 299 + unsigned int freq_next; 242 300 unsigned int freq_down_sampling_rate; 243 301 static int down_skip[NR_CPUS]; 244 302 struct cpu_dbs_info_s *this_dbs_info; ··· 252 310 253 311 policy = this_dbs_info->cur_policy; 254 312 /* 255 - * The default safe range is 20% to 80% 256 - * Every sampling_rate, we check 257 - * - If current idle time is less than 20%, then we try to 258 - * increase frequency 259 - * Every sampling_rate*sampling_down_factor, we check 260 - * - If current idle time is more than 80%, then we try to 261 - * decrease frequency 313 + * Every sampling_rate, we check, if current idle time is less 314 + * than 20% (default), then we try to increase frequency 315 + * Every sampling_rate*sampling_down_factor, we look for a the lowest 316 + * frequency which can sustain the load while keeping idle time over 317 + * 30%. If such a frequency exist, we try to decrease to this frequency. 262 318 * 263 319 * Any frequency increase takes it to the maximum frequency. 264 320 * Frequency reduction happens at minimum steps of 265 - * 5% (default) of max_frequency 321 + * 5% (default) of current frequency 266 322 */ 267 323 268 324 /* Check for frequency increase */ ··· 323 383 idle_ticks = tmp_idle_ticks; 324 384 } 325 385 326 - /* Scale idle ticks by 100 and compare with up and down ticks */ 327 - idle_ticks *= 100; 328 386 down_skip[cpu] = 0; 387 + /* if we cannot reduce the frequency anymore, break out early */ 388 + if (policy->cur == policy->min) 389 + return; 329 390 391 + /* Compute how many ticks there are between two measurements */ 330 392 freq_down_sampling_rate = dbs_tuners_ins.sampling_rate * 331 393 dbs_tuners_ins.sampling_down_factor; 332 - down_idle_ticks = (100 - dbs_tuners_ins.down_threshold) * 333 - usecs_to_jiffies(freq_down_sampling_rate); 394 + total_ticks = usecs_to_jiffies(freq_down_sampling_rate); 334 395 335 - if (idle_ticks > down_idle_ticks) { 336 - /* if we are already at the lowest speed then break out early 337 - * or if we 'cannot' reduce the speed as the user might want 338 - * freq_step to be zero */ 339 - if (policy->cur == policy->min || dbs_tuners_ins.freq_step == 0) 340 - return; 396 + /* 397 + * The optimal frequency is the frequency that is the lowest that 398 + * can support the current CPU usage without triggering the up 399 + * policy. To be safe, we focus 10 points under the threshold. 400 + */ 401 + freq_next = ((total_ticks - idle_ticks) * 100) / total_ticks; 402 + freq_next = (freq_next * policy->cur) / 403 + (dbs_tuners_ins.up_threshold - 10); 341 404 342 - freq_down_step = (dbs_tuners_ins.freq_step * policy->max) / 100; 343 - 344 - /* max freq cannot be less than 100. But who knows.... */ 345 - if (unlikely(freq_down_step == 0)) 346 - freq_down_step = 5; 347 - 348 - __cpufreq_driver_target(policy, 349 - policy->cur - freq_down_step, 350 - CPUFREQ_RELATION_H); 351 - return; 352 - } 405 + if (freq_next <= ((policy->cur * 95) / 100)) 406 + __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L); 353 407 } 354 408 355 409 static void do_dbs_timer(void *data) ··· 421 487 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER; 422 488 dbs_tuners_ins.sampling_rate = def_sampling_rate; 423 489 dbs_tuners_ins.ignore_nice = 0; 424 - dbs_tuners_ins.freq_step = 5; 425 490 426 491 dbs_timer_init(); 427 492 }