Skip to content

Commit f3eca38

Browse files
committed
x86/aperfmperf: Replace arch_freq_get_on_cpu()
Reading the current CPU frequency from /sys/..../scaling_cur_freq involves in the worst case two IPIs due to the ad hoc sampling. The frequency invariance infrastructure provides the APERF/MPERF samples already. Utilize them and consolidate this with the /proc/cpuinfo readout. The sample is considered valid for 20ms. So for idle or isolated NOHZ full CPUs the function returns 0, which is matching the previous behaviour. The resulting text size vs. the original APERF/MPERF plus the separate frequency invariance code: text: 2411 -> 723 init.text: 0 -> 767 Signed-off-by: Thomas Gleixner <[email protected]> Tested-by: Eric Dumazet <[email protected]> Reviewed-by: Rafael J. Wysocki <[email protected]> Acked-by: Peter Zijlstra (Intel) <[email protected]> Acked-by: Paul E. McKenney <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 7d84c1e commit f3eca38

File tree

2 files changed

+2
-94
lines changed

2 files changed

+2
-94
lines changed

arch/x86/kernel/cpu/aperfmperf.c

Lines changed: 1 addition & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -36,98 +36,6 @@ static DEFINE_PER_CPU_SHARED_ALIGNED(struct aperfmperf, cpu_samples) = {
3636
.seq = SEQCNT_ZERO(cpu_samples.seq)
3737
};
3838

39-
struct aperfmperf_sample {
40-
unsigned int khz;
41-
atomic_t scfpending;
42-
ktime_t time;
43-
u64 aperf;
44-
u64 mperf;
45-
};
46-
47-
static DEFINE_PER_CPU(struct aperfmperf_sample, samples);
48-
49-
#define APERFMPERF_CACHE_THRESHOLD_MS 10
50-
#define APERFMPERF_REFRESH_DELAY_MS 10
51-
#define APERFMPERF_STALE_THRESHOLD_MS 1000
52-
53-
/*
54-
* aperfmperf_snapshot_khz()
55-
* On the current CPU, snapshot APERF, MPERF, and jiffies
56-
* unless we already did it within 10ms
57-
* calculate kHz, save snapshot
58-
*/
59-
static void aperfmperf_snapshot_khz(void *dummy)
60-
{
61-
u64 aperf, aperf_delta;
62-
u64 mperf, mperf_delta;
63-
struct aperfmperf_sample *s = this_cpu_ptr(&samples);
64-
unsigned long flags;
65-
66-
local_irq_save(flags);
67-
rdmsrl(MSR_IA32_APERF, aperf);
68-
rdmsrl(MSR_IA32_MPERF, mperf);
69-
local_irq_restore(flags);
70-
71-
aperf_delta = aperf - s->aperf;
72-
mperf_delta = mperf - s->mperf;
73-
74-
/*
75-
* There is no architectural guarantee that MPERF
76-
* increments faster than we can read it.
77-
*/
78-
if (mperf_delta == 0)
79-
return;
80-
81-
s->time = ktime_get();
82-
s->aperf = aperf;
83-
s->mperf = mperf;
84-
s->khz = div64_u64((cpu_khz * aperf_delta), mperf_delta);
85-
atomic_set_release(&s->scfpending, 0);
86-
}
87-
88-
static bool aperfmperf_snapshot_cpu(int cpu, ktime_t now, bool wait)
89-
{
90-
s64 time_delta = ktime_ms_delta(now, per_cpu(samples.time, cpu));
91-
struct aperfmperf_sample *s = per_cpu_ptr(&samples, cpu);
92-
93-
/* Don't bother re-computing within the cache threshold time. */
94-
if (time_delta < APERFMPERF_CACHE_THRESHOLD_MS)
95-
return true;
96-
97-
if (!atomic_xchg(&s->scfpending, 1) || wait)
98-
smp_call_function_single(cpu, aperfmperf_snapshot_khz, NULL, wait);
99-
100-
/* Return false if the previous iteration was too long ago. */
101-
return time_delta <= APERFMPERF_STALE_THRESHOLD_MS;
102-
}
103-
104-
unsigned int arch_freq_get_on_cpu(int cpu)
105-
{
106-
struct aperfmperf_sample *s = per_cpu_ptr(&samples, cpu);
107-
108-
if (!cpu_khz)
109-
return 0;
110-
111-
if (!boot_cpu_has(X86_FEATURE_APERFMPERF))
112-
return 0;
113-
114-
if (!housekeeping_cpu(cpu, HK_TYPE_MISC))
115-
return 0;
116-
117-
if (rcu_is_idle_cpu(cpu))
118-
return 0;
119-
120-
if (aperfmperf_snapshot_cpu(cpu, ktime_get(), true))
121-
return per_cpu(samples.khz, cpu);
122-
123-
msleep(APERFMPERF_REFRESH_DELAY_MS);
124-
atomic_set(&s->scfpending, 1);
125-
smp_mb(); /* ->scfpending before smp_call_function_single(). */
126-
smp_call_function_single(cpu, aperfmperf_snapshot_khz, NULL, 1);
127-
128-
return per_cpu(samples.khz, cpu);
129-
}
130-
13139
static void init_counter_refs(void)
13240
{
13341
u64 aperf, mperf;
@@ -494,7 +402,7 @@ void arch_scale_freq_tick(void)
494402
*/
495403
#define MAX_SAMPLE_AGE ((unsigned long)HZ / 50)
496404

497-
unsigned int aperfmperf_get_khz(int cpu)
405+
unsigned int arch_freq_get_on_cpu(int cpu)
498406
{
499407
struct aperfmperf *s = per_cpu_ptr(&cpu_samples, cpu);
500408
unsigned long last;

arch/x86/kernel/cpu/proc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static int show_cpuinfo(struct seq_file *m, void *v)
8484
seq_printf(m, "microcode\t: 0x%x\n", c->microcode);
8585

8686
if (cpu_has(c, X86_FEATURE_TSC)) {
87-
unsigned int freq = aperfmperf_get_khz(cpu);
87+
unsigned int freq = arch_freq_get_on_cpu(cpu);
8888

8989
if (!freq)
9090
freq = cpufreq_quick_get(cpu);

0 commit comments

Comments
 (0)