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 #if defined(__ANDROID__) && !defined(__ANDROID_APEX__) && !defined(__ANDROID_VNDK__)
32 #include "bpf/BpfMap.h"
33 #endif
34 #include <sstream>
35 #include <string>
36 #include <unordered_set>
37 #include <utility>
38 #include <vector>
39
40 #include <android-base/file.h>
41 #include <android-base/logging.h>
42 #include <android-base/parseint.h>
43 #include <android-base/stringprintf.h>
44 #include <android-base/strings.h>
45 #include <android-base/unique_fd.h>
46 #include <dmabufinfo/dmabuf_sysfs_stats.h>
47
48 #include "meminfo_private.h"
49
50 namespace android {
51 namespace meminfo {
52
ReadMemInfo(const char * path)53 bool SysMemInfo::ReadMemInfo(const char* path) {
54 return ReadMemInfo(path, SysMemInfo::kDefaultSysMemInfoTags.size(),
55 &*SysMemInfo::kDefaultSysMemInfoTags.begin(),
56 [&](std::string_view tag, uint64_t val) {
57 // Safe to store the string_view in the map
58 // because the tags from
59 // kDefaultSysMemInfoTags are all
60 // statically-allocated.
61 mem_in_kb_[tag] = val;
62 });
63 }
64
ReadMemInfo(std::vector<uint64_t> * out,const char * path)65 bool SysMemInfo::ReadMemInfo(std::vector<uint64_t>* out, const char* path) {
66 out->clear();
67 out->resize(SysMemInfo::kDefaultSysMemInfoTags.size());
68 return ReadMemInfo(SysMemInfo::kDefaultSysMemInfoTags.size(),
69 &*SysMemInfo::kDefaultSysMemInfoTags.begin(), out->data(), path);
70 }
71
ReadMemInfo(size_t ntags,const std::string_view * tags,uint64_t * out,const char * path)72 bool SysMemInfo::ReadMemInfo(size_t ntags, const std::string_view* tags, uint64_t* out,
73 const char* path) {
74 return ReadMemInfo(path, ntags, tags, [&]([[maybe_unused]] std::string_view tag, uint64_t val) {
75 auto it = std::find(tags, tags + ntags, tag);
76 if (it == tags + ntags) {
77 LOG(ERROR) << "Tried to store invalid tag: " << tag;
78 return;
79 }
80 auto index = std::distance(tags, it);
81 // store the values in the same order as the tags
82 out[index] = val;
83 });
84 }
85
ReadVmallocInfo()86 uint64_t SysMemInfo::ReadVmallocInfo() {
87 return ::android::meminfo::ReadVmallocInfo();
88 }
89
ReadMemInfo(const char * path,size_t ntags,const std::string_view * tags,std::function<void (std::string_view,uint64_t)> store_val)90 bool SysMemInfo::ReadMemInfo(const char* path, size_t ntags, const std::string_view* tags,
91 std::function<void(std::string_view, uint64_t)> store_val) {
92 char buffer[4096];
93 int fd = open(path, O_RDONLY | O_CLOEXEC);
94 if (fd < 0) {
95 PLOG(ERROR) << "Failed to open file :" << path;
96 return false;
97 }
98
99 const int len = read(fd, buffer, sizeof(buffer) - 1);
100 close(fd);
101 if (len < 0) {
102 return false;
103 }
104
105 buffer[len] = '\0';
106 char* p = buffer;
107 uint32_t found = 0;
108 uint32_t lineno = 0;
109 bool zram_tag_found = false;
110 while (*p && found < ntags) {
111 for (size_t tagno = 0; tagno < ntags; ++tagno) {
112 const std::string_view& tag = tags[tagno];
113 // Special case for "Zram:" tag that android_os_Debug and friends look
114 // up along with the rest of the numbers from /proc/meminfo
115 if (!zram_tag_found && tag == "Zram:") {
116 store_val(tag, mem_zram_kb());
117 zram_tag_found = true;
118 found++;
119 continue;
120 }
121
122 if (strncmp(p, tag.data(), tag.size()) == 0) {
123 p += tag.size();
124 while (*p == ' ') p++;
125 char* endptr = nullptr;
126 uint64_t val = strtoull(p, &endptr, 10);
127 if (p == endptr) {
128 PLOG(ERROR) << "Failed to parse line:" << lineno + 1 << " in file: " << path;
129 return false;
130 }
131 store_val(tag, val);
132 p = endptr;
133 found++;
134 break;
135 }
136 }
137
138 while (*p && *p != '\n') {
139 p++;
140 }
141 if (*p) p++;
142 lineno++;
143 }
144
145 return true;
146 }
147
mem_zram_kb(const char * zram_dev_cstr) const148 uint64_t SysMemInfo::mem_zram_kb(const char* zram_dev_cstr) const {
149 uint64_t mem_zram_total = 0;
150 if (zram_dev_cstr) {
151 if (!MemZramDevice(zram_dev_cstr, &mem_zram_total)) {
152 return 0;
153 }
154 return mem_zram_total / 1024;
155 }
156
157 constexpr uint32_t kMaxZramDevices = 256;
158 for (uint32_t i = 0; i < kMaxZramDevices; i++) {
159 std::string zram_dev_abspath = ::android::base::StringPrintf("/sys/block/zram%u/", i);
160 if (access(zram_dev_abspath.c_str(), F_OK)) {
161 // We assume zram devices appear in range 0-255 and appear always in sequence
162 // under /sys/block. So, stop looking for them once we find one is missing.
163 break;
164 }
165
166 uint64_t mem_zram_dev;
167 if (!MemZramDevice(zram_dev_abspath.c_str(), &mem_zram_dev)) {
168 return 0;
169 }
170
171 mem_zram_total += mem_zram_dev;
172 }
173
174 return mem_zram_total / 1024;
175 }
176
MemZramDevice(const char * zram_dev,uint64_t * mem_zram_dev) const177 bool SysMemInfo::MemZramDevice(const char* zram_dev, uint64_t* mem_zram_dev) const {
178 std::string mmstat = ::android::base::StringPrintf("%s/%s", zram_dev, "mm_stat");
179 auto mmstat_fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(mmstat.c_str(), "re"), fclose};
180 if (mmstat_fp != nullptr) {
181 // only if we do have mmstat, use it. Otherwise, fall through to trying out the old
182 // 'mem_used_total'
183 if (fscanf(mmstat_fp.get(), "%*" SCNu64 " %*" SCNu64 " %" SCNu64, mem_zram_dev) != 1) {
184 PLOG(ERROR) << "Malformed mm_stat file in: " << zram_dev;
185 return false;
186 }
187 return true;
188 }
189
190 std::string content;
191 if (::android::base::ReadFileToString(
192 ::android::base::StringPrintf("%s/mem_used_total", zram_dev), &content)) {
193 *mem_zram_dev = strtoull(content.c_str(), NULL, 10);
194 if (*mem_zram_dev == ULLONG_MAX) {
195 PLOG(ERROR) << "Malformed mem_used_total file for zram dev: " << zram_dev
196 << " content: " << content;
197 return false;
198 }
199
200 return true;
201 }
202
203 LOG(ERROR) << "Can't find memory status under: " << zram_dev;
204 return false;
205 }
206
mem_compacted_kb(const char * zram_dev_cstr)207 uint64_t SysMemInfo::mem_compacted_kb(const char* zram_dev_cstr) {
208 uint64_t mem_compacted_total = 0;
209 if (zram_dev_cstr) {
210 // Fast-path, single device
211 if (!GetTotalMemCompacted(zram_dev_cstr, &mem_compacted_total)) {
212 return 0;
213 }
214 return mem_compacted_total / 1024;
215 }
216
217 // Slow path - multiple devices
218 constexpr uint32_t kMaxZramDevices = 256;
219 for (uint32_t i = 0; i < kMaxZramDevices; i++) {
220 std::string zram_dev_abspath = ::android::base::StringPrintf("/sys/block/zram%u/", i);
221 if (access(zram_dev_abspath.c_str(), F_OK)) {
222 // We assume zram devices appear in range 0-255 and appear always in sequence
223 // under /sys/block. So, stop looking for them once we find one is missing.
224 break;
225 }
226
227 uint64_t mem_compacted;
228 if (!GetTotalMemCompacted(zram_dev_abspath.c_str(), &mem_compacted)) {
229 return 0;
230 }
231
232 mem_compacted_total += mem_compacted;
233 }
234
235 return mem_compacted_total / 1024; // transform to KBs
236 }
237
238 // Returns the total memory compacted in bytes which corresponds to the following formula
239 // compacted memory = uncompressed memory size - compressed memory size
GetTotalMemCompacted(const char * zram_dev,uint64_t * out_mem_compacted)240 bool SysMemInfo::GetTotalMemCompacted(const char* zram_dev, uint64_t* out_mem_compacted) {
241 std::string mmstat = ::android::base::StringPrintf("%s/%s", zram_dev, "mm_stat");
242 auto mmstat_fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(mmstat.c_str(), "re"), fclose};
243 if (mmstat_fp != nullptr) {
244 uint64_t uncompressed_size_bytes;
245 uint64_t compressed_size_bytes;
246
247 if (fscanf(mmstat_fp.get(), "%" SCNu64 "%" SCNu64, &uncompressed_size_bytes,
248 &compressed_size_bytes) != 2) {
249 PLOG(ERROR) << "Malformed mm_stat file in: " << zram_dev;
250 *out_mem_compacted = 0;
251 return false;
252 }
253
254 *out_mem_compacted = uncompressed_size_bytes - compressed_size_bytes;
255 return true;
256 }
257
258 *out_mem_compacted = 0;
259 return false;
260 }
261
262 // Public methods
ReadVmallocInfo(const char * path)263 uint64_t ReadVmallocInfo(const char* path) {
264 uint64_t vmalloc_total = 0;
265 auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path, "re"), fclose};
266 if (fp == nullptr) {
267 return vmalloc_total;
268 }
269
270 char* line = nullptr;
271 size_t line_alloc = 0;
272 while (getline(&line, &line_alloc, fp.get()) > 0) {
273 // We are looking for lines like
274 //
275 // 0x0000000000000000-0x0000000000000000 12288 drm_property_create_blob+0x44/0xec pages=2 vmalloc
276 // 0x0000000000000000-0x0000000000000000 8192 wlan_logging_sock_init_svc+0xf8/0x4f0 [wlan] pages=1 vmalloc
277 //
278 // Notice that if the caller is coming from a module, the kernel prints and extra
279 // "[module_name]" after the address and the symbol of the call site. This means we can't
280 // use the old sscanf() method of getting the # of pages.
281 char* p_start = strstr(line, "pages=");
282 if (p_start == nullptr) {
283 // we didn't find anything
284 continue;
285 }
286
287 uint64_t nr_pages;
288 if (sscanf(p_start, "pages=%" SCNu64 "", &nr_pages) == 1) {
289 vmalloc_total += (nr_pages * getpagesize());
290 }
291 }
292
293 free(line);
294
295 return vmalloc_total;
296 }
297
ReadSysfsFile(const std::string & path,uint64_t * value)298 static bool ReadSysfsFile(const std::string& path, uint64_t* value) {
299 std::string content;
300 if (!::android::base::ReadFileToString(path, &content)) {
301 LOG(ERROR) << "Can't open file: " << path;
302 return false;
303 }
304
305 *value = strtoull(content.c_str(), NULL, 10);
306 if (*value == ULLONG_MAX) {
307 PLOG(ERROR) << "Invalid file format: " << path;
308 return false;
309 }
310
311 return true;
312 }
313
ReadIonHeapsSizeKb(uint64_t * size,const std::string & path)314 bool ReadIonHeapsSizeKb(uint64_t* size, const std::string& path) {
315 return ReadSysfsFile(path, size);
316 }
317
ReadIonPoolsSizeKb(uint64_t * size,const std::string & path)318 bool ReadIonPoolsSizeKb(uint64_t* size, const std::string& path) {
319 return ReadSysfsFile(path, size);
320 }
321
ReadDmabufHeapPoolsSizeKb(uint64_t * size,const std::string & dma_heap_pool_size_path)322 bool ReadDmabufHeapPoolsSizeKb(uint64_t* size, const std::string& dma_heap_pool_size_path) {
323 static bool support_dmabuf_heap_pool_size = [dma_heap_pool_size_path]() -> bool {
324 bool ret = (access(dma_heap_pool_size_path.c_str(), R_OK) == 0);
325 if (!ret)
326 LOG(ERROR) << "Unable to read DMA-BUF heap total pool size, read ION total pool "
327 "size instead.";
328 return ret;
329 }();
330
331 if (!support_dmabuf_heap_pool_size) return ReadIonPoolsSizeKb(size);
332
333 return ReadSysfsFile(dma_heap_pool_size_path, size);
334 }
335
ReadDmabufHeapTotalExportedKb(uint64_t * size,const std::string & dma_heap_root_path,const std::string & dmabuf_sysfs_stats_path)336 bool ReadDmabufHeapTotalExportedKb(uint64_t* size, const std::string& dma_heap_root_path,
337 const std::string& dmabuf_sysfs_stats_path) {
338 static bool support_dmabuf_heaps = [dma_heap_root_path]() -> bool {
339 bool ret = (access(dma_heap_root_path.c_str(), R_OK) == 0);
340 if (!ret) LOG(ERROR) << "DMA-BUF heaps not supported, read ION heap total instead.";
341 return ret;
342 }();
343
344 if (!support_dmabuf_heaps) return ReadIonHeapsSizeKb(size);
345
346 std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(dma_heap_root_path.c_str()), closedir);
347
348 if (!dir) {
349 return false;
350 }
351
352 std::unordered_set<std::string> heap_list;
353 struct dirent* dent;
354 while ((dent = readdir(dir.get()))) {
355 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) continue;
356
357 heap_list.insert(dent->d_name);
358 }
359
360 if (heap_list.empty()) return false;
361
362 android::dmabufinfo::DmabufSysfsStats stats;
363 if (!android::dmabufinfo::GetDmabufSysfsStats(&stats, dmabuf_sysfs_stats_path)) return false;
364
365 auto exporter_info = stats.exporter_info();
366
367 *size = 0;
368 for (const auto& heap : heap_list) {
369 auto iter = exporter_info.find(heap);
370 if (iter != exporter_info.end()) *size += iter->second.size;
371 }
372
373 *size = *size / 1024;
374
375 return true;
376 }
377
ReadPerProcessGpuMem(std::unordered_map<uint32_t,uint64_t> * out)378 bool ReadPerProcessGpuMem([[maybe_unused]] std::unordered_map<uint32_t, uint64_t>* out) {
379 #if defined(__ANDROID__) && !defined(__ANDROID_APEX__) && !defined(__ANDROID_VNDK__)
380 static constexpr const char kBpfGpuMemTotalMap[] = "/sys/fs/bpf/map_gpuMem_gpu_mem_total_map";
381
382 // Use the read-only wrapper BpfMapRO to properly retrieve the read-only map.
383 auto map = bpf::BpfMapRO<uint64_t, uint64_t>(kBpfGpuMemTotalMap);
384 if (!map.isValid()) {
385 LOG(ERROR) << "Can't open file: " << kBpfGpuMemTotalMap;
386 return false;
387 }
388
389 if (!out) {
390 LOG(ERROR) << "ReadPerProcessGpuMem: out param is null";
391 return false;
392 }
393 out->clear();
394
395 auto map_key = map.getFirstKey();
396 if (!map_key.ok()) {
397 return true;
398 }
399
400 do {
401 uint64_t key = map_key.value();
402 uint32_t pid = key; // BPF Key [32-bits GPU ID | 32-bits PID]
403
404 auto gpu_mem = map.readValue(key);
405 if (!gpu_mem.ok()) {
406 LOG(ERROR) << "Invalid file format: " << kBpfGpuMemTotalMap;
407 return false;
408 }
409
410 const auto& iter = out->find(pid);
411 if (iter == out->end()) {
412 out->insert({pid, gpu_mem.value() / 1024});
413 } else {
414 iter->second += gpu_mem.value() / 1024;
415 }
416
417 map_key = map.getNextKey(key);
418 } while (map_key.ok());
419
420 return true;
421 #else
422 return false;
423 #endif
424 }
425
ReadProcessGpuUsageKb(uint32_t pid,uint32_t gpu_id,uint64_t * size)426 bool ReadProcessGpuUsageKb([[maybe_unused]] uint32_t pid, [[maybe_unused]] uint32_t gpu_id,
427 uint64_t* size) {
428 #if defined(__ANDROID__) && !defined(__ANDROID_APEX__) && !defined(__ANDROID_VNDK__)
429 static constexpr const char kBpfGpuMemTotalMap[] = "/sys/fs/bpf/map_gpuMem_gpu_mem_total_map";
430
431 uint64_t gpu_mem;
432
433 // BPF Key [32-bits GPU ID | 32-bits PID]
434 uint64_t kBpfKeyGpuUsage = ((uint64_t)gpu_id << 32) | pid;
435
436 // Use the read-only wrapper BpfMapRO to properly retrieve the read-only map.
437 auto map = bpf::BpfMapRO<uint64_t, uint64_t>(kBpfGpuMemTotalMap);
438 if (!map.isValid()) {
439 LOG(ERROR) << "Can't open file: " << kBpfGpuMemTotalMap;
440 return false;
441 }
442
443 auto res = map.readValue(kBpfKeyGpuUsage);
444
445 if (res.ok()) {
446 gpu_mem = res.value();
447 } else if (res.error().code() == ENOENT) {
448 gpu_mem = 0;
449 } else {
450 LOG(ERROR) << "Invalid file format: " << kBpfGpuMemTotalMap;
451 return false;
452 }
453
454 if (size) {
455 *size = gpu_mem / 1024;
456 }
457 return true;
458 #else
459 if (size) {
460 *size = 0;
461 }
462 return false;
463 #endif
464 }
465
ReadGpuTotalUsageKb(uint64_t * size)466 bool ReadGpuTotalUsageKb(uint64_t* size) {
467 // gpu_mem_total tracepoint defines PID 0 as global total
468 // GPU ID 0 suffices for current android devices.
469 // This will need to check all GPU IDs in future if more than
470 // one is GPU device is present on the device.
471 return ReadProcessGpuUsageKb(0, 0, size);
472 }
473
474 } // namespace meminfo
475 } // namespace android
476