nhm_idle.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * (C) 2010,2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
  3. *
  4. * Licensed under the terms of the GNU GPL License version 2.
  5. *
  6. * Based on Len Brown's <lenb@kernel.org> turbostat tool.
  7. */
  8. #if defined(__i386__) || defined(__x86_64__)
  9. #include <stdio.h>
  10. #include <stdint.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "helpers/helpers.h"
  14. #include "idle_monitor/cpupower-monitor.h"
  15. #define MSR_PKG_C3_RESIDENCY 0x3F8
  16. #define MSR_PKG_C6_RESIDENCY 0x3F9
  17. #define MSR_CORE_C3_RESIDENCY 0x3FC
  18. #define MSR_CORE_C6_RESIDENCY 0x3FD
  19. #define MSR_TSC 0x10
  20. #define NHM_CSTATE_COUNT 4
  21. enum intel_nhm_id { C3 = 0, C6, PC3, PC6, TSC = 0xFFFF };
  22. static int nhm_get_count_percent(unsigned int self_id, double *percent,
  23. unsigned int cpu);
  24. static cstate_t nhm_cstates[NHM_CSTATE_COUNT] = {
  25. {
  26. .name = "C3",
  27. .desc = N_("Processor Core C3"),
  28. .id = C3,
  29. .range = RANGE_CORE,
  30. .get_count_percent = nhm_get_count_percent,
  31. },
  32. {
  33. .name = "C6",
  34. .desc = N_("Processor Core C6"),
  35. .id = C6,
  36. .range = RANGE_CORE,
  37. .get_count_percent = nhm_get_count_percent,
  38. },
  39. {
  40. .name = "PC3",
  41. .desc = N_("Processor Package C3"),
  42. .id = PC3,
  43. .range = RANGE_PACKAGE,
  44. .get_count_percent = nhm_get_count_percent,
  45. },
  46. {
  47. .name = "PC6",
  48. .desc = N_("Processor Package C6"),
  49. .id = PC6,
  50. .range = RANGE_PACKAGE,
  51. .get_count_percent = nhm_get_count_percent,
  52. },
  53. };
  54. static unsigned long long tsc_at_measure_start;
  55. static unsigned long long tsc_at_measure_end;
  56. static unsigned long long *previous_count[NHM_CSTATE_COUNT];
  57. static unsigned long long *current_count[NHM_CSTATE_COUNT];
  58. /* valid flag for all CPUs. If a MSR read failed it will be zero */
  59. static int *is_valid;
  60. static int nhm_get_count(enum intel_nhm_id id, unsigned long long *val,
  61. unsigned int cpu)
  62. {
  63. int msr;
  64. switch (id) {
  65. case C3:
  66. msr = MSR_CORE_C3_RESIDENCY;
  67. break;
  68. case C6:
  69. msr = MSR_CORE_C6_RESIDENCY;
  70. break;
  71. case PC3:
  72. msr = MSR_PKG_C3_RESIDENCY;
  73. break;
  74. case PC6:
  75. msr = MSR_PKG_C6_RESIDENCY;
  76. break;
  77. case TSC:
  78. msr = MSR_TSC;
  79. break;
  80. default:
  81. return -1;
  82. };
  83. if (read_msr(cpu, msr, val))
  84. return -1;
  85. return 0;
  86. }
  87. static int nhm_get_count_percent(unsigned int id, double *percent,
  88. unsigned int cpu)
  89. {
  90. *percent = 0.0;
  91. if (!is_valid[cpu])
  92. return -1;
  93. *percent = (100.0 *
  94. (current_count[id][cpu] - previous_count[id][cpu])) /
  95. (tsc_at_measure_end - tsc_at_measure_start);
  96. dprint("%s: previous: %llu - current: %llu - (%u)\n",
  97. nhm_cstates[id].name, previous_count[id][cpu],
  98. current_count[id][cpu], cpu);
  99. dprint("%s: tsc_diff: %llu - count_diff: %llu - percent: %2.f (%u)\n",
  100. nhm_cstates[id].name,
  101. (unsigned long long) tsc_at_measure_end - tsc_at_measure_start,
  102. current_count[id][cpu] - previous_count[id][cpu],
  103. *percent, cpu);
  104. return 0;
  105. }
  106. static int nhm_start(void)
  107. {
  108. int num, cpu;
  109. unsigned long long dbg, val;
  110. nhm_get_count(TSC, &tsc_at_measure_start, base_cpu);
  111. for (num = 0; num < NHM_CSTATE_COUNT; num++) {
  112. for (cpu = 0; cpu < cpu_count; cpu++) {
  113. is_valid[cpu] = !nhm_get_count(num, &val, cpu);
  114. previous_count[num][cpu] = val;
  115. }
  116. }
  117. nhm_get_count(TSC, &dbg, base_cpu);
  118. dprint("TSC diff: %llu\n", dbg - tsc_at_measure_start);
  119. return 0;
  120. }
  121. static int nhm_stop(void)
  122. {
  123. unsigned long long val;
  124. unsigned long long dbg;
  125. int num, cpu;
  126. nhm_get_count(TSC, &tsc_at_measure_end, base_cpu);
  127. for (num = 0; num < NHM_CSTATE_COUNT; num++) {
  128. for (cpu = 0; cpu < cpu_count; cpu++) {
  129. is_valid[cpu] = !nhm_get_count(num, &val, cpu);
  130. current_count[num][cpu] = val;
  131. }
  132. }
  133. nhm_get_count(TSC, &dbg, base_cpu);
  134. dprint("TSC diff: %llu\n", dbg - tsc_at_measure_end);
  135. return 0;
  136. }
  137. struct cpuidle_monitor intel_nhm_monitor;
  138. struct cpuidle_monitor *intel_nhm_register(void)
  139. {
  140. int num;
  141. if (cpupower_cpu_info.vendor != X86_VENDOR_INTEL)
  142. return NULL;
  143. if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_INV_TSC))
  144. return NULL;
  145. if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_APERF))
  146. return NULL;
  147. /* Free this at program termination */
  148. is_valid = calloc(cpu_count, sizeof(int));
  149. for (num = 0; num < NHM_CSTATE_COUNT; num++) {
  150. previous_count[num] = calloc(cpu_count,
  151. sizeof(unsigned long long));
  152. current_count[num] = calloc(cpu_count,
  153. sizeof(unsigned long long));
  154. }
  155. intel_nhm_monitor.name_len = strlen(intel_nhm_monitor.name);
  156. return &intel_nhm_monitor;
  157. }
  158. void intel_nhm_unregister(void)
  159. {
  160. int num;
  161. for (num = 0; num < NHM_CSTATE_COUNT; num++) {
  162. free(previous_count[num]);
  163. free(current_count[num]);
  164. }
  165. free(is_valid);
  166. }
  167. struct cpuidle_monitor intel_nhm_monitor = {
  168. .name = "Nehalem",
  169. .hw_states_num = NHM_CSTATE_COUNT,
  170. .hw_states = nhm_cstates,
  171. .start = nhm_start,
  172. .stop = nhm_stop,
  173. .do_register = intel_nhm_register,
  174. .unregister = intel_nhm_unregister,
  175. .needs_root = 1,
  176. .overflow_s = 922000000 /* 922337203 seconds TSC overflow
  177. at 20GHz */
  178. };
  179. #endif