[libvirt] [PATCH] php: implement libvirt_node_get_free_memory.

Dawid Zamirski dzamirski at dattobackup.com
Thu May 1 18:33:48 UTC 2014


This patch adds support for virNodeGetFreeMemory which is available in
libvirt since v0.3.3. While the php bindings alredy provide
libvirt_node_get_mem_stats from which such info could be obtained, not
all hypervisors support it, e.g. vbox and esx driver don't have it but
they do implement virNodeGetFreeMemory.

Since virNodeGetFreeMemory returns free bytes as unsigned long long
which PHP cannot handle, I've added LONGLONG_RETURN_AS_STRING macro
which returns the amount bytes as a string - similarly to how bcmath
handles such numbers.
---
 src/libvirt-php.c | 30 ++++++++++++++++++++++++++++++
 src/libvirt-php.h |  1 +
 2 files changed, 31 insertions(+)

diff --git a/src/libvirt-php.c b/src/libvirt-php.c
index 07ae137..94a0c6b 100644
--- a/src/libvirt-php.c
+++ b/src/libvirt-php.c
@@ -198,6 +198,7 @@ static zend_function_entry libvirt_functions[] = {
 	PHP_FE(libvirt_node_get_cpu_stats, NULL)
 	PHP_FE(libvirt_node_get_cpu_stats_for_each_cpu, NULL)
 	PHP_FE(libvirt_node_get_mem_stats, NULL)
+	PHP_FE(libvirt_node_get_free_memory, NULL)
 	/* Nodedev functions */
 	PHP_FE(libvirt_nodedev_get, NULL)
 	PHP_FE(libvirt_nodedev_capabilities, NULL)
@@ -1428,6 +1429,11 @@ str_out = estrndup(str_in, strlen(str_in)); \
            add_index_long(out, key,in); \
 	}
 
+#define LONGLONG_RETURN_AS_STRING(in) \
+	snprintf(tmpnumber, 63, "%llu", in); \
+	RETURN_STRING(tmpnumber, 1);
+
+
 /* Authentication callback function. Should receive list of credentials via cbdata and pass the requested one to libvirt */
 static int libvirt_virConnectAuthCallback(virConnectCredentialPtr cred, unsigned int ncred, void *cbdata)
 {
@@ -1889,6 +1895,30 @@ PHP_FUNCTION(libvirt_node_get_mem_stats)
 }
 #endif
 
+/*
+	Function name:	libvirt_node_get_free_memory
+	Since version:	0.5.1
+	Description:	Function is used to get free memory available on the node.
+	Arguments:	@conn [resource]: resource for connection.
+	Returns: the available free memery in bytes as string or FALSE for error.
+*/
+PHP_FUNCTION(libvirt_node_get_free_memory)
+{
+	php_libvirt_connection *conn = NULL;
+	zval *zconn;
+	unsigned long long ret;
+	LONGLONG_INIT
+
+	GET_CONNECTION_FROM_ARGS("r", &zconn);
+
+	if ((ret = virNodeGetFreeMemory(conn->conn)) != 0) {
+		LONGLONG_RETURN_AS_STRING(ret);
+	} else {
+		set_error("Cannot get the free memory for the node" TSRMLS_CC);
+		RETURN_FALSE
+	}
+}
+
 //virsh capabilities | xpath '//capabilities/guest/arch[@name="x86_64"]/machine[@maxCpus=1]'
 
 /*
diff --git a/src/libvirt-php.h b/src/libvirt-php.h
index 450fbea..737c47d 100644
--- a/src/libvirt-php.h
+++ b/src/libvirt-php.h
@@ -347,6 +347,7 @@ PHP_FUNCTION(libvirt_node_get_info);
 PHP_FUNCTION(libvirt_node_get_cpu_stats);
 PHP_FUNCTION(libvirt_node_get_cpu_stats_for_each_cpu);
 PHP_FUNCTION(libvirt_node_get_mem_stats);
+PHP_FUNCTION(libvirt_node_get_free_memory);
 /* Domain functions */
 PHP_FUNCTION(libvirt_domain_new);
 PHP_FUNCTION(libvirt_domain_new_get_vnc);
-- 
1.9.0




More information about the libvir-list mailing list