blob: 5cfa6c373c74503bbc336d64e93647269b3f9d53 [file] [log] [blame]
Sandeep Patil54d87212018-08-29 17:10:47 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <ctype.h>
18#include <errno.h>
19#include <fcntl.h>
Sandeep Patil70fa72d2018-11-09 19:18:29 -080020#include <inttypes.h>
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -080021#include <stdio.h>
Sandeep Patil54d87212018-08-29 17:10:47 -070022#include <stdlib.h>
Sandeep Patilc24f1e32018-12-29 14:34:20 -080023#include <string.h>
Sandeep Patil54d87212018-08-29 17:10:47 -070024#include <unistd.h>
25
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -080026#include <algorithm>
Sandeep Patil54d87212018-08-29 17:10:47 -070027#include <cctype>
Sandeep Patil70fa72d2018-11-09 19:18:29 -080028#include <cstdio>
Sandeep Patil54d87212018-08-29 17:10:47 -070029#include <fstream>
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -080030#include <iterator>
Sandeep Patilc24f1e32018-12-29 14:34:20 -080031#include <sstream>
Sandeep Patil54d87212018-08-29 17:10:47 -070032#include <string>
33#include <utility>
34#include <vector>
35
36#include <android-base/file.h>
37#include <android-base/logging.h>
38#include <android-base/parseint.h>
Sandeep Patil70fa72d2018-11-09 19:18:29 -080039#include <android-base/stringprintf.h>
Sandeep Patil54d87212018-08-29 17:10:47 -070040#include <android-base/strings.h>
Sandeep Patil70fa72d2018-11-09 19:18:29 -080041#include <android-base/unique_fd.h>
Sandeep Patil54d87212018-08-29 17:10:47 -070042
43#include "meminfo_private.h"
44
45namespace android {
46namespace meminfo {
47
48const std::vector<std::string> SysMemInfo::kDefaultSysMemInfoTags = {
Sandeep Patil70fa72d2018-11-09 19:18:29 -080049 SysMemInfo::kMemTotal, SysMemInfo::kMemFree, SysMemInfo::kMemBuffers,
50 SysMemInfo::kMemCached, SysMemInfo::kMemShmem, SysMemInfo::kMemSlab,
51 SysMemInfo::kMemSReclaim, SysMemInfo::kMemSUnreclaim, SysMemInfo::kMemSwapTotal,
52 SysMemInfo::kMemSwapFree, SysMemInfo::kMemMapped, SysMemInfo::kMemVmallocUsed,
53 SysMemInfo::kMemPageTables, SysMemInfo::kMemKernelStack,
Sandeep Patil54d87212018-08-29 17:10:47 -070054};
55
56bool SysMemInfo::ReadMemInfo(const std::string& path) {
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -080057 return ReadMemInfo(SysMemInfo::kDefaultSysMemInfoTags, path,
58 [&](const std::string& tag, uint64_t val) { mem_in_kb_[tag] = val; });
59}
60
61bool SysMemInfo::ReadMemInfo(std::vector<uint64_t>* out, const std::string& path) {
62 return ReadMemInfo(SysMemInfo::kDefaultSysMemInfoTags, out, path);
63}
64
65bool SysMemInfo::ReadMemInfo(const std::vector<std::string>& tags, std::vector<uint64_t>* out,
66 const std::string& path) {
67 out->clear();
68 out->resize(tags.size());
69
70 return ReadMemInfo(tags, path, [&]([[maybe_unused]] const std::string& tag, uint64_t val) {
71 auto it = std::find(tags.begin(), tags.end(), tag);
72 if (it == tags.end()) {
73 LOG(ERROR) << "Tried to store invalid tag: " << tag;
74 return;
75 }
76 auto index = std::distance(tags.begin(), it);
77 // store the values in the same order as the tags
78 out->at(index) = val;
79 });
Sandeep Patil54d87212018-08-29 17:10:47 -070080}
81
Sandeep Patile04680d2019-01-19 12:04:18 -080082uint64_t SysMemInfo::ReadVmallocInfo() {
83 return ::android::meminfo::ReadVmallocInfo();
Sandeep Patilc24f1e32018-12-29 14:34:20 -080084}
85
Sandeep Patil54d87212018-08-29 17:10:47 -070086// TODO: Delete this function if it can't match up with the c-like implementation below.
87// Currently, this added about 50 % extra overhead on hikey.
88#if 0
89bool SysMemInfo::ReadMemInfo(const std::vector<std::string>& tags, const std::string& path) {
90 std::string buffer;
91 if (!::android::base::ReadFileToString(path, &buffer)) {
92 PLOG(ERROR) << "Failed to read : " << path;
93 return false;
94 }
95
96 uint32_t total_found = 0;
97 for (auto s = buffer.begin(); s < buffer.end() && total_found < tags.size();) {
98 for (auto& tag : tags) {
99 if (tag == std::string(s, s + tag.size())) {
100 s += tag.size();
101 while (isspace(*s)) s++;
102 auto num_start = s;
103 while (std::isdigit(*s)) s++;
104
105 std::string number(num_start, num_start + (s - num_start));
106 if (!::android::base::ParseUint(number, &mem_in_kb_[tag])) {
107 LOG(ERROR) << "Failed to parse uint";
108 return false;
109 }
110 total_found++;
111 break;
112 }
113 }
114 while (s < buffer.end() && *s != '\n') s++;
115 if (s < buffer.end()) s++;
116 }
117
118 return true;
119}
120
121#else
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -0800122bool SysMemInfo::ReadMemInfo(const std::vector<std::string>& tags, const std::string& path,
123 std::function<void(const std::string&, uint64_t)> store_val) {
Sandeep Patil54d87212018-08-29 17:10:47 -0700124 char buffer[4096];
125 int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
126 if (fd < 0) {
127 PLOG(ERROR) << "Failed to open file :" << path;
128 return false;
129 }
130
131 const int len = read(fd, buffer, sizeof(buffer) - 1);
132 close(fd);
133 if (len < 0) {
134 return false;
135 }
136
137 buffer[len] = '\0';
138 char* p = buffer;
139 uint32_t found = 0;
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800140 uint32_t lineno = 0;
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -0800141 bool zram_tag_found = false;
Sandeep Patil54d87212018-08-29 17:10:47 -0700142 while (*p && found < tags.size()) {
143 for (auto& tag : tags) {
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -0800144 // Special case for "Zram:" tag that android_os_Debug and friends look
145 // up along with the rest of the numbers from /proc/meminfo
146 if (!zram_tag_found && tag == "Zram:") {
147 store_val(tag, mem_zram_kb());
148 zram_tag_found = true;
149 found++;
150 continue;
151 }
152
Sandeep Patil54d87212018-08-29 17:10:47 -0700153 if (strncmp(p, tag.c_str(), tag.size()) == 0) {
154 p += tag.size();
155 while (*p == ' ') p++;
156 char* endptr = nullptr;
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -0800157 uint64_t val = strtoull(p, &endptr, 10);
Sandeep Patil54d87212018-08-29 17:10:47 -0700158 if (p == endptr) {
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800159 PLOG(ERROR) << "Failed to parse line:" << lineno + 1 << " in file: " << path;
Sandeep Patil54d87212018-08-29 17:10:47 -0700160 return false;
161 }
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -0800162 store_val(tag, val);
Sandeep Patil54d87212018-08-29 17:10:47 -0700163 p = endptr;
164 found++;
165 break;
166 }
167 }
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -0800168
Sandeep Patil54d87212018-08-29 17:10:47 -0700169 while (*p && *p != '\n') {
170 p++;
171 }
172 if (*p) p++;
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800173 lineno++;
Sandeep Patil54d87212018-08-29 17:10:47 -0700174 }
175
176 return true;
177}
178#endif
179
Sandeep Patil70fa72d2018-11-09 19:18:29 -0800180uint64_t SysMemInfo::mem_zram_kb(const std::string& zram_dev) {
181 uint64_t mem_zram_total = 0;
182 if (!zram_dev.empty()) {
183 if (!MemZramDevice(zram_dev, &mem_zram_total)) {
184 return 0;
185 }
186 return mem_zram_total / 1024;
187 }
188
189 constexpr uint32_t kMaxZramDevices = 256;
190 for (uint32_t i = 0; i < kMaxZramDevices; i++) {
191 std::string zram_dev = ::android::base::StringPrintf("/sys/block/zram%u/", i);
192 if (access(zram_dev.c_str(), F_OK)) {
193 // We assume zram devices appear in range 0-255 and appear always in sequence
194 // under /sys/block. So, stop looking for them once we find one is missing.
195 break;
196 }
197
198 uint64_t mem_zram_dev;
199 if (!MemZramDevice(zram_dev, &mem_zram_dev)) {
200 return 0;
201 }
202
203 mem_zram_total += mem_zram_dev;
204 }
205
206 return mem_zram_total / 1024;
207}
208
209bool SysMemInfo::MemZramDevice(const std::string& zram_dev, uint64_t* mem_zram_dev) {
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -0800210 std::string mmstat = ::android::base::StringPrintf("%s/%s", zram_dev.c_str(), "mm_stat");
211 auto mmstat_fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(mmstat.c_str(), "re"), fclose};
212 if (mmstat_fp != nullptr) {
213 // only if we do have mmstat, use it. Otherwise, fall through to trying out the old
214 // 'mem_used_total'
215 if (fscanf(mmstat_fp.get(), "%*" SCNu64 " %*" SCNu64 " %" SCNu64, mem_zram_dev) != 1) {
216 PLOG(ERROR) << "Malformed mm_stat file in: " << zram_dev;
Sandeep Patil70fa72d2018-11-09 19:18:29 -0800217 return false;
218 }
Sandeep Patil70fa72d2018-11-09 19:18:29 -0800219 return true;
220 }
221
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -0800222 std::string content;
Sandeep Patil70fa72d2018-11-09 19:18:29 -0800223 if (::android::base::ReadFileToString(zram_dev + "mem_used_total", &content)) {
224 *mem_zram_dev = strtoull(content.c_str(), NULL, 10);
225 if (*mem_zram_dev == ULLONG_MAX) {
226 PLOG(ERROR) << "Malformed mem_used_total file for zram dev: " << zram_dev
227 << " content: " << content;
228 return false;
229 }
230
231 return true;
232 }
233
234 LOG(ERROR) << "Can't find memory status under: " << zram_dev;
235 return false;
236}
237
Sandeep Patile04680d2019-01-19 12:04:18 -0800238// Public methods
239uint64_t ReadVmallocInfo(const std::string& path) {
240 uint64_t vmalloc_total = 0;
241 auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
242 if (fp == nullptr) {
243 return vmalloc_total;
244 }
245
246 char* line = nullptr;
247 size_t line_alloc = 0;
248 while (getline(&line, &line_alloc, fp.get()) > 0) {
249 // We are looking for lines like
250 //
251 // 0x0000000000000000-0x0000000000000000 12288 drm_property_create_blob+0x44/0xec pages=2 vmalloc
252 // 0x0000000000000000-0x0000000000000000 8192 wlan_logging_sock_init_svc+0xf8/0x4f0 [wlan] pages=1 vmalloc
253 //
254 // Notice that if the caller is coming from a module, the kernel prints and extra
255 // "[module_name]" after the address and the symbol of the call site. This means we can't
256 // use the old sscanf() method of getting the # of pages.
257 char* p_start = strstr(line, "pages=");
258 if (p_start == nullptr) {
259 // we didn't find anything
260 continue;
261 }
262
263 uint64_t nr_pages;
264 if (sscanf(p_start, "pages=%" SCNu64 "", &nr_pages) == 1) {
265 vmalloc_total += (nr_pages * getpagesize());
266 }
267 }
268
269 free(line);
270
271 return vmalloc_total;
272}
273
Sandeep Patil54d87212018-08-29 17:10:47 -0700274} // namespace meminfo
275} // namespace android