[libvirt] [PATCH] virsh: added --all flag to freecell command

This will iterate over all NUMA nodes, showing free memory for each and sum at the end. Existing default behavior is not changed. --- tools/virsh.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 61 insertions(+), 12 deletions(-) diff --git a/tools/virsh.c b/tools/virsh.c index cd54174..1ddc7cf 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -2273,36 +2273,85 @@ static const vshCmdInfo info_freecell[] = { static const vshCmdOptDef opts_freecell[] = { {"cellno", VSH_OT_INT, 0, N_("NUMA cell number")}, + {"all", VSH_OT_BOOL, 0, N_("show free memory for all NUMA cells")}, {NULL, 0, 0, NULL} }; static int cmdFreecell(vshControl *ctl, const vshCmd *cmd) { + int func_ret = FALSE; int ret; int cell, cell_given; unsigned long long memory; + unsigned long long *nodes = NULL; + int all_given; + virNodeInfo info; + if (!vshConnectionUsability(ctl, ctl->conn)) return FALSE; cell = vshCommandOptInt(cmd, "cellno", &cell_given); - if (!cell_given) { - memory = virNodeGetFreeMemory(ctl->conn); - if (memory == 0) - return FALSE; + all_given = vshCommandOptBool(cmd, "all"); + + if (all_given && cell_given) { + vshError(ctl, "%s", _("--cellno and --all are mutual exclusive. Please choose only one.")); + goto cleanup; + } + + if (all_given) { + if (virNodeGetInfo(ctl->conn, &info) < 0) { + vshError(ctl, "%s", _("failed to get NUMA nodes count")); + goto cleanup; + } + + if (!info.nodes) { + vshError(ctl, "%s", _("no NUMA nodes presented")); + goto cleanup; + } + + if (VIR_ALLOC_N(nodes, info.nodes) < 0) { + vshError(ctl, "%s", _("could not allocate memory")); + goto cleanup; + } + + ret = virNodeGetCellsFreeMemory(ctl->conn, nodes, 0, info.nodes); + if (ret != info.nodes) { + vshError(ctl, "%s", _("could not get information about all NUMA nodes")); + goto cleanup; + } + + memory = 0; + for (cell = 0; cell < info.nodes; cell++) { + vshPrint(ctl, "%d: %llu kB\n", cell, (nodes[cell]/1024)); + memory += nodes[cell]; + } + + vshPrintExtra(ctl, "--------------------\n"); + vshPrintExtra(ctl, "%s %llu kB\n",_("Total"), memory/1024); } else { - ret = virNodeGetCellsFreeMemory(ctl->conn, &memory, cell, 1); - if (ret != 1) - return FALSE; + if (!cell_given) { + memory = virNodeGetFreeMemory(ctl->conn); + if (memory == 0) + goto cleanup; + } else { + ret = virNodeGetCellsFreeMemory(ctl->conn, &memory, cell, 1); + if (ret != 1) + goto cleanup; + } + + if (cell == -1) + vshPrint(ctl, "%s: %llu kB\n", _("Total"), (memory/1024)); + else + vshPrint(ctl, "%d: %llu kB\n", cell, (memory/1024)); } - if (cell == -1) - vshPrint(ctl, "%s: %llu kB\n", _("Total"), (memory/1024)); - else - vshPrint(ctl, "%d: %llu kB\n", cell, (memory/1024)); + func_ret = TRUE; - return TRUE; +cleanup: + VIR_FREE(nodes); + return func_ret; } /* -- 1.7.3.5

On 01/28/2011 09:42 AM, Michal Privoznik wrote:
This will iterate over all NUMA nodes, showing free memory for each and sum at the end. Existing default behavior is not changed. --- tools/virsh.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 61 insertions(+), 12 deletions(-)
static int cmdFreecell(vshControl *ctl, const vshCmd *cmd) { + int func_ret = FALSE; int ret; int cell, cell_given; unsigned long long memory; + unsigned long long *nodes = NULL; + int all_given;
At first, I thought to suggest s/int/bool/, given it's use
+ virNodeInfo info; +
if (!vshConnectionUsability(ctl, ctl->conn)) return FALSE;
cell = vshCommandOptInt(cmd, "cellno", &cell_given); - if (!cell_given) { - memory = virNodeGetFreeMemory(ctl->conn); - if (memory == 0) - return FALSE; + all_given = vshCommandOptBool(cmd, "all");
...but this (still) returns int instead of bool (we should probably fix that, but that's a separate cleanup patch).
+ + if (all_given && cell_given) { + vshError(ctl, "%s", _("--cellno and --all are mutual exclusive. Please choose only one."));
s/mutual/mutually/ I'd drop the last sentence; it adds politeness, but not any new information over the first sentence, and just makes the line longer than 80 columns
+ goto cleanup; + } + + if (all_given) { + if (virNodeGetInfo(ctl->conn, &info) < 0) { + vshError(ctl, "%s", _("failed to get NUMA nodes count")); + goto cleanup; + } + + if (!info.nodes) { + vshError(ctl, "%s", _("no NUMA nodes presented"));
s/presented/present/
+ memory = 0; + for (cell = 0; cell < info.nodes; cell++) { + vshPrint(ctl, "%d: %llu kB\n", cell, (nodes[cell]/1024));
Should we try to align columns here?
+ memory += nodes[cell]; + } + + vshPrintExtra(ctl, "--------------------\n"); + vshPrintExtra(ctl, "%s %llu kB\n",_("Total"), memory/1024);
and also here? But I like the idea. -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org
participants (2)
-
Eric Blake
-
Michal Privoznik