blob: 38710a89de5d1aafc6bd66b9dd4b6e6de356fc91 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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 Adams91d1ea02017-01-09 09:55:07 +000015package com.google.devtools.build.lib.unix;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010016
17import com.google.common.annotations.VisibleForTesting;
18import com.google.common.base.CharMatcher;
19import com.google.common.collect.ImmutableMap;
20import com.google.common.io.Files;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010021import java.io.File;
22import java.io.IOException;
23import java.nio.charset.Charset;
Linksae036712020-04-29 06:16:11 -070024import java.util.HashMap;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010025import java.util.List;
26import java.util.Map;
27
28/**
Linksae036712020-04-29 06:16:11 -070029 * 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 Nienhuysd08b27f2015-02-25 16:45:20 +010031 */
32public 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());
Linksae036712020-04-29 06:16:11 -070049 Map<String, Long> newMemInfo = new HashMap<>();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010050 for (String line : lines) {
Ulf Adams07dba942015-03-05 14:47:37 +000051 int colon = line.indexOf(':');
Kristina Chodorow3d9312e2015-12-14 16:05:49 +000052 if (colon == -1) {
53 continue;
54 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010055 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));
Linksae036712020-04-29 06:16:11 -070059 newMemInfo.putIfAbsent(keyword, val);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010060 } catch (NumberFormatException e) {
61 // Ignore: we'll fail later if somebody tries to capture this value.
62 }
63 }
Linksae036712020-04-29 06:16:11 -070064 memInfo = ImmutableMap.copyOf(newMemInfo);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010065 }
66
janakr8601d4a2017-10-12 03:42:37 +020067 /** Gets a named field in KB. */
68 long getRamKb(String keyword) throws KeywordNotFoundException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010069 Long val = memInfo.get(keyword);
70 if (val == null) {
janakr8601d4a2017-10-12 03:42:37 +020071 throw new KeywordNotFoundException(keyword);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010072 }
73 return val;
74 }
75
janakr8601d4a2017-10-12 03:42:37 +020076 /** Return the total physical memory. */
77 public long getTotalKb() throws KeywordNotFoundException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010078 return getRamKb("MemTotal");
79 }
80
81 /**
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010082 * Convert KB to MB.
83 */
84 public static double kbToMb(long kb) {
Googlere7af2092017-10-05 02:07:54 +020085 return kb >> 10;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010086 }
87
88 /**
Googlere7af2092017-10-05 02:07:54 +020089 * 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 Nienhuysd08b27f2015-02-25 16:45:20 +010092 */
janakr8601d4a2017-10-12 03:42:37 +020093 public long getFreeRamKb() throws KeywordNotFoundException {
Googler0faf1712017-10-16 20:59:27 +020094 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 Nienhuysd08b27f2015-02-25 16:45:20 +0100105 }
janakr8601d4a2017-10-12 03:42:37 +0200106
107 /** Exception thrown when /proc/meminfo does not have a requested key. Should be tolerated. */
ulfjack5d288372019-08-08 05:07:39 -0700108 public static class KeywordNotFoundException extends IOException {
janakr8601d4a2017-10-12 03:42:37 +0200109 private KeywordNotFoundException(String keyword) {
110 super("Can't locate " + keyword + " in the /proc/meminfo");
111 }
112 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100113}