Skip to contentSkip to conversation

Add guest cpu entitlement reporting

RFC PATCH In review Michael Wolf 2 participants 4 messages Aug 23, 2012 – Aug 25, 2012

lore
Share & tools
lore
#0 lore raw

cover Add guest cpu entitlement reporting

User can be confused by stealtime when running a guest in either a capped or overcommitted cpu environment. The user has concerns that they are not receiving their full cpu allotment when steal time shows up in the accounting tools. So add a cpu entitlement. This entitlement will be used to compute the amount of steal time display by the accounting tools.

Michael Wolf (2):
The cpu entitlement value will be passed to qemu as part of the cpu parameters.

Add an ioctl to pass the entitlement information from qemu to the hypervisor.

---
 cpu-defs.h                |    1 +
 cpus.c                    |    1 +
 cpus.h                    |    1 +
 kvm-all.c                 |    1 +
 linux-headers/linux/kvm.h |    2 ++
 qemu-options.hx           |    7 +++++--
 vl.c                      |   22 +++++++++++++++++++++-
 7 files changed, 32 insertions(+), 3 deletions(-)
#1 lore raw

RFC PATCH 1/2 Parse the cpu entitlement from the qemu commandline.

The cpu entitlement value will be passed to qemu as part of the cpu parameters. Add cpu_parse to read this value from the commandline.

---
 qemu-options.hx |    7 +++++--
 vl.c            |   23 ++++++++++++++++++++++-
 2 files changed, 27 insertions(+), 3 deletions(-)
qemu-options.hx +5 -2
index 3c411c4..d13aa24 100644
@@ -64,9 +64,12 @@ HXCOMM Deprecated by -machine
 DEF("M", HAS_ARG, QEMU_OPTION_M, "", QEMU_ARCH_ALL)
 
 DEF("cpu", HAS_ARG, QEMU_OPTION_cpu,
-    "-cpu cpu        select CPU (-cpu ? for list)\n", QEMU_ARCH_ALL)
+    "-cpu cpu[,entitlement=cpu use entitlement %]\n"
+    "select CPU (-cpu ? for list)\n"
+    "entitlement= percentage of cpu that the guest can expect to utilize\n",
+    QEMU_ARCH_ALL)
 STEXI
-@item -cpu @var{model}
+@item -cpu @var{model}[,entitlement=@var{entitlement}]
 @findex -cpu
 Select CPU model (-cpu ? for list and additional feature selection)
 ETEXI
vl.c +22 -1
index 7c577fa..8f0c12a 100644
@@ -205,6 +205,8 @@ CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
 int win2k_install_hack = 0;
 int usb_enabled = 0;
 int singlestep = 0;
+const char *cpu_model;
+int cpu_entitlement = 100;
 int smp_cpus = 1;
 int max_cpus = 0;
 int smp_cores = 1;
@@ -1026,6 +1028,25 @@ static void numa_add(const char *optarg)
     return;
 }
 
+static void cpu_parse(const char *optarg)
+{
+    char option[128];
+    char *endptr;
+
+    endptr = (char *) get_opt_name(option, 128, optarg, ',');
+    *endptr = '\0';
+    endptr++;
+    if (get_param_value(option, 128, "entitlement", endptr) != 0) {
+        cpu_entitlement = strtoull(option, NULL, 10);
+    }
+    /* Make sure that the entitlement is within 1 - 100 */
+    if (cpu_entitlement < 1 || cpu_entitlement > 100) {
+        fprintf(stderr, "cpu_entitlement=%d is invalid. "
+                        "Valid range is 1 - 100\n", cpu_entitlement);
+        cpu_entitlement = 100;
+    }
+}
+
 static void smp_parse(const char *optarg)
 {
     int smp, sockets = 0, threads = 0, cores = 0;
@@ -2359,7 +2380,6 @@ int main(int argc, char **argv, char **envp)
     const char *optarg;
     const char *loadvm = NULL;
     QEMUMachine *machine;
-    const char *cpu_model;
     const char *vga_model = "none";
     const char *pid_file = NULL;
     const char *incoming = NULL;
@@ -2472,6 +2492,7 @@ int main(int argc, char **argv, char **envp)
                 break;
             case QEMU_OPTION_cpu:
                 /* hw initialization will check this */
+                cpu_parse(optarg);
                 cpu_model = optarg;
                 break;
             case QEMU_OPTION_hda:
#2 lore raw

Re: [Qemu-devel] [PATCH RFC 1/2] Parse the cpu entitlement from the qemu commandline.

Quoted 33 lines · Michael Wolf

The cpu entitlement value will be passed to qemu as part of the cpu parameters. Add cpu_parse to read this value from the commandline.

---
 qemu-options.hx |    7 +++++--
 vl.c            |   23 ++++++++++++++++++++++-
 2 files changed, 27 insertions(+), 3 deletions(-)
qemu-options.hx +5 -2
index 3c411c4..d13aa24 100644
@@ -64,9 +64,12 @@ HXCOMM Deprecated by -machine
 DEF("M", HAS_ARG, QEMU_OPTION_M, "", QEMU_ARCH_ALL)
 
 DEF("cpu", HAS_ARG, QEMU_OPTION_cpu,
-    "-cpu cpu        select CPU (-cpu ? for list)\n", QEMU_ARCH_ALL)
+    "-cpu cpu[,entitlement=cpu use entitlement %]\n"
+    "select CPU (-cpu ? for list)\n"
+    "entitlement= percentage of cpu that the guest can expect to utilize\n",
+    QEMU_ARCH_ALL)
 STEXI
-@item -cpu @var{model}
+@item -cpu @var{model}[,entitlement=@var{entitlement}]
 @findex -cpu
 Select CPU model (-cpu ? for list and additional feature selection)
 ETEXI
vl.c +0 -0
index 7c577fa..8f0c12a 100644

vl.c CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES]; · line 209

Quoted 5 lines
vl.c +2 -0
@@ -205,6 +205,8 @@ CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
 int win2k_install_hack = 0;
 int usb_enabled = 0;
 int singlestep = 0;
+const char *cpu_model;
+int cpu_entitlement = 100;

Missing 'static' for the above. I'd merge this patch with the other patch which uses the variable.

Quoted 7 lines
vl.c +0 -0
 int smp_cpus = 1;
 int max_cpus = 0;
 int smp_cores = 1;
@@ -1026,6 +1028,25 @@ static void numa_add(const char *optarg)
     return;
 }
 

vl.c static void numa_add(const char *optarg) · line 1040

Quoted 10 lines
vl.c +10 -0
+static void cpu_parse(const char *optarg)
+{
+    char option[128];
+    char *endptr;
+
+    endptr = (char *) get_opt_name(option, 128, optarg, ',');
+    *endptr = '\0';
+    endptr++;
+    if (get_param_value(option, 128, "entitlement", endptr) != 0) {
+        cpu_entitlement = strtoull(option, NULL, 10);

strtoul() should be enough.

vl.c static void numa_add(const char *optarg) · line 1045

Quoted 5 lines
vl.c +5 -0
+    }
+    /* Make sure that the entitlement is within 1 - 100 */
+    if (cpu_entitlement < 1 || cpu_entitlement > 100) {
+        fprintf(stderr, "cpu_entitlement=%d is invalid. "
+                        "Valid range is 1 - 100\n", cpu_entitlement);

Exit or tell user that value of 100 is actually used.

vl.c static void numa_add(const char *optarg) · line 1047

Quoted 2 lines
vl.c +2 -0
+        cpu_entitlement = 100;
+    }

This block belongs inside the previous 'if' block, it's useless to check the value if the option hasn't been used.

Quoted 23 lines
vl.c +3 -1
+}
+
 static void smp_parse(const char *optarg)
 {
     int smp, sockets = 0, threads = 0, cores = 0;
@@ -2359,7 +2380,6 @@ int main(int argc, char **argv, char **envp)
     const char *optarg;
     const char *loadvm = NULL;
     QEMUMachine *machine;
-    const char *cpu_model;
     const char *vga_model = "none";
     const char *pid_file = NULL;
     const char *incoming = NULL;
@@ -2472,6 +2492,7 @@ int main(int argc, char **argv, char **envp)
                 break;
             case QEMU_OPTION_cpu:
                 /* hw initialization will check this */
+                cpu_parse(optarg);
                 cpu_model = optarg;
                 break;
             case QEMU_OPTION_hda:
#3 lore raw

RFC PATCH 2/2 Add an ioctl to pass the entitlement information from qemu to the hypervisor.

In reply to Michael Wolf

Add KVM_SET_ENTITLEMENT ioctl to pass the entitlement to the hypervisor.

---
 cpu-defs.h                |    1 +
 cpus.c                    |    1 +
 cpus.h                    |    1 +
 kvm-all.c                 |    1 +
 linux-headers/linux/kvm.h |    2 ++
 5 files changed, 6 insertions(+)
cpu-defs.h +1 -0
index 4018b88..ae24e8e 100644
@@ -200,6 +200,7 @@ typedef struct CPUWatchpoint {
     int numa_node; /* NUMA node this cpu is belonging to  */            \
     int nr_cores;  /* number of cores within this CPU package */        \
     int nr_threads;/* number of threads within this CPU */              \
+    int cpu_entitlement; /* the % of the pcpu that the vcpu receives */ \
     int running; /* Nonzero if cpu is currently running(usermode).  */  \
     int thread_id;                                                      \
     /* user data */                                                     \
cpus.c +1 -0
index e476a3c..ef8b762 100644
@@ -1042,6 +1042,7 @@ void qemu_init_vcpu(void *_env)
 
     env->nr_cores = smp_cores;
     env->nr_threads = smp_threads;
+    env->cpu_entitlement = cpu_entitlement;
     env->stopped = 1;
     if (kvm_enabled()) {
         qemu_kvm_start_vcpu(env);
cpus.h +1 -0
index 81bd817..704d6df 100644
@@ -16,6 +16,7 @@ void qtest_clock_warp(int64_t dest);
 /* vl.c */
 extern int smp_cores;
 extern int smp_threads;
+extern int cpu_entitlement;
 void set_numa_modes(void);
 void set_cpu_log(const char *optarg);
 void set_cpu_log_filename(const char *optarg);
kvm-all.c +1 -0
index 34b02c1..dee1b9b 100644
@@ -246,6 +246,7 @@ int kvm_init_vcpu(CPUArchState *env)
         s->coalesced_mmio_ring =
             (void *)env->kvm_run + s->coalesced_mmio * PAGE_SIZE;
     }
+    kvm_vm_ioctl(s, KVM_SET_ENTITLEMENT, env->cpu_entitlement);
 
     ret = kvm_arch_init_vcpu(env);
     if (ret == 0) {
linux-headers/linux/kvm.h +2 -0
index 5a9d4e3..46dbde5 100644
@@ -901,6 +901,8 @@ struct kvm_s390_ucas_mapping {
 #define KVM_SET_ONE_REG		  _IOW(KVMIO,  0xac, struct kvm_one_reg)
 /* VM is being stopped by host */
 #define KVM_KVMCLOCK_CTRL	  _IO(KVMIO,   0xad)
+/* Set the cpu entitlement this will be used to adjust stealtime reporting */
+#define KVM_SET_ENTITLEMENT       _IOW(KVMIO, 0xae, unsigned long)
 
 #define KVM_DEV_ASSIGN_ENABLE_IOMMU	(1 << 0)
 #define KVM_DEV_ASSIGN_PCI_2_3		(1 << 1)