sched_avg.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. /*
  13. * Scheduler hook for average runqueue determination
  14. */
  15. #include <linux/module.h>
  16. #include <linux/percpu.h>
  17. #include <linux/hrtimer.h>
  18. #include <linux/sched.h>
  19. #include <linux/math64.h>
  20. static DEFINE_PER_CPU(u64, nr_prod_sum);
  21. static DEFINE_PER_CPU(u64, last_time);
  22. static DEFINE_PER_CPU(u64, nr);
  23. static DEFINE_PER_CPU(unsigned long, iowait_prod_sum);
  24. static DEFINE_PER_CPU(spinlock_t, nr_lock) = __SPIN_LOCK_UNLOCKED(nr_lock);
  25. static s64 last_get_time;
  26. /**
  27. * sched_get_nr_running_avg
  28. * @return: Average nr_running and iowait value since last poll.
  29. * Returns the avg * 100 to return up to two decimal points
  30. * of accuracy.
  31. *
  32. * Obtains the average nr_running value since the last poll.
  33. * This function may not be called concurrently with itself
  34. */
  35. void sched_get_nr_running_avg(int *avg, int *iowait_avg)
  36. {
  37. int cpu;
  38. u64 curr_time = sched_clock();
  39. u64 diff = curr_time - last_get_time;
  40. u64 tmp_avg = 0, tmp_iowait = 0;
  41. *avg = 0;
  42. *iowait_avg = 0;
  43. if (!diff)
  44. return;
  45. last_get_time = curr_time;
  46. /* read and reset nr_running counts */
  47. for_each_possible_cpu(cpu) {
  48. unsigned long flags;
  49. spin_lock_irqsave(&per_cpu(nr_lock, cpu), flags);
  50. tmp_avg += per_cpu(nr_prod_sum, cpu);
  51. tmp_avg += per_cpu(nr, cpu) *
  52. (curr_time - per_cpu(last_time, cpu));
  53. tmp_iowait = per_cpu(iowait_prod_sum, cpu);
  54. tmp_iowait += nr_iowait_cpu(cpu) *
  55. (curr_time - per_cpu(last_time, cpu));
  56. per_cpu(last_time, cpu) = curr_time;
  57. per_cpu(nr_prod_sum, cpu) = 0;
  58. per_cpu(iowait_prod_sum, cpu) = 0;
  59. spin_unlock_irqrestore(&per_cpu(nr_lock, cpu), flags);
  60. }
  61. *avg = (int)div64_u64(tmp_avg * 100, diff);
  62. *iowait_avg = (int)div64_u64(tmp_iowait * 100, diff);
  63. BUG_ON(*avg < 0);
  64. pr_debug("%s - avg:%d\n", __func__, *avg);
  65. BUG_ON(*iowait_avg < 0);
  66. pr_debug("%s - avg:%d\n", __func__, *iowait_avg);
  67. }
  68. EXPORT_SYMBOL(sched_get_nr_running_avg);
  69. /**
  70. * sched_update_nr_prod
  71. * @cpu: The core id of the nr running driver.
  72. * @nr: Updated nr running value for cpu.
  73. * @inc: Whether we are increasing or decreasing the count
  74. * @return: N/A
  75. *
  76. * Update average with latest nr_running value for CPU
  77. */
  78. void sched_update_nr_prod(int cpu, unsigned long nr_running, bool inc)
  79. {
  80. int diff;
  81. s64 curr_time;
  82. unsigned long flags;
  83. spin_lock_irqsave(&per_cpu(nr_lock, cpu), flags);
  84. curr_time = sched_clock();
  85. diff = curr_time - per_cpu(last_time, cpu);
  86. per_cpu(last_time, cpu) = curr_time;
  87. per_cpu(nr, cpu) = nr_running + (inc ? 1 : -1);
  88. BUG_ON(per_cpu(nr, cpu) < 0);
  89. per_cpu(nr_prod_sum, cpu) += nr_running * diff;
  90. per_cpu(iowait_prod_sum, cpu) += nr_iowait_cpu(cpu) * diff;
  91. spin_unlock_irqrestore(&per_cpu(nr_lock, cpu), flags);
  92. }
  93. EXPORT_SYMBOL(sched_update_nr_prod);