Blame view
tools/power/cpupower/utils/idle_monitor/mperf_monitor.c
9.16 KB
7fe2f6399 cpupowerutils - c... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/* * (C) 2010,2011 Thomas Renninger <trenn@suse.de>, Novell Inc. * * Licensed under the terms of the GNU GPL License version 2. */ #if defined(__i386__) || defined(__x86_64__) #include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <cpufreq.h> #include "helpers/helpers.h" #include "idle_monitor/cpupower-monitor.h" #define MSR_APERF 0xE8 #define MSR_MPERF 0xE7 #define MSR_TSC 0x10 |
2dfc818b3 cpupower: mperf m... |
24 |
#define MSR_AMD_HWCR 0xc0010015 |
7fe2f6399 cpupowerutils - c... |
25 26 27 28 29 30 |
enum mperf_id { C0 = 0, Cx, AVG_FREQ, MPERF_CSTATE_COUNT }; static int mperf_get_count_percent(unsigned int self_id, double *percent, unsigned int cpu); static int mperf_get_count_freq(unsigned int id, unsigned long long *count, unsigned int cpu); |
2dfc818b3 cpupower: mperf m... |
31 |
static struct timespec time_start, time_end; |
7fe2f6399 cpupowerutils - c... |
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
static cstate_t mperf_cstates[MPERF_CSTATE_COUNT] = { { .name = "C0", .desc = N_("Processor Core not idle"), .id = C0, .range = RANGE_THREAD, .get_count_percent = mperf_get_count_percent, }, { .name = "Cx", .desc = N_("Processor Core in an idle state"), .id = Cx, .range = RANGE_THREAD, .get_count_percent = mperf_get_count_percent, }, { .name = "Freq", .desc = N_("Average Frequency (including boost) in MHz"), .id = AVG_FREQ, .range = RANGE_THREAD, .get_count = mperf_get_count_freq, }, }; |
2dfc818b3 cpupower: mperf m... |
57 58 59 60 61 62 63 64 65 |
enum MAX_FREQ_MODE { MAX_FREQ_SYSFS, MAX_FREQ_TSC_REF }; static int max_freq_mode; /* * The max frequency mperf is ticking at (in C0), either retrieved via: * 1) calculated after measurements if we know TSC ticks at mperf/P0 frequency * 2) cpufreq /sys/devices/.../cpu0/cpufreq/cpuinfo_max_freq at init time * 1. Is preferred as it also works without cpufreq subsystem (e.g. on Xen) */ static unsigned long max_frequency; |
7fe2f6399 cpupowerutils - c... |
66 67 |
static unsigned long long tsc_at_measure_start; static unsigned long long tsc_at_measure_end; |
7fe2f6399 cpupowerutils - c... |
68 69 70 71 |
static unsigned long long *mperf_previous_count; static unsigned long long *aperf_previous_count; static unsigned long long *mperf_current_count; static unsigned long long *aperf_current_count; |
2dfc818b3 cpupower: mperf m... |
72 |
|
7fe2f6399 cpupowerutils - c... |
73 74 75 76 77 |
/* valid flag for all CPUs. If a MSR read failed it will be zero */ static int *is_valid; static int mperf_get_tsc(unsigned long long *tsc) { |
2dfc818b3 cpupower: mperf m... |
78 79 80 81 82 83 |
int ret; ret = read_msr(0, MSR_TSC, tsc); if (ret) dprint("Reading TSC MSR failed, returning %llu ", *tsc); return ret; |
7fe2f6399 cpupowerutils - c... |
84 85 86 87 88 89 90 91 92 93 94 95 |
} static int mperf_init_stats(unsigned int cpu) { unsigned long long val; int ret; ret = read_msr(cpu, MSR_APERF, &val); aperf_previous_count[cpu] = val; ret |= read_msr(cpu, MSR_MPERF, &val); mperf_previous_count[cpu] = val; is_valid[cpu] = !ret; |
b510b5412 cpupowerutils: id... |
96 |
|
7fe2f6399 cpupowerutils - c... |
97 98 99 100 101 102 103 104 105 106 107 108 109 |
return 0; } static int mperf_measure_stats(unsigned int cpu) { unsigned long long val; int ret; ret = read_msr(cpu, MSR_APERF, &val); aperf_current_count[cpu] = val; ret |= read_msr(cpu, MSR_MPERF, &val); mperf_current_count[cpu] = val; is_valid[cpu] = !ret; |
b510b5412 cpupowerutils: id... |
110 |
|
7fe2f6399 cpupowerutils - c... |
111 112 |
return 0; } |
7fe2f6399 cpupowerutils - c... |
113 114 115 116 |
static int mperf_get_count_percent(unsigned int id, double *percent, unsigned int cpu) { unsigned long long aperf_diff, mperf_diff, tsc_diff; |
2dfc818b3 cpupower: mperf m... |
117 |
unsigned long long timediff; |
7fe2f6399 cpupowerutils - c... |
118 119 120 121 122 123 124 125 126 |
if (!is_valid[cpu]) return -1; if (id != C0 && id != Cx) return -1; mperf_diff = mperf_current_count[cpu] - mperf_previous_count[cpu]; aperf_diff = aperf_current_count[cpu] - aperf_previous_count[cpu]; |
7fe2f6399 cpupowerutils - c... |
127 |
|
2dfc818b3 cpupower: mperf m... |
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
if (max_freq_mode == MAX_FREQ_TSC_REF) { tsc_diff = tsc_at_measure_end - tsc_at_measure_start; *percent = 100.0 * mperf_diff / tsc_diff; dprint("%s: TSC Ref - mperf_diff: %llu, tsc_diff: %llu ", mperf_cstates[id].name, mperf_diff, tsc_diff); } else if (max_freq_mode == MAX_FREQ_SYSFS) { timediff = timespec_diff_us(time_start, time_end); *percent = 100.0 * mperf_diff / timediff; dprint("%s: MAXFREQ - mperf_diff: %llu, time_diff: %llu ", mperf_cstates[id].name, mperf_diff, timediff); } else return -1; |
7fe2f6399 cpupowerutils - c... |
142 143 144 |
if (id == Cx) *percent = 100.0 - *percent; |
b510b5412 cpupowerutils: id... |
145 146 147 |
dprint("%s: previous: %llu - current: %llu - (%u) ", mperf_cstates[id].name, mperf_diff, aperf_diff, cpu); |
7fe2f6399 cpupowerutils - c... |
148 149 150 151 152 153 |
dprint("%s: %f ", mperf_cstates[id].name, *percent); return 0; } static int mperf_get_count_freq(unsigned int id, unsigned long long *count, |
b510b5412 cpupowerutils: id... |
154 |
unsigned int cpu) |
7fe2f6399 cpupowerutils - c... |
155 |
{ |
2dfc818b3 cpupower: mperf m... |
156 |
unsigned long long aperf_diff, mperf_diff, time_diff, tsc_diff; |
7fe2f6399 cpupowerutils - c... |
157 158 159 160 161 162 163 164 165 |
if (id != AVG_FREQ) return 1; if (!is_valid[cpu]) return -1; mperf_diff = mperf_current_count[cpu] - mperf_previous_count[cpu]; aperf_diff = aperf_current_count[cpu] - aperf_previous_count[cpu]; |
2dfc818b3 cpupower: mperf m... |
166 167 168 169 170 171 |
if (max_freq_mode == MAX_FREQ_TSC_REF) { /* Calculate max_freq from TSC count */ tsc_diff = tsc_at_measure_end - tsc_at_measure_start; time_diff = timespec_diff_us(time_start, time_end); max_frequency = tsc_diff / time_diff; } |
7fe2f6399 cpupowerutils - c... |
172 |
|
2dfc818b3 cpupower: mperf m... |
173 174 175 176 177 178 179 180 181 182 183 184 |
*count = max_frequency * ((double)aperf_diff / mperf_diff); dprint("%s: Average freq based on %s maximum frequency: ", mperf_cstates[id].name, (max_freq_mode == MAX_FREQ_TSC_REF) ? "TSC calculated" : "sysfs read"); dprint("%max_frequency: %lu", max_frequency); dprint("aperf_diff: %llu ", aperf_diff); dprint("mperf_diff: %llu ", mperf_diff); dprint("avg freq: %llu ", *count); |
7fe2f6399 cpupowerutils - c... |
185 186 187 188 189 190 191 |
return 0; } static int mperf_start(void) { int cpu; unsigned long long dbg; |
2dfc818b3 cpupower: mperf m... |
192 |
clock_gettime(CLOCK_REALTIME, &time_start); |
7fe2f6399 cpupowerutils - c... |
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
mperf_get_tsc(&tsc_at_measure_start); for (cpu = 0; cpu < cpu_count; cpu++) mperf_init_stats(cpu); mperf_get_tsc(&dbg); dprint("TSC diff: %llu ", dbg - tsc_at_measure_start); return 0; } static int mperf_stop(void) { unsigned long long dbg; int cpu; |
7fe2f6399 cpupowerutils - c... |
208 209 |
for (cpu = 0; cpu < cpu_count; cpu++) mperf_measure_stats(cpu); |
2dfc818b3 cpupower: mperf m... |
210 211 |
mperf_get_tsc(&tsc_at_measure_end); clock_gettime(CLOCK_REALTIME, &time_end); |
7fe2f6399 cpupowerutils - c... |
212 213 214 215 216 217 |
mperf_get_tsc(&dbg); dprint("TSC diff: %llu ", dbg - tsc_at_measure_end); return 0; } |
2dfc818b3 cpupower: mperf m... |
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
/* * Mperf register is defined to tick at P0 (maximum) frequency * * Instead of reading out P0 which can be tricky to read out from HW, * we use TSC counter if it reliably ticks at P0/mperf frequency. * * Still try to fall back to: * /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq * on older Intel HW without invariant TSC feature. * Or on AMD machines where TSC does not tick at P0 (do not exist yet, but * it's still double checked (MSR_AMD_HWCR)). * * On these machines the user would still get useful mperf * stats when acpi-cpufreq driver is loaded. */ static int init_maxfreq_mode(void) |
b510b5412 cpupowerutils: id... |
234 |
{ |
2dfc818b3 cpupower: mperf m... |
235 236 |
int ret; unsigned long long hwcr; |
7fe2f6399 cpupowerutils - c... |
237 |
unsigned long min; |
2dfc818b3 cpupower: mperf m... |
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
if (!cpupower_cpu_info.caps & CPUPOWER_CAP_INV_TSC) goto use_sysfs; if (cpupower_cpu_info.vendor == X86_VENDOR_AMD) { /* MSR_AMD_HWCR tells us whether TSC runs at P0/mperf * freq. * A test whether hwcr is accessable/available would be: * (cpupower_cpu_info.family > 0x10 || * cpupower_cpu_info.family == 0x10 && * cpupower_cpu_info.model >= 0x2)) * This should be the case for all aperf/mperf * capable AMD machines and is therefore safe to test here. * Compare with Linus kernel git commit: acf01734b1747b1ec4 */ ret = read_msr(0, MSR_AMD_HWCR, &hwcr); /* * If the MSR read failed, assume a Xen system that did * not explicitly provide access to it and assume TSC works */ if (ret != 0) { dprint("TSC read 0x%x failed - assume TSC working ", MSR_AMD_HWCR); return 0; } else if (1 & (hwcr >> 24)) { max_freq_mode = MAX_FREQ_TSC_REF; return 0; } else { /* Use sysfs max frequency if available */ } } else if (cpupower_cpu_info.vendor == X86_VENDOR_INTEL) { /* * On Intel we assume mperf (in C0) is ticking at same * rate than TSC */ max_freq_mode = MAX_FREQ_TSC_REF; return 0; } use_sysfs: |
7fe2f6399 cpupowerutils - c... |
275 276 277 278 |
if (cpufreq_get_hardware_limits(0, &min, &max_frequency)) { dprint("Cannot retrieve max freq from cpufreq kernel " "subsystem "); |
2dfc818b3 cpupower: mperf m... |
279 |
return -1; |
7fe2f6399 cpupowerutils - c... |
280 |
} |
2dfc818b3 cpupower: mperf m... |
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
max_freq_mode = MAX_FREQ_SYSFS; return 0; } /* * This monitor provides: * * 1) Average frequency a CPU resided in * This always works if the CPU has aperf/mperf capabilities * * 2) C0 and Cx (any sleep state) time a CPU resided in * Works if mperf timer stops ticking in sleep states which * seem to be the case on all current HW. * Both is directly retrieved from HW registers and is independent * from kernel statistics. */ struct cpuidle_monitor mperf_monitor; struct cpuidle_monitor *mperf_register(void) { if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_APERF)) return NULL; if (init_maxfreq_mode()) return NULL; |
7fe2f6399 cpupowerutils - c... |
305 306 |
/* Free this at program termination */ |
b510b5412 cpupowerutils: id... |
307 308 309 310 311 |
is_valid = calloc(cpu_count, sizeof(int)); mperf_previous_count = calloc(cpu_count, sizeof(unsigned long long)); aperf_previous_count = calloc(cpu_count, sizeof(unsigned long long)); mperf_current_count = calloc(cpu_count, sizeof(unsigned long long)); aperf_current_count = calloc(cpu_count, sizeof(unsigned long long)); |
7fe2f6399 cpupowerutils - c... |
312 313 314 |
mperf_monitor.name_len = strlen(mperf_monitor.name); return &mperf_monitor; } |
b510b5412 cpupowerutils: id... |
315 316 |
void mperf_unregister(void) { |
7fe2f6399 cpupowerutils - c... |
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
free(mperf_previous_count); free(aperf_previous_count); free(mperf_current_count); free(aperf_current_count); free(is_valid); } struct cpuidle_monitor mperf_monitor = { .name = "Mperf", .hw_states_num = MPERF_CSTATE_COUNT, .hw_states = mperf_cstates, .start = mperf_start, .stop = mperf_stop, .do_register = mperf_register, .unregister = mperf_unregister, .needs_root = 1, .overflow_s = 922000000 /* 922337203 seconds TSC overflow at 20GHz */ }; #endif /* #if defined(__i386__) || defined(__x86_64__) */ |