Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Ulf Adams | 91d1ea0 | 2017-01-09 09:55:07 +0000 | [diff] [blame] | 15 | package com.google.devtools.build.lib.unix; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 16 | |
| 17 | import com.google.common.annotations.VisibleForTesting; |
| 18 | import com.google.common.base.CharMatcher; |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.io.Files; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import java.io.File; |
| 22 | import java.io.IOException; |
| 23 | import java.nio.charset.Charset; |
Links | ae03671 | 2020-04-29 06:16:11 -0700 | [diff] [blame] | 24 | import java.util.HashMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import java.util.List; |
| 26 | import java.util.Map; |
| 27 | |
| 28 | /** |
Links | ae03671 | 2020-04-29 06:16:11 -0700 | [diff] [blame] | 29 | * Parse and return information from /proc/meminfo. In case of duplicate entries the first one is |
| 30 | * used and other values are skipped. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | */ |
| 32 | public class ProcMeminfoParser { |
| 33 | |
| 34 | public static final String FILE = "/proc/meminfo"; |
| 35 | |
| 36 | private final Map<String, Long> memInfo; |
| 37 | |
| 38 | /** |
| 39 | * Populates memory information by reading /proc/meminfo. |
| 40 | * @throws IOException if reading the file failed. |
| 41 | */ |
| 42 | public ProcMeminfoParser() throws IOException { |
| 43 | this(FILE); |
| 44 | } |
| 45 | |
| 46 | @VisibleForTesting |
| 47 | public ProcMeminfoParser(String fileName) throws IOException { |
| 48 | List<String> lines = Files.readLines(new File(fileName), Charset.defaultCharset()); |
Links | ae03671 | 2020-04-29 06:16:11 -0700 | [diff] [blame] | 49 | Map<String, Long> newMemInfo = new HashMap<>(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 50 | for (String line : lines) { |
Ulf Adams | 07dba94 | 2015-03-05 14:47:37 +0000 | [diff] [blame] | 51 | int colon = line.indexOf(':'); |
Kristina Chodorow | 3d9312e | 2015-12-14 16:05:49 +0000 | [diff] [blame] | 52 | if (colon == -1) { |
| 53 | continue; |
| 54 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 55 | String keyword = line.substring(0, colon); |
| 56 | String valString = line.substring(colon + 1); |
| 57 | try { |
| 58 | long val = Long.parseLong(CharMatcher.inRange('0', '9').retainFrom(valString)); |
Links | ae03671 | 2020-04-29 06:16:11 -0700 | [diff] [blame] | 59 | newMemInfo.putIfAbsent(keyword, val); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 60 | } catch (NumberFormatException e) { |
| 61 | // Ignore: we'll fail later if somebody tries to capture this value. |
| 62 | } |
| 63 | } |
Links | ae03671 | 2020-04-29 06:16:11 -0700 | [diff] [blame] | 64 | memInfo = ImmutableMap.copyOf(newMemInfo); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 65 | } |
| 66 | |
janakr | 8601d4a | 2017-10-12 03:42:37 +0200 | [diff] [blame] | 67 | /** Gets a named field in KB. */ |
| 68 | long getRamKb(String keyword) throws KeywordNotFoundException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 69 | Long val = memInfo.get(keyword); |
| 70 | if (val == null) { |
janakr | 8601d4a | 2017-10-12 03:42:37 +0200 | [diff] [blame] | 71 | throw new KeywordNotFoundException(keyword); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 72 | } |
| 73 | return val; |
| 74 | } |
| 75 | |
janakr | 8601d4a | 2017-10-12 03:42:37 +0200 | [diff] [blame] | 76 | /** Return the total physical memory. */ |
| 77 | public long getTotalKb() throws KeywordNotFoundException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 78 | return getRamKb("MemTotal"); |
| 79 | } |
| 80 | |
| 81 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 82 | * Convert KB to MB. |
| 83 | */ |
| 84 | public static double kbToMb(long kb) { |
Googler | e7af209 | 2017-10-05 02:07:54 +0200 | [diff] [blame] | 85 | return kb >> 10; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
Googler | e7af209 | 2017-10-05 02:07:54 +0200 | [diff] [blame] | 89 | * Reads the amount of *available* memory as reported by the kernel. See https://goo.gl/ABn283 for |
| 90 | * why this is better than trying to figure it out ourselves. This corresponds to the MemAvailable |
| 91 | * line in /proc/meminfo. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 92 | */ |
janakr | 8601d4a | 2017-10-12 03:42:37 +0200 | [diff] [blame] | 93 | public long getFreeRamKb() throws KeywordNotFoundException { |
Googler | 0faf171 | 2017-10-16 20:59:27 +0200 | [diff] [blame] | 94 | if (memInfo.containsKey("MemAvailable")) { |
| 95 | return getRamKb("MemAvailable"); |
| 96 | } |
| 97 | // We have no MemAvailable in /proc/meminfo; fall back to the previous estimation. |
| 98 | return getRamKb("MemTotal") |
| 99 | - getRamKb("Active") |
| 100 | // Blaze doesn't want to use more than a third of inactive ram... |
| 101 | - (long) (getRamKb("Inactive") * 0.3) |
| 102 | // ...and doesn't want to assume more than 80% of the slab memory can be reallocated. |
| 103 | - (long) (getRamKb("Slab") * 0.8); |
| 104 | // That said, this estimate will be more inaccurate as it diverges from kernel internals. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 105 | } |
janakr | 8601d4a | 2017-10-12 03:42:37 +0200 | [diff] [blame] | 106 | |
| 107 | /** Exception thrown when /proc/meminfo does not have a requested key. Should be tolerated. */ |
ulfjack | 5d28837 | 2019-08-08 05:07:39 -0700 | [diff] [blame] | 108 | public static class KeywordNotFoundException extends IOException { |
janakr | 8601d4a | 2017-10-12 03:42:37 +0200 | [diff] [blame] | 109 | private KeywordNotFoundException(String keyword) { |
| 110 | super("Can't locate " + keyword + " in the /proc/meminfo"); |
| 111 | } |
| 112 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 113 | } |