2 * (C) 2010,2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
4 * Licensed under the terms of the GNU GPL License version 2.
7 #if defined(__i386__) || defined(__x86_64__)
17 #include "helpers/helpers.h"
18 #include "idle_monitor/cpupower-monitor.h"
20 #define MSR_APERF 0xE8
21 #define MSR_MPERF 0xE7
25 #define MSR_AMD_HWCR 0xc0010015
27 enum mperf_id
{ C0
= 0, Cx
, AVG_FREQ
, MPERF_CSTATE_COUNT
};
29 static int mperf_get_count_percent(unsigned int self_id
, double *percent
,
31 static int mperf_get_count_freq(unsigned int id
, unsigned long long *count
,
33 static struct timespec time_start
, time_end
;
35 static cstate_t mperf_cstates
[MPERF_CSTATE_COUNT
] = {
38 .desc
= N_("Processor Core not idle"),
40 .range
= RANGE_THREAD
,
41 .get_count_percent
= mperf_get_count_percent
,
45 .desc
= N_("Processor Core in an idle state"),
47 .range
= RANGE_THREAD
,
48 .get_count_percent
= mperf_get_count_percent
,
53 .desc
= N_("Average Frequency (including boost) in MHz"),
55 .range
= RANGE_THREAD
,
56 .get_count
= mperf_get_count_freq
,
60 enum MAX_FREQ_MODE
{ MAX_FREQ_SYSFS
, MAX_FREQ_TSC_REF
};
61 static int max_freq_mode
;
63 * The max frequency mperf is ticking at (in C0), either retrieved via:
64 * 1) calculated after measurements if we know TSC ticks at mperf/P0 frequency
65 * 2) cpufreq /sys/devices/.../cpu0/cpufreq/cpuinfo_max_freq at init time
66 * 1. Is preferred as it also works without cpufreq subsystem (e.g. on Xen)
68 static unsigned long max_frequency
;
70 static unsigned long long tsc_at_measure_start
;
71 static unsigned long long tsc_at_measure_end
;
72 static unsigned long long *mperf_previous_count
;
73 static unsigned long long *aperf_previous_count
;
74 static unsigned long long *mperf_current_count
;
75 static unsigned long long *aperf_current_count
;
77 /* valid flag for all CPUs. If a MSR read failed it will be zero */
80 static int mperf_get_tsc(unsigned long long *tsc
)
83 ret
= read_msr(0, MSR_TSC
, tsc
);
85 dprint("Reading TSC MSR failed, returning %llu\n", *tsc
);
89 static int mperf_init_stats(unsigned int cpu
)
91 unsigned long long val
;
94 ret
= read_msr(cpu
, MSR_APERF
, &val
);
95 aperf_previous_count
[cpu
] = val
;
96 ret
|= read_msr(cpu
, MSR_MPERF
, &val
);
97 mperf_previous_count
[cpu
] = val
;
103 static int mperf_measure_stats(unsigned int cpu
)
105 unsigned long long val
;
108 ret
= read_msr(cpu
, MSR_APERF
, &val
);
109 aperf_current_count
[cpu
] = val
;
110 ret
|= read_msr(cpu
, MSR_MPERF
, &val
);
111 mperf_current_count
[cpu
] = val
;
112 is_valid
[cpu
] = !ret
;
117 static int mperf_get_count_percent(unsigned int id
, double *percent
,
120 unsigned long long aperf_diff
, mperf_diff
, tsc_diff
;
121 unsigned long long timediff
;
126 if (id
!= C0
&& id
!= Cx
)
129 mperf_diff
= mperf_current_count
[cpu
] - mperf_previous_count
[cpu
];
130 aperf_diff
= aperf_current_count
[cpu
] - aperf_previous_count
[cpu
];
132 if (max_freq_mode
== MAX_FREQ_TSC_REF
) {
133 tsc_diff
= tsc_at_measure_end
- tsc_at_measure_start
;
134 *percent
= 100.0 * mperf_diff
/ tsc_diff
;
135 dprint("%s: TSC Ref - mperf_diff: %llu, tsc_diff: %llu\n",
136 mperf_cstates
[id
].name
, mperf_diff
, tsc_diff
);
137 } else if (max_freq_mode
== MAX_FREQ_SYSFS
) {
138 timediff
= timespec_diff_us(time_start
, time_end
);
139 *percent
= 100.0 * mperf_diff
/ timediff
;
140 dprint("%s: MAXFREQ - mperf_diff: %llu, time_diff: %llu\n",
141 mperf_cstates
[id
].name
, mperf_diff
, timediff
);
146 *percent
= 100.0 - *percent
;
148 dprint("%s: previous: %llu - current: %llu - (%u)\n",
149 mperf_cstates
[id
].name
, mperf_diff
, aperf_diff
, cpu
);
150 dprint("%s: %f\n", mperf_cstates
[id
].name
, *percent
);
154 static int mperf_get_count_freq(unsigned int id
, unsigned long long *count
,
157 unsigned long long aperf_diff
, mperf_diff
, time_diff
, tsc_diff
;
165 mperf_diff
= mperf_current_count
[cpu
] - mperf_previous_count
[cpu
];
166 aperf_diff
= aperf_current_count
[cpu
] - aperf_previous_count
[cpu
];
168 if (max_freq_mode
== MAX_FREQ_TSC_REF
) {
169 /* Calculate max_freq from TSC count */
170 tsc_diff
= tsc_at_measure_end
- tsc_at_measure_start
;
171 time_diff
= timespec_diff_us(time_start
, time_end
);
172 max_frequency
= tsc_diff
/ time_diff
;
175 *count
= max_frequency
* ((double)aperf_diff
/ mperf_diff
);
176 dprint("%s: Average freq based on %s maximum frequency:\n",
177 mperf_cstates
[id
].name
,
178 (max_freq_mode
== MAX_FREQ_TSC_REF
) ? "TSC calculated" : "sysfs read");
179 dprint("%max_frequency: %lu", max_frequency
);
180 dprint("aperf_diff: %llu\n", aperf_diff
);
181 dprint("mperf_diff: %llu\n", mperf_diff
);
182 dprint("avg freq: %llu\n", *count
);
186 static int mperf_start(void)
189 unsigned long long dbg
;
191 clock_gettime(CLOCK_REALTIME
, &time_start
);
192 mperf_get_tsc(&tsc_at_measure_start
);
194 for (cpu
= 0; cpu
< cpu_count
; cpu
++)
195 mperf_init_stats(cpu
);
198 dprint("TSC diff: %llu\n", dbg
- tsc_at_measure_start
);
202 static int mperf_stop(void)
204 unsigned long long dbg
;
207 for (cpu
= 0; cpu
< cpu_count
; cpu
++)
208 mperf_measure_stats(cpu
);
210 mperf_get_tsc(&tsc_at_measure_end
);
211 clock_gettime(CLOCK_REALTIME
, &time_end
);
214 dprint("TSC diff: %llu\n", dbg
- tsc_at_measure_end
);
220 * Mperf register is defined to tick at P0 (maximum) frequency
222 * Instead of reading out P0 which can be tricky to read out from HW,
223 * we use TSC counter if it reliably ticks at P0/mperf frequency.
225 * Still try to fall back to:
226 * /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
227 * on older Intel HW without invariant TSC feature.
228 * Or on AMD machines where TSC does not tick at P0 (do not exist yet, but
229 * it's still double checked (MSR_AMD_HWCR)).
231 * On these machines the user would still get useful mperf
232 * stats when acpi-cpufreq driver is loaded.
234 static int init_maxfreq_mode(void)
237 unsigned long long hwcr
;
240 if (!cpupower_cpu_info
.caps
& CPUPOWER_CAP_INV_TSC
)
243 if (cpupower_cpu_info
.vendor
== X86_VENDOR_AMD
) {
244 /* MSR_AMD_HWCR tells us whether TSC runs at P0/mperf
246 * A test whether hwcr is accessable/available would be:
247 * (cpupower_cpu_info.family > 0x10 ||
248 * cpupower_cpu_info.family == 0x10 &&
249 * cpupower_cpu_info.model >= 0x2))
250 * This should be the case for all aperf/mperf
251 * capable AMD machines and is therefore safe to test here.
252 * Compare with Linus kernel git commit: acf01734b1747b1ec4
254 ret
= read_msr(0, MSR_AMD_HWCR
, &hwcr
);
256 * If the MSR read failed, assume a Xen system that did
257 * not explicitly provide access to it and assume TSC works
260 dprint("TSC read 0x%x failed - assume TSC working\n",
263 } else if (1 & (hwcr
>> 24)) {
264 max_freq_mode
= MAX_FREQ_TSC_REF
;
266 } else { /* Use sysfs max frequency if available */ }
267 } else if (cpupower_cpu_info
.vendor
== X86_VENDOR_INTEL
) {
269 * On Intel we assume mperf (in C0) is ticking at same
272 max_freq_mode
= MAX_FREQ_TSC_REF
;
276 if (cpufreq_get_hardware_limits(0, &min
, &max_frequency
)) {
277 dprint("Cannot retrieve max freq from cpufreq kernel "
281 max_freq_mode
= MAX_FREQ_SYSFS
;
286 * This monitor provides:
288 * 1) Average frequency a CPU resided in
289 * This always works if the CPU has aperf/mperf capabilities
291 * 2) C0 and Cx (any sleep state) time a CPU resided in
292 * Works if mperf timer stops ticking in sleep states which
293 * seem to be the case on all current HW.
294 * Both is directly retrieved from HW registers and is independent
295 * from kernel statistics.
297 struct cpuidle_monitor mperf_monitor
;
298 struct cpuidle_monitor
*mperf_register(void)
300 if (!(cpupower_cpu_info
.caps
& CPUPOWER_CAP_APERF
))
303 if (init_maxfreq_mode())
306 /* Free this at program termination */
307 is_valid
= calloc(cpu_count
, sizeof(int));
308 mperf_previous_count
= calloc(cpu_count
, sizeof(unsigned long long));
309 aperf_previous_count
= calloc(cpu_count
, sizeof(unsigned long long));
310 mperf_current_count
= calloc(cpu_count
, sizeof(unsigned long long));
311 aperf_current_count
= calloc(cpu_count
, sizeof(unsigned long long));
313 mperf_monitor
.name_len
= strlen(mperf_monitor
.name
);
314 return &mperf_monitor
;
317 void mperf_unregister(void)
319 free(mperf_previous_count
);
320 free(aperf_previous_count
);
321 free(mperf_current_count
);
322 free(aperf_current_count
);
326 struct cpuidle_monitor mperf_monitor
= {
328 .hw_states_num
= MPERF_CSTATE_COUNT
,
329 .hw_states
= mperf_cstates
,
330 .start
= mperf_start
,
332 .do_register
= mperf_register
,
333 .unregister
= mperf_unregister
,
335 .overflow_s
= 922000000 /* 922337203 seconds TSC overflow
338 #endif /* #if defined(__i386__) || defined(__x86_64__) */