| Sandeep Patil | 54d8721 | 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 | 70fa72d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 20 | #include <inttypes.h> | 
| Sandeep Patil | 2f0b6eb | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 21 | #include <stdio.h> | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 22 | #include <stdlib.h> | 
| Sandeep Patil | c24f1e3 | 2018-12-29 14:34:20 -0800 | [diff] [blame] | 23 | #include <string.h> | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 24 | #include <unistd.h> | 
|  | 25 |  | 
| Sandeep Patil | 2f0b6eb | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 26 | #include <algorithm> | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 27 | #include <cctype> | 
| Sandeep Patil | 70fa72d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 28 | #include <cstdio> | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 29 | #include <fstream> | 
| Sandeep Patil | 2f0b6eb | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 30 | #include <iterator> | 
| Sandeep Patil | c24f1e3 | 2018-12-29 14:34:20 -0800 | [diff] [blame] | 31 | #include <sstream> | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 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> | 
| Sandeep Patil | 70fa72d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 39 | #include <android-base/stringprintf.h> | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 40 | #include <android-base/strings.h> | 
| Sandeep Patil | 70fa72d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 41 | #include <android-base/unique_fd.h> | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 42 |  | 
|  | 43 | #include "meminfo_private.h" | 
|  | 44 |  | 
|  | 45 | namespace android { | 
|  | 46 | namespace meminfo { | 
|  | 47 |  | 
|  | 48 | const std::vector<std::string> SysMemInfo::kDefaultSysMemInfoTags = { | 
| Sandeep Patil | 70fa72d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 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, | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 54 | }; | 
|  | 55 |  | 
|  | 56 | bool SysMemInfo::ReadMemInfo(const std::string& path) { | 
| Sandeep Patil | 2f0b6eb | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 57 | return ReadMemInfo(SysMemInfo::kDefaultSysMemInfoTags, path, | 
|  | 58 | [&](const std::string& tag, uint64_t val) { mem_in_kb_[tag] = val; }); | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | bool SysMemInfo::ReadMemInfo(std::vector<uint64_t>* out, const std::string& path) { | 
|  | 62 | return ReadMemInfo(SysMemInfo::kDefaultSysMemInfoTags, out, path); | 
|  | 63 | } | 
|  | 64 |  | 
|  | 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 | }); | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 80 | } | 
|  | 81 |  | 
| Sandeep Patil | e04680d | 2019-01-19 12:04:18 -0800 | [diff] [blame] | 82 | uint64_t SysMemInfo::ReadVmallocInfo() { | 
|  | 83 | return ::android::meminfo::ReadVmallocInfo(); | 
| Sandeep Patil | c24f1e3 | 2018-12-29 14:34:20 -0800 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 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 | 
| Sandeep Patil | 2f0b6eb | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 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) { | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 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; | 
| Sandeep Patil | 2259fdf | 2018-11-09 16:42:45 -0800 | [diff] [blame] | 140 | uint32_t lineno = 0; | 
| Sandeep Patil | 2f0b6eb | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 141 | bool zram_tag_found = false; | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 142 | while (*p && found < tags.size()) { | 
|  | 143 | for (auto& tag : tags) { | 
| Sandeep Patil | 2f0b6eb | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 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 |  | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 153 | if (strncmp(p, tag.c_str(), tag.size()) == 0) { | 
|  | 154 | p += tag.size(); | 
|  | 155 | while (*p == ' ') p++; | 
|  | 156 | char* endptr = nullptr; | 
| Sandeep Patil | 2f0b6eb | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 157 | uint64_t val = strtoull(p, &endptr, 10); | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 158 | if (p == endptr) { | 
| Sandeep Patil | 2259fdf | 2018-11-09 16:42:45 -0800 | [diff] [blame] | 159 | PLOG(ERROR) << "Failed to parse line:" << lineno + 1 << " in file: " << path; | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 160 | return false; | 
|  | 161 | } | 
| Sandeep Patil | 2f0b6eb | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 162 | store_val(tag, val); | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 163 | p = endptr; | 
|  | 164 | found++; | 
|  | 165 | break; | 
|  | 166 | } | 
|  | 167 | } | 
| Sandeep Patil | 2f0b6eb | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 168 |  | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 169 | while (*p && *p != '\n') { | 
|  | 170 | p++; | 
|  | 171 | } | 
|  | 172 | if (*p) p++; | 
| Sandeep Patil | 2259fdf | 2018-11-09 16:42:45 -0800 | [diff] [blame] | 173 | lineno++; | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 174 | } | 
|  | 175 |  | 
|  | 176 | return true; | 
|  | 177 | } | 
|  | 178 | #endif | 
|  | 179 |  | 
| Sandeep Patil | 70fa72d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 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 |  | 
|  | 209 | bool SysMemInfo::MemZramDevice(const std::string& zram_dev, uint64_t* mem_zram_dev) { | 
| Sandeep Patil | 2f0b6eb | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 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; | 
| Sandeep Patil | 70fa72d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 217 | return false; | 
|  | 218 | } | 
| Sandeep Patil | 70fa72d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 219 | return true; | 
|  | 220 | } | 
|  | 221 |  | 
| Sandeep Patil | 2f0b6eb | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 222 | std::string content; | 
| Sandeep Patil | 70fa72d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 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 |  | 
| Sandeep Patil | e04680d | 2019-01-19 12:04:18 -0800 | [diff] [blame] | 238 | // Public methods | 
|  | 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 |  | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 274 | }  // namespace meminfo | 
|  | 275 | }  // namespace android |