diff options
author | 2017-10-12 03:42:37 +0200 | |
---|---|---|
committer | 2017-10-12 10:17:14 +0200 | |
commit | 8601d4a357f8c1618fb6a8144ea93736f24d973a (patch) | |
tree | 4804047242e1013cb300dc4d594ac05faa31154b /src/main/java/com/google/devtools/build/lib/unix | |
parent | 3c9ef6e814f5028817a3fc77d016edeee06d3642 (diff) |
Throw a checked exception if there's an error reading /proc/meminfo, and handle it properly.
PiperOrigin-RevId: 171906091
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/unix')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/unix/ProcMeminfoParser.java | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/unix/ProcMeminfoParser.java b/src/main/java/com/google/devtools/build/lib/unix/ProcMeminfoParser.java index e7ea1035af..3c8baab489 100644 --- a/src/main/java/com/google/devtools/build/lib/unix/ProcMeminfoParser.java +++ b/src/main/java/com/google/devtools/build/lib/unix/ProcMeminfoParser.java @@ -62,21 +62,17 @@ public class ProcMeminfoParser { memInfo = builder.build(); } - /** - * Gets a named field in KB. - */ - public long getRamKb(String keyword) { + /** Gets a named field in KB. */ + long getRamKb(String keyword) throws KeywordNotFoundException { Long val = memInfo.get(keyword); if (val == null) { - throw new IllegalArgumentException("Can't locate " + keyword + " in the /proc/meminfo"); + throw new KeywordNotFoundException(keyword); } return val; } - /** - * Return the total physical memory. - */ - public long getTotalKb() { + /** Return the total physical memory. */ + public long getTotalKb() throws KeywordNotFoundException { return getRamKb("MemTotal"); } @@ -92,7 +88,14 @@ public class ProcMeminfoParser { * why this is better than trying to figure it out ourselves. This corresponds to the MemAvailable * line in /proc/meminfo. */ - public long getFreeRamKb() { + public long getFreeRamKb() throws KeywordNotFoundException { return getRamKb("MemAvailable"); } + + /** Exception thrown when /proc/meminfo does not have a requested key. Should be tolerated. */ + public static class KeywordNotFoundException extends Exception { + private KeywordNotFoundException(String keyword) { + super("Can't locate " + keyword + " in the /proc/meminfo"); + } + } } |