blob: 4ec1c99297f12122b9b463531dc8552ec75345aa [file] [log] [blame]
Sandeep Patil54d87212018-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 Patil70fa72d2018-11-09 19:18:29 -080020#include <inttypes.h>
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -080021#include <stdio.h>
Sandeep Patil54d87212018-08-29 17:10:47 -070022#include <stdlib.h>
23#include <unistd.h>
24
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -080025#include <algorithm>
Sandeep Patil54d87212018-08-29 17:10:47 -070026#include <cctype>
Sandeep Patil70fa72d2018-11-09 19:18:29 -080027#include <cstdio>
Sandeep Patil54d87212018-08-29 17:10:47 -070028#include <fstream>
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -080029#include <iterator>
Sandeep Patil54d87212018-08-29 17:10:47 -070030#include <string>
31#include <utility>
32#include <vector>
33
34#include <android-base/file.h>
35#include <android-base/logging.h>
36#include <android-base/parseint.h>
Sandeep Patil70fa72d2018-11-09 19:18:29 -080037#include <android-base/stringprintf.h>
Sandeep Patil54d87212018-08-29 17:10:47 -070038#include <android-base/strings.h>
Sandeep Patil70fa72d2018-11-09 19:18:29 -080039#include <android-base/unique_fd.h>
Sandeep Patil54d87212018-08-29 17:10:47 -070040
41#include "meminfo_private.h"
42
43namespace android {
44namespace meminfo {
45
46const std::vector<std::string> SysMemInfo::kDefaultSysMemInfoTags = {
Sandeep Patil70fa72d2018-11-09 19:18:29 -080047 SysMemInfo::kMemTotal, SysMemInfo::kMemFree, SysMemInfo::kMemBuffers,
48 SysMemInfo::kMemCached, SysMemInfo::kMemShmem, SysMemInfo::kMemSlab,
49 SysMemInfo::kMemSReclaim, SysMemInfo::kMemSUnreclaim, SysMemInfo::kMemSwapTotal,
50 SysMemInfo::kMemSwapFree, SysMemInfo::kMemMapped, SysMemInfo::kMemVmallocUsed,
51 SysMemInfo::kMemPageTables, SysMemInfo::kMemKernelStack,
Sandeep Patil54d87212018-08-29 17:10:47 -070052};
53
54bool SysMemInfo::ReadMemInfo(const std::string& path) {
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -080055 return ReadMemInfo(SysMemInfo::kDefaultSysMemInfoTags, path,
56 [&](const std::string& tag, uint64_t val) { mem_in_kb_[tag] = val; });
57}
58
59bool SysMemInfo::ReadMemInfo(std::vector<uint64_t>* out, const std::string& path) {
60 return ReadMemInfo(SysMemInfo::kDefaultSysMemInfoTags, out, path);
61}
62
63bool SysMemInfo::ReadMemInfo(const std::vector<std::string>& tags, std::vector<uint64_t>* out,
64 const std::string& path) {
65 out->clear();
66 out->resize(tags.size());
67
68 return ReadMemInfo(tags, path, [&]([[maybe_unused]] const std::string& tag, uint64_t val) {
69 auto it = std::find(tags.begin(), tags.end(), tag);
70 if (it == tags.end()) {
71 LOG(ERROR) << "Tried to store invalid tag: " << tag;
72 return;
73 }
74 auto index = std::distance(tags.begin(), it);
75 // store the values in the same order as the tags
76 out->at(index) = val;
77 });
Sandeep Patil54d87212018-08-29 17:10:47 -070078}
79
80// TODO: Delete this function if it can't match up with the c-like implementation below.
81// Currently, this added about 50 % extra overhead on hikey.
82#if 0
83bool SysMemInfo::ReadMemInfo(const std::vector<std::string>& tags, const std::string& path) {
84 std::string buffer;
85 if (!::android::base::ReadFileToString(path, &buffer)) {
86 PLOG(ERROR) << "Failed to read : " << path;
87 return false;
88 }
89
90 uint32_t total_found = 0;
91 for (auto s = buffer.begin(); s < buffer.end() && total_found < tags.size();) {
92 for (auto& tag : tags) {
93 if (tag == std::string(s, s + tag.size())) {
94 s += tag.size();
95 while (isspace(*s)) s++;
96 auto num_start = s;
97 while (std::isdigit(*s)) s++;
98
99 std::string number(num_start, num_start + (s - num_start));
100 if (!::android::base::ParseUint(number, &mem_in_kb_[tag])) {
101 LOG(ERROR) << "Failed to parse uint";
102 return false;
103 }
104 total_found++;
105 break;
106 }
107 }
108 while (s < buffer.end() && *s != '\n') s++;
109 if (s < buffer.end()) s++;
110 }
111
112 return true;
113}
114
115#else
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -0800116bool SysMemInfo::ReadMemInfo(const std::vector<std::string>& tags, const std::string& path,
117 std::function<void(const std::string&, uint64_t)> store_val) {
Sandeep Patil54d87212018-08-29 17:10:47 -0700118 char buffer[4096];
119 int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
120 if (fd < 0) {
121 PLOG(ERROR) << "Failed to open file :" << path;
122 return false;
123 }
124
125 const int len = read(fd, buffer, sizeof(buffer) - 1);
126 close(fd);
127 if (len < 0) {
128 return false;
129 }
130
131 buffer[len] = '\0';
132 char* p = buffer;
133 uint32_t found = 0;
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800134 uint32_t lineno = 0;
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -0800135 bool zram_tag_found = false;
Sandeep Patil54d87212018-08-29 17:10:47 -0700136 while (*p && found < tags.size()) {
137 for (auto& tag : tags) {
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -0800138 // Special case for "Zram:" tag that android_os_Debug and friends look
139 // up along with the rest of the numbers from /proc/meminfo
140 if (!zram_tag_found && tag == "Zram:") {
141 store_val(tag, mem_zram_kb());
142 zram_tag_found = true;
143 found++;
144 continue;
145 }
146
Sandeep Patil54d87212018-08-29 17:10:47 -0700147 if (strncmp(p, tag.c_str(), tag.size()) == 0) {
148 p += tag.size();
149 while (*p == ' ') p++;
150 char* endptr = nullptr;
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -0800151 uint64_t val = strtoull(p, &endptr, 10);
Sandeep Patil54d87212018-08-29 17:10:47 -0700152 if (p == endptr) {
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800153 PLOG(ERROR) << "Failed to parse line:" << lineno + 1 << " in file: " << path;
Sandeep Patil54d87212018-08-29 17:10:47 -0700154 return false;
155 }
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -0800156 store_val(tag, val);
Sandeep Patil54d87212018-08-29 17:10:47 -0700157 p = endptr;
158 found++;
159 break;
160 }
161 }
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -0800162
Sandeep Patil54d87212018-08-29 17:10:47 -0700163 while (*p && *p != '\n') {
164 p++;
165 }
166 if (*p) p++;
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800167 lineno++;
Sandeep Patil54d87212018-08-29 17:10:47 -0700168 }
169
170 return true;
171}
172#endif
173
Sandeep Patil70fa72d2018-11-09 19:18:29 -0800174uint64_t SysMemInfo::mem_zram_kb(const std::string& zram_dev) {
175 uint64_t mem_zram_total = 0;
176 if (!zram_dev.empty()) {
177 if (!MemZramDevice(zram_dev, &mem_zram_total)) {
178 return 0;
179 }
180 return mem_zram_total / 1024;
181 }
182
183 constexpr uint32_t kMaxZramDevices = 256;
184 for (uint32_t i = 0; i < kMaxZramDevices; i++) {
185 std::string zram_dev = ::android::base::StringPrintf("/sys/block/zram%u/", i);
186 if (access(zram_dev.c_str(), F_OK)) {
187 // We assume zram devices appear in range 0-255 and appear always in sequence
188 // under /sys/block. So, stop looking for them once we find one is missing.
189 break;
190 }
191
192 uint64_t mem_zram_dev;
193 if (!MemZramDevice(zram_dev, &mem_zram_dev)) {
194 return 0;
195 }
196
197 mem_zram_total += mem_zram_dev;
198 }
199
200 return mem_zram_total / 1024;
201}
202
203bool SysMemInfo::MemZramDevice(const std::string& zram_dev, uint64_t* mem_zram_dev) {
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -0800204 std::string mmstat = ::android::base::StringPrintf("%s/%s", zram_dev.c_str(), "mm_stat");
205 auto mmstat_fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(mmstat.c_str(), "re"), fclose};
206 if (mmstat_fp != nullptr) {
207 // only if we do have mmstat, use it. Otherwise, fall through to trying out the old
208 // 'mem_used_total'
209 if (fscanf(mmstat_fp.get(), "%*" SCNu64 " %*" SCNu64 " %" SCNu64, mem_zram_dev) != 1) {
210 PLOG(ERROR) << "Malformed mm_stat file in: " << zram_dev;
Sandeep Patil70fa72d2018-11-09 19:18:29 -0800211 return false;
212 }
Sandeep Patil70fa72d2018-11-09 19:18:29 -0800213 return true;
214 }
215
Sandeep Patil2f0b6eb2018-12-11 09:28:38 -0800216 std::string content;
Sandeep Patil70fa72d2018-11-09 19:18:29 -0800217 if (::android::base::ReadFileToString(zram_dev + "mem_used_total", &content)) {
218 *mem_zram_dev = strtoull(content.c_str(), NULL, 10);
219 if (*mem_zram_dev == ULLONG_MAX) {
220 PLOG(ERROR) << "Malformed mem_used_total file for zram dev: " << zram_dev
221 << " content: " << content;
222 return false;
223 }
224
225 return true;
226 }
227
228 LOG(ERROR) << "Can't find memory status under: " << zram_dev;
229 return false;
230}
231
Sandeep Patil54d87212018-08-29 17:10:47 -0700232} // namespace meminfo
233} // namespace android