| 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 | c24f1e3 | 2018-12-29 14:34:20 -0800 | [diff] [blame] | 82 | uint64_t SysMemInfo::ReadVmallocInfo(const std::string& path) { | 
|  | 83 | uint64_t vmalloc_total = 0; | 
|  | 84 | auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose}; | 
|  | 85 | if (fp == nullptr) { | 
|  | 86 | return vmalloc_total; | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | char line[1024]; | 
|  | 90 | while (fgets(line, 1024, fp.get()) != nullptr) { | 
|  | 91 | // We are looking for lines like | 
|  | 92 | // 0x0000000000000000-0x0000000000000000   12288 drm_property_create_blob+0x44/0xec pages=2 | 
|  | 93 | // vmalloc 0x0000000000000000-0x0000000000000000    8192 | 
|  | 94 | // wlan_logging_sock_init_svc+0xf8/0x4f0 [wlan] pages=1 vmalloc Notice that if the caller is | 
|  | 95 | // coming from a module, the kernel prints and extra "[module_name]" after the address and | 
|  | 96 | // the symbol of the call site. This means we can't use the old sscanf() method of getting | 
|  | 97 | // the # of pages. | 
|  | 98 | char* p_start = strstr(line, "pages="); | 
|  | 99 | if (p_start == nullptr) { | 
|  | 100 | // we didn't find anything | 
|  | 101 | continue; | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | p_start = strtok(p_start, " "); | 
|  | 105 | long nr_pages; | 
|  | 106 | if (sscanf(p_start, "pages=%ld", &nr_pages) == 1) { | 
|  | 107 | vmalloc_total += (nr_pages * getpagesize()); | 
|  | 108 | } | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | return vmalloc_total; | 
|  | 112 | } | 
|  | 113 |  | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 114 | // TODO: Delete this function if it can't match up with the c-like implementation below. | 
|  | 115 | // Currently, this added about 50 % extra overhead on hikey. | 
|  | 116 | #if 0 | 
|  | 117 | bool SysMemInfo::ReadMemInfo(const std::vector<std::string>& tags, const std::string& path) { | 
|  | 118 | std::string buffer; | 
|  | 119 | if (!::android::base::ReadFileToString(path, &buffer)) { | 
|  | 120 | PLOG(ERROR) << "Failed to read : " << path; | 
|  | 121 | return false; | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | uint32_t total_found = 0; | 
|  | 125 | for (auto s = buffer.begin(); s < buffer.end() && total_found < tags.size();) { | 
|  | 126 | for (auto& tag : tags) { | 
|  | 127 | if (tag == std::string(s, s + tag.size())) { | 
|  | 128 | s += tag.size(); | 
|  | 129 | while (isspace(*s)) s++; | 
|  | 130 | auto num_start = s; | 
|  | 131 | while (std::isdigit(*s)) s++; | 
|  | 132 |  | 
|  | 133 | std::string number(num_start, num_start + (s - num_start)); | 
|  | 134 | if (!::android::base::ParseUint(number, &mem_in_kb_[tag])) { | 
|  | 135 | LOG(ERROR) << "Failed to parse uint"; | 
|  | 136 | return false; | 
|  | 137 | } | 
|  | 138 | total_found++; | 
|  | 139 | break; | 
|  | 140 | } | 
|  | 141 | } | 
|  | 142 | while (s < buffer.end() && *s != '\n') s++; | 
|  | 143 | if (s < buffer.end()) s++; | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | return true; | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | #else | 
| Sandeep Patil | 2f0b6eb | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 150 | bool SysMemInfo::ReadMemInfo(const std::vector<std::string>& tags, const std::string& path, | 
|  | 151 | std::function<void(const std::string&, uint64_t)> store_val) { | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 152 | char buffer[4096]; | 
|  | 153 | int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); | 
|  | 154 | if (fd < 0) { | 
|  | 155 | PLOG(ERROR) << "Failed to open file :" << path; | 
|  | 156 | return false; | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | const int len = read(fd, buffer, sizeof(buffer) - 1); | 
|  | 160 | close(fd); | 
|  | 161 | if (len < 0) { | 
|  | 162 | return false; | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | buffer[len] = '\0'; | 
|  | 166 | char* p = buffer; | 
|  | 167 | uint32_t found = 0; | 
| Sandeep Patil | 2259fdf | 2018-11-09 16:42:45 -0800 | [diff] [blame] | 168 | uint32_t lineno = 0; | 
| Sandeep Patil | 2f0b6eb | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 169 | bool zram_tag_found = false; | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 170 | while (*p && found < tags.size()) { | 
|  | 171 | for (auto& tag : tags) { | 
| Sandeep Patil | 2f0b6eb | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 172 | // Special case for "Zram:" tag that android_os_Debug and friends look | 
|  | 173 | // up along with the rest of the numbers from /proc/meminfo | 
|  | 174 | if (!zram_tag_found && tag == "Zram:") { | 
|  | 175 | store_val(tag, mem_zram_kb()); | 
|  | 176 | zram_tag_found = true; | 
|  | 177 | found++; | 
|  | 178 | continue; | 
|  | 179 | } | 
|  | 180 |  | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 181 | if (strncmp(p, tag.c_str(), tag.size()) == 0) { | 
|  | 182 | p += tag.size(); | 
|  | 183 | while (*p == ' ') p++; | 
|  | 184 | char* endptr = nullptr; | 
| Sandeep Patil | 2f0b6eb | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 185 | uint64_t val = strtoull(p, &endptr, 10); | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 186 | if (p == endptr) { | 
| Sandeep Patil | 2259fdf | 2018-11-09 16:42:45 -0800 | [diff] [blame] | 187 | PLOG(ERROR) << "Failed to parse line:" << lineno + 1 << " in file: " << path; | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 188 | return false; | 
|  | 189 | } | 
| Sandeep Patil | 2f0b6eb | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 190 | store_val(tag, val); | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 191 | p = endptr; | 
|  | 192 | found++; | 
|  | 193 | break; | 
|  | 194 | } | 
|  | 195 | } | 
| Sandeep Patil | 2f0b6eb | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 196 |  | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 197 | while (*p && *p != '\n') { | 
|  | 198 | p++; | 
|  | 199 | } | 
|  | 200 | if (*p) p++; | 
| Sandeep Patil | 2259fdf | 2018-11-09 16:42:45 -0800 | [diff] [blame] | 201 | lineno++; | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 202 | } | 
|  | 203 |  | 
|  | 204 | return true; | 
|  | 205 | } | 
|  | 206 | #endif | 
|  | 207 |  | 
| Sandeep Patil | 70fa72d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 208 | uint64_t SysMemInfo::mem_zram_kb(const std::string& zram_dev) { | 
|  | 209 | uint64_t mem_zram_total = 0; | 
|  | 210 | if (!zram_dev.empty()) { | 
|  | 211 | if (!MemZramDevice(zram_dev, &mem_zram_total)) { | 
|  | 212 | return 0; | 
|  | 213 | } | 
|  | 214 | return mem_zram_total / 1024; | 
|  | 215 | } | 
|  | 216 |  | 
|  | 217 | constexpr uint32_t kMaxZramDevices = 256; | 
|  | 218 | for (uint32_t i = 0; i < kMaxZramDevices; i++) { | 
|  | 219 | std::string zram_dev = ::android::base::StringPrintf("/sys/block/zram%u/", i); | 
|  | 220 | if (access(zram_dev.c_str(), F_OK)) { | 
|  | 221 | // We assume zram devices appear in range 0-255 and appear always in sequence | 
|  | 222 | // under /sys/block. So, stop looking for them once we find one is missing. | 
|  | 223 | break; | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | uint64_t mem_zram_dev; | 
|  | 227 | if (!MemZramDevice(zram_dev, &mem_zram_dev)) { | 
|  | 228 | return 0; | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | mem_zram_total += mem_zram_dev; | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | return mem_zram_total / 1024; | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | 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] | 238 | std::string mmstat = ::android::base::StringPrintf("%s/%s", zram_dev.c_str(), "mm_stat"); | 
|  | 239 | auto mmstat_fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(mmstat.c_str(), "re"), fclose}; | 
|  | 240 | if (mmstat_fp != nullptr) { | 
|  | 241 | // only if we do have mmstat, use it. Otherwise, fall through to trying out the old | 
|  | 242 | // 'mem_used_total' | 
|  | 243 | if (fscanf(mmstat_fp.get(), "%*" SCNu64 " %*" SCNu64 " %" SCNu64, mem_zram_dev) != 1) { | 
|  | 244 | PLOG(ERROR) << "Malformed mm_stat file in: " << zram_dev; | 
| Sandeep Patil | 70fa72d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 245 | return false; | 
|  | 246 | } | 
| Sandeep Patil | 70fa72d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 247 | return true; | 
|  | 248 | } | 
|  | 249 |  | 
| Sandeep Patil | 2f0b6eb | 2018-12-11 09:28:38 -0800 | [diff] [blame] | 250 | std::string content; | 
| Sandeep Patil | 70fa72d | 2018-11-09 19:18:29 -0800 | [diff] [blame] | 251 | if (::android::base::ReadFileToString(zram_dev + "mem_used_total", &content)) { | 
|  | 252 | *mem_zram_dev = strtoull(content.c_str(), NULL, 10); | 
|  | 253 | if (*mem_zram_dev == ULLONG_MAX) { | 
|  | 254 | PLOG(ERROR) << "Malformed mem_used_total file for zram dev: " << zram_dev | 
|  | 255 | << " content: " << content; | 
|  | 256 | return false; | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | return true; | 
|  | 260 | } | 
|  | 261 |  | 
|  | 262 | LOG(ERROR) << "Can't find memory status under: " << zram_dev; | 
|  | 263 | return false; | 
|  | 264 | } | 
|  | 265 |  | 
| Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame] | 266 | }  // namespace meminfo | 
|  | 267 | }  // namespace android |