blob: b7a9c70326a97eaa0498146ff163ca1ed0ebb985 [file] [log] [blame]
Sandeep Patila60a7762018-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 Patil9f4028d2018-11-09 19:18:29 -080020#include <inttypes.h>
Sandeep Patile473d7e2018-12-11 09:28:38 -080021#include <stdio.h>
Sandeep Patila60a7762018-08-29 17:10:47 -070022#include <stdlib.h>
Sandeep Patil76a34f02018-12-29 14:34:20 -080023#include <string.h>
Sandeep Patila60a7762018-08-29 17:10:47 -070024#include <unistd.h>
25
Sandeep Patile473d7e2018-12-11 09:28:38 -080026#include <algorithm>
Sandeep Patila60a7762018-08-29 17:10:47 -070027#include <cctype>
Sandeep Patil9f4028d2018-11-09 19:18:29 -080028#include <cstdio>
Sandeep Patila60a7762018-08-29 17:10:47 -070029#include <fstream>
Sandeep Patile473d7e2018-12-11 09:28:38 -080030#include <iterator>
Kevin Jeonc851b322021-03-30 06:33:12 +000031#if defined(__ANDROID__) && !defined(__ANDROID_APEX__) && !defined(__ANDROID_VNDK__)
Suren Baghdasaryan5106ef72020-10-22 12:56:51 -070032#include "bpf/BpfMap.h"
33#endif
Sandeep Patil76a34f02018-12-29 14:34:20 -080034#include <sstream>
Sandeep Patila60a7762018-08-29 17:10:47 -070035#include <string>
Hridya Valsaraju7d1343d2021-02-09 21:48:24 -080036#include <unordered_set>
Sandeep Patila60a7762018-08-29 17:10:47 -070037#include <utility>
38#include <vector>
39
40#include <android-base/file.h>
41#include <android-base/logging.h>
42#include <android-base/parseint.h>
Sandeep Patil9f4028d2018-11-09 19:18:29 -080043#include <android-base/stringprintf.h>
Sandeep Patila60a7762018-08-29 17:10:47 -070044#include <android-base/strings.h>
Sandeep Patil9f4028d2018-11-09 19:18:29 -080045#include <android-base/unique_fd.h>
Hridya Valsaraju7d1343d2021-02-09 21:48:24 -080046#include <dmabufinfo/dmabuf_sysfs_stats.h>
Sandeep Patila60a7762018-08-29 17:10:47 -070047
48#include "meminfo_private.h"
49
50namespace android {
51namespace meminfo {
52
Daniel Colascioned64b5c92019-09-03 18:17:19 -070053bool 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 });
Sandeep Patile473d7e2018-12-11 09:28:38 -080063}
64
Daniel Colascioned64b5c92019-09-03 18:17:19 -070065bool SysMemInfo::ReadMemInfo(std::vector<uint64_t>* out, const char* path) {
Sandeep Patile473d7e2018-12-11 09:28:38 -080066 out->clear();
Daniel Colascioned64b5c92019-09-03 18:17:19 -070067 out->resize(SysMemInfo::kDefaultSysMemInfoTags.size());
68 return ReadMemInfo(SysMemInfo::kDefaultSysMemInfoTags.size(),
69 &*SysMemInfo::kDefaultSysMemInfoTags.begin(), out->data(), path);
70}
Sandeep Patile473d7e2018-12-11 09:28:38 -080071
Daniel Colascioned64b5c92019-09-03 18:17:19 -070072bool 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) {
Sandeep Patile473d7e2018-12-11 09:28:38 -080077 LOG(ERROR) << "Tried to store invalid tag: " << tag;
78 return;
79 }
Daniel Colascioned64b5c92019-09-03 18:17:19 -070080 auto index = std::distance(tags, it);
Sandeep Patile473d7e2018-12-11 09:28:38 -080081 // store the values in the same order as the tags
Daniel Colascioned64b5c92019-09-03 18:17:19 -070082 out[index] = val;
Sandeep Patile473d7e2018-12-11 09:28:38 -080083 });
Sandeep Patila60a7762018-08-29 17:10:47 -070084}
85
Sandeep Patil818d9f22019-01-19 12:04:18 -080086uint64_t SysMemInfo::ReadVmallocInfo() {
87 return ::android::meminfo::ReadVmallocInfo();
Sandeep Patil76a34f02018-12-29 14:34:20 -080088}
89
Daniel Colascioned64b5c92019-09-03 18:17:19 -070090bool SysMemInfo::ReadMemInfo(const char* path, size_t ntags, const std::string_view* tags,
91 std::function<void(std::string_view, uint64_t)> store_val) {
Sandeep Patila60a7762018-08-29 17:10:47 -070092 char buffer[4096];
Daniel Colascioned64b5c92019-09-03 18:17:19 -070093 int fd = open(path, O_RDONLY | O_CLOEXEC);
Sandeep Patila60a7762018-08-29 17:10:47 -070094 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;
Sandeep Patil4c174ef2018-11-09 16:42:45 -0800108 uint32_t lineno = 0;
Sandeep Patile473d7e2018-12-11 09:28:38 -0800109 bool zram_tag_found = false;
Daniel Colascioned64b5c92019-09-03 18:17:19 -0700110 while (*p && found < ntags) {
111 for (size_t tagno = 0; tagno < ntags; ++tagno) {
112 const std::string_view& tag = tags[tagno];
Sandeep Patile473d7e2018-12-11 09:28:38 -0800113 // 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
Daniel Colascioned64b5c92019-09-03 18:17:19 -0700122 if (strncmp(p, tag.data(), tag.size()) == 0) {
Sandeep Patila60a7762018-08-29 17:10:47 -0700123 p += tag.size();
124 while (*p == ' ') p++;
125 char* endptr = nullptr;
Sandeep Patile473d7e2018-12-11 09:28:38 -0800126 uint64_t val = strtoull(p, &endptr, 10);
Sandeep Patila60a7762018-08-29 17:10:47 -0700127 if (p == endptr) {
Sandeep Patil4c174ef2018-11-09 16:42:45 -0800128 PLOG(ERROR) << "Failed to parse line:" << lineno + 1 << " in file: " << path;
Sandeep Patila60a7762018-08-29 17:10:47 -0700129 return false;
130 }
Sandeep Patile473d7e2018-12-11 09:28:38 -0800131 store_val(tag, val);
Sandeep Patila60a7762018-08-29 17:10:47 -0700132 p = endptr;
133 found++;
134 break;
135 }
136 }
Sandeep Patile473d7e2018-12-11 09:28:38 -0800137
Sandeep Patila60a7762018-08-29 17:10:47 -0700138 while (*p && *p != '\n') {
139 p++;
140 }
141 if (*p) p++;
Sandeep Patil4c174ef2018-11-09 16:42:45 -0800142 lineno++;
Sandeep Patila60a7762018-08-29 17:10:47 -0700143 }
144
145 return true;
146}
Sandeep Patila60a7762018-08-29 17:10:47 -0700147
Daniel Colascioned64b5c92019-09-03 18:17:19 -0700148uint64_t SysMemInfo::mem_zram_kb(const char* zram_dev_cstr) {
Sandeep Patil9f4028d2018-11-09 19:18:29 -0800149 uint64_t mem_zram_total = 0;
Daniel Colascioned64b5c92019-09-03 18:17:19 -0700150 if (zram_dev_cstr) {
151 if (!MemZramDevice(zram_dev_cstr, &mem_zram_total)) {
Sandeep Patil9f4028d2018-11-09 19:18:29 -0800152 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++) {
Daniel Colascioned64b5c92019-09-03 18:17:19 -0700159 std::string zram_dev_abspath = ::android::base::StringPrintf("/sys/block/zram%u/", i);
160 if (access(zram_dev_abspath.c_str(), F_OK)) {
Sandeep Patil9f4028d2018-11-09 19:18:29 -0800161 // 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;
Daniel Colascioned64b5c92019-09-03 18:17:19 -0700167 if (!MemZramDevice(zram_dev_abspath.c_str(), &mem_zram_dev)) {
Sandeep Patil9f4028d2018-11-09 19:18:29 -0800168 return 0;
169 }
170
171 mem_zram_total += mem_zram_dev;
172 }
173
174 return mem_zram_total / 1024;
175}
176
Daniel Colascioned64b5c92019-09-03 18:17:19 -0700177bool SysMemInfo::MemZramDevice(const char* zram_dev, uint64_t* mem_zram_dev) {
178 std::string mmstat = ::android::base::StringPrintf("%s/%s", zram_dev, "mm_stat");
Sandeep Patile473d7e2018-12-11 09:28:38 -0800179 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;
Sandeep Patil9f4028d2018-11-09 19:18:29 -0800185 return false;
186 }
Sandeep Patil9f4028d2018-11-09 19:18:29 -0800187 return true;
188 }
189
Sandeep Patile473d7e2018-12-11 09:28:38 -0800190 std::string content;
Daniel Colascioned64b5c92019-09-03 18:17:19 -0700191 if (::android::base::ReadFileToString(
192 ::android::base::StringPrintf("%s/mem_used_total", zram_dev), &content)) {
Sandeep Patil9f4028d2018-11-09 19:18:29 -0800193 *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
Sandeep Patil818d9f22019-01-19 12:04:18 -0800207// Public methods
Daniel Colascioned64b5c92019-09-03 18:17:19 -0700208uint64_t ReadVmallocInfo(const char* path) {
Sandeep Patil818d9f22019-01-19 12:04:18 -0800209 uint64_t vmalloc_total = 0;
Daniel Colascioned64b5c92019-09-03 18:17:19 -0700210 auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path, "re"), fclose};
Sandeep Patil818d9f22019-01-19 12:04:18 -0800211 if (fp == nullptr) {
212 return vmalloc_total;
213 }
214
215 char* line = nullptr;
216 size_t line_alloc = 0;
217 while (getline(&line, &line_alloc, fp.get()) > 0) {
218 // We are looking for lines like
219 //
220 // 0x0000000000000000-0x0000000000000000 12288 drm_property_create_blob+0x44/0xec pages=2 vmalloc
221 // 0x0000000000000000-0x0000000000000000 8192 wlan_logging_sock_init_svc+0xf8/0x4f0 [wlan] pages=1 vmalloc
222 //
223 // Notice that if the caller is coming from a module, the kernel prints and extra
224 // "[module_name]" after the address and the symbol of the call site. This means we can't
225 // use the old sscanf() method of getting the # of pages.
226 char* p_start = strstr(line, "pages=");
227 if (p_start == nullptr) {
228 // we didn't find anything
229 continue;
230 }
231
232 uint64_t nr_pages;
233 if (sscanf(p_start, "pages=%" SCNu64 "", &nr_pages) == 1) {
234 vmalloc_total += (nr_pages * getpagesize());
235 }
236 }
237
238 free(line);
239
240 return vmalloc_total;
241}
242
Suren Baghdasaryanc94063b2019-11-25 19:05:45 -0800243static bool ReadSysfsFile(const std::string& path, uint64_t* value) {
244 std::string content;
245 if (!::android::base::ReadFileToString(path, &content)) {
246 LOG(ERROR) << "Can't open file: " << path;
247 return false;
248 }
249
250 *value = strtoull(content.c_str(), NULL, 10);
251 if (*value == ULLONG_MAX) {
252 PLOG(ERROR) << "Invalid file format: " << path;
253 return false;
254 }
255
256 return true;
257}
258
259bool ReadIonHeapsSizeKb(uint64_t* size, const std::string& path) {
260 return ReadSysfsFile(path, size);
261}
262
263bool ReadIonPoolsSizeKb(uint64_t* size, const std::string& path) {
264 return ReadSysfsFile(path, size);
265}
266
Hridya Valsaraju554a5692021-03-04 13:49:44 -0800267bool ReadDmabufHeapPoolsSizeKb(uint64_t* size, const std::string& dma_heap_pool_size_path) {
268 static bool support_dmabuf_heap_pool_size = [dma_heap_pool_size_path]() -> bool {
269 bool ret = (access(dma_heap_pool_size_path.c_str(), R_OK) == 0);
270 if (!ret)
271 LOG(ERROR) << "Unable to read DMA-BUF heap total pool size, read ION total pool "
272 "size instead.";
273 return ret;
274 }();
275
276 if (!support_dmabuf_heap_pool_size) return ReadIonPoolsSizeKb(size);
277
278 return ReadSysfsFile(dma_heap_pool_size_path, size);
Hridya Valsarajubc029162021-02-02 11:07:04 -0800279}
280
Hridya Valsaraju7d1343d2021-02-09 21:48:24 -0800281bool ReadDmabufHeapTotalExportedKb(uint64_t* size, const std::string& dma_heap_root_path,
282 const std::string& dmabuf_sysfs_stats_path) {
Hridya Valsaraju554a5692021-03-04 13:49:44 -0800283 static bool support_dmabuf_heaps = [dma_heap_root_path]() -> bool {
284 bool ret = (access(dma_heap_root_path.c_str(), R_OK) == 0);
285 if (!ret) LOG(ERROR) << "DMA-BUF heaps not supported, read ION heap total instead.";
286 return ret;
287 }();
288
289 if (!support_dmabuf_heaps) return ReadIonHeapsSizeKb(size);
Hridya Valsaraju7d1343d2021-02-09 21:48:24 -0800290
291 std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(dma_heap_root_path.c_str()), closedir);
292
Hridya Valsaraju554a5692021-03-04 13:49:44 -0800293 if (!dir) {
294 return false;
295 }
Hridya Valsaraju7d1343d2021-02-09 21:48:24 -0800296
Hridya Valsaraju554a5692021-03-04 13:49:44 -0800297 std::unordered_set<std::string> heap_list;
298 struct dirent* dent;
299 while ((dent = readdir(dir.get()))) {
300 if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) continue;
301
302 heap_list.insert(dent->d_name);
Hridya Valsaraju7d1343d2021-02-09 21:48:24 -0800303 }
304
305 if (heap_list.empty()) return false;
306
307 android::dmabufinfo::DmabufSysfsStats stats;
308 if (!android::dmabufinfo::GetDmabufSysfsStats(&stats, dmabuf_sysfs_stats_path)) return false;
309
310 auto exporter_info = stats.exporter_info();
311
312 *size = 0;
313 for (const auto& heap : heap_list) {
314 auto iter = exporter_info.find(heap);
315 if (iter != exporter_info.end()) *size += iter->second.size;
316 }
317
318 *size = *size / 1024;
319
320 return true;
321}
322
Kalesh Singh8c798142021-06-28 13:48:45 -0400323bool ReadPerProcessGpuMem([[maybe_unused]] std::unordered_map<uint32_t, uint64_t>* out) {
Kevin Jeonc851b322021-03-30 06:33:12 +0000324#if defined(__ANDROID__) && !defined(__ANDROID_APEX__) && !defined(__ANDROID_VNDK__)
Kalesh Singh8c798142021-06-28 13:48:45 -0400325 static constexpr const char kBpfGpuMemTotalMap[] = "/sys/fs/bpf/map_gpu_mem_gpu_mem_total_map";
Suren Baghdasaryan5106ef72020-10-22 12:56:51 -0700326
327 // Use the read-only wrapper BpfMapRO to properly retrieve the read-only map.
328 auto map = bpf::BpfMapRO<uint64_t, uint64_t>(kBpfGpuMemTotalMap);
329 if (!map.isValid()) {
330 LOG(ERROR) << "Can't open file: " << kBpfGpuMemTotalMap;
331 return false;
332 }
333
Kalesh Singh8c798142021-06-28 13:48:45 -0400334 if (!out) {
335 LOG(ERROR) << "ReadPerProcessGpuMem: out param is null";
336 return false;
337 }
338 out->clear();
339
340 auto map_key = map.getFirstKey();
341 if (!map_key.ok()) {
342 return true;
343 }
344
345 do {
346 uint64_t key = map_key.value();
347 uint32_t pid = key; // BPF Key [32-bits GPU ID | 32-bits PID]
348
349 auto gpu_mem = map.readValue(key);
350 if (!gpu_mem.ok()) {
351 LOG(ERROR) << "Invalid file format: " << kBpfGpuMemTotalMap;
352 return false;
353 }
354
355 const auto& iter = out->find(pid);
356 if (iter == out->end()) {
357 out->insert({pid, gpu_mem.value() / 1024});
358 } else {
359 iter->second += gpu_mem.value() / 1024;
360 }
361
362 map_key = map.getNextKey(key);
363 } while (map_key.ok());
364
365 return true;
366#else
367 return false;
368#endif
369}
370
371bool ReadProcessGpuUsageKb([[maybe_unused]] uint32_t pid, [[maybe_unused]] uint32_t gpu_id,
372 uint64_t* size) {
373#if defined(__ANDROID__) && !defined(__ANDROID_APEX__) && !defined(__ANDROID_VNDK__)
374 static constexpr const char kBpfGpuMemTotalMap[] = "/sys/fs/bpf/map_gpu_mem_gpu_mem_total_map";
375
Kalesh Singha7162682021-12-02 10:17:35 -0800376 uint64_t gpu_mem;
377
Kalesh Singh8c798142021-06-28 13:48:45 -0400378 // BPF Key [32-bits GPU ID | 32-bits PID]
379 uint64_t kBpfKeyGpuUsage = ((uint64_t)gpu_id << 32) | pid;
380
381 // Use the read-only wrapper BpfMapRO to properly retrieve the read-only map.
382 auto map = bpf::BpfMapRO<uint64_t, uint64_t>(kBpfGpuMemTotalMap);
383 if (!map.isValid()) {
384 LOG(ERROR) << "Can't open file: " << kBpfGpuMemTotalMap;
385 return false;
386 }
387
388 auto res = map.readValue(kBpfKeyGpuUsage);
Kalesh Singha7162682021-12-02 10:17:35 -0800389
390 if (res.ok()) {
391 gpu_mem = res.value();
392 } else if (res.error().code() == ENOENT) {
393 gpu_mem = 0;
394 } else {
Suren Baghdasaryan5106ef72020-10-22 12:56:51 -0700395 LOG(ERROR) << "Invalid file format: " << kBpfGpuMemTotalMap;
396 return false;
397 }
398
399 if (size) {
Kalesh Singha7162682021-12-02 10:17:35 -0800400 *size = gpu_mem / 1024;
Suren Baghdasaryan5106ef72020-10-22 12:56:51 -0700401 }
402 return true;
403#else
404 if (size) {
405 *size = 0;
406 }
407 return false;
408#endif
409}
410
Kalesh Singh8c798142021-06-28 13:48:45 -0400411bool ReadGpuTotalUsageKb(uint64_t* size) {
412 // gpu_mem_total tracepoint defines PID 0 as global total
413 // GPU ID 0 suffices for current android devices.
414 // This will need to check all GPU IDs in future if more than
415 // one is GPU device is present on the device.
416 return ReadProcessGpuUsageKb(0, 0, size);
417}
418
Sandeep Patila60a7762018-08-29 17:10:47 -0700419} // namespace meminfo
420} // namespace android