From nobody Fri May 16 04:04:17 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1501624249413332.69454874480334; Tue, 1 Aug 2017 14:50:49 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 88D3F64E94; Tue, 1 Aug 2017 21:50:47 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 62A431820A; Tue, 1 Aug 2017 21:50:47 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id D7CC3180B467; Tue, 1 Aug 2017 21:50:46 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v71LkSjV007860 for ; Tue, 1 Aug 2017 17:46:28 -0400 Received: by smtp.corp.redhat.com (Postfix) id 1158E63777; Tue, 1 Aug 2017 21:46:28 +0000 (UTC) Received: from mx1.redhat.com (ext-mx09.extmail.prod.ext.phx2.redhat.com [10.5.110.38]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 031EB4DA95 for ; Tue, 1 Aug 2017 21:46:27 +0000 (UTC) Received: from mail-qt0-f194.google.com (mail-qt0-f194.google.com [209.85.216.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 552C3CAA88 for ; Tue, 1 Aug 2017 21:46:20 +0000 (UTC) Received: by mail-qt0-f194.google.com with SMTP id p3so2910504qtg.5 for ; Tue, 01 Aug 2017 14:46:20 -0700 (PDT) Received: from dawid-fedora.datto.lan ([47.19.105.250]) by smtp.gmail.com with ESMTPSA id f56sm1892090qta.79.2017.08.01.14.46.18 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 01 Aug 2017 14:46:18 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 88D3F64E94 Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 88D3F64E94 Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ly2Dcxi5" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 552C3CAA88 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dzrudy@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 552C3CAA88 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=w9NpXS9+TmrdNHnufCbRmERmddsHjQxwEw2zeNPWueY=; b=ly2Dcxi5VyrdKWZqIdO/xe9Ci+YhfUI5P7iCo7fm/Wf3XKOh9YO+q4KoKpH2pYpxCz h0Yv5XNXflWQy7zQsrW6ZEST9zRMxNGL8sevvz3fzFa3rqaEckZ242pxxWvxPJRI4bLX 0rGLbo4zTYNzg2OXTBIyzn+v+f+ITCh6vg1tsuveoiNEyQPrZNO9EhU5GlvbN8XdktFv fBewAzn7EGk96ii11OhDtcyd9WtYP2RjYLVwyZ1iAwekgoJo1Obkm4tX+MqmmhGuTZvR DNrHFe8yeXV0jiA7Hn0dzH+Lp8DShk8h1zaHCkpzWkH09i6k5dYWIbcCw9hGKhe+hMes Aa0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=w9NpXS9+TmrdNHnufCbRmERmddsHjQxwEw2zeNPWueY=; b=BCO5tj6RjAG7ffkATsAiWf2hzNvD5rxGnP6GwOVStluOhCHeSRZG/V/SzqRUNZKkVd E6mfBX/5npyQOpnmtG8Eflo1PvkzFaJe9noQ3Yga7DqeN3y/ltPzOaWi/BlFoyOWccnY yp/DkXIJKnxc9TERpZX9OTXudAIW+5BvG2M1rvan2sX8TJHktG/zVPmlFVZQYlPTwieG SBHIW81q0RgTA3jprd/JTCArvNXf0xEj+208NZtB+T4LVMmPNXJ1rCVlSekTd88G0nSC apCY6VNF8XaRlmXXdehah4NAMNKz8jhKcbB1PldB0zqxrrYooPM5BO5NO3sGUoa5BZGB Q/wg== X-Gm-Message-State: AIVw112gYS8VAwyyte/DSH/G3lIliWXHEkEi5o1ljArZXX+ln1xdOAJ4 2QQRIuV6Uk0LweNV+hE= X-Received: by 10.200.35.23 with SMTP id a23mr28491032qta.56.1501623979102; Tue, 01 Aug 2017 14:46:19 -0700 (PDT) From: Dawid Zamirski To: libvir-list@redhat.com Date: Tue, 1 Aug 2017 17:46:06 -0400 Message-Id: <20170801214614.8915-6-dzrudy@gmail.com> In-Reply-To: <20170801214614.8915-1-dzrudy@gmail.com> References: <20170801214614.8915-1-dzrudy@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Tue, 01 Aug 2017 21:46:20 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Tue, 01 Aug 2017 21:46:20 +0000 (UTC) for IP:'209.85.216.194' DOMAIN:'mail-qt0-f194.google.com' HELO:'mail-qt0-f194.google.com' FROM:'dzrudy@gmail.com' RCPT:'' X-RedHat-Spam-Score: 0.37 (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_SPAM, SPF_PASS) 209.85.216.194 mail-qt0-f194.google.com 209.85.216.194 mail-qt0-f194.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.38 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [libvirt-php PATCH 05/13] Split up the bindings for libvirt node API X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 01 Aug 2017 21:50:48 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" --- src/Makefile.am | 3 +- src/libvirt-node.c | 304 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-node.h | 25 +++++ src/libvirt-php.c | 322 +------------------------------------------------= ---- src/libvirt-php.h | 6 - src/util.h | 25 +++++ 6 files changed, 358 insertions(+), 327 deletions(-) create mode 100644 src/libvirt-node.c create mode 100644 src/libvirt-node.h diff --git a/src/Makefile.am b/src/Makefile.am index 0819dc6..ba2be62 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -22,7 +22,8 @@ libvirt_php_la_SOURCES =3D \ vncfunc.c vncfunc.h \ sockets.c sockets.h \ libvirt-php.c libvirt-php.h \ - libvirt-connection.c libvirt-connection.h + libvirt-connection.c libvirt-connection.h \ + libvirt-node.c libvirt-node.h libvirt_php_la_CFLAGS =3D \ $(AM_CFLAGS) \ -DCOMPILE_DL_LIBVIRT=3D1 diff --git a/src/libvirt-node.c b/src/libvirt-node.c new file mode 100644 index 0000000..a6de1ac --- /dev/null +++ b/src/libvirt-node.c @@ -0,0 +1,304 @@ +/* + * libvirt-node.c: The PHP bindings to libvirt connection API + * + * See COPYING for the license of this software + */ + +#include + +#include "libvirt-node.h" +#include "libvirt-connection.h" + +DEBUG_INIT("node"); + +/* + * Function name: libvirt_node_get_info + * Since version: 0.4.1(-1) + * Description: Function is used to get the information about host nod= e, mainly total memory installed, total CPUs installed and model informatio= n are useful + * Arguments: @conn [resource]: resource for connection + * Returns: array of node information or FALSE for error + */ +PHP_FUNCTION(libvirt_node_get_info) +{ + virNodeInfo info; + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int retval; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + retval =3D virNodeGetInfo (conn->conn, &info); + DPRINTF("%s: virNodeGetInfo returned %d\n", PHPFUNC, retval); + if (retval =3D=3D -1) + RETURN_FALSE; + + array_init(return_value); + VIRT_ADD_ASSOC_STRING(return_value, "model", info.model); + add_assoc_long(return_value, "memory", (long)info.memory); + add_assoc_long(return_value, "cpus", (long)info.cpus); + add_assoc_long(return_value, "nodes", (long)info.nodes); + add_assoc_long(return_value, "sockets", (long)info.sockets); + add_assoc_long(return_value, "cores", (long)info.cores); + add_assoc_long(return_value, "threads", (long)info.threads); + add_assoc_long(return_value, "mhz", (long)info.mhz); +} + +/* + * Function name: libvirt_node_get_cpu_stats + * Since version: 0.4.6 + * Description: Function is used to get the CPU stats per nodes + * Arguments: @conn [resource]: resource for connection + * @cpunr [int]: CPU number to get information about, def= aults to VIR_NODE_CPU_STATS_ALL_CPUS to get information about all CPUs + * Returns: array of node CPU statistics including time (in second= s since UNIX epoch), cpu number and total number of CPUs on node or FALSE f= or error + */ +PHP_FUNCTION(libvirt_node_get_cpu_stats) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int cpuNum =3D VIR_NODE_CPU_STATS_ALL_CPUS; + virNodeCPUStatsPtr params; + virNodeInfo info; + zend_long cpunr =3D -1; + int nparams =3D 0; + int i, j, numCpus; + + GET_CONNECTION_FROM_ARGS("r|l", &zconn, &cpunr); + + if (virNodeGetInfo(conn->conn, &info) !=3D 0) { + set_error("Cannot get number of CPUs" TSRMLS_CC); + RETURN_FALSE; + } + + numCpus =3D info.cpus; + if (cpunr > numCpus - 1) { + char tmp[256] =3D { 0 }; + snprintf(tmp, sizeof(tmp), "Invalid CPU number, valid numbers in r= ange 0 to %d or VIR_NODE_CPU_STATS_ALL_CPUS", + numCpus - 1); + set_error(tmp TSRMLS_CC); + + RETURN_FALSE; + } + + cpuNum =3D (int)cpunr; + + if (virNodeGetCPUStats(conn->conn, cpuNum, NULL, &nparams, 0) !=3D 0) { + set_error("Cannot get number of CPU stats" TSRMLS_CC); + RETURN_FALSE; + } + + if (nparams =3D=3D 0) + RETURN_TRUE; + + DPRINTF("%s: Number of parameters got from virNodeGetCPUStats is %d\n"= , __FUNCTION__, nparams); + + params =3D (virNodeCPUStatsPtr)calloc(nparams, nparams * sizeof(*param= s)); + + array_init(return_value); + for (i =3D 0; i < 2; i++) { + zval *arr; + + if (i > 0) +#ifdef EXTWIN + Sleep(1000); +#else + sleep(1); +#endif + + if (virNodeGetCPUStats(conn->conn, cpuNum, params, &nparams, 0) != =3D 0) { + set_error("Unable to get node cpu stats" TSRMLS_CC); + RETURN_FALSE; + } + + VIRT_ARRAY_INIT(arr); + + for (j =3D 0; j < nparams; j++) { + DPRINTF("%s: Field %s has value of %llu\n", __FUNCTION__, para= ms[j].field, params[j].value); + + add_assoc_long(arr, params[j].field, params[j].value); + } + + add_assoc_long(arr, "time", time(NULL)); + + add_index_zval(return_value, i, arr); + } + + add_assoc_long(return_value, "cpus", numCpus); + if (cpuNum >=3D 0) { + add_assoc_long(return_value, "cpu", cpunr); + } else { + if (cpuNum =3D=3D VIR_NODE_CPU_STATS_ALL_CPUS) + VIRT_ADD_ASSOC_STRING(return_value, "cpu", "all"); + else + VIRT_ADD_ASSOC_STRING(return_value, "cpu", "unknown"); + } + + free(params); + params =3D NULL; +} + +/* + * Function name: libvirt_node_get_cpu_stats_for_each_cpu + * Since version: 0.4.6 + * Description: Function is used to get the CPU stats for each CPU on = the host node + * Arguments: @conn [resource]: resource for connection + * @time [int]: time in seconds to get the information ab= out, without aggregation for further processing + * Returns: array of node CPU statistics for each CPU including ti= me (in seconds since UNIX epoch), cpu number and total number of CPUs on no= de or FALSE for error + */ +PHP_FUNCTION(libvirt_node_get_cpu_stats_for_each_cpu) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + virNodeCPUStatsPtr params; + virNodeInfo info; + int nparams =3D 0; + zend_long avg =3D 0, iter =3D 0; + int done =3D 0; + int i, j, numCpus; + time_t startTime =3D 0; + zval *time_array; + + GET_CONNECTION_FROM_ARGS("r|l", &zconn, &avg); + + if (virNodeGetInfo(conn->conn, &info) !=3D 0) { + set_error("Cannot get number of CPUs" TSRMLS_CC); + RETURN_FALSE; + } + + if (virNodeGetCPUStats(conn->conn, VIR_NODE_CPU_STATS_ALL_CPUS, NULL, = &nparams, 0) !=3D 0) { + set_error("Cannot get number of CPU stats" TSRMLS_CC); + RETURN_FALSE; + } + + if (nparams =3D=3D 0) + RETURN_TRUE; + + DPRINTF("%s: Number of parameters got from virNodeGetCPUStats is %d\n"= , __FUNCTION__, nparams); + + params =3D (virNodeCPUStatsPtr)calloc(nparams, nparams * sizeof(*param= s)); + + numCpus =3D info.cpus; + array_init(return_value); + + startTime =3D time(NULL); + + iter =3D 0; + done =3D 0; + while (!done) { + zval *arr; + VIRT_ARRAY_INIT(arr); + + for (i =3D 0; i < numCpus; i++) { + zval *arr2; + + if (virNodeGetCPUStats(conn->conn, i, params, &nparams, 0) != =3D 0) { + set_error("Unable to get node cpu stats" TSRMLS_CC); + RETURN_FALSE; + } + + VIRT_ARRAY_INIT(arr2); + + for (j =3D 0; j < nparams; j++) + add_assoc_long(arr2, params[j].field, params[j].value); + + add_assoc_long(arr, "time", time(NULL)); + add_index_zval(arr, i, arr2); + } + + add_index_zval(return_value, iter, arr); + + if ((avg <=3D 0) || (iter =3D=3D avg - 1)) { + done =3D 1; + break; + } + +#ifndef EXTWIN + sleep(1); +#else + Sleep(1000); +#endif + iter++; + } + + VIRT_ARRAY_INIT(time_array); + add_assoc_long(time_array, "start", startTime); + add_assoc_long(time_array, "finish", time(NULL)); + add_assoc_long(time_array, "duration", time(NULL) - startTime); + + add_assoc_zval(return_value, "times", time_array); + + free(params); + params =3D NULL; +} + +/* + * Function name: libvirt_node_get_mem_stats + * Since version: 0.4.6 + * Description: Function is used to get the memory stats per node + * Arguments: @conn [resource]: resource for connection + * Returns: array of node memory statistics including time (in sec= onds since UNIX epoch) or FALSE for error + */ +PHP_FUNCTION(libvirt_node_get_mem_stats) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + int memNum =3D VIR_NODE_MEMORY_STATS_ALL_CELLS; + virNodeMemoryStatsPtr params; + int nparams =3D 0; + int j; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + if (virNodeGetMemoryStats(conn->conn, memNum, NULL, &nparams, 0) !=3D = 0) { + set_error("Cannot get number of memory stats" TSRMLS_CC); + RETURN_FALSE; + } + + if (nparams =3D=3D 0) + RETURN_TRUE; + + DPRINTF("%s: Number of parameters got from virNodeGetMemoryStats is %d= \n", __FUNCTION__, nparams); + + params =3D (virNodeMemoryStatsPtr)calloc(nparams, nparams * sizeof(*pa= rams)); + + array_init(return_value); + if (virNodeGetMemoryStats(conn->conn, memNum, params, &nparams, 0) != =3D 0) { + set_error("Unable to get node memory stats" TSRMLS_CC); + RETURN_FALSE; + } + + for (j =3D 0; j < nparams; j++) { + DPRINTF("%s: Field %s has value of %llu\n", __FUNCTION__, params[j= ].field, params[j].value); + + add_assoc_long(return_value, params[j].field, params[j].value); + } + + add_assoc_long(return_value, "time", time(NULL)); + + free(params); + params =3D NULL; +} + +/* + * Function name: libvirt_node_get_free_memory + * Since version: 0.5.3 + * Description: Function is used to get free memory available on the n= ode. + * 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 =3D NULL; + zval *zconn; + unsigned long long ret; + LONGLONG_INIT; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + if ((ret =3D virNodeGetFreeMemory(conn->conn)) !=3D 0) { + LONGLONG_RETURN_AS_STRING(ret); + } else { + set_error("Cannot get the free memory for the node" TSRMLS_CC); + RETURN_FALSE; + } +} + diff --git a/src/libvirt-node.h b/src/libvirt-node.h new file mode 100644 index 0000000..63d79b6 --- /dev/null +++ b/src/libvirt-node.h @@ -0,0 +1,25 @@ +/* + * libvirt-node.h: The PHP bindings to libvirt node API + * + * See COPYING for the license of this software + */ + +#ifndef __LIBVIRT_NODE_H__ +# define __LIBVIRT_NODE_H__ + +# include "libvirt-php.h"=20 + +# define PHP_FE_LIBVIRT_NODE = \ + PHP_FE(libvirt_node_get_info, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_node_get_cpu_stats, arginfo_libvirt_conn_o= ptcpunr) \ + PHP_FE(libvirt_node_get_cpu_stats_for_each_cpu, arginfo_libvirt_conn_o= pttime) \ + PHP_FE(libvirt_node_get_mem_stats, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_node_get_free_memory, arginfo_libvirt_conn) + +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); + +#endif diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 3e11e9d..7e9c96b 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -21,6 +21,7 @@ #include "vncfunc.h" #include "sockets.h" #include "libvirt-connection.h" +#include "libvirt-node.h" =20 DEBUG_INIT("core"); =20 @@ -610,11 +611,7 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE(libvirt_network_get_autostart, arginfo_libvirt_conn) PHP_FE(libvirt_network_set_autostart, arginfo_libvirt_conn_flag= s) /* Node functions */ - PHP_FE(libvirt_node_get_info, arginfo_libvirt_conn) - PHP_FE(libvirt_node_get_cpu_stats, arginfo_libvirt_conn_optc= punr) - PHP_FE(libvirt_node_get_cpu_stats_for_each_cpu, arginfo_libvirt_conn_o= pttime) - PHP_FE(libvirt_node_get_mem_stats, arginfo_libvirt_conn) - PHP_FE(libvirt_node_get_free_memory, arginfo_libvirt_conn) + PHP_FE_LIBVIRT_NODE /* Nodedev functions */ PHP_FE(libvirt_nodedev_get, arginfo_libvirt_conn) PHP_FE(libvirt_nodedev_capabilities, arginfo_libvirt_conn) @@ -1968,30 +1965,6 @@ PHP_MSHUTDOWN_FUNCTION(libvirt) RETURN_FALSE; = \ } while (0) = \ =20 -#define LONGLONG_INIT \ - char tmpnumber[64] - -#define LONGLONG_ASSOC(out, key, in) \ - if (LIBVIRT_G(longlong_to_string_ini)) { \ - snprintf(tmpnumber, 63, "%llu", in); \ - VIRT_ADD_ASSOC_STRING(out, key, tmpnumber); \ - } else { \ - add_assoc_long(out, key, in); \ - } - -#define LONGLONG_INDEX(out, key, in) \ - if (LIBVIRT_G(longlong_to_string_ini)) { \ - snprintf(tmpnumber, 63, "%llu", in); \ - VIRT_ADD_INDEX_STRING(out, key, tmpnumber); \ - } else { \ - add_index_long(out, key, in); \ - } - -#define LONGLONG_RETURN_AS_STRING(in) \ - do { \ - snprintf(tmpnumber, 63, "%llu", in); \ - VIRT_RETURN_STRING(tmpnumber); \ - } while (0) =20 /* Common functions */ =20 @@ -2009,297 +1982,6 @@ PHP_FUNCTION(libvirt_get_last_error) } =20 /* - * Function name: libvirt_node_get_info - * Since version: 0.4.1(-1) - * Description: Function is used to get the information about host nod= e, mainly total memory installed, total CPUs installed and model informatio= n are useful - * Arguments: @conn [resource]: resource for connection - * Returns: array of node information or FALSE for error - */ -PHP_FUNCTION(libvirt_node_get_info) -{ - virNodeInfo info; - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int retval; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - retval =3D virNodeGetInfo (conn->conn, &info); - DPRINTF("%s: virNodeGetInfo returned %d\n", PHPFUNC, retval); - if (retval =3D=3D -1) - RETURN_FALSE; - - array_init(return_value); - VIRT_ADD_ASSOC_STRING(return_value, "model", info.model); - add_assoc_long(return_value, "memory", (long)info.memory); - add_assoc_long(return_value, "cpus", (long)info.cpus); - add_assoc_long(return_value, "nodes", (long)info.nodes); - add_assoc_long(return_value, "sockets", (long)info.sockets); - add_assoc_long(return_value, "cores", (long)info.cores); - add_assoc_long(return_value, "threads", (long)info.threads); - add_assoc_long(return_value, "mhz", (long)info.mhz); -} - -/* - * Function name: libvirt_node_get_cpu_stats - * Since version: 0.4.6 - * Description: Function is used to get the CPU stats per nodes - * Arguments: @conn [resource]: resource for connection - * @cpunr [int]: CPU number to get information about, def= aults to VIR_NODE_CPU_STATS_ALL_CPUS to get information about all CPUs - * Returns: array of node CPU statistics including time (in second= s since UNIX epoch), cpu number and total number of CPUs on node or FALSE f= or error - */ -PHP_FUNCTION(libvirt_node_get_cpu_stats) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int cpuNum =3D VIR_NODE_CPU_STATS_ALL_CPUS; - virNodeCPUStatsPtr params; - virNodeInfo info; - zend_long cpunr =3D -1; - int nparams =3D 0; - int i, j, numCpus; - - GET_CONNECTION_FROM_ARGS("r|l", &zconn, &cpunr); - - if (virNodeGetInfo(conn->conn, &info) !=3D 0) { - set_error("Cannot get number of CPUs" TSRMLS_CC); - RETURN_FALSE; - } - - numCpus =3D info.cpus; - if (cpunr > numCpus - 1) { - char tmp[256] =3D { 0 }; - snprintf(tmp, sizeof(tmp), "Invalid CPU number, valid numbers in r= ange 0 to %d or VIR_NODE_CPU_STATS_ALL_CPUS", - numCpus - 1); - set_error(tmp TSRMLS_CC); - - RETURN_FALSE; - } - - cpuNum =3D (int)cpunr; - - if (virNodeGetCPUStats(conn->conn, cpuNum, NULL, &nparams, 0) !=3D 0) { - set_error("Cannot get number of CPU stats" TSRMLS_CC); - RETURN_FALSE; - } - - if (nparams =3D=3D 0) - RETURN_TRUE; - - DPRINTF("%s: Number of parameters got from virNodeGetCPUStats is %d\n"= , __FUNCTION__, nparams); - - params =3D (virNodeCPUStatsPtr)calloc(nparams, nparams * sizeof(*param= s)); - - array_init(return_value); - for (i =3D 0; i < 2; i++) { - zval *arr; - - if (i > 0) -#ifdef EXTWIN - Sleep(1000); -#else - sleep(1); -#endif - - if (virNodeGetCPUStats(conn->conn, cpuNum, params, &nparams, 0) != =3D 0) { - set_error("Unable to get node cpu stats" TSRMLS_CC); - RETURN_FALSE; - } - - VIRT_ARRAY_INIT(arr); - - for (j =3D 0; j < nparams; j++) { - DPRINTF("%s: Field %s has value of %llu\n", __FUNCTION__, para= ms[j].field, params[j].value); - - add_assoc_long(arr, params[j].field, params[j].value); - } - - add_assoc_long(arr, "time", time(NULL)); - - add_index_zval(return_value, i, arr); - } - - add_assoc_long(return_value, "cpus", numCpus); - if (cpuNum >=3D 0) { - add_assoc_long(return_value, "cpu", cpunr); - } else { - if (cpuNum =3D=3D VIR_NODE_CPU_STATS_ALL_CPUS) - VIRT_ADD_ASSOC_STRING(return_value, "cpu", "all"); - else - VIRT_ADD_ASSOC_STRING(return_value, "cpu", "unknown"); - } - - free(params); - params =3D NULL; -} - -/* - * Function name: libvirt_node_get_cpu_stats_for_each_cpu - * Since version: 0.4.6 - * Description: Function is used to get the CPU stats for each CPU on = the host node - * Arguments: @conn [resource]: resource for connection - * @time [int]: time in seconds to get the information ab= out, without aggregation for further processing - * Returns: array of node CPU statistics for each CPU including ti= me (in seconds since UNIX epoch), cpu number and total number of CPUs on no= de or FALSE for error - */ -PHP_FUNCTION(libvirt_node_get_cpu_stats_for_each_cpu) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - virNodeCPUStatsPtr params; - virNodeInfo info; - int nparams =3D 0; - zend_long avg =3D 0, iter =3D 0; - int done =3D 0; - int i, j, numCpus; - time_t startTime =3D 0; - zval *time_array; - - GET_CONNECTION_FROM_ARGS("r|l", &zconn, &avg); - - if (virNodeGetInfo(conn->conn, &info) !=3D 0) { - set_error("Cannot get number of CPUs" TSRMLS_CC); - RETURN_FALSE; - } - - if (virNodeGetCPUStats(conn->conn, VIR_NODE_CPU_STATS_ALL_CPUS, NULL, = &nparams, 0) !=3D 0) { - set_error("Cannot get number of CPU stats" TSRMLS_CC); - RETURN_FALSE; - } - - if (nparams =3D=3D 0) - RETURN_TRUE; - - DPRINTF("%s: Number of parameters got from virNodeGetCPUStats is %d\n"= , __FUNCTION__, nparams); - - params =3D (virNodeCPUStatsPtr)calloc(nparams, nparams * sizeof(*param= s)); - - numCpus =3D info.cpus; - array_init(return_value); - - startTime =3D time(NULL); - - iter =3D 0; - done =3D 0; - while (!done) { - zval *arr; - VIRT_ARRAY_INIT(arr); - - for (i =3D 0; i < numCpus; i++) { - zval *arr2; - - if (virNodeGetCPUStats(conn->conn, i, params, &nparams, 0) != =3D 0) { - set_error("Unable to get node cpu stats" TSRMLS_CC); - RETURN_FALSE; - } - - VIRT_ARRAY_INIT(arr2); - - for (j =3D 0; j < nparams; j++) - add_assoc_long(arr2, params[j].field, params[j].value); - - add_assoc_long(arr, "time", time(NULL)); - add_index_zval(arr, i, arr2); - } - - add_index_zval(return_value, iter, arr); - - if ((avg <=3D 0) || (iter =3D=3D avg - 1)) { - done =3D 1; - break; - } - -#ifndef EXTWIN - sleep(1); -#else - Sleep(1000); -#endif - iter++; - } - - VIRT_ARRAY_INIT(time_array); - add_assoc_long(time_array, "start", startTime); - add_assoc_long(time_array, "finish", time(NULL)); - add_assoc_long(time_array, "duration", time(NULL) - startTime); - - add_assoc_zval(return_value, "times", time_array); - - free(params); - params =3D NULL; -} - -/* - * Function name: libvirt_node_get_mem_stats - * Since version: 0.4.6 - * Description: Function is used to get the memory stats per node - * Arguments: @conn [resource]: resource for connection - * Returns: array of node memory statistics including time (in sec= onds since UNIX epoch) or FALSE for error - */ -PHP_FUNCTION(libvirt_node_get_mem_stats) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - int memNum =3D VIR_NODE_MEMORY_STATS_ALL_CELLS; - virNodeMemoryStatsPtr params; - int nparams =3D 0; - int j; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - if (virNodeGetMemoryStats(conn->conn, memNum, NULL, &nparams, 0) !=3D = 0) { - set_error("Cannot get number of memory stats" TSRMLS_CC); - RETURN_FALSE; - } - - if (nparams =3D=3D 0) - RETURN_TRUE; - - DPRINTF("%s: Number of parameters got from virNodeGetMemoryStats is %d= \n", __FUNCTION__, nparams); - - params =3D (virNodeMemoryStatsPtr)calloc(nparams, nparams * sizeof(*pa= rams)); - - array_init(return_value); - if (virNodeGetMemoryStats(conn->conn, memNum, params, &nparams, 0) != =3D 0) { - set_error("Unable to get node memory stats" TSRMLS_CC); - RETURN_FALSE; - } - - for (j =3D 0; j < nparams; j++) { - DPRINTF("%s: Field %s has value of %llu\n", __FUNCTION__, params[j= ].field, params[j].value); - - add_assoc_long(return_value, params[j].field, params[j].value); - } - - add_assoc_long(return_value, "time", time(NULL)); - - free(params); - params =3D NULL; -} - -/* - * Function name: libvirt_node_get_free_memory - * Since version: 0.5.3 - * Description: Function is used to get free memory available on the n= ode. - * 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 =3D NULL; - zval *zconn; - unsigned long long ret; - LONGLONG_INIT; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - if ((ret =3D virNodeGetFreeMemory(conn->conn)) !=3D 0) { - LONGLONG_RETURN_AS_STRING(ret); - } else { - set_error("Cannot get the free memory for the node" TSRMLS_CC); - RETURN_FALSE; - } -} - -/* * Function name: libvirt_image_create * Since version: 0.4.2 * Description: Function is used to create the image of desired name, = size and format. The image will be created in the image path (libvirt.image= _path INI variable). Works only o diff --git a/src/libvirt-php.h b/src/libvirt-php.h index 6cbcd27..02b9cec 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -274,12 +274,6 @@ PHP_MINFO_FUNCTION(libvirt); =20 /* Common functions */ PHP_FUNCTION(libvirt_get_last_error); -/* Node functions */ -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); diff --git a/src/util.h b/src/util.h index 167bd90..d18b51d 100644 --- a/src/util.h +++ b/src/util.h @@ -205,6 +205,31 @@ =20 # endif /* PHP_MAJOR_VERSION < 7 */ =20 +# define LONGLONG_INIT = \ + char tmpnumber[64] + +# define LONGLONG_ASSOC(out, key, in) = \ + if (LIBVIRT_G(longlong_to_string_ini)) { = \ + snprintf(tmpnumber, 63, "%llu", in); = \ + VIRT_ADD_ASSOC_STRING(out, key, tmpnumber); = \ + } else { = \ + add_assoc_long(out, key, in); = \ + } + +# define LONGLONG_INDEX(out, key, in) = \ + if (LIBVIRT_G(longlong_to_string_ini)) { = \ + snprintf(tmpnumber, 63, "%llu", in); = \ + VIRT_ADD_INDEX_STRING(out, key, tmpnumber); = \ + } else { = \ + add_index_long(out, key, in); = \ + } + +# define LONGLONG_RETURN_AS_STRING(in) = \ + do { = \ + snprintf(tmpnumber, 63, "%llu", in); = \ + VIRT_RETURN_STRING(tmpnumber); = \ + } while (0) + # ifndef PHP_FE_END # define PHP_FE_END {NULL, NULL, NULL} # endif --=20 2.13.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list