From: Marcelo Tosatti <mtosatti@redhat.com>
To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Radim Krcmar <rkrcmar@redhat.com>,
"Rafael J. Wysocki" <rjw@rjwysocki.net>,
Viresh Kumar <viresh.kumar@linaro.org>,
Marcelo Tosatti <mtosatti@redhat.com>
Subject: [patch 1/3] cpufreq: implement min/max/up/down functions
Date: Thu, 02 Feb 2017 15:47:56 -0200 [thread overview]
Message-ID: <20170202174921.814002151@redhat.com> (raw)
In-Reply-To: 20170202174755.946578704@redhat.com
[-- Attachment #1: cpufreq-userspace --]
[-- Type: text/plain, Size: 6522 bytes --]
Implement functions in cpufreq userspace code to:
* Change current frequency to {max,min,up,down} frequencies.
up/down being relative to current one.
These will be used to implement KVM hypercalls for the guest
to issue frequency changes.
Current situation with DPDK and frequency changes is as follows:
An algorithm in the guest decides when to increase/decrease
frequency based on the queue length of the device.
On the host, a power manager daemon is used to listen for
frequency change requests (on another core) and issue these
requests.
However frequency changes are performance sensitive events because:
On a change from low load condition to max load condition,
the frequency should be raised as soon as possible.
Sending a virtio-serial notification to another pCPU,
waiting for that pCPU to initiate an IPI to the requestor pCPU
to change frequency, is slower and more cache costly than
a direct hypercall to host to switch the frequency.
Moreover, if the pCPU where the power manager daemon is running
is not busy spinning on requests from the isolated DPDK vcpus,
there is also the cost of HLT wakeup for that pCPU.
Instructions to setup:
Disable the intel_pstate driver (intel_pstate=disable host kernel
command line option), and set cpufreq userspace governor for
the isolated pCPU.
Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
---
drivers/cpufreq/cpufreq_userspace.c | 172 ++++++++++++++++++++++++++++++++++++
include/linux/cpufreq.h | 7 +
2 files changed, 179 insertions(+)
Index: kvm-pvfreq/drivers/cpufreq/cpufreq_userspace.c
===================================================================
--- kvm-pvfreq.orig/drivers/cpufreq/cpufreq_userspace.c 2017-01-31 10:41:54.102575877 -0200
+++ kvm-pvfreq/drivers/cpufreq/cpufreq_userspace.c 2017-02-02 15:32:53.456262640 -0200
@@ -118,6 +118,178 @@
mutex_unlock(&userspace_mutex);
}
+static int cpufreq_is_userspace_governor(int cpu)
+{
+ int ret;
+
+ mutex_lock(&userspace_mutex);
+ ret = per_cpu(cpu_is_managed, cpu);
+ mutex_unlock(&userspace_mutex);
+
+ return ret;
+}
+
+int cpufreq_userspace_freq_up(int cpu)
+{
+ unsigned int curfreq, nextminfreq;
+ unsigned int ret = 0;
+ struct cpufreq_frequency_table *pos, *table;
+ struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
+
+ if (!policy)
+ return -EINVAL;
+
+ if (!cpufreq_is_userspace_governor(cpu)) {
+ cpufreq_cpu_put(policy);
+ return -EINVAL;
+ }
+
+ cpufreq_cpu_put(policy);
+
+ mutex_lock(&userspace_mutex);
+ table = policy->freq_table;
+ if (!table) {
+ mutex_unlock(&userspace_mutex);
+ return -ENODEV;
+ }
+ nextminfreq = cpufreq_quick_get_max(cpu);
+ curfreq = policy->cur;
+
+ cpufreq_for_each_valid_entry(pos, table) {
+ if (pos->frequency > curfreq &&
+ pos->frequency < nextminfreq)
+ nextminfreq = pos->frequency;
+ }
+
+ if (nextminfreq != curfreq) {
+ unsigned int *setspeed = policy->governor_data;
+
+ *setspeed = nextminfreq;
+ ret = __cpufreq_driver_target(policy, nextminfreq,
+ CPUFREQ_RELATION_L);
+ } else
+ ret = 1;
+ mutex_unlock(&userspace_mutex);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(cpufreq_userspace_freq_up);
+
+int cpufreq_userspace_freq_down(int cpu)
+{
+ unsigned int curfreq, prevmaxfreq;
+ unsigned int ret = 0;
+ struct cpufreq_frequency_table *pos, *table;
+ struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
+
+ if (!policy)
+ return -EINVAL;
+
+ if (!cpufreq_is_userspace_governor(cpu)) {
+ cpufreq_cpu_put(policy);
+ return -EINVAL;
+ }
+
+ cpufreq_cpu_put(policy);
+
+ mutex_lock(&userspace_mutex);
+ table = policy->freq_table;
+ if (!table) {
+ mutex_unlock(&userspace_mutex);
+ return -ENODEV;
+ }
+ prevmaxfreq = policy->min;
+ curfreq = policy->cur;
+
+ cpufreq_for_each_valid_entry(pos, table) {
+ if (pos->frequency < curfreq &&
+ pos->frequency > prevmaxfreq)
+ prevmaxfreq = pos->frequency;
+ }
+
+ if (prevmaxfreq != curfreq) {
+ unsigned int *setspeed = policy->governor_data;
+
+ *setspeed = prevmaxfreq;
+ ret = __cpufreq_driver_target(policy, prevmaxfreq,
+ CPUFREQ_RELATION_L);
+ } else
+ ret = 1;
+ mutex_unlock(&userspace_mutex);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(cpufreq_userspace_freq_down);
+
+int cpufreq_userspace_freq_max(int cpu)
+{
+ unsigned int maxfreq;
+ unsigned int ret = 0;
+ struct cpufreq_frequency_table *table;
+ struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
+ unsigned int *setspeed = policy->governor_data;
+
+
+ if (!policy)
+ return -EINVAL;
+
+ if (!cpufreq_is_userspace_governor(cpu)) {
+ cpufreq_cpu_put(policy);
+ return -EINVAL;
+ }
+
+ cpufreq_cpu_put(policy);
+
+ mutex_lock(&userspace_mutex);
+ table = policy->freq_table;
+ if (!table) {
+ mutex_unlock(&userspace_mutex);
+ return -ENODEV;
+ }
+ maxfreq = cpufreq_quick_get_max(cpu);
+
+ *setspeed = maxfreq;
+ ret = __cpufreq_driver_target(policy, maxfreq, CPUFREQ_RELATION_L);
+ mutex_unlock(&userspace_mutex);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(cpufreq_userspace_freq_max);
+
+int cpufreq_userspace_freq_min(int cpu)
+{
+ unsigned int minfreq;
+ unsigned int ret = 0;
+ struct cpufreq_frequency_table *table;
+ struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
+ unsigned int *setspeed = policy->governor_data;
+
+ if (!policy)
+ return -EINVAL;
+
+ if (!cpufreq_is_userspace_governor(cpu)) {
+ cpufreq_cpu_put(policy);
+ return -EINVAL;
+ }
+ minfreq = policy->min;
+
+ cpufreq_cpu_put(policy);
+
+ mutex_lock(&userspace_mutex);
+ table = policy->freq_table;
+ if (!table) {
+ mutex_unlock(&userspace_mutex);
+ return -ENODEV;
+ }
+
+ *setspeed = minfreq;
+ ret = __cpufreq_driver_target(policy, minfreq, CPUFREQ_RELATION_L);
+ mutex_unlock(&userspace_mutex);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(cpufreq_userspace_freq_min);
+
static struct cpufreq_governor cpufreq_gov_userspace = {
.name = "userspace",
.init = cpufreq_userspace_policy_init,
Index: kvm-pvfreq/include/linux/cpufreq.h
===================================================================
--- kvm-pvfreq.orig/include/linux/cpufreq.h 2017-01-31 10:41:54.102575877 -0200
+++ kvm-pvfreq/include/linux/cpufreq.h 2017-01-31 14:20:00.508613672 -0200
@@ -890,4 +890,11 @@
int cpufreq_generic_init(struct cpufreq_policy *policy,
struct cpufreq_frequency_table *table,
unsigned int transition_latency);
+#ifdef CONFIG_CPU_FREQ
+int cpufreq_userspace_freq_down(int cpu);
+int cpufreq_userspace_freq_up(int cpu);
+int cpufreq_userspace_freq_max(int cpu);
+int cpufreq_userspace_freq_min(int cpu);
+#else
+#endif
#endif /* _LINUX_CPUFREQ_H */
next prev parent reply other threads:[~2017-02-02 17:58 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-02-02 17:47 [patch 0/3] KVM CPU frequency change hypercalls Marcelo Tosatti
2017-02-02 17:47 ` Marcelo Tosatti [this message]
2017-02-03 4:09 ` [patch 1/3] cpufreq: implement min/max/up/down functions Viresh Kumar
2017-02-02 17:47 ` [patch 2/3] KVM: x86: introduce ioctl to allow frequency hypercalls Marcelo Tosatti
2017-02-03 17:03 ` Radim Krcmar
2017-02-22 21:18 ` Marcelo Tosatti
2017-02-23 16:48 ` Radim Krcmar
2017-02-23 17:31 ` Paolo Bonzini
2017-02-02 17:47 ` [patch 3/3] KVM: x86: frequency change hypercalls Marcelo Tosatti
2017-02-02 18:01 ` Marcelo Tosatti
2017-02-03 17:40 ` Radim Krcmar
2017-02-03 18:24 ` Marcelo Tosatti
2017-02-03 19:28 ` Radim Krcmar
2017-02-03 12:50 ` [patch 0/3] KVM CPU " Rafael J. Wysocki
2017-02-03 16:43 ` Radim Krcmar
2017-02-03 18:14 ` Marcelo Tosatti
2017-02-03 19:09 ` Radim Krcmar
2017-02-23 17:35 ` Paolo Bonzini
2017-02-23 23:19 ` Marcelo Tosatti
2017-02-24 9:18 ` Paolo Bonzini
2017-02-24 11:50 ` Marcelo Tosatti
2017-02-24 12:17 ` Paolo Bonzini
2017-02-24 13:04 ` Marcelo Tosatti
2017-02-24 15:34 ` Paolo Bonzini
2017-02-24 16:54 ` Rafael J. Wysocki
2017-02-28 2:45 ` Marcelo Tosatti
2017-03-01 14:21 ` Paolo Bonzini
2017-03-01 15:11 ` Marcelo Tosatti
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170202174921.814002151@redhat.com \
--to=mtosatti@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=rjw@rjwysocki.net \
--cc=rkrcmar@redhat.com \
--cc=viresh.kumar@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).