From 4583a49ccf41e78c4f2cc7788a193a6c5509bd09 Mon Sep 17 00:00:00 2001 From: Lin Ma Date: Tue, 10 Nov 2020 17:50:53 +0800 Subject: [PATCH] virsh: Add logical CPU IDs completion for nodecpustats command Signed-off-by: Lin Ma Signed-off-by: Michal Privoznik Reviewed-by: Michal Privoznik --- tools/virsh-completer-host.c | 31 +++++++++++++++++++++++++++++++ tools/virsh-completer-host.h | 4 ++++ tools/virsh-host.c | 1 + 3 files changed, 36 insertions(+) diff --git a/tools/virsh-completer-host.c b/tools/virsh-completer-host.c index 339390aa00..4c277f2ada 100644 --- a/tools/virsh-completer-host.c +++ b/tools/virsh-completer-host.c @@ -136,3 +136,34 @@ virshCellnoCompleter(vshControl *ctl, return g_steal_pointer(&tmp); } + + +char ** +virshNodeCpuCompleter(vshControl *ctl, + const vshCmd *cmd G_GNUC_UNUSED, + unsigned int flags) +{ + virshControlPtr priv = ctl->privData; + VIR_AUTOSTRINGLIST tmp = NULL; + size_t i; + int cpunum; + size_t offset = 0; + unsigned int online; + g_autofree unsigned char *cpumap = NULL; + + virCheckFlags(0, NULL); + + if ((cpunum = virNodeGetCPUMap(priv->conn, &cpumap, &online, 0)) < 0) + return NULL; + + tmp = g_new0(char *, online + 1); + + for (i = 0; i < cpunum; i++) { + if (VIR_CPU_USED(cpumap, i) == 0) + continue; + + tmp[offset++] = g_strdup_printf("%zu", i); + } + + return g_steal_pointer(&tmp); +} diff --git a/tools/virsh-completer-host.h b/tools/virsh-completer-host.h index 921beb7a2d..a502216584 100644 --- a/tools/virsh-completer-host.h +++ b/tools/virsh-completer-host.h @@ -29,3 +29,7 @@ char ** virshAllocpagesPagesizeCompleter(vshControl *ctl, char ** virshCellnoCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); + +char ** virshNodeCpuCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); diff --git a/tools/virsh-host.c b/tools/virsh-host.c index cda2ef4ac1..ba524523f5 100644 --- a/tools/virsh-host.c +++ b/tools/virsh-host.c @@ -751,6 +751,7 @@ static const vshCmdInfo info_nodecpustats[] = { static const vshCmdOptDef opts_node_cpustats[] = { {.name = "cpu", .type = VSH_OT_INT, + .completer = virshNodeCpuCompleter, .help = N_("prints specified cpu statistics only.") }, {.name = "percent",