From patchwork Thu Jun 30 16:40:23 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Cooper X-Patchwork-Id: 9208615 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 30A0360752 for ; Thu, 30 Jun 2016 16:43:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 17118281C3 for ; Thu, 30 Jun 2016 16:43:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0ADF7283AC; Thu, 30 Jun 2016 16:43:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 01279281C3 for ; Thu, 30 Jun 2016 16:43:19 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bIf11-0002ir-Fe; Thu, 30 Jun 2016 16:40:35 +0000 Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bIf10-0002il-1n for xen-devel@lists.xen.org; Thu, 30 Jun 2016 16:40:34 +0000 Received: from [193.109.254.147] by server-7.bemta-14.messagelabs.com id 44/C5-09881-18B45775; Thu, 30 Jun 2016 16:40:33 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrGLMWRWlGSWpSXmKPExsXitHSDvW6Dd2m 4wZUeTYslHxezODB6HN39mymAMYo1My8pvyKBNePyzevMBZ3OFU9mvmNsYFxg2MXIySEh4C+x f/4ONhCbTUBfYveLT0wgtoiAusTpjousIDazQJnE1EmvmUFsYYEQiS8H/zGC2CwCqhIX/nWyd zFycPAKeEg07y+AGCkncf74T7ByIQE1iWv9l9hBbF4BQYmTM5+wQIyUkDj44gUzSKuEALfE32 77CYw8s5BUzUJStYCRaRWjenFqUVlqka65XlJRZnpGSW5iZo6uoaGJXm5qcXFiempOYlKxXnJ +7iZGYHgwAMEOxi9LnA8xSnIwKYnyPnIuDRfiS8pPqcxILM6ILyrNSS0+xCjDwaEkwSvhBZQT LEpNT61Iy8wBBipMWoKDR0mEVxkkzVtckJhbnJkOkTrFqCglzpsDkhAASWSU5sG1waLjEqOsl DAvI9AhQjwFqUW5mSWo8q8YxTkYlYR5tUCm8GTmlcBNfwW0mAloMXNpMcjikkSElFQDo5Tfsv Pts/7IzdSO/9J//mzHpy2vI+ykTGIaXQ5MkHIXDs68Xu7JKVxspVcodqj0m399r6azk0rkG7O 0j1v0fm75FrnNJzV89pPM465GL55ulbY1mFAYNyVQLqv5Rdr53GULdngcL1YMCVtSs1qx43Lr lYLDdqx3TnGsbKpdEhelk+nKefyxEktxRqKhFnNRcSIA2gYFKIkCAAA= X-Env-Sender: prvs=9824fb700=Andrew.Cooper3@citrix.com X-Msg-Ref: server-10.tower-27.messagelabs.com!1467304831!51182488!1 X-Originating-IP: [66.165.176.63] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni42MyA9PiAzMDYwNDg=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 8.46; banners=-,-,- X-VirusChecked: Checked Received: (qmail 4856 invoked from network); 30 Jun 2016 16:40:32 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-10.tower-27.messagelabs.com with RC4-SHA encrypted SMTP; 30 Jun 2016 16:40:32 -0000 X-IronPort-AV: E=Sophos;i="5.26,553,1459814400"; d="scan'208";a="370677940" From: Andrew Cooper To: Xen-devel Date: Thu, 30 Jun 2016 17:40:23 +0100 Message-ID: <1467304823-9328-1-git-send-email-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.1.4 MIME-Version: 1.0 X-DLP: MIA1 Cc: Andrew Cooper , Ian Jackson , Wei Liu Subject: [Xen-devel] [PATCH] tools/xl: Allow callers of `xl info` to select specific information X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP When scripting, it is much more convenient to use: [root@fusebot ~]# xl info xen_version 4.8-unstable than to construct some sed/awk/other to parse: [root@fusebot ~]# xl info ... xen_version : 4.8-unstable ... This works by wrapping all printf() calls in main_info() with maybe_printf(), which formats its arguments, compares the resulting string to the provided restriction, and discards it if no match is found. A restriction like this doesn't make sense in combination with --numa, so is excluded in that case. Signed-off-by: Andrew Cooper Acked-by: Wei Liu --- CC: Ian Jackson CC: Wei Liu Embarrassingly, this has been kicking around in the XenServer patch queue since Xen 4.4. It is high time it got sent upstream. This patch is far more easily reviewed with `git diff --color-words` --- tools/libxl/xl_cmdimpl.c | 118 +++++++++++++++++++++++++++++++---------------- 1 file changed, 77 insertions(+), 41 deletions(-) diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c index 6459eec..d1fcfa4 100644 --- a/tools/libxl/xl_cmdimpl.c +++ b/tools/libxl/xl_cmdimpl.c @@ -5928,6 +5928,33 @@ int main_vcpuset(int argc, char **argv) return EXIT_SUCCESS; } +/* Possibly select a specific piece of `xl info` to print. */ +static const char *info_name; +static int maybe_printf(const char *fmt, ...) __attribute__((format(printf,1,2))); +static int maybe_printf(const char *fmt, ...) +{ + va_list ap; + char *str; + int count = 0; + + va_start(ap, fmt); + if (vasprintf(&str, fmt, ap) != -1) { + if (info_name) { + char *s; + + if (!strncmp(str, info_name, strlen(info_name)) && + (s = strchr(str, ':')) && s[1] == ' ') + count = fputs(&s[2], stdout); + } else + count = fputs(str, stdout); + + free(str); + } + va_end(ap); + + return count; +} + static void output_xeninfo(void) { const libxl_version_info *info; @@ -5946,22 +5973,22 @@ static void output_xeninfo(void) } sched = rc; - printf("xen_major : %d\n", info->xen_version_major); - printf("xen_minor : %d\n", info->xen_version_minor); - printf("xen_extra : %s\n", info->xen_version_extra); - printf("xen_version : %d.%d%s\n", info->xen_version_major, + maybe_printf("xen_major : %d\n", info->xen_version_major); + maybe_printf("xen_minor : %d\n", info->xen_version_minor); + maybe_printf("xen_extra : %s\n", info->xen_version_extra); + maybe_printf("xen_version : %d.%d%s\n", info->xen_version_major, info->xen_version_minor, info->xen_version_extra); - printf("xen_caps : %s\n", info->capabilities); - printf("xen_scheduler : %s\n", libxl_scheduler_to_string(sched)); - printf("xen_pagesize : %u\n", info->pagesize); - printf("platform_params : virt_start=0x%"PRIx64"\n", info->virt_start); - printf("xen_changeset : %s\n", info->changeset); - printf("xen_commandline : %s\n", info->commandline); - printf("cc_compiler : %s\n", info->compiler); - printf("cc_compile_by : %s\n", info->compile_by); - printf("cc_compile_domain : %s\n", info->compile_domain); - printf("cc_compile_date : %s\n", info->compile_date); - printf("build_id : %s\n", info->build_id); + maybe_printf("xen_caps : %s\n", info->capabilities); + maybe_printf("xen_scheduler : %s\n", libxl_scheduler_to_string(sched)); + maybe_printf("xen_pagesize : %u\n", info->pagesize); + maybe_printf("platform_params : virt_start=0x%"PRIx64"\n", info->virt_start); + maybe_printf("xen_changeset : %s\n", info->changeset); + maybe_printf("xen_commandline : %s\n", info->commandline); + maybe_printf("cc_compiler : %s\n", info->compiler); + maybe_printf("cc_compile_by : %s\n", info->compile_by); + maybe_printf("cc_compile_domain : %s\n", info->compile_domain); + maybe_printf("cc_compile_date : %s\n", info->compile_date); + maybe_printf("build_id : %s\n", info->build_id); return; } @@ -5973,10 +6000,10 @@ static void output_nodeinfo(void) if (uname(&utsbuf) < 0) return; - printf("host : %s\n", utsbuf.nodename); - printf("release : %s\n", utsbuf.release); - printf("version : %s\n", utsbuf.version); - printf("machine : %s\n", utsbuf.machine); + maybe_printf("host : %s\n", utsbuf.nodename); + maybe_printf("release : %s\n", utsbuf.release); + maybe_printf("version : %s\n", utsbuf.version); + maybe_printf("machine : %s\n", utsbuf.machine); } static void output_physinfo(void) @@ -5991,35 +6018,37 @@ static void output_physinfo(void) fprintf(stderr, "libxl_physinfo failed.\n"); return; } - printf("nr_cpus : %d\n", info.nr_cpus); - printf("max_cpu_id : %d\n", info.max_cpu_id); - printf("nr_nodes : %d\n", info.nr_nodes); - printf("cores_per_socket : %d\n", info.cores_per_socket); - printf("threads_per_core : %d\n", info.threads_per_core); - printf("cpu_mhz : %d\n", info.cpu_khz / 1000); - printf("hw_caps : "); - for (i = 0; i < 8; i++) - printf("%08x%c", info.hw_cap[i], i < 7 ? ':' : '\n'); - printf("virt_caps :"); - if (info.cap_hvm) - printf(" hvm"); - if (info.cap_hvm_directio) - printf(" hvm_directio"); - printf("\n"); + maybe_printf("nr_cpus : %d\n", info.nr_cpus); + maybe_printf("max_cpu_id : %d\n", info.max_cpu_id); + maybe_printf("nr_nodes : %d\n", info.nr_nodes); + maybe_printf("cores_per_socket : %d\n", info.cores_per_socket); + maybe_printf("threads_per_core : %d\n", info.threads_per_core); + maybe_printf("cpu_mhz : %d\n", info.cpu_khz / 1000); + + maybe_printf("hw_caps : %08x:%08x:%08x:%08x:%08x:%08x:%08x:%08x\n", + info.hw_cap[0], info.hw_cap[1], info.hw_cap[2], info.hw_cap[3], + info.hw_cap[4], info.hw_cap[5], info.hw_cap[6], info.hw_cap[7] + ); + + maybe_printf("virt_caps :%s%s\n", + info.cap_hvm ? " hvm" : "", + info.cap_hvm_directio ? " hvm_directio" : "" + ); + vinfo = libxl_get_version_info(ctx); if (vinfo) { i = (1 << 20) / vinfo->pagesize; - printf("total_memory : %"PRIu64"\n", info.total_pages / i); - printf("free_memory : %"PRIu64"\n", (info.free_pages - info.outstanding_pages) / i); - printf("sharing_freed_memory : %"PRIu64"\n", info.sharing_freed_pages / i); - printf("sharing_used_memory : %"PRIu64"\n", info.sharing_used_frames / i); - printf("outstanding_claims : %"PRIu64"\n", info.outstanding_pages / i); + maybe_printf("total_memory : %"PRIu64"\n", info.total_pages / i); + maybe_printf("free_memory : %"PRIu64"\n", (info.free_pages - info.outstanding_pages) / i); + maybe_printf("sharing_freed_memory : %"PRIu64"\n", info.sharing_freed_pages / i); + maybe_printf("sharing_used_memory : %"PRIu64"\n", info.sharing_used_frames / i); + maybe_printf("outstanding_claims : %"PRIu64"\n", info.outstanding_pages / i); } if (!libxl_get_freecpus(ctx, &cpumap)) { libxl_for_each_bit(i, cpumap) if (libxl_bitmap_test(&cpumap, i)) n++; - printf("free_cpus : %d\n", n); + maybe_printf("free_cpus : %d\n", n); free(cpumap.map); } libxl_physinfo_dispose(&info); @@ -6119,7 +6148,7 @@ static void print_info(int numa) } output_xeninfo(); - printf("xend_config_format : 4\n"); + maybe_printf("xend_config_format : 4\n"); return; } @@ -6139,6 +6168,13 @@ int main_info(int argc, char **argv) break; } + /* + * If an extra argument is provided, filter out a specific piece of + * information. + */ + if (numa == 0 && argc > optind) + info_name = argv[optind]; + print_info(numa); return 0; }