public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <levinsasha928@gmail.com>
To: penberg@kernel.org
Cc: kvm@vger.kernel.org, mingo@elte.hu, asias.hejun@gmail.com,
	gorcunov@gmail.com, Sasha Levin <levinsasha928@gmail.com>
Subject: [PATCH 7/7] kvm tools: Improve 'kvm balloon' parameters
Date: Fri, 12 Aug 2011 18:21:00 +0300	[thread overview]
Message-ID: <1313162460-14397-7-git-send-email-levinsasha928@gmail.com> (raw)
In-Reply-To: <1313162460-14397-1-git-send-email-levinsasha928@gmail.com>

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 <levinsasha928@gmail.com>
---
 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 <kvm/parse-options.h>
 #include <kvm/kvm.h>
 
+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} <size in MiB> <instance name>",
 	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


  parent reply	other threads:[~2011-08-12 15:22 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-12 15:20 [PATCH 1/7] kvm tools: Print version when running 'kvm --version' Sasha Levin
2011-08-12 15:20 ` [PATCH 2/7] kvm tools: Connect existing command helpers to 'kvm help' Sasha Levin
2011-08-12 15:20 ` [PATCH 3/7] kvm tools: Improve 'kvm stop' parameters Sasha Levin
2011-08-12 15:20 ` [PATCH 4/7] kvm tools: Improve 'kvm resume' parameters Sasha Levin
2011-08-12 15:20 ` [PATCH 5/7] kvm tools: Improve 'kvm pause' parameters Sasha Levin
2011-08-12 15:20 ` [PATCH 6/7] kvm tools: Improve 'kvm debug' parameters Sasha Levin
2011-08-12 15:21 ` Sasha Levin [this message]
2011-08-12 15:22 ` [PATCH 1/7] kvm tools: Print version when running 'kvm --version' walimis
2011-08-12 15:39   ` Pekka Enberg
2011-08-12 15:43     ` walimis
2011-08-12 15:47   ` Sasha Levin
2011-08-12 15:45     ` walimis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1313162460-14397-7-git-send-email-levinsasha928@gmail.com \
    --to=levinsasha928@gmail.com \
    --cc=asias.hejun@gmail.com \
    --cc=gorcunov@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=penberg@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox