virsh-host.c 39.4 KB
Newer Older
1 2 3
/*
 * virsh-host.c: Commands in "Host and Hypervisor" group.
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2005, 2007-2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
18 19 20 21 22 23 24 25
 * <http://www.gnu.org/licenses/>.
 *
 *  Daniel Veillard <veillard@redhat.com>
 *  Karel Zak <kzak@redhat.com>
 *  Daniel P. Berrange <berrange@redhat.com>
 *
 */

E
Eric Blake 已提交
26 27 28 29 30 31 32 33 34
#include <config.h>
#include "virsh-host.h"

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/xmlsave.h>

#include "internal.h"
35
#include "virbitmap.h"
36
#include "virbuffer.h"
37
#include "viralloc.h"
38
#include "virsh-domain.h"
39
#include "virxml.h"
40
#include "virtypedparam.h"
41
#include "virstring.h"
E
Eric Blake 已提交
42

43 44 45 46
/*
 * "capabilities" command
 */
static const vshCmdInfo info_capabilities[] = {
47 48 49 50 51 52 53
    {.name = "help",
     .data = N_("capabilities")
    },
    {.name = "desc",
     .data = N_("Returns capabilities of hypervisor/driver.")
    },
    {.name = NULL}
54 55 56 57 58 59
};

static bool
cmdCapabilities(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
{
    char *caps;
60
    virshControlPtr priv = ctl->privData;
61

62
    if ((caps = virConnectGetCapabilities(priv->conn)) == NULL) {
63 64 65 66 67 68 69 70 71
        vshError(ctl, "%s", _("failed to get capabilities"));
        return false;
    }
    vshPrint(ctl, "%s\n", caps);
    VIR_FREE(caps);

    return true;
}

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
/*
 * "domcapabilities" command
 */
static const vshCmdInfo info_domcapabilities[] = {
    {.name = "help",
     .data = N_("domain capabilities")
    },
    {.name = "desc",
     .data = N_("Returns capabilities of emulator with respect to host and libvirt.")
    },
    {.name = NULL}
};

static const vshCmdOptDef opts_domcapabilities[] = {
    {.name = "virttype",
     .type = VSH_OT_STRING,
     .help = N_("virtualization type (/domain/@type)"),
    },
    {.name = "emulatorbin",
     .type = VSH_OT_STRING,
     .help = N_("path to emulator binary (/domain/devices/emulator)"),
    },
    {.name = "arch",
     .type = VSH_OT_STRING,
     .help = N_("domain architecture (/domain/os/type/@arch)"),
    },
    {.name = "machine",
     .type = VSH_OT_STRING,
     .help = N_("machine type (/domain/os/type/@machine)"),
    },
    {.name = NULL}
};

static bool
cmdDomCapabilities(vshControl *ctl, const vshCmd *cmd)
{
    bool ret = false;
    char *caps = NULL;
    const char *virttype = NULL;
    const char *emulatorbin = NULL;
    const char *arch = NULL;
    const char *machine = NULL;
    const unsigned int flags = 0; /* No flags so far */
115
    virshControlPtr priv = ctl->privData;
116 117 118 119 120 121 122

    if (vshCommandOptStringReq(ctl, cmd, "virttype", &virttype) < 0 ||
        vshCommandOptStringReq(ctl, cmd, "emulatorbin", &emulatorbin) < 0 ||
        vshCommandOptStringReq(ctl, cmd, "arch", &arch) < 0 ||
        vshCommandOptStringReq(ctl, cmd, "machine", &machine) < 0)
        return ret;

123
    caps = virConnectGetDomainCapabilities(priv->conn, emulatorbin,
124 125 126 127 128 129 130 131 132 133 134 135 136
                                           arch, machine, virttype, flags);
    if (!caps) {
        vshError(ctl, "%s", _("failed to get emulator capabilities"));
        goto cleanup;
    }

    vshPrint(ctl, "%s\n", caps);
    ret = true;
 cleanup:
    VIR_FREE(caps);
    return ret;
}

137 138 139 140
/*
 * "freecell" command
 */
static const vshCmdInfo info_freecell[] = {
141 142 143 144 145 146 147
    {.name = "help",
     .data = N_("NUMA free memory")
    },
    {.name = "desc",
     .data = N_("display available free memory for the NUMA cell.")
    },
    {.name = NULL}
148 149 150
};

static const vshCmdOptDef opts_freecell[] = {
151 152 153 154 155 156 157 158 159
    {.name = "cellno",
     .type = VSH_OT_INT,
     .help = N_("NUMA cell number")
    },
    {.name = "all",
     .type = VSH_OT_BOOL,
     .help = N_("show free memory for all NUMA cells")
    },
    {.name = NULL}
160 161 162 163 164
};

static bool
cmdFreecell(vshControl *ctl, const vshCmd *cmd)
{
P
Peter Krempa 已提交
165 166 167
    bool ret = false;
    int cell = -1;
    unsigned long long memory = 0;
168 169 170 171
    xmlNodePtr *nodes = NULL;
    unsigned long nodes_cnt;
    unsigned long *nodes_id = NULL;
    unsigned long long *nodes_free = NULL;
P
Peter Krempa 已提交
172 173
    bool all = vshCommandOptBool(cmd, "all");
    bool cellno = vshCommandOptBool(cmd, "cellno");
174
    size_t i;
175 176 177
    char *cap_xml = NULL;
    xmlDocPtr xml = NULL;
    xmlXPathContextPtr ctxt = NULL;
178
    virshControlPtr priv = ctl->privData;
179

P
Peter Krempa 已提交
180
    VSH_EXCLUSIVE_OPTIONS_VAR(all, cellno);
181

182
    if (cellno && vshCommandOptInt(ctl, cmd, "cellno", &cell) < 0)
P
Peter Krempa 已提交
183
        return false;
184

P
Peter Krempa 已提交
185
    if (all) {
186
        if (!(cap_xml = virConnectGetCapabilities(priv->conn))) {
187 188 189 190 191 192 193 194 195
            vshError(ctl, "%s", _("unable to get node capabilities"));
            goto cleanup;
        }

        xml = virXMLParseStringCtxt(cap_xml, _("(capabilities)"), &ctxt);
        if (!xml) {
            vshError(ctl, "%s", _("unable to get node capabilities"));
            goto cleanup;
        }
P
Peter Krempa 已提交
196

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
        nodes_cnt = virXPathNodeSet("/capabilities/host/topology/cells/cell",
                                    ctxt, &nodes);

        if (nodes_cnt == -1) {
            vshError(ctl, "%s", _("could not get information about "
                                  "NUMA topology"));
            goto cleanup;
        }

        nodes_free = vshCalloc(ctl, nodes_cnt, sizeof(*nodes_free));
        nodes_id = vshCalloc(ctl, nodes_cnt, sizeof(*nodes_id));

        for (i = 0; i < nodes_cnt; i++) {
            unsigned long id;
            char *val = virXMLPropString(nodes[i], "id");
            if (virStrToLong_ul(val, NULL, 10, &id)) {
                vshError(ctl, "%s", _("conversion from string failed"));
                VIR_FREE(val);
                goto cleanup;
            }
            VIR_FREE(val);
218
            nodes_id[i] = id;
219
            if (virNodeGetCellsFreeMemory(priv->conn, &(nodes_free[i]),
P
Peter Krempa 已提交
220
                                          id, 1) != 1) {
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
                vshError(ctl, _("failed to get free memory for NUMA node "
                                "number: %lu"), id);
                goto cleanup;
            }
        }

        for (cell = 0; cell < nodes_cnt; cell++) {
            vshPrint(ctl, "%5lu: %10llu KiB\n", nodes_id[cell],
                    (nodes_free[cell]/1024));
            memory += nodes_free[cell];
        }

        vshPrintExtra(ctl, "--------------------\n");
        vshPrintExtra(ctl, "%5s: %10llu KiB\n", _("Total"), memory/1024);
    } else {
P
Peter Krempa 已提交
236
        if (cellno) {
237
            if (virNodeGetCellsFreeMemory(priv->conn, &memory, cell, 1) != 1)
238
                goto cleanup;
P
Peter Krempa 已提交
239 240

            vshPrint(ctl, "%d: %llu KiB\n", cell, (memory/1024));
241
        } else {
242
            if ((memory = virNodeGetFreeMemory(priv->conn)) == 0)
243 244 245
                goto cleanup;

            vshPrint(ctl, "%s: %llu KiB\n", _("Total"), (memory/1024));
P
Peter Krempa 已提交
246
        }
247 248
    }

P
Peter Krempa 已提交
249
    ret = true;
250

251
 cleanup:
252 253 254 255 256 257
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);
    VIR_FREE(nodes);
    VIR_FREE(nodes_free);
    VIR_FREE(nodes_id);
    VIR_FREE(cap_xml);
P
Peter Krempa 已提交
258
    return ret;
259 260
}

261 262 263 264 265 266

/*
 * "freepages" command
 */
static const vshCmdInfo info_freepages[] = {
    {.name = "help",
L
Li Yang 已提交
267
     .data = N_("NUMA free pages")
268 269
    },
    {.name = "desc",
L
Li Yang 已提交
270
     .data = N_("display available free pages for the NUMA cell.")
271 272 273 274 275 276 277 278 279 280 281
    },
    {.name = NULL}
};

static const vshCmdOptDef opts_freepages[] = {
    {.name = "cellno",
     .type = VSH_OT_INT,
     .help = N_("NUMA cell number")
    },
    {.name = "pagesize",
     .type = VSH_OT_INT,
E
Eric Blake 已提交
282
     .help = N_("page size (in kibibytes)")
283 284 285 286 287 288 289 290
    },
    {.name = "all",
     .type = VSH_OT_BOOL,
     .help = N_("show free pages for all NUMA cells")
    },
    {.name = NULL}
};

291 292 293 294 295 296 297 298 299
static int
vshPageSizeSorter(const void *a, const void *b)
{
    unsigned int pa = *(unsigned int *)a;
    unsigned int pb = *(unsigned int *)b;

    return pa - pb;
}

300 301 302 303 304 305
static bool
cmdFreepages(vshControl *ctl, const vshCmd *cmd)
{
    bool ret = false;
    unsigned int npages;
    unsigned int *pagesize = NULL;
306 307
    unsigned long long bytes = 0;
    unsigned int kibibytes = 0;
308 309 310 311 312 313 314 315 316 317
    int cell;
    unsigned long long *counts = NULL;
    size_t i, j;
    xmlNodePtr *nodes = NULL;
    int nodes_cnt;
    char *cap_xml = NULL;
    xmlDocPtr doc = NULL;
    xmlXPathContextPtr ctxt = NULL;
    bool all = vshCommandOptBool(cmd, "all");
    bool cellno = vshCommandOptBool(cmd, "cellno");
318
    bool pagesz = vshCommandOptBool(cmd, "pagesize");
319
    virshControlPtr priv = ctl->privData;
320 321 322

    VSH_EXCLUSIVE_OPTIONS_VAR(all, cellno);

323
    if (vshCommandOptScaledInt(ctl, cmd, "pagesize", &bytes, 1024, UINT_MAX) < 0)
324 325 326
        goto cleanup;
    kibibytes = VIR_DIV_UP(bytes, 1024);

327
    if (all) {
328
        if (!(cap_xml = virConnectGetCapabilities(priv->conn))) {
329 330 331 332 333 334 335 336 337
            vshError(ctl, "%s", _("unable to get node capabilities"));
            goto cleanup;
        }

        if (!(doc = virXMLParseStringCtxt(cap_xml, _("capabilities"), &ctxt))) {
            vshError(ctl, "%s", _("unable to parse node capabilities"));
            goto cleanup;
        }

338 339
        if (!pagesz) {
            nodes_cnt = virXPathNodeSet("/capabilities/host/cpu/pages", ctxt, &nodes);
340

341
            if (nodes_cnt <= 0) {
342 343 344 345 346 347 348 349 350
                /* Some drivers don't export page sizes under the
                 * XPath above. Do another trick to get them. */
                nodes_cnt = virXPathNodeSet("/capabilities/host/topology/cells/cell/pages",
                                            ctxt, &nodes);
                if (nodes_cnt <= 0) {
                    vshError(ctl, "%s", _("could not get information about "
                                          "supported page sizes"));
                    goto cleanup;
                }
351
            }
352

353
            pagesize = vshCalloc(ctl, nodes_cnt, sizeof(*pagesize));
354

355 356 357 358 359 360 361 362
            for (i = 0; i < nodes_cnt; i++) {
                char *val = virXMLPropString(nodes[i], "size");

                if (virStrToLong_ui(val, NULL, 10, &pagesize[i]) < 0) {
                    vshError(ctl, _("unable to parse page size: %s"), val);
                    VIR_FREE(val);
                    goto cleanup;
                }
363 364 365 366

                VIR_FREE(val);
            }

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
            /* Here, if we've done the trick few lines above,
             * @pagesize array will contain duplicates. We should
             * remove them otherwise not very nice output will be
             * produced. */
            qsort(pagesize, nodes_cnt, sizeof(*pagesize), vshPageSizeSorter);

            for (i = 0; i < nodes_cnt - 1;) {
                if (pagesize[i] == pagesize[i + 1]) {
                    memmove(pagesize + i, pagesize + i + 1,
                            (nodes_cnt - i + 1) * sizeof(*pagesize));
                    nodes_cnt--;
                } else {
                    i++;
                }
            }

383 384 385 386 387 388
            npages = nodes_cnt;
            VIR_FREE(nodes);
        } else {
            pagesize = vshMalloc(ctl, sizeof(*pagesize));
            pagesize[0] = kibibytes;
            npages = 1;
389 390
        }

E
Eric Blake 已提交
391
        counts = vshCalloc(ctl, npages, sizeof(*counts));
392 393 394 395 396 397 398 399 400 401 402 403 404

        nodes_cnt = virXPathNodeSet("/capabilities/host/topology/cells/cell",
                                    ctxt, &nodes);
        for (i = 0; i < nodes_cnt; i++) {
            char *val = virXMLPropString(nodes[i], "id");

            if (virStrToLong_i(val, NULL, 10, &cell) < 0) {
                vshError(ctl, _("unable to parse numa node id: %s"), val);
                VIR_FREE(val);
                goto cleanup;
            }
            VIR_FREE(val);

405
            if (virNodeGetFreePages(priv->conn, npages, pagesize,
406 407 408 409
                                    cell, 1, counts, 0) < 0)
                goto cleanup;

            vshPrint(ctl, _("Node %d:\n"), cell);
410
            for (j = 0; j < npages; j++)
411 412 413 414 415 416 417 418 419 420
                vshPrint(ctl, "%uKiB: %lld\n", pagesize[j], counts[j]);
            vshPrint(ctl, "%c", '\n');
        }

    } else {
        if (!cellno) {
            vshError(ctl, "%s", _("missing cellno argument"));
            goto cleanup;
        }

421
        if (vshCommandOptInt(ctl, cmd, "cellno", &cell) < 0)
422 423 424
            goto cleanup;

        if (cell < -1) {
425 426
            vshError(ctl, "%s",
                     _("cell number must be non-negative integer or -1"));
427 428 429
            goto cleanup;
        }

430 431
        if (!pagesz) {
            vshError(ctl, "%s", _("missing pagesize argument"));
432 433
            goto cleanup;
        }
434

435
        /* page size is expected in kibibytes */
E
Eric Blake 已提交
436
        pagesize = vshMalloc(ctl, sizeof(*pagesize));
437
        pagesize[0] = kibibytes;
438 439 440

        counts = vshMalloc(ctl, sizeof(*counts));

441 442
        if (virNodeGetFreePages(priv->conn, 1, pagesize,
                                cell, 1, counts, 0) < 0)
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
            goto cleanup;

        vshPrint(ctl, "%uKiB: %lld\n", *pagesize, counts[0]);
    }

    ret = true;
 cleanup:
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(doc);
    VIR_FREE(cap_xml);
    VIR_FREE(nodes);
    VIR_FREE(counts);
    VIR_FREE(pagesize);
    return ret;
}


460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
/*
 * "allocpages" command
 */
static const vshCmdInfo info_allocpages[] = {
    {.name = "help",
     .data = N_("Manipulate pages pool size")
    },
    {.name = "desc",
     .data = N_("Allocate or free some pages in the pool for NUMA cell.")
    },
    {.name = NULL}
};
static const vshCmdOptDef opts_allocpages[] = {
    {.name = "pagesize",
     .type = VSH_OT_INT,
     .flags = VSH_OFLAG_REQ,
     .help = N_("page size (in kibibytes)")
    },
    {.name = "pagecount",
     .type = VSH_OT_INT,
     .flags = VSH_OFLAG_REQ,
     .help = N_("page count")
    },
    {.name = "cellno",
     .type = VSH_OT_INT,
     .help = N_("NUMA cell number")
    },
    {.name = "add",
     .type = VSH_OT_BOOL,
     .help = N_("instead of setting new pool size add pages to it")
    },
    {.name = "all",
     .type = VSH_OT_BOOL,
     .help = N_("set on all NUMA cells")
    },
    {.name = NULL}
};

static bool
cmdAllocpages(vshControl *ctl, const vshCmd *cmd)
{
    bool ret = false;
    bool add = vshCommandOptBool(cmd, "add");
    bool all = vshCommandOptBool(cmd, "all");
    bool cellno = vshCommandOptBool(cmd, "cellno");
    int startCell = -1;
    int cellCount = 1;
    unsigned int pageSizes[1];
    unsigned long long pageCounts[1], tmp;
    unsigned int flags = 0;
    char *cap_xml = NULL;
    xmlDocPtr xml = NULL;
    xmlXPathContextPtr ctxt = NULL;
    xmlNodePtr *nodes = NULL;
514
    virshControlPtr priv = ctl->privData;
515 516 517

    VSH_EXCLUSIVE_OPTIONS_VAR(all, cellno);

518
    if (cellno && vshCommandOptInt(ctl, cmd, "cellno", &startCell) < 0)
519 520
        return false;

521
    if (vshCommandOptScaledInt(ctl, cmd, "pagesize", &tmp, 1024, UINT_MAX) < 0)
522 523 524
        return false;
    pageSizes[0] = VIR_DIV_UP(tmp, 1024);

525
    if (vshCommandOptULongLong(ctl, cmd, "pagecount", &pageCounts[0]) < 0)
526 527 528 529 530 531 532 533
        return false;

    flags |= add ? VIR_NODE_ALLOC_PAGES_ADD : VIR_NODE_ALLOC_PAGES_SET;

    if (all) {
        unsigned long nodes_cnt;
        size_t i;

534
        if (!(cap_xml = virConnectGetCapabilities(priv->conn))) {
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
            vshError(ctl, "%s", _("unable to get node capabilities"));
            goto cleanup;
        }

        xml = virXMLParseStringCtxt(cap_xml, _("(capabilities)"), &ctxt);
        if (!xml) {
            vshError(ctl, "%s", _("unable to get node capabilities"));
            goto cleanup;
        }

        nodes_cnt = virXPathNodeSet("/capabilities/host/topology/cells/cell",
                                    ctxt, &nodes);

        if (nodes_cnt == -1) {
            vshError(ctl, "%s", _("could not get information about "
                                  "NUMA topology"));
            goto cleanup;
        }

        for (i = 0; i < nodes_cnt; i++) {
            unsigned long id;
            char *val = virXMLPropString(nodes[i], "id");
            if (virStrToLong_ul(val, NULL, 10, &id)) {
                vshError(ctl, "%s", _("conversion from string failed"));
                VIR_FREE(val);
                goto cleanup;
            }
            VIR_FREE(val);

564
            if (virNodeAllocPages(priv->conn, 1, pageSizes,
565 566 567 568
                                  pageCounts, id, 1, flags) < 0)
                goto cleanup;
        }
    } else {
569
        if (virNodeAllocPages(priv->conn, 1, pageSizes, pageCounts,
570 571 572 573 574 575 576 577 578 579 580 581 582 583
                              startCell, cellCount, flags) < 0)
            goto cleanup;
    }

    ret = true;
 cleanup:
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);
    VIR_FREE(nodes);
    VIR_FREE(cap_xml);
    return ret;
}


584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
/*
 * "maxvcpus" command
 */
static const vshCmdInfo info_maxvcpus[] = {
    {.name = "help",
     .data = N_("connection vcpu maximum")
    },
    {.name = "desc",
     .data = N_("Show maximum number of virtual CPUs for guests on this connection.")
    },
    {.name = NULL}
};

static const vshCmdOptDef opts_maxvcpus[] = {
    {.name = "type",
     .type = VSH_OT_STRING,
     .help = N_("domain type")
    },
    {.name = NULL}
};

static bool
cmdMaxvcpus(vshControl *ctl, const vshCmd *cmd)
{
    const char *type = NULL;
609 610 611 612
    int vcpus = -1;
    char *caps = NULL;
    xmlDocPtr xml = NULL;
    xmlXPathContextPtr ctxt = NULL;
613
    virshControlPtr priv = ctl->privData;
614
    bool ret = false;
615 616 617 618

    if (vshCommandOptStringReq(ctl, cmd, "type", &type) < 0)
        return false;

619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
    if ((caps = virConnectGetDomainCapabilities(priv->conn, NULL, NULL, NULL,
                                                type, 0))) {
        if (!(xml = virXMLParseStringCtxt(caps, _("(domainCapabilities)"), &ctxt)))
            goto cleanup;

        ignore_value(virXPathInt("string(./vcpu[1]/@max)", ctxt, &vcpus));
    } else {
        if (last_error && last_error->code != VIR_ERR_NO_SUPPORT)
            goto cleanup;

       vshResetLibvirtError();
    }

    if (vcpus < 0 && (vcpus = virConnectGetMaxVcpus(priv->conn, type)) < 0)
        goto cleanup;
634 635

    vshPrint(ctl, "%d\n", vcpus);
636
    ret = true;
637

638 639 640 641 642
 cleanup:
    xmlXPathFreeContext(ctxt);
    xmlFreeDoc(xml);
    VIR_FREE(caps);
    return ret;
643 644
}

645 646 647 648
/*
 * "nodeinfo" command
 */
static const vshCmdInfo info_nodeinfo[] = {
649 650 651 652 653 654 655
    {.name = "help",
     .data = N_("node information")
    },
    {.name = "desc",
     .data = N_("Returns basic information about the node.")
    },
    {.name = NULL}
656 657 658 659 660 661
};

static bool
cmdNodeinfo(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
{
    virNodeInfo info;
662
    virshControlPtr priv = ctl->privData;
663

664
    if (virNodeGetInfo(priv->conn, &info) < 0) {
665 666 667 668 669
        vshError(ctl, "%s", _("failed to get node information"));
        return false;
    }
    vshPrint(ctl, "%-20s %s\n", _("CPU model:"), info.model);
    vshPrint(ctl, "%-20s %d\n", _("CPU(s):"), info.cpus);
670 671
    if (info.mhz)
        vshPrint(ctl, "%-20s %d MHz\n", _("CPU frequency:"), info.mhz);
672 673 674 675 676 677 678 679 680
    vshPrint(ctl, "%-20s %d\n", _("CPU socket(s):"), info.sockets);
    vshPrint(ctl, "%-20s %d\n", _("Core(s) per socket:"), info.cores);
    vshPrint(ctl, "%-20s %d\n", _("Thread(s) per core:"), info.threads);
    vshPrint(ctl, "%-20s %d\n", _("NUMA cell(s):"), info.nodes);
    vshPrint(ctl, "%-20s %lu KiB\n", _("Memory size:"), info.memory);

    return true;
}

681 682 683 684
/*
 * "nodecpumap" command
 */
static const vshCmdInfo info_node_cpumap[] = {
685 686 687 688 689 690 691 692
    {.name = "help",
     .data = N_("node cpu map")
    },
    {.name = "desc",
     .data = N_("Displays the node's total number of CPUs, the number of"
                " online CPUs and the list of online CPUs.")
    },
    {.name = NULL}
693 694
};

695 696 697 698 699 700 701 702
static const vshCmdOptDef opts_node_cpumap[] = {
    {.name = "pretty",
     .type = VSH_OT_BOOL,
     .help = N_("return human readable output")
    },
    {.name = NULL}
};

703 704 705 706 707 708
static bool
cmdNodeCpuMap(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
{
    int cpu, cpunum;
    unsigned char *cpumap = NULL;
    unsigned int online;
709
    bool pretty = vshCommandOptBool(cmd, "pretty");
710
    bool ret = false;
711
    virshControlPtr priv = ctl->privData;
712

713
    cpunum = virNodeGetCPUMap(priv->conn, &cpumap, &online, 0);
714 715 716 717 718 719 720 721 722
    if (cpunum < 0) {
        vshError(ctl, "%s", _("Unable to get cpu map"));
        goto cleanup;
    }

    vshPrint(ctl, "%-15s %d\n", _("CPUs present:"), cpunum);
    vshPrint(ctl, "%-15s %d\n", _("CPUs online:"), online);

    vshPrint(ctl, "%-15s ", _("CPU map:"));
723
    if (pretty) {
724
        char *str = virBitmapDataToString(cpumap, VIR_CPU_MAPLEN(cpunum));
725 726 727 728 729 730 731 732 733

        if (!str)
            goto cleanup;
        vshPrint(ctl, "%s", str);
        VIR_FREE(str);
    } else {
        for (cpu = 0; cpu < cpunum; cpu++)
            vshPrint(ctl, "%c", VIR_CPU_USED(cpumap, cpu) ? 'y' : '-');
    }
734 735 736 737
    vshPrint(ctl, "\n");

    ret = true;

738
 cleanup:
739 740 741 742
    VIR_FREE(cpumap);
    return ret;
}

743 744 745 746
/*
 * "nodecpustats" command
 */
static const vshCmdInfo info_nodecpustats[] = {
747 748 749 750 751 752 753
    {.name = "help",
     .data = N_("Prints cpu stats of the node.")
    },
    {.name = "desc",
     .data = N_("Returns cpu stats of the node, in nanoseconds.")
    },
    {.name = NULL}
754 755 756
};

static const vshCmdOptDef opts_node_cpustats[] = {
757 758 759 760 761 762 763 764 765
    {.name = "cpu",
     .type = VSH_OT_INT,
     .help = N_("prints specified cpu statistics only.")
    },
    {.name = "percent",
     .type = VSH_OT_BOOL,
     .help = N_("prints by percentage during 1 second.")
    },
    {.name = NULL}
766 767
};

768
typedef enum {
769 770 771 772 773 774 775 776 777 778 779
    VIRSH_CPU_USER,
    VIRSH_CPU_SYSTEM,
    VIRSH_CPU_IDLE,
    VIRSH_CPU_IOWAIT,
    VIRSH_CPU_INTR,
    VIRSH_CPU_USAGE,
    VIRSH_CPU_LAST
} virshCPUStats;

VIR_ENUM_DECL(virshCPUStats);
VIR_ENUM_IMPL(virshCPUStats, VIRSH_CPU_LAST,
780 781 782 783 784 785 786
              VIR_NODE_CPU_STATS_USER,
              VIR_NODE_CPU_STATS_KERNEL,
              VIR_NODE_CPU_STATS_IDLE,
              VIR_NODE_CPU_STATS_IOWAIT,
              VIR_NODE_CPU_STATS_INTR,
              VIR_NODE_CPU_STATS_UTILIZATION);

787
const char *virshCPUOutput[] = {
788 789 790 791 792 793
    N_("user:"),
    N_("system:"),
    N_("idle:"),
    N_("iowait:"),
    N_("intr:"),
    N_("usage:")
794 795
};

796 797 798
static bool
cmdNodeCpuStats(vshControl *ctl, const vshCmd *cmd)
{
799
    size_t i, j;
800 801 802 803 804
    bool flag_percent = vshCommandOptBool(cmd, "percent");
    int cpuNum = VIR_NODE_CPU_STATS_ALL_CPUS;
    virNodeCPUStatsPtr params;
    int nparams = 0;
    bool ret = false;
805 806 807
    unsigned long long cpu_stats[VIRSH_CPU_LAST] = { 0 };
    bool present[VIRSH_CPU_LAST] = { false };
    virshControlPtr priv = ctl->privData;
808

809
    if (vshCommandOptInt(ctl, cmd, "cpu", &cpuNum) < 0)
810 811
        return false;

812
    if (virNodeGetCPUStats(priv->conn, cpuNum, NULL, &nparams, 0) != 0) {
813 814 815 816 817 818 819 820 821 822 823 824 825
        vshError(ctl, "%s",
                 _("Unable to get number of cpu stats"));
        return false;
    }
    if (nparams == 0) {
        /* nothing to output */
        return true;
    }

    memset(cpu_stats, 0, sizeof(cpu_stats));
    params = vshCalloc(ctl, nparams, sizeof(*params));

    for (i = 0; i < 2; i++) {
826
        if (virNodeGetCPUStats(priv->conn, cpuNum, params, &nparams, 0) != 0) {
827 828 829 830 831
            vshError(ctl, "%s", _("Unable to get node cpu stats"));
            goto cleanup;
        }

        for (j = 0; j < nparams; j++) {
832
            int field = virshCPUStatsTypeFromString(params[j].field);
833 834 835 836 837 838 839 840 841

            if (field < 0)
                continue;

            if (i == 0) {
                cpu_stats[field] = params[j].value;
                present[field] = true;
            } else if (present[field]) {
                cpu_stats[field] = params[j].value - cpu_stats[field];
842 843 844
            }
        }

845
        if (present[VIRSH_CPU_USAGE] || !flag_percent)
846
            break;
847 848

        sleep(1);
849 850 851
    }

    if (!flag_percent) {
852
        for (i = 0; i < VIRSH_CPU_USAGE; i++) {
853
            if (present[i]) {
854
                vshPrint(ctl, "%-15s %20llu\n", _(virshCPUOutput[i]),
855 856
                         cpu_stats[i]);
            }
857 858
        }
    } else {
859 860 861 862 863
        if (present[VIRSH_CPU_USAGE]) {
            vshPrint(ctl, "%-15s %5.1llu%%\n",
                     _("usage:"), cpu_stats[VIRSH_CPU_USAGE]);
            vshPrint(ctl, "%-15s %5.1llu%%\n",
                     _("idle:"), 100 - cpu_stats[VIRSH_CPU_USAGE]);
864 865
        } else {
            double usage, total_time = 0;
866
            for (i = 0; i < VIRSH_CPU_USAGE; i++)
867 868
                total_time += cpu_stats[i];

869 870
            usage = (cpu_stats[VIRSH_CPU_USER] + cpu_stats[VIRSH_CPU_SYSTEM])
                / total_time * 100;
871 872

            vshPrint(ctl, "%-15s %5.1lf%%\n", _("usage:"), usage);
873
            for (i = 0; i < VIRSH_CPU_USAGE; i++) {
874
                if (present[i]) {
875
                    vshPrint(ctl, "%-15s %5.1lf%%\n", _(virshCPUOutput[i]),
876 877 878
                             cpu_stats[i] / total_time * 100);
                }
            }
879 880 881 882 883
        }
    }

    ret = true;

884
 cleanup:
885 886 887 888 889 890 891 892
    VIR_FREE(params);
    return ret;
}

/*
 * "nodememstats" command
 */
static const vshCmdInfo info_nodememstats[] = {
893 894 895 896 897 898 899
    {.name = "help",
     .data = N_("Prints memory stats of the node.")
    },
    {.name = "desc",
     .data = N_("Returns memory stats of the node, in kilobytes.")
    },
    {.name = NULL}
900 901 902
};

static const vshCmdOptDef opts_node_memstats[] = {
903 904 905 906 907
    {.name = "cell",
     .type = VSH_OT_INT,
     .help = N_("prints specified cell statistics only.")
    },
    {.name = NULL}
908 909 910 911 912 913
};

static bool
cmdNodeMemStats(vshControl *ctl, const vshCmd *cmd)
{
    int nparams = 0;
914
    size_t i;
915 916 917
    int cellNum = VIR_NODE_MEMORY_STATS_ALL_CELLS;
    virNodeMemoryStatsPtr params = NULL;
    bool ret = false;
918
    virshControlPtr priv = ctl->privData;
919

920
    if (vshCommandOptInt(ctl, cmd, "cell", &cellNum) < 0)
921 922 923
        return false;

    /* get the number of memory parameters */
924
    if (virNodeGetMemoryStats(priv->conn, cellNum, NULL, &nparams, 0) != 0) {
925 926 927 928 929 930 931 932 933 934 935 936 937
        vshError(ctl, "%s",
                 _("Unable to get number of memory stats"));
        goto cleanup;
    }

    if (nparams == 0) {
        /* nothing to output */
        ret = true;
        goto cleanup;
    }

    /* now go get all the memory parameters */
    params = vshCalloc(ctl, nparams, sizeof(*params));
938
    if (virNodeGetMemoryStats(priv->conn, cellNum, params, &nparams, 0) != 0) {
939 940 941 942 943 944 945 946 947
        vshError(ctl, "%s", _("Unable to get memory stats"));
        goto cleanup;
    }

    for (i = 0; i < nparams; i++)
        vshPrint(ctl, "%-7s: %20llu KiB\n", params[i].field, params[i].value);

    ret = true;

948
 cleanup:
949 950 951 952 953 954 955 956
    VIR_FREE(params);
    return ret;
}

/*
 * "nodesuspend" command
 */
static const vshCmdInfo info_nodesuspend[] = {
957 958 959 960 961
    {.name = "help",
     .data = N_("suspend the host node for a given time duration")
    },
    {.name = "desc",
     .data = N_("Suspend the host node for a given time duration "
962
                "and attempt to resume thereafter.")
963 964
    },
    {.name = NULL}
965 966 967
};

static const vshCmdOptDef opts_node_suspend[] = {
968 969 970
    {.name = "target",
     .type = VSH_OT_DATA,
     .flags = VSH_OFLAG_REQ,
971 972
     .help = N_("mem(Suspend-to-RAM), disk(Suspend-to-Disk), "
                "hybrid(Hybrid-Suspend)")
973 974 975 976 977 978 979
    },
    {.name = "duration",
     .type = VSH_OT_INT,
     .flags = VSH_OFLAG_REQ,
     .help = N_("Suspend duration in seconds, at least 60")
    },
    {.name = NULL}
980 981 982 983 984 985 986 987
};

static bool
cmdNodeSuspend(vshControl *ctl, const vshCmd *cmd)
{
    const char *target = NULL;
    unsigned int suspendTarget;
    long long duration;
988
    virshControlPtr priv = ctl->privData;
989

990
    if (vshCommandOptStringReq(ctl, cmd, "target", &target) < 0)
991 992
        return false;

993
    if (vshCommandOptLongLong(ctl, cmd, "duration", &duration) < 0)
994 995
        return false;

996
    if (STREQ(target, "mem")) {
997
        suspendTarget = VIR_NODE_SUSPEND_TARGET_MEM;
998
    } else if (STREQ(target, "disk")) {
999
        suspendTarget = VIR_NODE_SUSPEND_TARGET_DISK;
1000
    } else if (STREQ(target, "hybrid")) {
1001
        suspendTarget = VIR_NODE_SUSPEND_TARGET_HYBRID;
1002
    } else {
1003 1004 1005 1006
        vshError(ctl, "%s", _("Invalid target"));
        return false;
    }

1007
    if (duration < 0) {
1008 1009 1010 1011
        vshError(ctl, "%s", _("Invalid duration"));
        return false;
    }

1012
    if (virNodeSuspendForDuration(priv->conn, suspendTarget, duration, 0) < 0) {
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
        vshError(ctl, "%s", _("The host was not suspended"));
        return false;
    }
    return true;
}

/*
 * "sysinfo" command
 */
static const vshCmdInfo info_sysinfo[] = {
1023 1024 1025 1026 1027 1028 1029
    {.name = "help",
     .data = N_("print the hypervisor sysinfo")
    },
    {.name = "desc",
     .data = N_("output an XML string for the hypervisor sysinfo, if available")
    },
    {.name = NULL}
1030 1031 1032 1033 1034 1035
};

static bool
cmdSysinfo(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
{
    char *sysinfo;
1036
    virshControlPtr priv = ctl->privData;
1037

1038
    sysinfo = virConnectGetSysinfo(priv->conn, 0);
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
    if (sysinfo == NULL) {
        vshError(ctl, "%s", _("failed to get sysinfo"));
        return false;
    }

    vshPrint(ctl, "%s", sysinfo);
    VIR_FREE(sysinfo);

    return true;
}

/*
 * "hostname" command
 */
static const vshCmdInfo info_hostname[] = {
1054 1055 1056 1057 1058 1059 1060
    {.name = "help",
     .data = N_("print the hypervisor hostname")
    },
    {.name = "desc",
     .data = ""
    },
    {.name = NULL}
1061 1062 1063 1064 1065 1066
};

static bool
cmdHostname(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
{
    char *hostname;
1067
    virshControlPtr priv = ctl->privData;
1068

1069
    hostname = virConnectGetHostname(priv->conn);
1070 1071 1072 1073 1074
    if (hostname == NULL) {
        vshError(ctl, "%s", _("failed to get hostname"));
        return false;
    }

1075
    vshPrint(ctl, "%s\n", hostname);
1076 1077 1078 1079 1080 1081 1082 1083 1084
    VIR_FREE(hostname);

    return true;
}

/*
 * "uri" command
 */
static const vshCmdInfo info_uri[] = {
1085 1086 1087 1088 1089 1090 1091
    {.name = "help",
     .data = N_("print the hypervisor canonical URI")
    },
    {.name = "desc",
     .data = ""
    },
    {.name = NULL}
1092 1093 1094 1095 1096 1097
};

static bool
cmdURI(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
{
    char *uri;
1098
    virshControlPtr priv = ctl->privData;
1099

1100
    uri = virConnectGetURI(priv->conn);
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
    if (uri == NULL) {
        vshError(ctl, "%s", _("failed to get URI"));
        return false;
    }

    vshPrint(ctl, "%s\n", uri);
    VIR_FREE(uri);

    return true;
}

1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
/*
 * "cpu-models" command
 */
static const vshCmdInfo info_cpu_models[] = {
    {.name = "help",
     .data = N_("CPU models")
    },
    {.name = "desc",
     .data = N_("Get the CPU models for an arch.")
    },
    {.name = NULL}
};

static const vshCmdOptDef opts_cpu_models[] = {
    {.name = "arch",
     .type = VSH_OT_DATA,
     .flags = VSH_OFLAG_REQ,
     .help = N_("architecture")
    },
    {.name = NULL}
};

static bool
cmdCPUModelNames(vshControl *ctl, const vshCmd *cmd)
{
    char **models;
    size_t i;
    int nmodels;
    const char *arch = NULL;
1141
    virshControlPtr priv = ctl->privData;
1142 1143 1144 1145

    if (vshCommandOptStringReq(ctl, cmd, "arch", &arch) < 0)
        return false;

1146
    nmodels = virConnectGetCPUModelNames(priv->conn, arch, &models, 0);
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
    if (nmodels < 0) {
        vshError(ctl, "%s", _("failed to get CPU model names"));
        return false;
    }

    for (i = 0; i < nmodels; i++) {
        vshPrint(ctl, "%s\n", models[i]);
        VIR_FREE(models[i]);
    }
    VIR_FREE(models);

    return true;
}

1161 1162 1163 1164
/*
 * "version" command
 */
static const vshCmdInfo info_version[] = {
1165 1166 1167 1168 1169 1170 1171
    {.name = "help",
     .data = N_("show version")
    },
    {.name = "desc",
     .data = N_("Display the system version information.")
    },
    {.name = NULL}
1172 1173 1174
};

static const vshCmdOptDef opts_version[] = {
1175 1176 1177 1178 1179
    {.name = "daemon",
     .type = VSH_OT_BOOL,
     .help = N_("report daemon version too")
    },
    {.name = NULL}
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
};

static bool
cmdVersion(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
{
    unsigned long hvVersion;
    const char *hvType;
    unsigned long libVersion;
    unsigned long includeVersion;
    unsigned long apiVersion;
    unsigned long daemonVersion;
    int ret;
    unsigned int major;
    unsigned int minor;
    unsigned int rel;
1195
    virshControlPtr priv = ctl->privData;
1196

1197
    hvType = virConnectGetType(priv->conn);
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
    if (hvType == NULL) {
        vshError(ctl, "%s", _("failed to get hypervisor type"));
        return false;
    }

    includeVersion = LIBVIR_VERSION_NUMBER;
    major = includeVersion / 1000000;
    includeVersion %= 1000000;
    minor = includeVersion / 1000;
    rel = includeVersion % 1000;
1208
    vshPrint(ctl, _("Compiled against library: libvirt %d.%d.%d\n"),
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
             major, minor, rel);

    ret = virGetVersion(&libVersion, hvType, &apiVersion);
    if (ret < 0) {
        vshError(ctl, "%s", _("failed to get the library version"));
        return false;
    }
    major = libVersion / 1000000;
    libVersion %= 1000000;
    minor = libVersion / 1000;
    rel = libVersion % 1000;
1220
    vshPrint(ctl, _("Using library: libvirt %d.%d.%d\n"),
1221 1222 1223 1224 1225 1226 1227 1228 1229
             major, minor, rel);

    major = apiVersion / 1000000;
    apiVersion %= 1000000;
    minor = apiVersion / 1000;
    rel = apiVersion % 1000;
    vshPrint(ctl, _("Using API: %s %d.%d.%d\n"), hvType,
             major, minor, rel);

1230
    ret = virConnectGetVersion(priv->conn, &hvVersion);
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
    if (ret < 0) {
        vshError(ctl, "%s", _("failed to get the hypervisor version"));
        return false;
    }
    if (hvVersion == 0) {
        vshPrint(ctl,
                 _("Cannot extract running %s hypervisor version\n"), hvType);
    } else {
        major = hvVersion / 1000000;
        hvVersion %= 1000000;
        minor = hvVersion / 1000;
        rel = hvVersion % 1000;

        vshPrint(ctl, _("Running hypervisor: %s %d.%d.%d\n"),
                 hvType, major, minor, rel);
    }

    if (vshCommandOptBool(cmd, "daemon")) {
1249
        ret = virConnectGetLibVersion(priv->conn, &daemonVersion);
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
        if (ret < 0) {
            vshError(ctl, "%s", _("failed to get the daemon version"));
        } else {
            major = daemonVersion / 1000000;
            daemonVersion %= 1000000;
            minor = daemonVersion / 1000;
            rel = daemonVersion % 1000;
            vshPrint(ctl, _("Running against daemon: %d.%d.%d\n"),
                     major, minor, rel);
        }
    }

    return true;
}
1264

1265 1266
static const vshCmdInfo info_node_memory_tune[] = {
    {"help", N_("Get or set node memory parameters")},
1267
    {"desc", N_("Get or set node memory parameters\n"
1268 1269 1270 1271 1272 1273
                "    To get the memory parameters, use following command: \n\n"
                "    virsh # node-memory-tune")},
    {NULL, NULL}
};

static const vshCmdOptDef opts_node_memory_tune[] = {
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
    {.name = "shm-pages-to-scan",
     .type = VSH_OT_INT,
     .help =  N_("number of pages to scan before the shared memory service "
                 "goes to sleep")
    },
    {.name = "shm-sleep-millisecs",
     .type = VSH_OT_INT,
     .help =  N_("number of millisecs the shared memory service should "
                 "sleep before next scan")
    },
    {.name = "shm-merge-across-nodes",
     .type = VSH_OT_INT,
     .help =  N_("Specifies if pages from different numa nodes can be merged")
    },
    {.name = NULL}
1289 1290 1291 1292 1293 1294 1295
};

static bool
cmdNodeMemoryTune(vshControl *ctl, const vshCmd *cmd)
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
1296
    int maxparams = 0;
1297
    unsigned int flags = 0;
1298
    unsigned int value;
1299
    bool ret = false;
1300
    int rc = -1;
1301
    size_t i;
1302
    virshControlPtr priv = ctl->privData;
1303

1304
    if ((rc = vshCommandOptUInt(ctl, cmd, "shm-pages-to-scan", &value)) < 0) {
1305
        goto cleanup;
1306
    } else if (rc > 0) {
1307 1308 1309 1310
        if (virTypedParamsAddUInt(&params, &nparams, &maxparams,
                                  VIR_NODE_MEMORY_SHARED_PAGES_TO_SCAN,
                                  value) < 0)
            goto save_error;
1311 1312
    }

1313
    if ((rc = vshCommandOptUInt(ctl, cmd, "shm-sleep-millisecs", &value)) < 0) {
1314
        goto cleanup;
1315
    } else if (rc > 0) {
1316 1317 1318 1319
        if (virTypedParamsAddUInt(&params, &nparams, &maxparams,
                                  VIR_NODE_MEMORY_SHARED_SLEEP_MILLISECS,
                                  value) < 0)
            goto save_error;
1320 1321
    }

1322
    if ((rc = vshCommandOptUInt(ctl, cmd, "shm-merge-across-nodes", &value)) < 0) {
1323
        goto cleanup;
1324
    } else if (rc > 0) {
1325 1326 1327 1328
        if (virTypedParamsAddUInt(&params, &nparams, &maxparams,
                                  VIR_NODE_MEMORY_SHARED_MERGE_ACROSS_NODES,
                                  value) < 0)
            goto save_error;
1329
    }
1330

1331 1332
    if (nparams == 0) {
        /* Get the number of memory parameters */
1333
        if (virNodeGetMemoryParameters(priv->conn, NULL, &nparams, flags) != 0) {
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
            vshError(ctl, "%s",
                     _("Unable to get number of memory parameters"));
            goto cleanup;
        }

        if (nparams == 0) {
            ret = true;
            goto cleanup;
        }

        /* Now go get all the memory parameters */
        params = vshCalloc(ctl, nparams, sizeof(*params));
1346
        if (virNodeGetMemoryParameters(priv->conn, params, &nparams, flags) != 0) {
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
            vshError(ctl, "%s", _("Unable to get memory parameters"));
            goto cleanup;
        }

        /* XXX: Need to sort the returned params once new parameter
         * fields not of shared memory are added.
         */
        vshPrint(ctl, _("Shared memory:\n"));
        for (i = 0; i < nparams; i++) {
            char *str = vshGetTypedParamValue(ctl, &params[i]);
            vshPrint(ctl, "\t%-15s %s\n", params[i].field, str);
            VIR_FREE(str);
        }
    } else {
1361
        if (virNodeSetMemoryParameters(priv->conn, params, nparams, flags) != 0)
1362 1363 1364
            goto error;
    }

1365 1366
    ret = true;

1367
 cleanup:
1368
    virTypedParamsFree(params, nparams);
1369 1370
    return ret;

1371
 save_error:
1372
    vshSaveLibvirtError();
1373
 error:
1374 1375 1376 1377
    vshError(ctl, "%s", _("Unable to change memory parameters"));
    goto cleanup;
}

E
Eric Blake 已提交
1378
const vshCmdDef hostAndHypervisorCmds[] = {
1379 1380 1381
    {.name = "allocpages",
     .handler = cmdAllocpages,
     .opts = opts_allocpages,
1382
     .info = info_allocpages,
1383 1384
     .flags = 0
    },
1385 1386 1387 1388 1389 1390
    {.name = "capabilities",
     .handler = cmdCapabilities,
     .opts = NULL,
     .info = info_capabilities,
     .flags = 0
    },
1391 1392 1393 1394 1395 1396
    {.name = "cpu-models",
     .handler = cmdCPUModelNames,
     .opts = opts_cpu_models,
     .info = info_cpu_models,
     .flags = 0
    },
1397 1398 1399 1400 1401 1402
    {.name = "domcapabilities",
     .handler = cmdDomCapabilities,
     .opts = opts_domcapabilities,
     .info = info_domcapabilities,
     .flags = 0
    },
1403 1404 1405 1406 1407 1408
    {.name = "freecell",
     .handler = cmdFreecell,
     .opts = opts_freecell,
     .info = info_freecell,
     .flags = 0
    },
1409 1410 1411 1412 1413 1414
    {.name = "freepages",
     .handler = cmdFreepages,
     .opts = opts_freepages,
     .info = info_freepages,
     .flags = 0
    },
1415 1416 1417 1418 1419 1420
    {.name = "hostname",
     .handler = cmdHostname,
     .opts = NULL,
     .info = info_hostname,
     .flags = 0
    },
1421 1422 1423 1424 1425 1426
    {.name = "maxvcpus",
     .handler = cmdMaxvcpus,
     .opts = opts_maxvcpus,
     .info = info_maxvcpus,
     .flags = 0
    },
1427 1428 1429 1430 1431 1432 1433 1434
    {.name = "node-memory-tune",
     .handler = cmdNodeMemoryTune,
     .opts = opts_node_memory_tune,
     .info = info_node_memory_tune,
     .flags = 0
    },
    {.name = "nodecpumap",
     .handler = cmdNodeCpuMap,
1435
     .opts = opts_node_cpumap,
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
     .info = info_node_cpumap,
     .flags = 0
    },
    {.name = "nodecpustats",
     .handler = cmdNodeCpuStats,
     .opts = opts_node_cpustats,
     .info = info_nodecpustats,
     .flags = 0
    },
    {.name = "nodeinfo",
     .handler = cmdNodeinfo,
     .opts = NULL,
     .info = info_nodeinfo,
     .flags = 0
    },
    {.name = "nodememstats",
     .handler = cmdNodeMemStats,
     .opts = opts_node_memstats,
     .info = info_nodememstats,
     .flags = 0
    },
    {.name = "nodesuspend",
     .handler = cmdNodeSuspend,
     .opts = opts_node_suspend,
     .info = info_nodesuspend,
     .flags = 0
    },
    {.name = "sysinfo",
     .handler = cmdSysinfo,
     .opts = NULL,
     .info = info_sysinfo,
     .flags = 0
    },
    {.name = "uri",
     .handler = cmdURI,
     .opts = NULL,
     .info = info_uri,
     .flags = 0
    },
    {.name = "version",
     .handler = cmdVersion,
     .opts = opts_version,
     .info = info_version,
     .flags = 0
    },
    {.name = NULL}
1482
};