[libvirt] [PATCH v2 09/11] Adding memtune command to virsh tool

Nikunj A. Dadhania nikunj at linux.vnet.ibm.com
Thu Sep 23 13:17:56 UTC 2010


From: Nikunj A. Dadhania <nikunj at linux.vnet.ibm.com>

The command helps to control the memory/swap parameters for the system, for
eg. hard_limit (max memory the vm can use), soft_limit (limit during memory
contention), swap_hard_limit(max swap the vm can use)

v2:
+ Use #define string constants for "hard_limit", etc

Signed-off-by: Nikunj A. Dadhania <nikunj at linux.vnet.ibm.com>
---
 src/libvirt_public.syms |    5 +++
 tools/virsh.c           |   89 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 94 insertions(+), 0 deletions(-)

diff --git a/src/libvirt_public.syms b/src/libvirt_public.syms
index 849c163..f33f14a 100644
--- a/src/libvirt_public.syms
+++ b/src/libvirt_public.syms
@@ -405,4 +405,9 @@ LIBVIRT_0.8.2 {
         virDomainCreateWithFlags;
 } LIBVIRT_0.8.1;
 
+LIBVIRT_0.8.5 {
+    global:
+        virDomainSetMemoryParameters;
+} LIBVIRT_0.8.2;
+
 # .... define new API here using predicted next version number ....
diff --git a/tools/virsh.c b/tools/virsh.c
index 85014f2..585e278 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -2614,6 +2614,94 @@ cmdSetmaxmem(vshControl *ctl, const vshCmd *cmd)
 }
 
 /*
+ * "memtune" command
+ */
+static const vshCmdInfo info_memtune[] = {
+    {"help", N_("change memory paramters")},
+    {"desc", N_("Change the current memory paramters for the guest domain.")},
+    {NULL, NULL}
+};
+
+static const vshCmdOptDef opts_memtune[] = {
+    {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
+    {VIR_DOMAIN_MEMORY_HARD_LIMIT, VSH_OT_STRING, VSH_OFLAG_NONE, N_("Max memory in kilobytes")},
+    {VIR_DOMAIN_MEMORY_SOFT_LIMIT, VSH_OT_STRING, VSH_OFLAG_NONE, N_("Memory during contention in kilobytes")},
+    {VIR_DOMAIN_SWAP_HARD_LIMIT, VSH_OT_STRING, VSH_OFLAG_NONE, N_("Max swap in kilobytes")},
+    {NULL, 0, 0, NULL}
+};
+
+static int
+cmdMemtune(vshControl *ctl, const vshCmd *cmd)
+{
+    virDomainPtr dom;
+    int hard_limit, soft_limit, swap_hard_limit;
+    unsigned short nparams = 0;
+    unsigned int i = 0;
+    virMemoryParameterPtr params = NULL, temp = NULL;
+    int ret = FALSE;
+    
+    if (!vshConnectionUsability(ctl, ctl->conn))
+        return FALSE;
+
+    if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
+        return FALSE;
+
+    hard_limit = vshCommandOptInt(cmd, VIR_DOMAIN_MEMORY_HARD_LIMIT, &hard_limit);
+    if (hard_limit)
+        nparams++;
+
+    soft_limit = vshCommandOptInt(cmd, VIR_DOMAIN_MEMORY_SOFT_LIMIT, &soft_limit);
+    if (soft_limit) 
+        nparams++;
+
+    swap_hard_limit = vshCommandOptInt(cmd, VIR_DOMAIN_SWAP_HARD_LIMIT, &swap_hard_limit);
+    if (swap_hard_limit)
+        nparams++;
+
+    if(nparams == 0) {
+        /* get the memory parameters */
+        vshError(ctl, _("virDomainGetMemoryParameters not implemented"));
+    } else {
+        /* set the memory parameters */
+        params = vshMalloc(ctl, sizeof(virMemoryParameter)* nparams);
+        
+        memset(params, 0, sizeof(virMemoryParameter)* nparams);
+        for(i = 0; i < nparams; i++)
+        {
+            temp = &params[i];
+            temp->type = VIR_DOMAIN_SCHED_FIELD_ULLONG;
+
+            /* 
+             * Some magic here, this is used to fill the params structure with
+             * the valid arguments passed, after filling the particular
+             * argument we purposely make them 0, so on the next pass it goes
+             * to the next valid argument and so on.
+             */
+            if (soft_limit) {
+                temp->value.ul = soft_limit;
+                strncpy(temp->field, VIR_DOMAIN_MEMORY_SOFT_LIMIT, sizeof(temp->field));
+                soft_limit = 0;
+            } else if (hard_limit) {
+                temp->value.ul = hard_limit;
+                strncpy(temp->field, VIR_DOMAIN_MEMORY_HARD_LIMIT, sizeof(temp->field));
+                hard_limit = 0;
+            } else if (swap_hard_limit) {
+                temp->value.ul = swap_hard_limit;
+                strncpy(temp->field, VIR_DOMAIN_SWAP_HARD_LIMIT, sizeof(temp->field));
+                swap_hard_limit = 0;
+            } 
+        }
+        if (virDomainSetMemoryParameters(dom, params, nparams) != 0) 
+            vshError(ctl, "%s", _("Unable to change Memory Parameters"));
+        else 
+            ret = TRUE;
+    }
+
+    virDomainFree(dom);
+    return ret;
+}
+
+/*
  * "nodeinfo" command
  */
 static const vshCmdInfo info_nodeinfo[] = {
@@ -9431,6 +9519,7 @@ static const vshCmdDef commands[] = {
     {"shutdown", cmdShutdown, opts_shutdown, info_shutdown},
     {"setmem", cmdSetmem, opts_setmem, info_setmem},
     {"setmaxmem", cmdSetmaxmem, opts_setmaxmem, info_setmaxmem},
+    {"memtune", cmdMemtune, opts_memtune, info_memtune},
     {"setvcpus", cmdSetvcpus, opts_setvcpus, info_setvcpus},
     {"suspend", cmdSuspend, opts_suspend, info_suspend},
     {"ttyconsole", cmdTTYConsole, opts_ttyconsole, info_ttyconsole},




More information about the libvir-list mailing list