From 8c1932533578415748d3e702b71fa14bc854f43a Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Sat, 7 May 2011 17:18:14 +0300 Subject: [PATCH] kvm tools: Scale guest RAM size by CPU count 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 Cc: Cyrill Gorcunov Cc: Ingo Molnar Cc: Prasad Joshi Cc: Sasha Levin Suggested-by: Ingo Molnar Signed-off-by: Pekka Enberg --- tools/kvm/kvm-run.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/tools/kvm/kvm-run.c b/tools/kvm/kvm-run.c index 2ff126d5c746..2504ab02a85e 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); -- 2.39.5