From e7bd0750af252abbcffd70352dde8e543631d464 Mon Sep 17 00:00:00 2001 From: Andrew Gallatin Date: Mon, 7 May 2018 15:24:03 +0000 Subject: [PATCH] Boost thread priority while changing CPU frequency Boost the priority of user-space threads when they set their affinity to a core to adjust its frequency. This avoids a situation where a CPU bound kernel thread with the same affinity is running on a down-clocked core, and will "block" powerd from up-clocking the core until the kernel thread yields. This can lead to poor perfomance, and to things potentially getting stuck on Giant. Reviewed by: kib (imp reviewed earlier version) Sponsored by: Netflix Differential Revision: https://reviews.freebsd.org/D15246 --- sys/kern/kern_cpu.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/sys/kern/kern_cpu.c b/sys/kern/kern_cpu.c index 193054a4f70..8c394460816 100644 --- a/sys/kern/kern_cpu.c +++ b/sys/kern/kern_cpu.c @@ -245,6 +245,7 @@ cf_set_method(device_t dev, const struct cf_level *level, int priority) struct cf_saved_freq *saved_freq, *curr_freq; struct pcpu *pc; int error, i; + u_char pri; sc = device_get_softc(dev); error = 0; @@ -333,6 +334,8 @@ cf_set_method(device_t dev, const struct cf_level *level, int priority) /* Bind to the target CPU before switching. */ pc = cpu_get_pcpu(set->dev); thread_lock(curthread); + pri = curthread->td_priority; + sched_prio(curthread, PRI_MIN); sched_bind(curthread, pc->pc_cpuid); thread_unlock(curthread); CF_DEBUG("setting abs freq %d on %s (cpu %d)\n", set->freq, @@ -340,6 +343,7 @@ cf_set_method(device_t dev, const struct cf_level *level, int priority) error = CPUFREQ_DRV_SET(set->dev, set); thread_lock(curthread); sched_unbind(curthread); + sched_prio(curthread, pri); thread_unlock(curthread); if (error) { goto out; @@ -357,6 +361,8 @@ cf_set_method(device_t dev, const struct cf_level *level, int priority) /* Bind to the target CPU before switching. */ pc = cpu_get_pcpu(set->dev); thread_lock(curthread); + pri = curthread->td_priority; + sched_prio(curthread, PRI_MIN); sched_bind(curthread, pc->pc_cpuid); thread_unlock(curthread); CF_DEBUG("setting rel freq %d on %s (cpu %d)\n", set->freq, @@ -364,6 +370,7 @@ cf_set_method(device_t dev, const struct cf_level *level, int priority) error = CPUFREQ_DRV_SET(set->dev, set); thread_lock(curthread); sched_unbind(curthread); + sched_prio(curthread, pri); thread_unlock(curthread); if (error) { /* XXX Back out any successful setting? */