[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH v4 11/13] Implement driver interface domainGetMemoryParamters for LXC



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

v4:
* prototype change: add unsigned int flags

Driver interface for getting memory parameters, eg. hard_limit, soft_limit and
swap_hard_limit.

Signed-off-by: Nikunj A. Dadhania <nikunj linux vnet ibm com>
---
 src/lxc/lxc_driver.c |  114 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 113 insertions(+), 1 deletions(-)

diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index 984a5fa..036dedf 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -766,6 +766,118 @@ cleanup:
     return ret;
 }
 
+static int lxcDomainGetMemoryParameters(virDomainPtr dom,
+                                        virMemoryParameterPtr params,
+                                        int *nparams, 
+                                        unsigned int flags ATTRIBUTE_UNUSED)
+{
+    lxc_driver_t *driver = dom->conn->privateData;
+    int i;
+    virCgroupPtr cgroup = NULL;
+    virDomainObjPtr vm = NULL;
+    unsigned long val;
+    int ret = -1;
+    int rc;
+
+    lxcDriverLock(driver);
+    vm = virDomainFindByUUID(&driver->domains, dom->uuid);
+
+    if (vm == NULL) {
+        char uuidstr[VIR_UUID_STRING_BUFLEN];
+        virUUIDFormat(dom->uuid, uuidstr);
+        lxcError(VIR_ERR_NO_DOMAIN,
+                 _("No domain with matching uuid '%s'"), uuidstr);
+        goto cleanup;
+    }
+
+    if ((*nparams) == 0) {
+        /* Current number of memory parameters supported by cgroups is 3
+         * FIXME: Magic number, need to see where should this go
+         */
+        *nparams = 3;
+        ret = 0;
+        goto cleanup;
+    }
+    if ((*nparams) != 3) {
+        lxcError(VIR_ERR_INVALID_ARG,
+                 "%s", _("Invalid parameter count"));
+        goto cleanup;
+    }
+
+    if (virCgroupForDomain(driver->cgroup, vm->def->name, &cgroup, 0) != 0) {
+        lxcError(VIR_ERR_INTERNAL_ERROR,
+                 _("Unable to get cgroup for %s"), vm->def->name);
+        goto cleanup;
+    }
+
+    for (i = 0; i < *nparams; i++) {
+        virMemoryParameterPtr param = &params[i];
+        val = 0;
+        param->value.ul = 0;
+        param->type = VIR_DOMAIN_MEMORY_FIELD_ULLONG;
+
+        switch(i) {
+        case 0: /* fill memory hard limit here */
+            rc = virCgroupGetMemoryHardLimit(cgroup, &val);
+            if (rc != 0) {
+                virReportSystemError(-rc, "%s",
+                                     _("unable to get memory hard limit"));
+                continue;
+            }
+            if (virStrcpyStatic(param->field, VIR_DOMAIN_MEMORY_HARD_LIMIT) == NULL) {
+                lxcError(VIR_ERR_INTERNAL_ERROR,
+                         "%s", _("Field memory hard limit too long for destination"));
+                continue;
+            }
+            param->value.ul = val;
+            break;
+
+        case 1: /* fill memory soft limit here */
+            rc = virCgroupGetMemorySoftLimit(cgroup, &val);
+            if (rc != 0) {
+                virReportSystemError(-rc, "%s",
+                                     _("unable to get memory soft limit"));
+                continue;
+            }
+            if (virStrcpyStatic(param->field, VIR_DOMAIN_MEMORY_SOFT_LIMIT) == NULL) {
+                lxcError(VIR_ERR_INTERNAL_ERROR,
+                         "%s", _("Field memory soft limit too long for destination"));
+                continue;
+            }
+            param->value.ul = val;
+            break;
+           
+        case 2: /* fill swap hard limit here */
+            rc = virCgroupGetSwapHardLimit(cgroup, &val);
+            if (rc != 0) {
+                virReportSystemError(-rc, "%s",
+                                     _("unable to get swap hard limit"));
+                continue;
+            }
+            if (virStrcpyStatic(param->field, VIR_DOMAIN_SWAP_HARD_LIMIT) == NULL) {
+                lxcError(VIR_ERR_INTERNAL_ERROR,
+                         "%s", _("Field swap hard limit too long for destination"));
+                continue;
+            }
+            param->value.ul = val;
+            break;
+            
+        default:
+            break;
+            /* should not hit here */
+        }
+    }
+    ret = 0;
+
+cleanup:
+    if (cgroup)
+        virCgroupFree(&cgroup);
+    if (vm)
+        virDomainObjUnlock(vm);
+    lxcDriverUnlock(driver);
+    return ret;
+}
+
 static char *lxcDomainDumpXML(virDomainPtr dom,
                               int flags)
 {
@@ -2710,7 +2822,7 @@ static virDriver lxcDriver = {
     NULL, /* domainSnapshotDelete */
     NULL, /* qemuDomainMonitorCommand */
     lxcDomainSetMemoryParameters, /* domainSetMemoryParameters */
-    NULL, /* domainGetMemoryParameters */
+    lxcDomainGetMemoryParameters, /* domainGetMemoryParameters */
 };
 
 static virStateDriver lxcStateDriver = {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]