From 448516e3f985dd13fb5cd16f2c9efbcf097f9fa5 Mon Sep 17 00:00:00 2001 From: Chirantan Ekbote Date: Tue, 24 Jul 2018 16:07:42 -0700 Subject: balloon: Implement device policy Implement a policy for the balloon device so that it starts taking memory away from the VM when the system is under low memory conditions. There are a few pieces here: * Change the madvise call in MemoryMapping::dont_need_range to use MADV_REMOVE instead of MADV_DONTNEED. The latter does nothing when the memory mapping is shared across multiple processes while the former immediately gives the pages in the specified range back to the kernel. Subsequent accesses to memory in that range returns zero pages. * Change the protocol between the balloon device process and the main crosvm process. Previously, the device process expected the main process to send it increments in the amount of memory consumed by the balloon device. Now, it instead just expects the absolute value of the memory that should be consumed. To properly implement the policy the main process needs to keep track of the total memory consumed by the balloon device so this makes it easier to handle all the policy in one place. * Add a policy for dealing with low memory situations. When the VM starts up, we determine the maximum amount of memory that the balloon device should consume: * If the VM has more than 1.5GB of memory, the balloon device max is the size of the VM memory minus 1GB. * Otherwise, if the VM has at least 500MB, the balloon device max is 50% of the size of the VM memory. * Otherwise, the max is 0. The increment used to change the size of the balloon is defined as 1/16 of the max memory that the balloon device will consume. When the crosvm main process detects that the system is low on memory, it immediately increases the balloon size by the increment (unless it has already reached the max). It then starts 2 timers: one to check for low memory conditions again in 1 seconds (+ jitter) and another to check if the system is no longer low on memory in 1 minute (+ jitter) with a subsequent interval of 30 seconds (+ jitter). Under persistent low memory conditions the balloon device will consume the maximum memory after 16 seconds. Once there is enough available memory the balloon size will shrink back down to 0 after at most 9 minutes. BUG=chromium:866193 TEST=manual Start 2 VMs and write out a large file (size > system RAM) in each. Observe /sys/kernel/mm/chromeos-low_mem/available and see that the available memory steadily decreases until it goes under the low memory margin at which point the available memory bounces back up as crosvm frees up pages. CQ-DEPEND=CL:1152214 Change-Id: I2046729683aa081c9d7ed039d902ad11737c1d52 Signed-off-by: Chirantan Ekbote Reviewed-on: https://chromium-review.googlesource.com/1149155 Reviewed-by: Sonny Rao --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Cargo.toml') diff --git a/Cargo.toml b/Cargo.toml index ec58da2..708c9b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,7 @@ protobuf = { version = "=1.4.3", optional = true } qcow_utils = { path = "qcow_utils" } resources = { path = "resources" } p9 = { path = "p9" } +rand = "=0.3.20" [target.'cfg(target_arch = "x86_64")'.dependencies] x86_64 = { path = "x86_64" } @@ -46,5 +47,4 @@ x86_64 = { path = "x86_64" } aarch64 = { path = "aarch64" } [dev-dependencies] -rand = "=0.3.20" sys_util = { path = "sys_util" } -- cgit 1.4.1