mperf.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <linux/kernel.h>
  2. #include <linux/smp.h>
  3. #include <linux/module.h>
  4. #include <linux/init.h>
  5. #include <linux/cpufreq.h>
  6. #include <linux/slab.h>
  7. #include "mperf.h"
  8. static DEFINE_PER_CPU(struct aperfmperf, acfreq_old_perf);
  9. /* Called via smp_call_function_single(), on the target CPU */
  10. static void read_measured_perf_ctrs(void *_cur)
  11. {
  12. struct aperfmperf *am = _cur;
  13. get_aperfmperf(am);
  14. }
  15. /*
  16. * Return the measured active (C0) frequency on this CPU since last call
  17. * to this function.
  18. * Input: cpu number
  19. * Return: Average CPU frequency in terms of max frequency (zero on error)
  20. *
  21. * We use IA32_MPERF and IA32_APERF MSRs to get the measured performance
  22. * over a period of time, while CPU is in C0 state.
  23. * IA32_MPERF counts at the rate of max advertised frequency
  24. * IA32_APERF counts at the rate of actual CPU frequency
  25. * Only IA32_APERF/IA32_MPERF ratio is architecturally defined and
  26. * no meaning should be associated with absolute values of these MSRs.
  27. */
  28. unsigned int cpufreq_get_measured_perf(struct cpufreq_policy *policy,
  29. unsigned int cpu)
  30. {
  31. struct aperfmperf perf;
  32. unsigned long ratio;
  33. unsigned int retval;
  34. if (smp_call_function_single(cpu, read_measured_perf_ctrs, &perf, 1))
  35. return 0;
  36. ratio = calc_aperfmperf_ratio(&per_cpu(acfreq_old_perf, cpu), &perf);
  37. per_cpu(acfreq_old_perf, cpu) = perf;
  38. retval = (policy->cpuinfo.max_freq * ratio) >> APERFMPERF_SHIFT;
  39. return retval;
  40. }
  41. EXPORT_SYMBOL_GPL(cpufreq_get_measured_perf);
  42. MODULE_LICENSE("GPL");