virNodeGetCpuTime: Implement virsh support Add nodecputime subcommand to virsh. This subcommand prints below output. # ./virsh nodecputime user : 4280000000000.0s system: 13620000000000.0s idle : 1697380000000000.0s iowait: 76120000000000.0s # ./virsh nodecputime --percent user : 6.4% system: 3.2% idle : 89.6% iowait: 0.8 --- tools/virsh.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/virsh.pod | 6 +++++ 2 files changed, 72 insertions(+), 0 deletions(-) diff --git a/tools/virsh.c b/tools/virsh.c index faeaf47..5da35d8 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -3387,6 +3387,71 @@ cmdNodeinfo(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED) } /* + * "nodecputime" command + */ +static const vshCmdInfo info_nodecputime[] = { + {"help", N_("Prints cpu time of the node.")}, + {"desc", N_("Returns user/system/idle/iowait time of the node.(nsec)")}, + {NULL, NULL} +}; + +static const vshCmdOptDef opts_node_cpu_time[] = { + {"percent", VSH_OT_BOOL, 0, N_("prints by percentage during 1 second.")}, + {NULL, 0, 0, NULL} +}; + +#define NSEC_TO_SEC ((double)1.0 / 1000 * 1000 * 1000) +static int +cmdNodeCpuTime(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED) +{ + virNodeCpuTime cpu_time[2]; + int percent = vshCommandOptBool(cmd, "percent"); + double user, sys, idle, iowait, total; + + if (!vshConnectionUsability(ctl, ctl->conn)) + return FALSE; + + memset(&cpu_time, 0, sizeof(cpu_time)); + + if (virNodeGetCpuTime(ctl->conn, &cpu_time[0]) < 0) { + vshError(ctl, "%s", _("failed to get cpu time of the node.")); + return FALSE; + } + + if (!percent) { + vshPrint(ctl, "%-15s %.1lfs\n", _("user :"), + cpu_time[0].user * NSEC_TO_SEC); + vshPrint(ctl, "%-15s %.1lfs\n", _("system:"), + cpu_time[0].system * NSEC_TO_SEC); + vshPrint(ctl, "%-15s %.1lfs\n", _("idle :"), + cpu_time[0].idle * NSEC_TO_SEC); + vshPrint(ctl, "%-15s %.1lfs\n", _("iowait:"), + cpu_time[0].iowait * NSEC_TO_SEC); + return TRUE; + } + + sleep(1); + + if (virNodeGetCpuTime(ctl->conn, &cpu_time[1]) < 0) { + vshError(ctl, "%s", _("failed to get cpu time of the node.")); + return FALSE; + } + + user = cpu_time[1].user - cpu_time[0].user; + sys = cpu_time[1].system - cpu_time[0].system; + idle = cpu_time[1].idle - cpu_time[0].idle; + iowait = cpu_time[1].iowait - cpu_time[0].iowait; + total = user + sys + idle + iowait; + + vshPrint(ctl, "%-15s %5.1lf%%\n", _("user :"), user / total * 100); + vshPrint(ctl, "%-15s %5.1lf%%\n", _("system:"), sys / total * 100); + vshPrint(ctl, "%-15s %5.1lf%%\n", _("idle :"), idle / total * 100); + vshPrint(ctl, "%-15s %5.1lf%%\n", _("iowait:"), iowait / total * 100); + + return TRUE; +} + +/* * "capabilities" command */ static const vshCmdInfo info_capabilities[] = { @@ -10851,6 +10916,7 @@ static const vshCmdDef hostAndHypervisorCmds[] = { {"freecell", cmdFreecell, opts_freecell, info_freecell}, {"hostname", cmdHostname, NULL, info_hostname}, {"nodeinfo", cmdNodeinfo, NULL, info_nodeinfo}, + {"nodecputime", cmdNodeCpuTime, opts_node_cpu_time, info_nodecputime}, {"qemu-monitor-command", cmdQemuMonitorCommand, opts_qemu_monitor_command, info_qemu_monitor_command}, {"sysinfo", cmdSysinfo, NULL, info_sysinfo}, {"uri", cmdURI, NULL, info_uri}, diff --git a/tools/virsh.pod b/tools/virsh.pod index e882261..244b097 100644 --- a/tools/virsh.pod +++ b/tools/virsh.pod @@ -237,6 +237,12 @@ Print the XML representation of the hypervisor sysinfo, if available. Returns basic information about the node, like number and type of CPU, and size of the physical memory. +=item B<nodecputime> optional I<--percent> + +Returns user/system/idle/iowait cpu time of the node, if I<--percent> +is specified this will prints percentage of each kind of cpu time +during 1 second. + =item B<capabilities> Print an XML document describing the capabilities of the hypervisor -- 1.7.1 -- Minoru Usui <usui@xxxxxxxxxxxxxxxxx> -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list