Skip to contentSkip to conversation

kvm tools: Don't use all of host RAM for guests by default

PATCH New Pekka Enberg 1 participant 2 messages May 7, 2011 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 kvm tools: Don't use all of host RAM for guests by default

This patch fixes the default guest RAM size maximum to 80% of the host RAM to avoid swapping the host to death.

Cc Asias He <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Prasad Joshi <[email protected]>
Cc Sasha Levin <[email protected]>
---
 tools/kvm/kvm-run.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
tools/kvm/kvm-run.c +7 -1
index ec1e462..b9cdec2 100644
@@ -213,6 +213,12 @@ static void kernel_usage_with_options(void)
 	fprintf(stderr, "\nPlease see 'kvm run --help' for more options.\n\n");
 }
 
+/*
+ * If user didn't specify how much memory it wants to allocate for the guest,
+ * avoid filling the whole host RAM.
+ */
+#define RAM_SIZE_RATIO		0.8
+
 static u64 get_ram_size(int nr_cpus)
 {
 	long available;
@@ -226,7 +232,7 @@ static u64 get_ram_size(int nr_cpus)
 
 	page_size	= sysconf(_SC_PAGE_SIZE);
 
-	available	= (nr_pages * page_size) >> MB_SHIFT;
+	available	= ((nr_pages * page_size) >> MB_SHIFT) * RAM_SIZE_RATIO;
 
 	if (ram_size > available)
 		ram_size	= available;
Signature · 1 line
1.7.0.4
#1 lore raw

PATCH 2/2 kvm tools: Warn if guest RAM size exceeds host RAM size

Guest memory size that's larger than host physical RAM can cause swap deaths on the host so warn the user about it.

Cc Asias He <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Prasad Joshi <[email protected]>
Cc Sasha Levin <[email protected]>
---
 tools/kvm/kvm-run.c |   23 ++++++++++++++++-------
 1 files changed, 16 insertions(+), 7 deletions(-)
tools/kvm/kvm-run.c +16 -7
index b9cdec2..764a242 100644
@@ -213,6 +213,18 @@ static void kernel_usage_with_options(void)
 	fprintf(stderr, "\nPlease see 'kvm run --help' for more options.\n\n");
 }
 
+static u64 host_ram_size(void)
+{
+	long page_size;
+	long nr_pages;
+
+	nr_pages	= sysconf(_SC_PHYS_PAGES);
+
+	page_size	= sysconf(_SC_PAGE_SIZE);
+
+	return (nr_pages * page_size) >> MB_SHIFT;
+}
+
 /*
  * If user didn't specify how much memory it wants to allocate for the guest,
  * avoid filling the whole host RAM.
@@ -222,17 +234,11 @@ static void kernel_usage_with_options(void)
 static u64 get_ram_size(int nr_cpus)
 {
 	long available;
-	long page_size;
-	long nr_pages;
 	long ram_size;
 
 	ram_size	= 64 * (nr_cpus + 3);
 
-	nr_pages	= sysconf(_SC_PHYS_PAGES);
-
-	page_size	= sysconf(_SC_PAGE_SIZE);
-
-	available	= ((nr_pages * page_size) >> MB_SHIFT) * RAM_SIZE_RATIO;
+	available	= host_ram_size() * RAM_SIZE_RATIO;
 
 	if (ram_size > available)
 		ram_size	= available;
@@ -364,6 +370,9 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 	if (ram_size < MIN_RAM_SIZE_MB)
 		die("Not enough memory specified: %lluMB (min %lluMB)", ram_size, MIN_RAM_SIZE_MB);
 
+	if (ram_size > host_ram_size())
+		warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB", ram_size, host_ram_size());
+
 	ram_size <<= MB_SHIFT;
 
 	if (!kvm_dev)
Signature · 1 line
1.7.0.4