[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH] virsh: make vcpucount use --current consistently



Rename the existing --current flag to the new name --active,
while adding a new flag --current to expose the new
VIR_DOMAIN_AFFECT_CURRENT flag of virDomainGetVcpusFlags.

For backwards compability, the output does not change (even
though the label "current" no longer matches the spelling of
the option that would trigger that number in isolation), and
we accept "--current --live" as an undocumented synonym for
"--active --live" to avoid breaking any existing clients.

* tools/virsh.c (cmdVcpucount): Add --active flag, and rearrange
existing flag handling to expose VIR_DOMAIN_AFFECT_CURRENT support.
* tools/virsh.pod (vcpucount): Document this.
---

Incorporating my proposal from:
https://www.redhat.com/archives/libvir-list/2011-July/msg01099.html

 tools/virsh.c   |   77 +++++++++++++++++++++++++++++++++++++++---------------
 tools/virsh.pod |   19 ++++++++-----
 2 files changed, 67 insertions(+), 29 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index 2eb218a..65fba0f 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -2738,9 +2738,11 @@ static const vshCmdInfo info_vcpucount[] = {
 static const vshCmdOptDef opts_vcpucount[] = {
     {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, N_("domain name, id or uuid")},
     {"maximum", VSH_OT_BOOL, 0, N_("get maximum cap on vcpus")},
-    {"current", VSH_OT_BOOL, 0, N_("get current vcpu usage")},
-    {"config", VSH_OT_BOOL, 0, N_("get value to be used on next boot")},
+    {"active", VSH_OT_BOOL, 0, N_("get number of currently active vcpus")},
     {"live", VSH_OT_BOOL, 0, N_("get value from running domain")},
+    {"config", VSH_OT_BOOL, 0, N_("get value to be used on next boot")},
+    {"current", VSH_OT_BOOL, 0,
+     N_("get value according to current domain state")},
     {NULL, 0, 0, NULL}
 };

@@ -2750,31 +2752,50 @@ cmdVcpucount(vshControl *ctl, const vshCmd *cmd)
     virDomainPtr dom;
     bool ret = true;
     int maximum = vshCommandOptBool(cmd, "maximum");
-    int current = vshCommandOptBool(cmd, "current");
+    int active = vshCommandOptBool(cmd, "active");
     int config = vshCommandOptBool(cmd, "config");
     int live = vshCommandOptBool(cmd, "live");
-    bool all = maximum + current + config + live == 0;
+    int current = vshCommandOptBool(cmd, "current");
+    bool all = maximum + active + current + config + live == 0;
     int count;

-    if (maximum && current) {
-        vshError(ctl, "%s",
-                 _("--maximum and --current cannot both be specified"));
+    /* We want one of each pair of mutually exclusive options; that
+     * is, use of flags requires exactly two options.  We reject the
+     * use of more than 2 flags later on.  */
+    if (maximum + active + current + config + live == 1) {
+        if (maximum || active) {
+            vshError(ctl,
+                     _("when using --%s, one of --config, --live, or --current "
+                       "must be specified"),
+                     maximum ? "maximum" : "active");
+        } else {
+            vshError(ctl,
+                     _("when using --%s, either --maximum or --active must be "
+                       "specified"),
+                     (current ? "current" : config ? "config" : "live"));
+        }
         return false;
     }
-    if (config && live) {
+
+    /* Backwards compatibility: prior to 0.9.4,
+     * VIR_DOMAIN_AFFECT_CURRENT was unsupported, and --current meant
+     * the opposite of --maximum.  Translate the old '--current
+     * --live' into the new '--active --live', while treating the new
+     * '--maximum --current' correctly rather than rejecting it as
+     * '--maximum --active'.  */
+    if (!maximum && !active && current) {
+        current = false;
+        active = true;
+    }
+
+    if (maximum && active) {
         vshError(ctl, "%s",
-                 _("--config and --live cannot both be specified"));
+                 _("--maximum and --active cannot both be specified"));
         return false;
     }
-    /* We want one of each pair of mutually exclusive options; that
-     * is, use of flags requires exactly two options.  */
-    if (maximum + current + config + live == 1) {
-        vshError(ctl,
-                 _("when using --%s, either --%s or --%s must be specified"),
-                 (maximum ? "maximum" : current ? "current"
-                  : config ? "config" : "live"),
-                 maximum + current ? "config" : "maximum",
-                 maximum + current ? "live" : "current");
+    if (current + config + live > 1) {
+        vshError(ctl, "%s",
+                 _("--config, --live, and --current are mutually exclusive"));
         return false;
     }

@@ -2785,8 +2806,20 @@ cmdVcpucount(vshControl *ctl, const vshCmd *cmd)
         return false;

     /* In all cases, try the new API first; if it fails because we are
-     * talking to an older client, try a fallback API before giving
-     * up.  */
+     * talking to an older client, generally we try a fallback API
+     * before giving up.  --current requires the new API, since we
+     * don't know whether the domain is running or inactive.  */
+    if (active) {
+        count = virDomainGetVcpusFlags(dom,
+                                       maximum ? VIR_DOMAIN_VCPU_MAXIMUM : 0);
+        if (count < 0) {
+            virshReportError(ctl);
+            ret = false;
+        } else {
+            vshPrint(ctl, "%d\n", count);
+        }
+    }
+
     if (all || (maximum && config)) {
         count = virDomainGetVcpusFlags(dom, (VIR_DOMAIN_VCPU_MAXIMUM |
                                              VIR_DOMAIN_AFFECT_CONFIG));
@@ -2838,7 +2871,7 @@ cmdVcpucount(vshControl *ctl, const vshCmd *cmd)
         last_error = NULL;
     }

-    if (all || (current && config)) {
+    if (all || (active && config)) {
         count = virDomainGetVcpusFlags(dom, VIR_DOMAIN_AFFECT_CONFIG);
         if (count < 0 && (last_error->code == VIR_ERR_NO_SUPPORT
                           || last_error->code == VIR_ERR_INVALID_ARG)) {
@@ -2875,7 +2908,7 @@ cmdVcpucount(vshControl *ctl, const vshCmd *cmd)
         last_error = NULL;
     }

-    if (all || (current && live)) {
+    if (all || (active && live)) {
         count = virDomainGetVcpusFlags(dom, VIR_DOMAIN_AFFECT_LIVE);
         if (count < 0 && (last_error->code == VIR_ERR_NO_SUPPORT
                           || last_error->code == VIR_ERR_INVALID_ARG)) {
diff --git a/tools/virsh.pod b/tools/virsh.pod
index 5dc3d2e..2659ecb 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -816,20 +816,25 @@ is not available the processes will provide an exit code of 1.
 Undefine the configuration for an inactive domain. Since it's not running
 the domain name or UUID must be used as the I<domain-id>.

-=item B<vcpucount> I<domain-id>  [{I<--maximum> | I<--current>}
-{I<--config> | I<--live>}]
+=item B<vcpucount> I<domain-id>  [{I<--maximum> | I<--active>}
+{I<--config> | I<--live> | I<--current>}]

 Print information about the virtual cpu counts of the given
 I<domain-id>.  If no flags are specified, all possible counts are
 listed in a table; otherwise, the output is limited to just the
-numeric value requested.
+numeric value requested.  For historical reasons, the table
+lists the label "current" on the rows that can be queried in isolation
+via the I<--active> flag, rather than relating to the I<--current> flag.

 I<--maximum> requests information on the maximum cap of vcpus that a
-domain can add via B<setvcpus>, while I<--current> shows the current
+domain can add via B<setvcpus>, while I<--active> shows the current
 usage; these two flags cannot both be specified.  I<--config>
-requests information regarding the next time the domain will be
-booted, while I<--live> requires a running domain and lists current
-values; these two flags cannot both be specified.
+requires a persistent domain and requests information regarding the next
+time the domain will be booted, I<--live> requires a running domain and
+lists current values, and I<--current> queries according to the current
+state of the domain (corresponding to I<--live> if running, or
+I<--config> if inactive); these three flags are mutually exclusive.
+Thus, this command always takes exactly zero or two flags.

 =item B<vcpuinfo> I<domain-id>

-- 
1.7.4.4


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]