Sandeep Patil | a60a776 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 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> |
Sandeep Patil | 9f4028d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 20 | #include <inttypes.h> |
Sandeep Patil | e473d7e | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 21 | #include <stdio.h> |
Sandeep Patil | a60a776 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 22 | #include <stdlib.h> |
Sandeep Patil | 76a34f0 | 2018-12-29 14:34:20 -0800 | [diff] [blame] | 23 | #include <string.h> |
Sandeep Patil | a60a776 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | |
Sandeep Patil | e473d7e | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 26 | #include <algorithm> |
Sandeep Patil | a60a776 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 27 | #include <cctype> |
Sandeep Patil | 9f4028d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 28 | #include <cstdio> |
Sandeep Patil | a60a776 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 29 | #include <fstream> |
Sandeep Patil | e473d7e | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 30 | #include <iterator> |
Kevin Jeon | c851b32 | 2021-03-30 06:33:12 +0000 | [diff] [blame] | 31 | #if defined(__ANDROID__) && !defined(__ANDROID_APEX__) && !defined(__ANDROID_VNDK__) |
Suren Baghdasaryan | 5106ef7 | 2020-10-22 12:56:51 -0700 | [diff] [blame] | 32 | #include "bpf/BpfMap.h" |
| 33 | #endif |
Sandeep Patil | 76a34f0 | 2018-12-29 14:34:20 -0800 | [diff] [blame] | 34 | #include <sstream> |
Sandeep Patil | a60a776 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 35 | #include <string> |
Hridya Valsaraju | 7d1343d | 2021-02-09 21:48:24 -0800 | [diff] [blame] | 36 | #include <unordered_set> |
Sandeep Patil | a60a776 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 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> |
Sandeep Patil | 9f4028d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 43 | #include <android-base/stringprintf.h> |
Sandeep Patil | a60a776 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 44 | #include <android-base/strings.h> |
Sandeep Patil | 9f4028d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 45 | #include <android-base/unique_fd.h> |
Hridya Valsaraju | 7d1343d | 2021-02-09 21:48:24 -0800 | [diff] [blame] | 46 | #include <dmabufinfo/dmabuf_sysfs_stats.h> |
Sandeep Patil | a60a776 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 47 | |
| 48 | #include "meminfo_private.h" |
| 49 | |
| 50 | namespace android { |
| 51 | namespace meminfo { |
| 52 | |
Daniel Colascione | d64b5c9 | 2019-09-03 18:17:19 -0700 | [diff] [blame] | 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 | }); |
Sandeep Patil | e473d7e | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Daniel Colascione | d64b5c9 | 2019-09-03 18:17:19 -0700 | [diff] [blame] | 65 | bool SysMemInfo::ReadMemInfo(std::vector<uint64_t>* out, const char* path) { |
Sandeep Patil | e473d7e | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 66 | out->clear(); |
Daniel Colascione | d64b5c9 | 2019-09-03 18:17:19 -0700 | [diff] [blame] | 67 | out->resize(SysMemInfo::kDefaultSysMemInfoTags.size()); |
| 68 | return ReadMemInfo(SysMemInfo::kDefaultSysMemInfoTags.size(), |
| 69 | &*SysMemInfo::kDefaultSysMemInfoTags.begin(), out->data(), path); |
| 70 | } |
Sandeep Patil | e473d7e | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 71 | |
Daniel Colascione | d64b5c9 | 2019-09-03 18:17:19 -0700 | [diff] [blame] | 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) { |
Sandeep Patil | e473d7e | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 77 | LOG(ERROR) << "Tried to store invalid tag: " << tag; |
| 78 | return; |
| 79 | } |
Daniel Colascione | d64b5c9 | 2019-09-03 18:17:19 -0700 | [diff] [blame] | 80 | auto index = std::distance(tags, it); |
Sandeep Patil | e473d7e | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 81 | // store the values in the same order as the tags |
Daniel Colascione | d64b5c9 | 2019-09-03 18:17:19 -0700 | [diff] [blame] | 82 | out[index] = val; |
Sandeep Patil | e473d7e | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 83 | }); |
Sandeep Patil | a60a776 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Sandeep Patil | 818d9f2 | 2019-01-19 12:04:18 -0800 | [diff] [blame] | 86 | uint64_t SysMemInfo::ReadVmallocInfo() { |
| 87 | return ::android::meminfo::ReadVmallocInfo(); |
Sandeep Patil | 76a34f0 | 2018-12-29 14:34:20 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Daniel Colascione | d64b5c9 | 2019-09-03 18:17:19 -0700 | [diff] [blame] | 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) { |
Sandeep Patil | a60a776 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 92 | char buffer[4096]; |
Daniel Colascione | d64b5c9 | 2019-09-03 18:17:19 -0700 | [diff] [blame] | 93 | int fd = open(path, O_RDONLY | O_CLOEXEC); |
Sandeep Patil | a60a776 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 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; |
Sandeep Patil | 4c174ef | 2018-11-09 16:42:45 -0800 | [diff] [blame] | 108 | uint32_t lineno = 0; |
Sandeep Patil | e473d7e | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 109 | bool zram_tag_found = false; |
Daniel Colascione | d64b5c9 | 2019-09-03 18:17:19 -0700 | [diff] [blame] | 110 | while (*p && found < ntags) { |
| 111 | for (size_t tagno = 0; tagno < ntags; ++tagno) { |
| 112 | const std::string_view& tag = tags[tagno]; |
Sandeep Patil | e473d7e | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 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 | |
Daniel Colascione | d64b5c9 | 2019-09-03 18:17:19 -0700 | [diff] [blame] | 122 | if (strncmp(p, tag.data(), tag.size()) == 0) { |
Sandeep Patil | a60a776 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 123 | p += tag.size(); |
| 124 | while (*p == ' ') p++; |
| 125 | char* endptr = nullptr; |
Sandeep Patil | e473d7e | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 126 | uint64_t val = strtoull(p, &endptr, 10); |
Sandeep Patil | a60a776 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 127 | if (p == endptr) { |
Sandeep Patil | 4c174ef | 2018-11-09 16:42:45 -0800 | [diff] [blame] | 128 | PLOG(ERROR) << "Failed to parse line:" << lineno + 1 << " in file: " << path; |
Sandeep Patil | a60a776 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 129 | return false; |
| 130 | } |
Sandeep Patil | e473d7e | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 131 | store_val(tag, val); |
Sandeep Patil | a60a776 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 132 | p = endptr; |
| 133 | found++; |
| 134 | break; |
| 135 | } |
| 136 | } |
Sandeep Patil | e473d7e | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 137 | |
Sandeep Patil | a60a776 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 138 | while (*p && *p != '\n') { |
| 139 | p++; |
| 140 | } |
| 141 | if (*p) p++; |
Sandeep Patil | 4c174ef | 2018-11-09 16:42:45 -0800 | [diff] [blame] | 142 | lineno++; |
Sandeep Patil | a60a776 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | return true; |
| 146 | } |
Sandeep Patil | a60a776 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 147 | |
Daniel Colascione | d64b5c9 | 2019-09-03 18:17:19 -0700 | [diff] [blame] | 148 | uint64_t SysMemInfo::mem_zram_kb(const char* zram_dev_cstr) { |
Sandeep Patil | 9f4028d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 149 | uint64_t mem_zram_total = 0; |
Daniel Colascione | d64b5c9 | 2019-09-03 18:17:19 -0700 | [diff] [blame] | 150 | if (zram_dev_cstr) { |
| 151 | if (!MemZramDevice(zram_dev_cstr, &mem_zram_total)) { |
Sandeep Patil | 9f4028d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 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++) { |
Daniel Colascione | d64b5c9 | 2019-09-03 18:17:19 -0700 | [diff] [blame] | 159 | std::string zram_dev_abspath = ::android::base::StringPrintf("/sys/block/zram%u/", i); |
| 160 | if (access(zram_dev_abspath.c_str(), F_OK)) { |
Sandeep Patil | 9f4028d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 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; |
Daniel Colascione | d64b5c9 | 2019-09-03 18:17:19 -0700 | [diff] [blame] | 167 | if (!MemZramDevice(zram_dev_abspath.c_str(), &mem_zram_dev)) { |
Sandeep Patil | 9f4028d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | mem_zram_total += mem_zram_dev; |
| 172 | } |
| 173 | |
| 174 | return mem_zram_total / 1024; |
| 175 | } |
| 176 | |
Daniel Colascione | d64b5c9 | 2019-09-03 18:17:19 -0700 | [diff] [blame] | 177 | bool 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 Patil | e473d7e | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 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; |
Sandeep Patil | 9f4028d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 185 | return false; |
| 186 | } |
Sandeep Patil | 9f4028d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 187 | return true; |
| 188 | } |
| 189 | |
Sandeep Patil | e473d7e | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 190 | std::string content; |
Daniel Colascione | d64b5c9 | 2019-09-03 18:17:19 -0700 | [diff] [blame] | 191 | if (::android::base::ReadFileToString( |
| 192 | ::android::base::StringPrintf("%s/mem_used_total", zram_dev), &content)) { |
Sandeep Patil | 9f4028d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 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 | |
Sandeep Patil | 818d9f2 | 2019-01-19 12:04:18 -0800 | [diff] [blame] | 207 | // Public methods |
Daniel Colascione | d64b5c9 | 2019-09-03 18:17:19 -0700 | [diff] [blame] | 208 | uint64_t ReadVmallocInfo(const char* path) { |
Sandeep Patil | 818d9f2 | 2019-01-19 12:04:18 -0800 | [diff] [blame] | 209 | uint64_t vmalloc_total = 0; |
Daniel Colascione | d64b5c9 | 2019-09-03 18:17:19 -0700 | [diff] [blame] | 210 | auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path, "re"), fclose}; |
Sandeep Patil | 818d9f2 | 2019-01-19 12:04:18 -0800 | [diff] [blame] | 211 | 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 Baghdasaryan | c94063b | 2019-11-25 19:05:45 -0800 | [diff] [blame] | 243 | static 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 | |
| 259 | bool ReadIonHeapsSizeKb(uint64_t* size, const std::string& path) { |
| 260 | return ReadSysfsFile(path, size); |
| 261 | } |
| 262 | |
| 263 | bool ReadIonPoolsSizeKb(uint64_t* size, const std::string& path) { |
| 264 | return ReadSysfsFile(path, size); |
| 265 | } |
| 266 | |
Hridya Valsaraju | 554a569 | 2021-03-04 13:49:44 -0800 | [diff] [blame] | 267 | bool 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 Valsaraju | bc02916 | 2021-02-02 11:07:04 -0800 | [diff] [blame] | 279 | } |
| 280 | |
Hridya Valsaraju | 7d1343d | 2021-02-09 21:48:24 -0800 | [diff] [blame] | 281 | bool ReadDmabufHeapTotalExportedKb(uint64_t* size, const std::string& dma_heap_root_path, |
| 282 | const std::string& dmabuf_sysfs_stats_path) { |
Hridya Valsaraju | 554a569 | 2021-03-04 13:49:44 -0800 | [diff] [blame] | 283 | 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 Valsaraju | 7d1343d | 2021-02-09 21:48:24 -0800 | [diff] [blame] | 290 | |
| 291 | std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(dma_heap_root_path.c_str()), closedir); |
| 292 | |
Hridya Valsaraju | 554a569 | 2021-03-04 13:49:44 -0800 | [diff] [blame] | 293 | if (!dir) { |
| 294 | return false; |
| 295 | } |
Hridya Valsaraju | 7d1343d | 2021-02-09 21:48:24 -0800 | [diff] [blame] | 296 | |
Hridya Valsaraju | 554a569 | 2021-03-04 13:49:44 -0800 | [diff] [blame] | 297 | 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 Valsaraju | 7d1343d | 2021-02-09 21:48:24 -0800 | [diff] [blame] | 303 | } |
| 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 Singh | 8c79814 | 2021-06-28 13:48:45 -0400 | [diff] [blame] | 323 | bool ReadPerProcessGpuMem([[maybe_unused]] std::unordered_map<uint32_t, uint64_t>* out) { |
Kevin Jeon | c851b32 | 2021-03-30 06:33:12 +0000 | [diff] [blame] | 324 | #if defined(__ANDROID__) && !defined(__ANDROID_APEX__) && !defined(__ANDROID_VNDK__) |
Kalesh Singh | 8c79814 | 2021-06-28 13:48:45 -0400 | [diff] [blame] | 325 | static constexpr const char kBpfGpuMemTotalMap[] = "/sys/fs/bpf/map_gpu_mem_gpu_mem_total_map"; |
Suren Baghdasaryan | 5106ef7 | 2020-10-22 12:56:51 -0700 | [diff] [blame] | 326 | |
| 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 Singh | 8c79814 | 2021-06-28 13:48:45 -0400 | [diff] [blame] | 334 | 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 | |
| 371 | bool 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 Singh | a716268 | 2021-12-02 10:17:35 -0800 | [diff] [blame] | 376 | uint64_t gpu_mem; |
| 377 | |
Kalesh Singh | 8c79814 | 2021-06-28 13:48:45 -0400 | [diff] [blame] | 378 | // 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 Singh | a716268 | 2021-12-02 10:17:35 -0800 | [diff] [blame] | 389 | |
| 390 | if (res.ok()) { |
| 391 | gpu_mem = res.value(); |
| 392 | } else if (res.error().code() == ENOENT) { |
| 393 | gpu_mem = 0; |
| 394 | } else { |
Suren Baghdasaryan | 5106ef7 | 2020-10-22 12:56:51 -0700 | [diff] [blame] | 395 | LOG(ERROR) << "Invalid file format: " << kBpfGpuMemTotalMap; |
| 396 | return false; |
| 397 | } |
| 398 | |
| 399 | if (size) { |
Kalesh Singh | a716268 | 2021-12-02 10:17:35 -0800 | [diff] [blame] | 400 | *size = gpu_mem / 1024; |
Suren Baghdasaryan | 5106ef7 | 2020-10-22 12:56:51 -0700 | [diff] [blame] | 401 | } |
| 402 | return true; |
| 403 | #else |
| 404 | if (size) { |
| 405 | *size = 0; |
| 406 | } |
| 407 | return false; |
| 408 | #endif |
| 409 | } |
| 410 | |
Kalesh Singh | 8c79814 | 2021-06-28 13:48:45 -0400 | [diff] [blame] | 411 | bool 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 Patil | a60a776 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 419 | } // namespace meminfo |
| 420 | } // namespace android |