summary refs log tree commit diff
path: root/src/linux.rs
diff options
context:
space:
mode:
authorSonny Rao <sonnyrao@chromium.org>2019-04-24 12:24:38 -0700
committerchrome-bot <chrome-bot@chromium.org>2019-04-29 20:57:24 -0700
commitd5f6608f3eb79fa9d058704252f7b91d69954490 (patch)
tree850a5a45f855b843140d1456187faae7efe774b3 /src/linux.rs
parentf55f75d6894fe997a619df3fc4ca6e5c9d7b79a6 (diff)
downloadcrosvm-d5f6608f3eb79fa9d058704252f7b91d69954490.tar
crosvm-d5f6608f3eb79fa9d058704252f7b91d69954490.tar.gz
crosvm-d5f6608f3eb79fa9d058704252f7b91d69954490.tar.bz2
crosvm-d5f6608f3eb79fa9d058704252f7b91d69954490.tar.lz
crosvm-d5f6608f3eb79fa9d058704252f7b91d69954490.tar.xz
crosvm-d5f6608f3eb79fa9d058704252f7b91d69954490.tar.zst
crosvm-d5f6608f3eb79fa9d058704252f7b91d69954490.zip
linux: handle margin file with multiple values
We're changing the content of the low_mem margin file to handle
multiple values to support notification for multiple memory pressure
levels.  The values will be from most critical to least, so we need to
handle this by fetching the first value.

BUG=chromium:736538
TEST=run vm.CrostiniStartEverything with and put memory pressure on the system

Change-Id: I0278ed492ddda1594d53750e0d4024a878210c9f
Reviewed-on: https://chromium-review.googlesource.com/1584644
Commit-Ready: Sonny Rao <sonnyrao@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Tested-by: Sonny Rao <sonnyrao@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Diffstat (limited to 'src/linux.rs')
-rw-r--r--src/linux.rs22
1 files changed, 18 insertions, 4 deletions
diff --git a/src/linux.rs b/src/linux.rs
index 223b3a3..803a82e 100644
--- a/src/linux.rs
+++ b/src/linux.rs
@@ -1086,8 +1086,9 @@ fn run_vcpu(
         .map_err(Error::SpawnVcpu)
 }
 
-// Reads the contents of a file and converts them into a u64.
-fn file_to_u64<P: AsRef<Path>>(path: P) -> io::Result<u64> {
+// Reads the contents of a file and converts the space-separated fields into a Vec of u64s.
+// Returns an error if any of the fields fail to parse.
+fn file_fields_to_u64<P: AsRef<Path>>(path: P) -> io::Result<Vec<u64>> {
     let mut file = File::open(path)?;
 
     let mut buf = [0u8; 32];
@@ -1097,8 +1098,21 @@ fn file_to_u64<P: AsRef<Path>>(path: P) -> io::Result<u64> {
         str::from_utf8(&buf[..count]).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
     content
         .trim()
-        .parse()
-        .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
+        .split_whitespace()
+        .map(|x| {
+            x.parse::<u64>()
+                .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
+        })
+        .collect()
+}
+
+// Reads the contents of a file and converts them into a u64, and if there
+// are multiple fields it only returns the first one.
+fn file_to_u64<P: AsRef<Path>>(path: P) -> io::Result<u64> {
+    file_fields_to_u64(path)?
+        .into_iter()
+        .next()
+        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "empty file"))
 }
 
 pub fn run_config(cfg: Config) -> Result<()> {