From mboxrd@z Thu Jan 1 00:00:00 1970 From: Sasha Levin Subject: [PATCH 7/7] kvm tools: Improve 'kvm balloon' parameters Date: Fri, 12 Aug 2011 18:21:00 +0300 Message-ID: <1313162460-14397-7-git-send-email-levinsasha928@gmail.com> References: <1313162460-14397-1-git-send-email-levinsasha928@gmail.com> Cc: kvm@vger.kernel.org, mingo@elte.hu, asias.hejun@gmail.com, gorcunov@gmail.com, Sasha Levin To: penberg@kernel.org Return-path: Received: from mail-wy0-f174.google.com ([74.125.82.174]:61789 "EHLO mail-wy0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751963Ab1HLPWW (ORCPT ); Fri, 12 Aug 2011 11:22:22 -0400 Received: by mail-wy0-f174.google.com with SMTP id 24so2218022wyg.19 for ; Fri, 12 Aug 2011 08:22:21 -0700 (PDT) In-Reply-To: <1313162460-14397-1-git-send-email-levinsasha928@gmail.com> Sender: kvm-owner@vger.kernel.org List-ID: kvm balloon now uses the git option parser to parse parameters. Added option to balloon instance by PID. Improved usage message. Signed-off-by: Sasha Levin --- tools/kvm/builtin-balloon.c | 61 ++++++++++++++++++++++++++++++++---------- 1 files changed, 46 insertions(+), 15 deletions(-) diff --git a/tools/kvm/builtin-balloon.c b/tools/kvm/builtin-balloon.c index 08795cd..5dfd2e4 100644 --- a/tools/kvm/builtin-balloon.c +++ b/tools/kvm/builtin-balloon.c @@ -8,13 +8,29 @@ #include #include +static u64 instance_pid; +static const char *instance_name; +static u64 inflate; +static u64 deflate; + static const char * const balloon_usage[] = { "kvm balloon {inflate|deflate} ", NULL }; +static const char * const debug_usage[] = { + "kvm balloon [-n name] [-p pid] [-i amount] [-d amount]", + NULL +}; + static const struct option balloon_options[] = { - OPT_END() + OPT_GROUP("Instance options:"), + OPT_STRING('n', "name", &instance_name, "name", "Instance name"), + OPT_U64('p', "pid", &instance_pid, "Instance pid"), + OPT_GROUP("Balloon options:"), + OPT_U64('i', "inflate", &inflate, "Amount to inflate"), + OPT_U64('d', "deflate", &deflate, "Amount to deflate"), + OPT_END(), }; void kvm_balloon_help(void) @@ -22,28 +38,43 @@ void kvm_balloon_help(void) usage_with_options(balloon_usage, balloon_options); } +static void parse_balloon_options(int argc, const char **argv) +{ + while (argc != 0) { + argc = parse_options(argc, argv, balloon_options, balloon_usage, + PARSE_OPT_STOP_AT_NON_OPTION); + if (argc != 0) + kvm_balloon_help(); + } +} + int kvm_cmd_balloon(int argc, const char **argv, const char *prefix) { - int pid; - int amount, i; - int inflate = 0; + u64 i; + + parse_balloon_options(argc, argv); - if (argc != 3) + if (inflate == 0 && deflate == 0) kvm_balloon_help(); - pid = kvm__get_pid_by_instance(argv[2]); - if (pid < 0) - die("Failed locating instance name"); + if (instance_name == NULL && + instance_pid == 0) + kvm_balloon_help(); - if (strcmp(argv[0], "inflate") == 0) - inflate = 1; - else if (strcmp(argv[0], "deflate")) - die("command can be either 'inflate' or 'deflate'"); + if (instance_name) + instance_pid = kvm__get_pid_by_instance(argv[0]); - amount = atoi(argv[1]); + if (instance_pid <= 0) + die("Failed locating instance"); - for (i = 0; i < amount; i++) - kill(pid, inflate ? SIGKVMADDMEM : SIGKVMDELMEM); + if (inflate) + for (i = 0; i < inflate; i++) + kill(instance_pid, SIGKVMADDMEM); + else if (deflate) + for (i = 0; i < deflate; i++) + kill(instance_pid, SIGKVMDELMEM); + else + kvm_balloon_help(); return 0; } -- 1.7.6