From 8bd977e9905c7add2bb55abd9fe25fc7f413ffff Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Bocahu?= <sebastien.bocahu@nuxit.com>
Date: Wed, 19 Sep 2012 15:42:04 +0200
Subject: [PATCH] Add support for cpu_cap and cpu_weight Xen params
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch adds support for Xen's CPU scheduler 'cpu_cap' and
'cpu_weight' parameters.

Ganeti default values (cap: 0=unlimited, weight: 256) are Xen defaults.

cpu_cap is not validated correctly because of actual Ganeti limitation
on checks. I'd need check methods to get beparams or vcpus to get moved
to hvparams.

Signed-off-by: SΓ©bastien Bocahu <sebastien.bocahu@nuxit.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>
---
 lib/constants.py         |  9 +++++++++
 lib/hypervisor/hv_xen.py | 18 ++++++++++++++++++
 man/gnt-instance.rst     | 12 ++++++++++++
 3 files changed, 39 insertions(+)

diff --git a/lib/constants.py b/lib/constants.py
index 86b4fd651..9738033bb 100644
--- a/lib/constants.py
+++ b/lib/constants.py
@@ -704,6 +704,9 @@ HV_PASSTHROUGH = "pci_pass"
 HV_BLOCKDEV_PREFIX = "blockdev_prefix"
 HV_REBOOT_BEHAVIOR = "reboot_behavior"
 HV_CPU_TYPE = "cpu_type"
+HV_CPU_CAP = "cpu_cap"
+HV_CPU_WEIGHT = "cpu_weight"
+
 
 HVS_PARAMETER_TYPES = {
   HV_BOOT_ORDER: VTYPE_STRING,
@@ -760,6 +763,8 @@ HVS_PARAMETER_TYPES = {
   HV_BLOCKDEV_PREFIX: VTYPE_STRING,
   HV_REBOOT_BEHAVIOR: VTYPE_STRING,
   HV_CPU_TYPE: VTYPE_STRING,
+  HV_CPU_CAP: VTYPE_INT,
+  HV_CPU_WEIGHT: VTYPE_INT,
   }
 
 HVS_PARAMETERS = frozenset(HVS_PARAMETER_TYPES.keys())
@@ -1701,6 +1706,8 @@ HVC_DEFAULTS = {
     HV_BLOCKDEV_PREFIX: "sd",
     HV_REBOOT_BEHAVIOR: INSTANCE_REBOOT_ALLOWED,
     HV_CPU_MASK: CPU_PINNING_ALL,
+    HV_CPU_CAP: 0,
+    HV_CPU_WEIGHT: 256,
     },
   HT_XEN_HVM: {
     HV_BOOT_ORDER: "cd",
@@ -1720,6 +1727,8 @@ HVC_DEFAULTS = {
     HV_PASSTHROUGH: "",
     HV_REBOOT_BEHAVIOR: INSTANCE_REBOOT_ALLOWED,
     HV_CPU_MASK: CPU_PINNING_ALL,
+    HV_CPU_CAP: 0,
+    HV_CPU_WEIGHT: 256,
     },
   HT_KVM: {
     HV_KERNEL_PATH: KVM_KERNEL,
diff --git a/lib/hypervisor/hv_xen.py b/lib/hypervisor/hv_xen.py
index 0f3daec25..57dcc20d9 100644
--- a/lib/hypervisor/hv_xen.py
+++ b/lib/hypervisor/hv_xen.py
@@ -630,6 +630,9 @@ class XenPvmHypervisor(XenHypervisor):
     constants.HV_REBOOT_BEHAVIOR:
       hv_base.ParamInSet(True, constants.REBOOT_BEHAVIORS),
     constants.HV_CPU_MASK: hv_base.OPT_MULTI_CPU_MASK_CHECK,
+    constants.HV_CPU_CAP: hv_base.NO_CHECK,
+    constants.HV_CPU_WEIGHT:
+      (False, lambda x: 0 < x < 65536, "invalid weight", None, None),
     }
 
   @classmethod
@@ -672,6 +675,12 @@ class XenPvmHypervisor(XenHypervisor):
     cpu_pinning = cls._CreateConfigCpus(hvp[constants.HV_CPU_MASK])
     if cpu_pinning:
       config.write("%s\n" % cpu_pinning)
+    cpu_cap = hvp[constants.HV_CPU_CAP]
+    if cpu_cap:
+      config.write("cpu_cap=%d\n" % cpu_cap)
+    cpu_weight = hvp[constants.HV_CPU_WEIGHT]
+    if cpu_weight:
+      config.write("cpu_weight=%d\n" % cpu_weight)
 
     config.write("name = '%s'\n" % instance.name)
 
@@ -743,6 +752,9 @@ class XenHvmHypervisor(XenHypervisor):
     constants.HV_REBOOT_BEHAVIOR:
       hv_base.ParamInSet(True, constants.REBOOT_BEHAVIORS),
     constants.HV_CPU_MASK: hv_base.OPT_MULTI_CPU_MASK_CHECK,
+    constants.HV_CPU_CAP: hv_base.NO_CHECK,
+    constants.HV_CPU_WEIGHT:
+      (False, lambda x: 0 < x < 65535, "invalid weight", None, None),
     }
 
   @classmethod
@@ -766,6 +778,12 @@ class XenHvmHypervisor(XenHypervisor):
     cpu_pinning = cls._CreateConfigCpus(hvp[constants.HV_CPU_MASK])
     if cpu_pinning:
       config.write("%s\n" % cpu_pinning)
+    cpu_cap = hvp[constants.HV_CPU_CAP]
+    if cpu_cap:
+      config.write("cpu_cap=%d\n" % cpu_cap)
+    cpu_weight = hvp[constants.HV_CPU_WEIGHT]
+    if cpu_weight:
+      config.write("cpu_weight=%d\n" % cpu_weight)
 
     config.write("name = '%s'\n" % instance.name)
     if hvp[constants.HV_PAE]:
diff --git a/man/gnt-instance.rst b/man/gnt-instance.rst
index 7e2bda2dc..acedb5b26 100644
--- a/man/gnt-instance.rst
+++ b/man/gnt-instance.rst
@@ -587,6 +587,18 @@ cpu\_mask
       # Turn off CPU pinning (default setting)
       gnt-instance modify -H cpu_mask=all my-inst
 
+cpu\_cap
+    Valid for the Xen hypervisor.
+
+    Set the maximum amount of cpu usage by the VM. The value is a percentage
+    between 0 and (100 * number of VCPUs). Default cap is 0: unlimited.
+
+cpu\_weight
+    Valid for the Xen hypervisor.
+
+    Set the cpu time ratio to be allocated to the VM. Valid values are
+    between 1 and 65535. Default weight is 256.
+
 usb\_mouse
     Valid for the KVM hypervisor.
 
-- 
GitLab