[PATCH] kvm tools: Scale guest RAM size by CPU count

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch increases default RAM size to 256 for one CPU and introduces RAM
size linear scaling based on CPUs as suggested by Ingo Molnar:

     64MB*(nr_cpus + 3)

     ------------------
       1 CPUs:   256 MB
       2 CPUs:   320 MB
       3 CPUs:   384 MB
       4 CPUs:   448 MB
       5 CPUs:   512 MB
       6 CPUs:   576 MB
       7 CPUs:   640 MB
       8 CPUs:   704 MB
       9 CPUs:   768 MB
      10 CPUs:   832 MB
      11 CPUs:   896 MB
      12 CPUs:   960 MB
      13 CPUs:  1024 MB
      14 CPUs:  1088 MB
      15 CPUs:  1152 MB
      16 CPUs:  1216 MB

Cc: Asias He <asias.hejun@xxxxxxxxx>
Cc: Cyrill Gorcunov <gorcunov@xxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxx>
Cc: Prasad Joshi <prasadjoshi124@xxxxxxxxx>
Cc: Sasha Levin <levinsasha928@xxxxxxxxx>
Suggested-by: Ingo Molnar <mingo@xxxxxxx>
Signed-off-by: Pekka Enberg <penberg@xxxxxxxxxx>
---
 tools/kvm/kvm-run.c |   26 +++++++++++++++++++++++++-
 1 files changed, 25 insertions(+), 1 deletions(-)

diff --git a/tools/kvm/kvm-run.c b/tools/kvm/kvm-run.c
index 2ff126d..2504ab0 100644
--- a/tools/kvm/kvm-run.c
+++ b/tools/kvm/kvm-run.c
@@ -46,7 +46,7 @@ static struct kvm *kvm;
 static struct kvm_cpu *kvm_cpus[KVM_NR_CPUS];
 static __thread struct kvm_cpu *current_kvm_cpu;
 
-static u64 ram_size = MIN_RAM_SIZE_MB;
+static u64 ram_size;
 static u8  image_count;
 static const char *kernel_cmdline;
 static const char *kernel_filename;
@@ -213,6 +213,27 @@ static void kernel_usage_with_options(void)
 	fprintf(stderr, "\nPlease see 'kvm run --help' for more options.\n\n");
 }
 
+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;
+
+	if (ram_size > available)
+		ram_size	= available;
+
+	return ram_size;
+}
+
 static const char *find_kernel(void)
 {
 	const char **k;
@@ -330,6 +351,9 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 	if (nrcpus < 1 || nrcpus > KVM_NR_CPUS)
 		die("Number of CPUs %d is out of [1;%d] range", nrcpus, KVM_NR_CPUS);
 
+	if (!ram_size)
+		ram_size	= get_ram_size(nrcpus);
+
 	if (ram_size < MIN_RAM_SIZE_MB)
 		die("Not enough memory specified: %lluMB (min %lluMB)", ram_size, MIN_RAM_SIZE_MB);
 
-- 
1.7.0.4

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux