1 /*
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>
20 #include <inttypes.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include <algorithm>
27 #include <cctype>
28 #include <cstdio>
29 #include <fstream>
30 #include <iterator>
31 #include <sstream>
32 #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>
39 #include <android-base/stringprintf.h>
40 #include <android-base/strings.h>
41 #include <android-base/unique_fd.h>
42
43 #include "meminfo_private.h"
44
45 namespace android {
46 namespace meminfo {
47
48 const std::vector<std::string> SysMemInfo::kDefaultSysMemInfoTags = {
49 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,
54 };
55
ReadMemInfo(const std::string & path)56 bool SysMemInfo::ReadMemInfo(const std::string& path) {
57 return ReadMemInfo(SysMemInfo::kDefaultSysMemInfoTags, path,
58 [&](const std::string& tag, uint64_t val) { mem_in_kb_[tag] = val; });
59 }
60
ReadMemInfo(std::vector<uint64_t> * out,const std::string & path)61 bool SysMemInfo::ReadMemInfo(std::vector<uint64_t>* out, const std::string& path) {
62 return ReadMemInfo(SysMemInfo::kDefaultSysMemInfoTags, out, path);
63 }
64
ReadMemInfo(const std::vector<std::string> & tags,std::vector<uint64_t> * out,const std::string & path)65 bool 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 });
80 }
81
ReadVmallocInfo()82 uint64_t SysMemInfo::ReadVmallocInfo() {
83 return ::android::meminfo::ReadVmallocInfo();
84 }
85
86 // 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
89 bool 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
ReadMemInfo(const std::vector<std::string> & tags,const std::string & path,std::function<void (const std::string &,uint64_t)> store_val)122 bool SysMemInfo::ReadMemInfo(const std::vector<std::string>& tags, const std::string& path,
123 std::function<void(const std::string&, uint64_t)> store_val) {
124 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;
140 uint32_t lineno = 0;
141 bool zram_tag_found = false;
142 while (*p && found < tags.size()) {
143 for (auto& tag : tags) {
144 // 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
153 if (strncmp(p, tag.c_str(), tag.size()) == 0) {
154 p += tag.size();
155 while (*p == ' ') p++;
156 char* endptr = nullptr;
157 uint64_t val = strtoull(p, &endptr, 10);
158 if (p == endptr) {
159 PLOG(ERROR) << "Failed to parse line:" << lineno + 1 << " in file: " << path;
160 return false;
161 }
162 store_val(tag, val);
163 p = endptr;
164 found++;
165 break;
166 }
167 }
168
169 while (*p && *p != '\n') {
170 p++;
171 }
172 if (*p) p++;
173 lineno++;
174 }
175
176 return true;
177 }
178 #endif
179
mem_zram_kb(const std::string & zram_dev)180 uint64_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
MemZramDevice(const std::string & zram_dev,uint64_t * mem_zram_dev)209 bool SysMemInfo::MemZramDevice(const std::string& zram_dev, uint64_t* mem_zram_dev) {
210 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;
217 return false;
218 }
219 return true;
220 }
221
222 std::string content;
223 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
238 // Public methods
ReadVmallocInfo(const std::string & path)239 uint64_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
274 } // namespace meminfo
275 } // namespace android
276