SHARE
TWEET

Team-droid-x governor

a guest Aug 12th, 2016 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * drivers/cpufreq/cpufreq_team-droid-x.c
  3.  *
  4.  * Copyright (C) 2016 Harshit Jain (Team Droid X) <harshitjain6751@gmail.com>
  5.  * Copyright (C) 2016 Vipul Jha2 (Team Droid X) <vipuljha08@gmail.com>
  6.  * Copyright (C) 2016 Evan Dantas (Team Droid X) <evandants9@gmail.com>
  7.  * Copyright (C) 2016 engstk <engstk@mail.ru>
  8.  *
  9.  * Based on the Conservative governor by:
  10.  *
  11.  *    Copyright (C)  2001 Russell King
  12.  *              (C)  2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
  13.  *                        Jun Nakajima <jun.nakajima@intel.com>
  14.  *              (C)  2009 Alexander Clouter <alex@digriz.org.uk>
  15.  *              (C)  2014 Jamison904 <infamousprollc@gmail.com>
  16.  *
  17.  * This program is free software; you can redistribute it and/or modify
  18.  * it under the terms of the GNU General Public License version 2 as
  19.  * published by the Free Software Foundation.
  20.  */
  21.  
  22. #include <asm/cputime.h>
  23. #include <linux/kthread.h>
  24. #include <linux/time.h>
  25. #include <linux/timer.h>
  26. #include <linux/cpumask.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/cpufreq.h>
  31. #include <linux/cpu.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/kernel_stat.h>
  34. #include <linux/mutex.h>
  35. #include <linux/hrtimer.h>
  36. #include <linux/tick.h>
  37. #include <linux/ktime.h>
  38. #include <linux/sched.h>
  39. #include <linux/input.h>
  40. #include <linux/workqueue.h>
  41. #include <linux/slab.h>
  42. #ifdef CONFIG_HAS_EARLYSUSPEND
  43. #include <linux/earlysuspend.h>
  44. #endif
  45. /*
  46.  * dbs is used in this file as a shortform for demandbased switching
  47.  * It helps to keep variable names smaller, simpler
  48.  */
  49.  
  50. #define DEF_FREQUENCY_UP_THRESHOLD      (70)
  51. #define DEF_FREQUENCY_DOWN_THRESHOLD        (30)
  52.  
  53. /*
  54.  * The polling frequency of this governor depends on the capability of
  55.  * the processor. Default polling frequency is 1000 times the transition
  56.  * latency of the processor. The governor will work on any processor with
  57.  * transition latency <= 10mS, using appropriate sampling
  58.  * rate.
  59.  * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
  60.  * this governor will not work.
  61.  * All times here are in uS.
  62.  */
  63. #define MIN_SAMPLING_RATE_RATIO         (2)
  64.  
  65. static unsigned int min_sampling_rate;
  66.  
  67. #define LATENCY_MULTIPLIER          (1000)
  68. #define MIN_LATENCY_MULTIPLIER          (100)
  69. #define DEF_SAMPLING_DOWN_FACTOR        (1)
  70. #define MAX_SAMPLING_DOWN_FACTOR        (10)
  71. #define TRANSITION_LATENCY_LIMIT        (10 * 1000 * 1000)
  72.  
  73. static void do_dbs_timer(struct work_struct *work);
  74.  
  75. struct cpu_dbs_info_s {
  76.     cputime64_t prev_cpu_idle;
  77.     cputime64_t prev_cpu_wall;
  78.     cputime64_t prev_cpu_nice;
  79.     struct cpufreq_policy *cur_policy;
  80.     struct delayed_work work;
  81.     unsigned int down_skip;
  82.     unsigned int requested_freq;
  83.     int cpu;
  84.     unsigned int enable:1;
  85.     /*
  86.      * percpu mutex that serializes governor limit change with
  87.      * do_dbs_timer invocation. We do not want do_dbs_timer to run
  88.      * when user is changing the governor or limits.
  89.      */
  90.     struct mutex timer_mutex;
  91. };
  92. static DEFINE_PER_CPU(struct cpu_dbs_info_s, cs_cpu_dbs_info);
  93.  
  94. static unsigned int dbs_enable; /* number of CPUs using this policy */
  95.  
  96. /*
  97.  * dbs_mutex protects dbs_enable in governor start/stop.
  98.  */
  99. static DEFINE_MUTEX(dbs_mutex);
  100.  
  101. static struct dbs_tuners {
  102.     unsigned int sampling_rate;
  103.     unsigned int sampling_down_factor;
  104.     unsigned int up_threshold;
  105.     unsigned int down_threshold;
  106.     unsigned int ignore_nice;
  107.     unsigned int freq_step;
  108. } dbs_tuners_ins = {
  109.     .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
  110.     .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD,
  111.     .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
  112.     .ignore_nice = 0,
  113.     .freq_step = 5,
  114. };
  115.  
  116. /* keep track of frequency transitions */
  117. static int
  118. dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  119.              void *data)
  120. {
  121.     struct cpufreq_freqs *freq = data;
  122.     struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cs_cpu_dbs_info,
  123.                             freq->cpu);
  124.  
  125.     struct cpufreq_policy *policy;
  126.  
  127.     if (!this_dbs_info->enable)
  128.         return 0;
  129.  
  130.     policy = this_dbs_info->cur_policy;
  131.  
  132.     /*
  133.      * we only care if our internally tracked freq moves outside
  134.      * the 'valid' ranges of freqency available to us otherwise
  135.      * we do not change it
  136.     */
  137.     if (this_dbs_info->requested_freq > policy->max
  138.             || this_dbs_info->requested_freq < policy->min)
  139.         this_dbs_info->requested_freq = freq->new;
  140.  
  141.     return 0;
  142. }
  143.  
  144. static struct notifier_block dbs_cpufreq_notifier_block = {
  145.     .notifier_call = dbs_cpufreq_notifier
  146. };
  147.  
  148. /************************** sysfs interface ************************/
  149. static ssize_t show_sampling_rate_min(struct kobject *kobj,
  150.                       struct attribute *attr, char *buf)
  151. {
  152.     return sprintf(buf, "%u\n", min_sampling_rate);
  153. }
  154.  
  155. define_one_global_ro(sampling_rate_min);
  156.  
  157. /* cpufreq_conservativex Governor Tunables */
  158. #define show_one(file_name, object)                 \
  159. static ssize_t show_##file_name                     \
  160. (struct kobject *kobj, struct attribute *attr, char *buf)       \
  161. {                                   \
  162.     return sprintf(buf, "%u\n", dbs_tuners_ins.object);     \
  163. }
  164. show_one(sampling_rate, sampling_rate);
  165. show_one(sampling_down_factor, sampling_down_factor);
  166. show_one(up_threshold, up_threshold);
  167. show_one(down_threshold, down_threshold);
  168. show_one(ignore_nice_load, ignore_nice);
  169. show_one(freq_step, freq_step);
  170.  
  171. static ssize_t store_sampling_down_factor(struct kobject *a,
  172.                       struct attribute *b,
  173.                       const char *buf, size_t count)
  174. {
  175.     unsigned int input;
  176.     int ret;
  177.     ret = sscanf(buf, "%u", &input);
  178.  
  179.     if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  180.         return -EINVAL;
  181.  
  182.     dbs_tuners_ins.sampling_down_factor = input;
  183.     return count;
  184. }
  185.  
  186. static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b,
  187.                    const char *buf, size_t count)
  188. {
  189.     unsigned int input;
  190.     int ret;
  191.     ret = sscanf(buf, "%u", &input);
  192.  
  193.     if (ret != 1)
  194.         return -EINVAL;
  195.  
  196.     dbs_tuners_ins.sampling_rate = max(input, min_sampling_rate);
  197.     return count;
  198. }
  199.  
  200. static ssize_t store_up_threshold(struct kobject *a, struct attribute *b,
  201.                   const char *buf, size_t count)
  202. {
  203.     unsigned int input;
  204.     int ret;
  205.     ret = sscanf(buf, "%u", &input);
  206.  
  207.     if (ret != 1 || input > 100 ||
  208.             input <= dbs_tuners_ins.down_threshold)
  209.         return -EINVAL;
  210.  
  211.     dbs_tuners_ins.up_threshold = input;
  212.     return count;
  213. }
  214.  
  215. static ssize_t store_down_threshold(struct kobject *a, struct attribute *b,
  216.                     const char *buf, size_t count)
  217. {
  218.     unsigned int input;
  219.     int ret;
  220.     ret = sscanf(buf, "%u", &input);
  221.  
  222.     /* cannot be lower than 11 otherwise freq will not fall */
  223.     if (ret != 1 || input < 11 || input > 100 ||
  224.             input >= dbs_tuners_ins.up_threshold)
  225.         return -EINVAL;
  226.  
  227.     dbs_tuners_ins.down_threshold = input;
  228.     return count;
  229. }
  230.  
  231. static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b,
  232.                       const char *buf, size_t count)
  233. {
  234.     unsigned int input;
  235.     int ret;
  236.  
  237.     unsigned int j;
  238.  
  239.     ret = sscanf(buf, "%u", &input);
  240.     if (ret != 1)
  241.         return -EINVAL;
  242.  
  243.     if (input > 1)
  244.         input = 1;
  245.  
  246.     if (input == dbs_tuners_ins.ignore_nice) /* nothing to do */
  247.         return count;
  248.  
  249.     dbs_tuners_ins.ignore_nice = input;
  250.  
  251.     /* we need to re-evaluate prev_cpu_idle */
  252.     for_each_online_cpu(j) {
  253.         struct cpu_dbs_info_s *dbs_info;
  254.         dbs_info = &per_cpu(cs_cpu_dbs_info, j);
  255.         dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
  256.                         &dbs_info->prev_cpu_wall, 0);
  257.         if (dbs_tuners_ins.ignore_nice)
  258.             dbs_info->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  259.     }
  260.     return count;
  261. }
  262.  
  263. static ssize_t store_freq_step(struct kobject *a, struct attribute *b,
  264.                    const char *buf, size_t count)
  265. {
  266.     unsigned int input;
  267.     int ret;
  268.     ret = sscanf(buf, "%u", &input);
  269.  
  270.     if (ret != 1)
  271.         return -EINVAL;
  272.  
  273.     if (input > 100)
  274.         input = 100;
  275.  
  276.     /* no need to test here if freq_step is zero as the user might actually
  277.      * want this, they would be crazy though :) */
  278.     dbs_tuners_ins.freq_step = input;
  279.     return count;
  280. }
  281.  
  282. define_one_global_rw(sampling_rate);
  283. define_one_global_rw(sampling_down_factor);
  284. define_one_global_rw(up_threshold);
  285. define_one_global_rw(down_threshold);
  286. define_one_global_rw(ignore_nice_load);
  287. define_one_global_rw(freq_step);
  288.  
  289. static struct attribute *dbs_attributes[] = {
  290.     &sampling_rate_min.attr,
  291.     &sampling_rate.attr,
  292.     &sampling_down_factor.attr,
  293.     &up_threshold.attr,
  294.     &down_threshold.attr,
  295.     &ignore_nice_load.attr,
  296.     &freq_step.attr,
  297.     NULL
  298. };
  299.  
  300. static struct attribute_group dbs_attr_group = {
  301.     .attrs = dbs_attributes,
  302.     .name = "team-droid-x",
  303. };
  304.  
  305. /************************** sysfs end ************************/
  306.  
  307. static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
  308. {
  309.     unsigned int load = 0;
  310.     unsigned int max_load = 0;
  311.     unsigned int freq_target;
  312.  
  313.     struct cpufreq_policy *policy;
  314.     unsigned int j;
  315.  
  316.     policy = this_dbs_info->cur_policy;
  317.  
  318.     /*
  319.      * Every sampling_rate, we check, if current idle time is less
  320.      * than 20% (default), then we try to increase frequency
  321.      * Every sampling_rate*sampling_down_factor, we check, if current
  322.      * idle time is more than 80%, then we try to decrease frequency
  323.      *
  324.      * Any frequency increase takes it to the maximum frequency.
  325.      * Frequency reduction happens at minimum steps of
  326.      * 5% (default) of maximum frequency
  327.      */
  328.  
  329.     /* Get Absolute Load */
  330.     for_each_cpu(j, policy->cpus) {
  331.         struct cpu_dbs_info_s *j_dbs_info;
  332.         cputime64_t cur_wall_time, cur_idle_time;
  333.         unsigned int idle_time, wall_time;
  334.  
  335.         j_dbs_info = &per_cpu(cs_cpu_dbs_info, j);
  336.  
  337.         cur_idle_time = get_cpu_idle_time(j, &cur_wall_time, 0);
  338.  
  339.         wall_time = (unsigned int)
  340.             (cur_wall_time - j_dbs_info->prev_cpu_wall);
  341.         j_dbs_info->prev_cpu_wall = cur_wall_time;
  342.  
  343.         idle_time = (unsigned int)
  344.             (cur_idle_time - j_dbs_info->prev_cpu_idle);
  345.         j_dbs_info->prev_cpu_idle = cur_idle_time;
  346.  
  347.         if (dbs_tuners_ins.ignore_nice) {
  348.             cputime64_t cur_nice;
  349.             unsigned long cur_nice_jiffies;
  350.  
  351.             cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] -
  352.                      j_dbs_info->prev_cpu_nice;
  353.             /*
  354.              * Assumption: nice time between sampling periods will
  355.              * be less than 2^32 jiffies for 32 bit sys
  356.              */
  357.             cur_nice_jiffies = (unsigned long)
  358.                     cputime64_to_jiffies64(cur_nice);
  359.  
  360.             j_dbs_info->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  361.             idle_time += jiffies_to_usecs(cur_nice_jiffies);
  362.         }
  363.  
  364.         if (unlikely(!wall_time || wall_time < idle_time))
  365.             continue;
  366.  
  367.         load = 100 * (wall_time - idle_time) / wall_time;
  368.  
  369.         if (load > max_load)
  370.             max_load = load;
  371.     }
  372.  
  373.     /*
  374.      * break out if we 'cannot' reduce the speed as the user might
  375.      * want freq_step to be zero
  376.      */
  377.     if (dbs_tuners_ins.freq_step == 0)
  378.         return;
  379.  
  380.     /* Check for frequency increase */
  381.     if (max_load > dbs_tuners_ins.up_threshold) {
  382.         this_dbs_info->down_skip = 0;
  383.  
  384.         /* if we are already at full speed then break out early */
  385.         if (this_dbs_info->requested_freq == policy->max)
  386.             return;
  387.  
  388.         freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100;
  389.  
  390.         /* max freq cannot be less than 100. But who knows.... */
  391.         if (unlikely(freq_target == 0))
  392.             freq_target = 5;
  393.  
  394.         this_dbs_info->requested_freq += freq_target;
  395.         if (this_dbs_info->requested_freq > policy->max)
  396.             this_dbs_info->requested_freq = policy->max;
  397.  
  398.         __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
  399.             CPUFREQ_RELATION_H);
  400.         return;
  401.     }
  402.  
  403.     /*
  404.      * The optimal frequency is the frequency that is the lowest that
  405.      * can support the current CPU usage without triggering the up
  406.      * policy. To be safe, we focus 10 points under the threshold.
  407.      */
  408.     if (max_load < (dbs_tuners_ins.down_threshold - 10)) {
  409.         freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100;
  410.  
  411.         this_dbs_info->requested_freq -= freq_target;
  412.         if (this_dbs_info->requested_freq < policy->min)
  413.             this_dbs_info->requested_freq = policy->min;
  414.  
  415.         /*
  416.          * if we cannot reduce the frequency anymore, break out early
  417.          */
  418.         if (policy->cur == policy->min)
  419.             return;
  420.  
  421.         __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
  422.                 CPUFREQ_RELATION_H);
  423.         return;
  424.     }
  425. }
  426.  
  427. static void do_dbs_timer(struct work_struct *work)
  428. {
  429.     struct cpu_dbs_info_s *dbs_info =
  430.         container_of(work, struct cpu_dbs_info_s, work.work);
  431.     unsigned int cpu = dbs_info->cpu;
  432.  
  433.     /* We want all CPUs to do sampling nearly on same jiffy */
  434.     int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  435.  
  436.     // delay -= jiffies % delay;
  437.  
  438.     mutex_lock(&dbs_info->timer_mutex);
  439.  
  440.     dbs_check_cpu(dbs_info);
  441.  
  442.     schedule_delayed_work_on(cpu, &dbs_info->work, delay);
  443.     mutex_unlock(&dbs_info->timer_mutex);
  444. }
  445.  
  446. static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
  447. {
  448.     /* We want all CPUs to do sampling nearly on same jiffy */
  449.     int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  450.  
  451.     // delay -= jiffies % delay;
  452.  
  453.     dbs_info->enable = 1;
  454.     INIT_DEFERRABLE_WORK(&dbs_info->work, do_dbs_timer);
  455.     schedule_delayed_work_on(dbs_info->cpu, &dbs_info->work, delay);
  456. }
  457.  
  458. static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
  459. {
  460.     dbs_info->enable = 0;
  461.     cancel_delayed_work_sync(&dbs_info->work);
  462. }
  463.  
  464. static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  465.                    unsigned int event)
  466. {
  467.     unsigned int cpu = policy->cpu;
  468.     struct cpu_dbs_info_s *this_dbs_info;
  469.     unsigned int j;
  470.     int rc;
  471.  
  472.     this_dbs_info = &per_cpu(cs_cpu_dbs_info, cpu);
  473.  
  474.     switch (event) {
  475.     case CPUFREQ_GOV_START:
  476.         if ((!cpu_online(cpu)) || (!policy->cur))
  477.             return -EINVAL;
  478.  
  479.         mutex_lock(&dbs_mutex);
  480.  
  481.         for_each_cpu(j, policy->cpus) {
  482.             struct cpu_dbs_info_s *j_dbs_info;
  483.             j_dbs_info = &per_cpu(cs_cpu_dbs_info, j);
  484.             j_dbs_info->cur_policy = policy;
  485.  
  486.             j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
  487.                         &j_dbs_info->prev_cpu_wall, 0);
  488.             if (dbs_tuners_ins.ignore_nice) {
  489.                 j_dbs_info->prev_cpu_nice =
  490.                         kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  491.             }
  492.         }
  493.         this_dbs_info->down_skip = 0;
  494.         this_dbs_info->requested_freq = policy->cur;
  495.  
  496.         mutex_init(&this_dbs_info->timer_mutex);
  497.         dbs_enable++;
  498.         /*
  499.          * Start the timerschedule work, when this governor
  500.          * is used for first time
  501.          */
  502.         if (dbs_enable == 1) {
  503.             unsigned int latency;
  504.             /* policy latency is in nS. Convert it to uS first */
  505.             latency = policy->cpuinfo.transition_latency / 1000;
  506.             if (latency == 0)
  507.                 latency = 1;
  508.  
  509.             rc = sysfs_create_group(cpufreq_global_kobject,
  510.                         &dbs_attr_group);
  511.             if (rc) {
  512.                 mutex_unlock(&dbs_mutex);
  513.                 return rc;
  514.             }
  515.  
  516.             min_sampling_rate = 10000;
  517.             dbs_tuners_ins.sampling_rate = 10000;
  518.  
  519.             cpufreq_register_notifier(
  520.                     &dbs_cpufreq_notifier_block,
  521.                     CPUFREQ_TRANSITION_NOTIFIER);
  522.         }
  523.         mutex_unlock(&dbs_mutex);
  524.  
  525.         dbs_timer_init(this_dbs_info);
  526.  
  527.         break;
  528.  
  529.     case CPUFREQ_GOV_STOP:
  530.         dbs_timer_exit(this_dbs_info);
  531.  
  532.         mutex_lock(&dbs_mutex);
  533.         dbs_enable--;
  534.         mutex_destroy(&this_dbs_info->timer_mutex);
  535.  
  536.         /*
  537.          * Stop the timerschedule work, when this governor
  538.          * is used for first time
  539.          */
  540.         if (dbs_enable == 0)
  541.             cpufreq_unregister_notifier(
  542.                     &dbs_cpufreq_notifier_block,
  543.                     CPUFREQ_TRANSITION_NOTIFIER);
  544.  
  545.         mutex_unlock(&dbs_mutex);
  546.         if (!dbs_enable)
  547.             sysfs_remove_group(cpufreq_global_kobject,
  548.                        &dbs_attr_group);
  549.  
  550.         break;
  551.  
  552.     case CPUFREQ_GOV_LIMITS:
  553.         mutex_lock(&this_dbs_info->timer_mutex);
  554.         if (policy->max < this_dbs_info->cur_policy->cur)
  555.             __cpufreq_driver_target(
  556.                     this_dbs_info->cur_policy,
  557.                     policy->max, CPUFREQ_RELATION_H);
  558.         else if (policy->min > this_dbs_info->cur_policy->cur)
  559.             __cpufreq_driver_target(
  560.                     this_dbs_info->cur_policy,
  561.                     policy->min, CPUFREQ_RELATION_L);
  562.         mutex_unlock(&this_dbs_info->timer_mutex);
  563.  
  564.         break;
  565.     }
  566.     return 0;
  567. }
  568.  
  569. #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_TEAM-DROID-X
  570. static
  571. #endif
  572. struct cpufreq_governor cpufreq_gov_team-droid-x = {
  573.     .name           = "team-droid-x",
  574.     .governor       = cpufreq_governor_dbs,
  575.     .max_transition_latency = TRANSITION_LATENCY_LIMIT,
  576.     .owner          = THIS_MODULE,
  577. };
  578.  
  579. static int __init cpufreq_gov_dbs_init(void)
  580. {
  581.     return cpufreq_register_governor(&cpufreq_gov_team-droid-x);
  582. }
  583.  
  584. static void __exit cpufreq_gov_dbs_exit(void)
  585. {
  586.     cpufreq_unregister_governor(&cpufreq_gov_team-droid-x);
  587. }
  588.  
  589. MODULE_AUTHOR("Harshit-Jain");
  590. MODULE_AUTHOR("Harsh!t Jain <harshitjain6751@gmail.com>");
  591. MODULE_DESCRIPTION("'cpufreq_team-droid-x' - A better governor for quad core devices ;) with love TDX");
  592. MODULE_LICENSE("GPL");
  593.  
  594. fs_initcall(cpufreq_gov_dbs_init);
  595. module_exit(cpufreq_gov_dbs_exit);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top