cpufreq_governor.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * drivers/cpufreq/cpufreq_governor.c
  3. *
  4. * CPUFREQ governors common code
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <asm/cputime.h>
  11. #include <linux/export.h>
  12. #include <linux/kernel_stat.h>
  13. #include <linux/tick.h>
  14. #include <linux/types.h>
  15. /*
  16. * Code picked from earlier governer implementations
  17. */
  18. static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
  19. {
  20. u64 idle_time;
  21. u64 cur_wall_time;
  22. u64 busy_time;
  23. cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
  24. busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
  25. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
  26. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
  27. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
  28. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
  29. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
  30. idle_time = cur_wall_time - busy_time;
  31. if (wall)
  32. *wall = jiffies_to_usecs(cur_wall_time);
  33. return jiffies_to_usecs(idle_time);
  34. }
  35. cputime64_t get_cpu_idle_time(unsigned int cpu, cputime64_t *wall)
  36. {
  37. u64 idle_time = get_cpu_idle_time_us(cpu, NULL);
  38. if (idle_time == -1ULL)
  39. return get_cpu_idle_time_jiffy(cpu, wall);
  40. else
  41. idle_time += get_cpu_iowait_time_us(cpu, wall);
  42. return idle_time;
  43. }
  44. EXPORT_SYMBOL_GPL(get_cpu_idle_time);