On 06/14/2011 03:21 AM, Daniel P. Berrange wrote:
On Tue, Jun 07, 2011 at 10:11:17AM +0900, Minoru Usui wrote:
> virNodeGetMemoryStats: Implement linux support
>
> Signed-off-by: Minoru Usui <usui(a)mxm.nes.nec.co.jp>
> ---
> src/libvirt_private.syms | 1 +
> src/lxc/lxc_driver.c | 1 +
> src/nodeinfo.c | 161 ++++++++++++++++++++++++++++++++++++++++++++++
> src/nodeinfo.h | 5 ++
> src/qemu/qemu_driver.c | 1 +
> src/uml/uml_driver.c | 1 +
> 6 files changed, 170 insertions(+), 0 deletions(-)
>
> +
> + sprintf(meminfo_path, "%s/node%d/meminfo", NODE_SYS_PATH,
cellNum);
> + }
Instead of using a static buffer, it is preferrable to use
virAsprintf() here to construct meminfo_path.
ACK with this squashed in (whitespace ignored, the real diff was a bit
bigger but had more indentation changed), so I pushed:
diff --git i/src/libvirt_private.syms w/src/libvirt_private.syms
index 57243f1..7a05539 100644
--- i/src/libvirt_private.syms
+++ w/src/libvirt_private.syms
@@ -734,10 +734,10 @@ virNodeDeviceObjUnlock;
# nodeinfo.h
nodeCapsInitNUMA;
nodeGetCPUStats;
-nodeGetMemoryStats;
nodeGetCellsFreeMemory;
nodeGetFreeMemory;
nodeGetInfo;
+nodeGetMemoryStats;
# nwfilter_conf.h
diff --git i/src/nodeinfo.c w/src/nodeinfo.c
index 809fc44..cb2f805 100644
--- i/src/nodeinfo.c
+++ w/src/nodeinfo.c
@@ -559,9 +563,8 @@ int linuxNodeGetMemoryStats(FILE *meminfo,
buf = p;
}
- if (sscanf(buf, "%s %lu kB", meminfo_hdr, &val) < 2) {
+ if (sscanf(buf, "%s %lu kB", meminfo_hdr, &val) < 2)
continue;
- }
for (j = 0; field_conv[j].meminfo_hdr != NULL; j++) {
struct field_conv *convp = &field_conv[j];
@@ -579,10 +582,9 @@ int linuxNodeGetMemoryStats(FILE *meminfo,
break;
}
}
- if (found >= nr_param) {
+ if (found >= nr_param)
break;
}
- }
if (found == 0) {
nodeReportError(VIR_ERR_INTERNAL_ERROR,
@@ -666,16 +668,22 @@ int nodeGetMemoryStats(virConnectPtr conn
ATTRIBUTE_UNUSED,
int cellNum,
virMemoryStatsPtr params,
int *nparams,
- unsigned int flags ATTRIBUTE_UNUSED)
+ unsigned int flags)
{
+ virCheckFlags(0, -1);
+
#ifdef __linux__
{
int ret;
- char meminfo_path[1024];
+ char *meminfo_path = NULL;
FILE *meminfo;
if (cellNum == VIR_MEMORY_STATS_ALL_CELLS) {
- sprintf(meminfo_path, MEMINFO_PATH);
+ meminfo_path = strdup(MEMINFO_PATH);
+ if (!meminfo_path) {
+ virReportOOMError();
+ return -1;
+ }
} else {
if (numa_available() < 0) {
nodeReportError(VIR_ERR_NO_SUPPORT,
@@ -684,21 +692,28 @@ int nodeGetMemoryStats(virConnectPtr conn
ATTRIBUTE_UNUSED,
}
if (cellNum > numa_max_node()) {
- nodeReportError(VIR_ERR_INVALID_ARG, "%s", _("Invalid cell
number"));
+ nodeReportError(VIR_ERR_INVALID_ARG, "%s",
+ _("Invalid cell number"));
return -1;
}
- sprintf(meminfo_path, "%s/node%d/meminfo", NODE_SYS_PATH, cellNum);
+ if (virAsprintf(&meminfo_path, "%s/node%d/meminfo",
+ NODE_SYS_PATH, cellNum) < 0) {
+ virReportOOMError();
+ return -1;
+ }
}
meminfo = fopen(meminfo_path, "r");
if (!meminfo) {
virReportSystemError(errno,
- _("cannot open %s"), MEMINFO_PATH);
+ _("cannot open %s"), meminfo_path);
+ VIR_FREE(meminfo_path);
return -1;
}
ret = linuxNodeGetMemoryStats(meminfo, cellNum, params, nparams);
VIR_FORCE_FCLOSE(meminfo);
+ VIR_FREE(meminfo_path);
return ret;
}
--
Eric Blake eblake(a)redhat.com +1-801-349-2682
Libvirt virtualization library
http://libvirt.org