[libvirt] [PATCH 3/6] virNodeGetMemoryStats: Implement public API

Minoru Usui usui at mxm.nes.nec.co.jp
Wed May 25 08:59:09 UTC 2011


virNodeGetMemoryStats: Implement public API

Signed-off-by: Minoru Usui <usui at mxm.nes.nec.co.jp>
---
 src/libvirt.c |   81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 81 insertions(+), 0 deletions(-)

diff --git a/src/libvirt.c b/src/libvirt.c
index ff16c48..2205ebb 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -4967,6 +4967,87 @@ error:
 }
 
 /**
+ * virNodeGetMemoryStats:
+ * @conn: pointer to the hypervisor connection.
+ * @params: pointer to node memory stats objects
+ * @nparams: number of node memory stats (this value should be same or
+ *          less than the number of stats supported)
+ * @flags: currently unused, for future extension. always pass 0.
+ *
+ * This function provides memory stats of the node.
+ * The @params array will be filled with the values equal to the number of
+ * stats suggested by @nparams
+ *
+ * As the value of @nparams is dynamic, call the API setting @nparams to 0 and
+ * @params as NULL, the API returns the number of parameters supported by the
+ * HV by updating @nparams on SUCCESS. The caller should then allocate @params
+ * array, i.e. (sizeof(@virMemoryStats) * @nparams) bytes and call
+ * the API again.
+ *
+ * Here is the sample code snippet:
+ *
+ * if ((virNodeGetMemoryStats(conn, NULL, &nparams, 0) == 0) &&
+ *     (nparams != 0)) {
+ *     params = vshMalloc(ctl, sizeof(virMemoryStats) * nparams);
+ *     memset(params, 0, sizeof(virMemoryStats) * nparams);
+ *     if (virNodeGetMemoryStats(conn, params, &nparams, 0)) {
+ *         vshError(ctl, "%s", _("Unable to get node memory stats"));
+ *         goto error;
+ *     }
+ * }
+ *
+ * This function doesn't requires privileged access to the hypervisor.
+ * This function expects the caller to allocate the @params.
+ *
+ * Memory Stats:
+ *
+ * VIR_MEMORY_STATS_TOTAL:
+ *     The total memory usage.(KB)
+ * VIR_MEMORY_STATS_FREE:
+ *     The free memory usage.(KB)
+ *     On linux, this usage includes buffers and cached.
+ * VIR_MEMORY_STATS_BUFFERS:
+ *     The buffers memory usage.(KB)
+ * VIR_MEMORY_STATS_CACHED:
+ *     The cached memory usage.(KB)
+ *
+ * Returns -1 in case of error, 0 in case of success.
+ */
+int virNodeGetMemoryStats (virConnectPtr conn,
+                           virMemoryStatsPtr params,
+                           int *nparams, unsigned int flags)
+{
+    VIR_DEBUG("conn=%p, params=%p, nparams=%d, flags=%u",
+              conn, params, nparams ? *nparams : -1, flags);
+
+    virResetLastError();
+
+    if (!VIR_IS_CONNECT(conn)) {
+        virLibConnError(VIR_ERR_INVALID_CONN, __FUNCTION__);
+        virDispatchError(NULL);
+        return -1;
+    }
+
+    if ((nparams == NULL) || (*nparams < 0)) {
+        virLibConnError(VIR_ERR_INVALID_ARG, __FUNCTION__);
+        goto error;
+    }
+
+    if (conn->driver->nodeGetMemoryStats) {
+        int ret;
+        ret = conn->driver->nodeGetMemoryStats (conn, params, nparams, flags);
+        if (ret < 0)
+            goto error;
+        return ret;
+    }
+    virLibConnError(VIR_ERR_NO_SUPPORT, __FUNCTION__);
+
+error:
+    virDispatchError(conn);
+    return -1;
+}
+
+/**
  * virNodeGetFreeMemory:
  * @conn: pointer to the hypervisor connection
  *
-- 
1.7.1




More information about the libvir-list mailing list