[libvirt] [libvirt-php PATCH 1/4] implement libvirt_node_get_free_memory

Michal Privoznik mprivozn at redhat.com
Mon Jul 4 11:35:32 UTC 2016


On 29.06.2016 18:29, Dawid Zamirski wrote:
> From: Dawid Zamirski <dzamirski at dattobackup.com>
> 
> 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 6f6e137..0e9e6b5 100644
> --- a/src/libvirt-php.c
> +++ b/src/libvirt-php.c
> @@ -664,6 +664,7 @@ static zend_function_entry libvirt_functions[] = {
>      PHP_FE(libvirt_node_get_cpu_stats,           arginfo_libvirt_conn_optcpunr)
>      PHP_FE(libvirt_node_get_cpu_stats_for_each_cpu, arginfo_libvirt_conn_opttime)
>      PHP_FE(libvirt_node_get_mem_stats,           arginfo_libvirt_conn)
> +    PHP_FE(libvirt_node_get_free_memory,         arginfo_libvirt_conn)
>      /* Nodedev functions */
>      PHP_FE(libvirt_nodedev_get,                  arginfo_libvirt_conn)
>      PHP_FE(libvirt_nodedev_capabilities,         arginfo_libvirt_conn)
> @@ -2089,6 +2090,11 @@ else \
>      add_index_long(out, key,in); \
>  }
>  
> +#define LONGLONG_RETURN_AS_STRING(in) \
> +    snprintf(tmpnumber, 63, "%llu", in); \
> +    VIRT_RETURN_STRING(tmpnumber);
> +
> +
>  /* 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)
>  {
> @@ -2578,6 +2584,30 @@ PHP_FUNCTION(libvirt_node_get_mem_stats)
>      params = NULL;
>  }
>  
> +/*
> + * Function name:   libvirt_node_get_free_memory
> + * Since version:   0.5.3
> + * 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);

Interesting approach.

> +    } 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 a8cccbe..561ccff 100644
> --- a/src/libvirt-php.h
> +++ b/src/libvirt-php.h
> @@ -366,6 +366,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);
>  /* Stream functions */
>  PHP_FUNCTION(libvirt_stream_create);
>  PHP_FUNCTION(libvirt_stream_close);
> 

ACK

Michal




More information about the libvir-list mailing list