[libvirt] [PATCH] php: implement libvirt_node_get_free_memory.

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

[meta-comment] On 05/01/2014 12:33 PM, Dawid Zamirski wrote:
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(+)
I'm not a PHP expert, so I'll let others review the patch itself. But when contributing patches to libvirt-php.git, it helps if you do: git config format.subjectprefix "php PATCH" to make it obvious which repo you are targetting with your patch. -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

On Thu, 2014-05-01 at 12:55 -0600, Eric Blake wrote:
[meta-comment]
I'm not a PHP expert, so I'll let others review the patch itself. But when contributing patches to libvirt-php.git, it helps if you do: git config format.subjectprefix "php PATCH" to make it obvious which repo you are targetting with your patch.
Hi Eric, First, I apologize for sending incorrectly formatted patch - I'm still learning on how to submit patches to mailing lists properly. Do you want me to resubmit this patch with proper prefix? Also should I mark it as v2, if I was to do so? Thank you, Dawid

On 05/01/2014 01:05 PM, Dawid Zamirski wrote:
On Thu, 2014-05-01 at 12:55 -0600, Eric Blake wrote:
[meta-comment]
I'm not a PHP expert, so I'll let others review the patch itself. But when contributing patches to libvirt-php.git, it helps if you do: git config format.subjectprefix "php PATCH" to make it obvious which repo you are targetting with your patch.
Hi Eric,
First, I apologize for sending incorrectly formatted patch - I'm still learning on how to submit patches to mailing lists properly.
Not a problem - we all had to learn and start somewhere :) The fact that you are submitting at all is to be applauded!
Do you want me to resubmit this patch with proper prefix? Also should I mark it as v2, if I was to do so?
No need to repost unless you are changing the patch itself; anything in the [PATCH] (or by my proposal, [php PATCH]) portion of the subject line is useful in mail clients, but stripped from what goes into git. -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
participants (2)
-
Dawid Zamirski
-
Eric Blake