nhm_idle.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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, unsigned int cpu)
  61. {
  62. int msr;
  63. switch(id) {
  64. case C3:
  65. msr = MSR_CORE_C3_RESIDENCY;
  66. break;
  67. case C6:
  68. msr = MSR_CORE_C6_RESIDENCY;
  69. break;
  70. case PC3:
  71. msr = MSR_PKG_C3_RESIDENCY;
  72. break;
  73. case PC6:
  74. msr = MSR_PKG_C6_RESIDENCY;
  75. break;
  76. case TSC:
  77. msr = MSR_TSC;
  78. break;
  79. default:
  80. return -1;
  81. };
  82. if (read_msr(cpu, msr, val))
  83. return -1;
  84. return 0;
  85. }
  86. static int nhm_get_count_percent(unsigned int id, double *percent,
  87. unsigned int cpu)
  88. {
  89. *percent = 0.0;
  90. if (!is_valid[cpu])
  91. return -1;
  92. *percent = (100.0 * (current_count[id][cpu] - previous_count[id][cpu])) /
  93. (tsc_at_measure_end - tsc_at_measure_start);
  94. dprint("%s: previous: %llu - current: %llu - (%u)\n", nhm_cstates[id].name,
  95. previous_count[id][cpu], current_count[id][cpu],
  96. cpu);
  97. dprint("%s: tsc_diff: %llu - count_diff: %llu - percent: %2.f (%u)\n",
  98. nhm_cstates[id].name,
  99. (unsigned long long) tsc_at_measure_end - tsc_at_measure_start,
  100. current_count[id][cpu] - previous_count[id][cpu],
  101. *percent, cpu);
  102. return 0;
  103. }
  104. static int nhm_start(void)
  105. {
  106. int num, cpu;
  107. unsigned long long dbg, val;
  108. nhm_get_count(TSC, &tsc_at_measure_start, 0);
  109. for (num = 0; num < NHM_CSTATE_COUNT; num++) {
  110. for (cpu = 0; cpu < cpu_count; cpu++) {
  111. is_valid[cpu] = !nhm_get_count(num, &val, cpu);
  112. previous_count[num][cpu] = val;
  113. }
  114. }
  115. nhm_get_count(TSC, &dbg, 0);
  116. dprint("TSC diff: %llu\n", dbg - tsc_at_measure_start);
  117. return 0;
  118. }
  119. static int nhm_stop(void)
  120. {
  121. unsigned long long val;
  122. unsigned long long dbg;
  123. int num, cpu;
  124. nhm_get_count(TSC, &tsc_at_measure_end, 0);
  125. for (num = 0; num < NHM_CSTATE_COUNT; num++) {
  126. for (cpu = 0; cpu < cpu_count; cpu++) {
  127. is_valid[cpu] = !nhm_get_count(num, &val, cpu);
  128. current_count[num][cpu] = val;
  129. }
  130. }
  131. nhm_get_count(TSC, &dbg, 0);
  132. dprint("TSC diff: %llu\n", dbg - tsc_at_measure_end);
  133. return 0;
  134. }
  135. struct cpuidle_monitor intel_nhm_monitor;
  136. struct cpuidle_monitor* intel_nhm_register(void) {
  137. int num;
  138. if (cpupower_cpu_info.vendor != X86_VENDOR_INTEL)
  139. return NULL;
  140. if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_INV_TSC))
  141. return NULL;
  142. if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_APERF))
  143. return NULL;
  144. /* Free this at program termination */
  145. is_valid = calloc(cpu_count, sizeof (int));
  146. for (num = 0; num < NHM_CSTATE_COUNT; num++) {
  147. previous_count[num] = calloc (cpu_count,
  148. sizeof(unsigned long long));
  149. current_count[num] = calloc (cpu_count,
  150. sizeof(unsigned long long));
  151. }
  152. intel_nhm_monitor.name_len = strlen(intel_nhm_monitor.name);
  153. return &intel_nhm_monitor;
  154. }
  155. void intel_nhm_unregister(void) {
  156. int num;
  157. for (num = 0; num < NHM_CSTATE_COUNT; num++) {
  158. free(previous_count[num]);
  159. free(current_count[num]);
  160. }
  161. free(is_valid);
  162. }
  163. struct cpuidle_monitor intel_nhm_monitor = {
  164. .name = "Nehalem",
  165. .hw_states_num = NHM_CSTATE_COUNT,
  166. .hw_states = nhm_cstates,
  167. .start = nhm_start,
  168. .stop = nhm_stop,
  169. .do_register = intel_nhm_register,
  170. .unregister = intel_nhm_unregister,
  171. .needs_root = 1,
  172. .overflow_s = 922000000 /* 922337203 seconds TSC overflow
  173. at 20GHz */
  174. };
  175. #endif