blob: a8b43c189311cbb1e8dd2af8e25107f868e566a4 [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 <errno.h>
18#include <fcntl.h>
19#include <inttypes.h>
20#include <linux/kernel-page-flags.h>
21#include <stdio.h>
22#include <unistd.h>
23
Sandeep Patildfd34be2019-01-13 17:39:08 -080024#include <atomic>
Sandeep Patil54d87212018-08-29 17:10:47 -070025#include <fstream>
26#include <iostream>
27#include <memory>
28#include <string>
29#include <utility>
Christopher Ferris7f8915c2019-06-25 17:30:56 -070030#include <vector>
Sandeep Patil54d87212018-08-29 17:10:47 -070031
32#include <android-base/file.h>
33#include <android-base/logging.h>
34#include <android-base/stringprintf.h>
Sandeep Patilfa2d8d52018-12-29 21:05:38 -080035#include <android-base/strings.h>
Sandeep Patil54d87212018-08-29 17:10:47 -070036#include <android-base/unique_fd.h>
37#include <procinfo/process_map.h>
38
39#include "meminfo_private.h"
40
41namespace android {
42namespace meminfo {
43
44static void add_mem_usage(MemUsage* to, const MemUsage& from) {
45 to->vss += from.vss;
46 to->rss += from.rss;
47 to->pss += from.pss;
48 to->uss += from.uss;
49
Sandeep Patil2259fdf2018-11-09 16:42:45 -080050 to->swap += from.swap;
51
Sandeep Patil54d87212018-08-29 17:10:47 -070052 to->private_clean += from.private_clean;
53 to->private_dirty += from.private_dirty;
54
55 to->shared_clean += from.shared_clean;
56 to->shared_dirty += from.shared_dirty;
57}
58
Sandeep Patil82a48b12019-01-01 16:04:04 -080059// Returns true if the line was valid smaps stats line false otherwise.
60static bool parse_smaps_field(const char* line, MemUsage* stats) {
61 char field[64];
62 int len;
63 if (sscanf(line, "%63s %n", field, &len) == 1 && *field && field[strlen(field) - 1] == ':') {
64 const char* c = line + len;
65 switch (field[0]) {
66 case 'P':
67 if (strncmp(field, "Pss:", 4) == 0) {
68 stats->pss = strtoull(c, nullptr, 10);
69 } else if (strncmp(field, "Private_Clean:", 14) == 0) {
70 uint64_t prcl = strtoull(c, nullptr, 10);
71 stats->private_clean = prcl;
72 stats->uss += prcl;
73 } else if (strncmp(field, "Private_Dirty:", 14) == 0) {
74 uint64_t prdi = strtoull(c, nullptr, 10);
75 stats->private_dirty = prdi;
76 stats->uss += prdi;
77 }
78 break;
79 case 'S':
80 if (strncmp(field, "Size:", 5) == 0) {
81 stats->vss = strtoull(c, nullptr, 10);
82 } else if (strncmp(field, "Shared_Clean:", 13) == 0) {
83 stats->shared_clean = strtoull(c, nullptr, 10);
84 } else if (strncmp(field, "Shared_Dirty:", 13) == 0) {
85 stats->shared_dirty = strtoull(c, nullptr, 10);
86 } else if (strncmp(field, "Swap:", 5) == 0) {
87 stats->swap = strtoull(c, nullptr, 10);
88 } else if (strncmp(field, "SwapPss:", 8) == 0) {
89 stats->swap_pss = strtoull(c, nullptr, 10);
90 }
91 break;
92 case 'R':
93 if (strncmp(field, "Rss:", 4) == 0) {
94 stats->rss = strtoull(c, nullptr, 10);
95 }
96 break;
97 }
98 return true;
99 }
100
101 return false;
102}
103
Sandeep Patilf1291992018-11-19 15:25:18 -0800104bool ProcMemInfo::ResetWorkingSet(pid_t pid) {
105 std::string clear_refs_path = ::android::base::StringPrintf("/proc/%d/clear_refs", pid);
106 if (!::android::base::WriteStringToFile("1\n", clear_refs_path)) {
107 PLOG(ERROR) << "Failed to write to " << clear_refs_path;
108 return false;
109 }
110
111 return true;
112}
113
Sandeep Patil549feab2018-11-19 11:38:40 -0800114ProcMemInfo::ProcMemInfo(pid_t pid, bool get_wss, uint64_t pgflags, uint64_t pgflags_mask)
Sandeep Patilc6497eb2018-11-20 09:31:36 -0800115 : pid_(pid), get_wss_(get_wss), pgflags_(pgflags), pgflags_mask_(pgflags_mask) {}
Sandeep Patil54d87212018-08-29 17:10:47 -0700116
117const std::vector<Vma>& ProcMemInfo::Maps() {
Sandeep Patilc6497eb2018-11-20 09:31:36 -0800118 if (maps_.empty() && !ReadMaps(get_wss_)) {
119 LOG(ERROR) << "Failed to read maps for Process " << pid_;
120 }
121
Sandeep Patil54d87212018-08-29 17:10:47 -0700122 return maps_;
123}
124
Sandeep Patil061b7132019-01-19 21:11:01 -0800125const std::vector<Vma>& ProcMemInfo::MapsWithPageIdle() {
126 if (maps_.empty() && !ReadMaps(get_wss_, true)) {
127 LOG(ERROR) << "Failed to read maps with page idle for Process " << pid_;
128 }
129
130 return maps_;
131}
132
Sandeep Patil82a48b12019-01-01 16:04:04 -0800133const std::vector<Vma>& ProcMemInfo::Smaps(const std::string& path) {
134 if (!maps_.empty()) {
135 return maps_;
136 }
137
138 auto collect_vmas = [&](const Vma& vma) { maps_.emplace_back(vma); };
139 if (path.empty() && !ForEachVma(collect_vmas)) {
140 LOG(ERROR) << "Failed to read smaps for Process " << pid_;
141 maps_.clear();
142 }
143
144 if (!path.empty() && !ForEachVmaFromFile(path, collect_vmas)) {
145 LOG(ERROR) << "Failed to read smaps from file " << path;
146 maps_.clear();
147 }
148
149 return maps_;
150}
151
Sandeep Patil54d87212018-08-29 17:10:47 -0700152const MemUsage& ProcMemInfo::Usage() {
153 if (get_wss_) {
Sandeep Patilf1291992018-11-19 15:25:18 -0800154 LOG(WARNING) << "Trying to read process memory usage for " << pid_
155 << " using invalid object";
Sandeep Patilc6497eb2018-11-20 09:31:36 -0800156 return usage_;
Sandeep Patil54d87212018-08-29 17:10:47 -0700157 }
Sandeep Patilc6497eb2018-11-20 09:31:36 -0800158
159 if (maps_.empty() && !ReadMaps(get_wss_)) {
160 LOG(ERROR) << "Failed to get memory usage for Process " << pid_;
161 }
162
Sandeep Patil54d87212018-08-29 17:10:47 -0700163 return usage_;
164}
165
166const MemUsage& ProcMemInfo::Wss() {
167 if (!get_wss_) {
Sandeep Patilf1291992018-11-19 15:25:18 -0800168 LOG(WARNING) << "Trying to read process working set for " << pid_
169 << " using invalid object";
Sandeep Patil56c414e2019-01-12 21:29:19 -0800170 return usage_;
Sandeep Patilc6497eb2018-11-20 09:31:36 -0800171 }
172
173 if (maps_.empty() && !ReadMaps(get_wss_)) {
174 LOG(ERROR) << "Failed to get working set for Process " << pid_;
Sandeep Patil54d87212018-08-29 17:10:47 -0700175 }
176
Sandeep Patil56c414e2019-01-12 21:29:19 -0800177 return usage_;
Sandeep Patil54d87212018-08-29 17:10:47 -0700178}
179
Sandeep Patil82a48b12019-01-01 16:04:04 -0800180bool ProcMemInfo::ForEachVma(const VmaCallback& callback) {
181 std::string path = ::android::base::StringPrintf("/proc/%d/smaps", pid_);
182 return ForEachVmaFromFile(path, callback);
183}
184
Sandeep Patildfd34be2019-01-13 17:39:08 -0800185bool ProcMemInfo::SmapsOrRollup(MemUsage* stats) const {
186 std::string path = ::android::base::StringPrintf(
187 "/proc/%d/%s", pid_, IsSmapsRollupSupported(pid_) ? "smaps_rollup" : "smaps");
Sandeep Patilfa2d8d52018-12-29 21:05:38 -0800188 return SmapsOrRollupFromFile(path, stats);
Sandeep Patildfd34be2019-01-13 17:39:08 -0800189}
Sandeep Patilfa2d8d52018-12-29 21:05:38 -0800190
Sandeep Patildfd34be2019-01-13 17:39:08 -0800191bool ProcMemInfo::SmapsOrRollupPss(uint64_t* pss) const {
192 std::string path = ::android::base::StringPrintf(
193 "/proc/%d/%s", pid_, IsSmapsRollupSupported(pid_) ? "smaps_rollup" : "smaps");
Sandeep Patil8871e7e2019-01-13 16:47:20 -0800194 return SmapsOrRollupPssFromFile(path, pss);
195}
196
Sandeep Patilc6497eb2018-11-20 09:31:36 -0800197const std::vector<uint16_t>& ProcMemInfo::SwapOffsets() {
198 if (get_wss_) {
199 LOG(WARNING) << "Trying to read process swap offsets for " << pid_
200 << " using invalid object";
201 return swap_offsets_;
202 }
203
204 if (maps_.empty() && !ReadMaps(get_wss_)) {
205 LOG(ERROR) << "Failed to get swap offsets for Process " << pid_;
206 }
207
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800208 return swap_offsets_;
209}
210
Sandeep Patilcbc8f122019-01-21 16:47:24 -0800211bool ProcMemInfo::PageMap(const Vma& vma, std::vector<uint64_t>* pagemap) {
212 pagemap->clear();
213 std::string pagemap_file = ::android::base::StringPrintf("/proc/%d/pagemap", pid_);
214 ::android::base::unique_fd pagemap_fd(
215 TEMP_FAILURE_RETRY(open(pagemap_file.c_str(), O_RDONLY | O_CLOEXEC)));
216 if (pagemap_fd < 0) {
217 PLOG(ERROR) << "Failed to open " << pagemap_file;
218 return false;
219 }
220
221 uint64_t nr_pages = (vma.end - vma.start) / getpagesize();
222 pagemap->reserve(nr_pages);
223
224 uint64_t idx = vma.start / getpagesize();
225 uint64_t last = idx + nr_pages;
226 uint64_t val;
227 for (; idx < last; idx++) {
228 if (pread64(pagemap_fd, &val, sizeof(uint64_t), idx * sizeof(uint64_t)) < 0) {
229 PLOG(ERROR) << "Failed to read page frames from page map for pid: " << pid_;
230 return false;
231 }
232 pagemap->emplace_back(val);
233 }
234
235 return true;
236}
237
Sandeep Patil061b7132019-01-19 21:11:01 -0800238bool ProcMemInfo::ReadMaps(bool get_wss, bool use_pageidle) {
Sandeep Patilc6497eb2018-11-20 09:31:36 -0800239 // Each object reads /proc/<pid>/maps only once. This is done to make sure programs that are
240 // running for the lifetime of the system can recycle the objects and don't have to
241 // unnecessarily retain and update this object in memory (which can get significantly large).
242 // E.g. A program that only needs to reset the working set will never all ->Maps(), ->Usage().
243 // E.g. A program that is monitoring smaps_rollup, may never call ->maps(), Usage(), so it
244 // doesn't make sense for us to parse and retain unnecessary memory accounting stats by default.
245 if (!maps_.empty()) return true;
246
Sandeep Patil54d87212018-08-29 17:10:47 -0700247 // parse and read /proc/<pid>/maps
248 std::string maps_file = ::android::base::StringPrintf("/proc/%d/maps", pid_);
249 if (!::android::procinfo::ReadMapFile(
Sandeep Patilf31c7092019-01-30 17:43:22 -0800250 maps_file, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t,
Sandeep Patil54d87212018-08-29 17:10:47 -0700251 const char* name) {
252 maps_.emplace_back(Vma(start, end, pgoff, flags, name));
253 })) {
254 LOG(ERROR) << "Failed to parse " << maps_file;
255 maps_.clear();
256 return false;
257 }
258
259 std::string pagemap_file = ::android::base::StringPrintf("/proc/%d/pagemap", pid_);
260 ::android::base::unique_fd pagemap_fd(
261 TEMP_FAILURE_RETRY(open(pagemap_file.c_str(), O_RDONLY | O_CLOEXEC)));
262 if (pagemap_fd < 0) {
263 PLOG(ERROR) << "Failed to open " << pagemap_file;
264 return false;
265 }
266
267 for (auto& vma : maps_) {
Sandeep Patil061b7132019-01-19 21:11:01 -0800268 if (!ReadVmaStats(pagemap_fd.get(), vma, get_wss, use_pageidle)) {
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800269 LOG(ERROR) << "Failed to read page map for vma " << vma.name << "[" << vma.start << "-"
270 << vma.end << "]";
Sandeep Patil54d87212018-08-29 17:10:47 -0700271 maps_.clear();
272 return false;
273 }
Sandeep Patil56c414e2019-01-12 21:29:19 -0800274 add_mem_usage(&usage_, vma.usage);
Sandeep Patil54d87212018-08-29 17:10:47 -0700275 }
276
277 return true;
278}
279
Sandeep Patil061b7132019-01-19 21:11:01 -0800280bool ProcMemInfo::ReadVmaStats(int pagemap_fd, Vma& vma, bool get_wss, bool use_pageidle) {
Sandeep Patil54d87212018-08-29 17:10:47 -0700281 PageAcct& pinfo = PageAcct::Instance();
Christopher Ferris7f8915c2019-06-25 17:30:56 -0700282 if (get_wss && use_pageidle && !pinfo.InitPageAcct(true)) {
283 LOG(ERROR) << "Failed to init idle page accounting";
Sandeep Patil54d87212018-08-29 17:10:47 -0700284 return false;
285 }
286
Christopher Ferris7f8915c2019-06-25 17:30:56 -0700287 uint64_t pagesz = getpagesize();
288 size_t num_pages = (vma.end - vma.start) / pagesz;
289 size_t first_page = vma.start / pagesz;
Sandeep Patil061b7132019-01-19 21:11:01 -0800290
Christopher Ferris7f8915c2019-06-25 17:30:56 -0700291 std::vector<uint64_t> page_cache;
292 size_t cur_page_cache_index = 0;
293 size_t num_in_page_cache = 0;
294 size_t num_leftover_pages = num_pages;
295 for (size_t cur_page = first_page; cur_page < first_page + num_pages; ++cur_page) {
Sandeep Patil54d87212018-08-29 17:10:47 -0700296 if (!get_wss) {
297 vma.usage.vss += pagesz;
298 }
Sandeep Patil54d87212018-08-29 17:10:47 -0700299
Christopher Ferris7f8915c2019-06-25 17:30:56 -0700300 // Cache page map data.
301 if (cur_page_cache_index == num_in_page_cache) {
302 static constexpr size_t kMaxPages = 2048;
303 num_leftover_pages -= num_in_page_cache;
304 if (num_leftover_pages > kMaxPages) {
305 num_in_page_cache = kMaxPages;
306 } else {
307 num_in_page_cache = num_leftover_pages;
308 }
309 page_cache.resize(num_in_page_cache);
310 size_t total_bytes = page_cache.size() * sizeof(uint64_t);
311 ssize_t bytes = pread64(pagemap_fd, page_cache.data(), total_bytes,
312 cur_page * sizeof(uint64_t));
313 if (bytes != total_bytes) {
314 if (bytes == -1) {
Christopher Ferrisd9433012019-06-28 11:24:30 -0700315 PLOG(ERROR) << "Failed to read page data at offset 0x" << std::hex
Christopher Ferris7f8915c2019-06-25 17:30:56 -0700316 << cur_page * sizeof(uint64_t);
317 } else {
Christopher Ferrisd9433012019-06-28 11:24:30 -0700318 LOG(ERROR) << "Failed to read page data at offset 0x" << std::hex
319 << cur_page * sizeof(uint64_t) << std::dec << " read bytes " << bytes
320 << " expected bytes " << total_bytes;
Christopher Ferris7f8915c2019-06-25 17:30:56 -0700321 }
322 return false;
323 }
324 cur_page_cache_index = 0;
325 }
326
327 uint64_t page_info = page_cache[cur_page_cache_index++];
328 if (!PAGE_PRESENT(page_info) && !PAGE_SWAPPED(page_info)) continue;
329
330 if (PAGE_SWAPPED(page_info)) {
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800331 vma.usage.swap += pagesz;
Christopher Ferris7f8915c2019-06-25 17:30:56 -0700332 swap_offsets_.emplace_back(PAGE_SWAP_OFFSET(page_info));
Sandeep Patil54d87212018-08-29 17:10:47 -0700333 continue;
334 }
335
Christopher Ferris7f8915c2019-06-25 17:30:56 -0700336 uint64_t page_frame = PAGE_PFN(page_info);
337 uint64_t cur_page_flags;
338 if (!pinfo.PageFlags(page_frame, &cur_page_flags)) {
Sandeep Patil54d87212018-08-29 17:10:47 -0700339 LOG(ERROR) << "Failed to get page flags for " << page_frame << " in process " << pid_;
Sandeep Patilc6497eb2018-11-20 09:31:36 -0800340 swap_offsets_.clear();
Sandeep Patil54d87212018-08-29 17:10:47 -0700341 return false;
342 }
343
Sandeep Patil549feab2018-11-19 11:38:40 -0800344 // skip unwanted pages from the count
Christopher Ferris7f8915c2019-06-25 17:30:56 -0700345 if ((cur_page_flags & pgflags_mask_) != pgflags_) continue;
Sandeep Patil549feab2018-11-19 11:38:40 -0800346
Christopher Ferris7f8915c2019-06-25 17:30:56 -0700347 uint64_t cur_page_counts;
348 if (!pinfo.PageMapCount(page_frame, &cur_page_counts)) {
Sandeep Patil54d87212018-08-29 17:10:47 -0700349 LOG(ERROR) << "Failed to get page count for " << page_frame << " in process " << pid_;
Sandeep Patilc6497eb2018-11-20 09:31:36 -0800350 swap_offsets_.clear();
Sandeep Patil54d87212018-08-29 17:10:47 -0700351 return false;
352 }
353
354 // Page was unmapped between the presence check at the beginning of the loop and here.
Christopher Ferris7f8915c2019-06-25 17:30:56 -0700355 if (cur_page_counts == 0) {
Sandeep Patil54d87212018-08-29 17:10:47 -0700356 continue;
357 }
358
Christopher Ferris7f8915c2019-06-25 17:30:56 -0700359 bool is_dirty = !!(cur_page_flags & (1 << KPF_DIRTY));
360 bool is_private = (cur_page_counts == 1);
Sandeep Patil54d87212018-08-29 17:10:47 -0700361 // Working set
362 if (get_wss) {
Sandeep Patil061b7132019-01-19 21:11:01 -0800363 bool is_referenced = use_pageidle ? (pinfo.IsPageIdle(page_frame) == 1)
Christopher Ferris7f8915c2019-06-25 17:30:56 -0700364 : !!(cur_page_flags & (1 << KPF_REFERENCED));
Sandeep Patil54d87212018-08-29 17:10:47 -0700365 if (!is_referenced) {
366 continue;
367 }
368 // This effectively makes vss = rss for the working set is requested.
369 // The libpagemap implementation returns vss > rss for
370 // working set, which doesn't make sense.
Sandeep Patil56c414e2019-01-12 21:29:19 -0800371 vma.usage.vss += pagesz;
372 }
373
374 vma.usage.rss += pagesz;
375 vma.usage.uss += is_private ? pagesz : 0;
Christopher Ferris7f8915c2019-06-25 17:30:56 -0700376 vma.usage.pss += pagesz / cur_page_counts;
Sandeep Patil56c414e2019-01-12 21:29:19 -0800377 if (is_private) {
378 vma.usage.private_dirty += is_dirty ? pagesz : 0;
379 vma.usage.private_clean += is_dirty ? 0 : pagesz;
Sandeep Patil54d87212018-08-29 17:10:47 -0700380 } else {
Sandeep Patil56c414e2019-01-12 21:29:19 -0800381 vma.usage.shared_dirty += is_dirty ? pagesz : 0;
382 vma.usage.shared_clean += is_dirty ? 0 : pagesz;
Sandeep Patil54d87212018-08-29 17:10:47 -0700383 }
384 }
Sandeep Patil54d87212018-08-29 17:10:47 -0700385 return true;
386}
387
Sandeep Patilfa2d8d52018-12-29 21:05:38 -0800388// Public APIs
Sandeep Patil82a48b12019-01-01 16:04:04 -0800389bool ForEachVmaFromFile(const std::string& path, const VmaCallback& callback) {
390 auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
391 if (fp == nullptr) {
392 return false;
393 }
394
395 char* line = nullptr;
396 bool parsing_vma = false;
397 ssize_t line_len;
Sandeep Patil002f02e2019-01-13 19:39:46 -0800398 size_t line_alloc = 0;
Sandeep Patil82a48b12019-01-01 16:04:04 -0800399 Vma vma;
Sandeep Patil002f02e2019-01-13 19:39:46 -0800400 while ((line_len = getline(&line, &line_alloc, fp.get())) > 0) {
Sandeep Patil82a48b12019-01-01 16:04:04 -0800401 // Make sure the line buffer terminates like a C string for ReadMapFile
402 line[line_len] = '\0';
403
404 if (parsing_vma) {
405 if (parse_smaps_field(line, &vma.usage)) {
406 // This was a stats field
407 continue;
408 }
409
410 // Done collecting stats, make the call back
411 callback(vma);
412 parsing_vma = false;
413 }
414
415 vma.clear();
416 // If it has, we are looking for the vma stats
417 // 00400000-00409000 r-xp 00000000 fc:00 426998 /usr/lib/gvfs/gvfsd-http
418 if (!::android::procinfo::ReadMapFileContent(
Sandeep Patilf31c7092019-01-30 17:43:22 -0800419 line, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, ino_t,
Sandeep Patil82a48b12019-01-01 16:04:04 -0800420 const char* name) {
421 vma.start = start;
422 vma.end = end;
423 vma.flags = flags;
424 vma.offset = pgoff;
425 vma.name = name;
426 })) {
427 LOG(ERROR) << "Failed to parse " << path;
428 return false;
429 }
430 parsing_vma = true;
431 }
432
433 // free getline() managed buffer
434 free(line);
435
436 if (parsing_vma) {
437 callback(vma);
438 }
439
440 return true;
441}
442
Sandeep Patildfd34be2019-01-13 17:39:08 -0800443enum smaps_rollup_support { UNTRIED, SUPPORTED, UNSUPPORTED };
444
445static std::atomic<smaps_rollup_support> g_rollup_support = UNTRIED;
446
447bool IsSmapsRollupSupported(pid_t pid) {
448 // Similar to OpenSmapsOrRollup checks from android_os_Debug.cpp, except
449 // the method only checks if rollup is supported and returns the status
450 // right away.
451 enum smaps_rollup_support rollup_support = g_rollup_support.load(std::memory_order_relaxed);
452 if (rollup_support != UNTRIED) {
453 return rollup_support == SUPPORTED;
454 }
455 std::string rollup_file = ::android::base::StringPrintf("/proc/%d/smaps_rollup", pid);
456 if (access(rollup_file.c_str(), F_OK | R_OK)) {
457 // No check for errno = ENOENT necessary here. The caller MUST fallback to
458 // using /proc/<pid>/smaps instead anyway.
459 g_rollup_support.store(UNSUPPORTED, std::memory_order_relaxed);
460 return false;
461 }
462
463 g_rollup_support.store(SUPPORTED, std::memory_order_relaxed);
464 LOG(INFO) << "Using smaps_rollup for pss collection";
465 return true;
466}
467
Sandeep Patilfa2d8d52018-12-29 21:05:38 -0800468bool SmapsOrRollupFromFile(const std::string& path, MemUsage* stats) {
469 auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
470 if (fp == nullptr) {
471 return false;
472 }
473
Sandeep Patil3fcb44b2019-01-14 17:44:34 -0800474 char* line = nullptr;
475 size_t line_alloc = 0;
Sandeep Patilfa2d8d52018-12-29 21:05:38 -0800476 stats->clear();
Sandeep Patil3fcb44b2019-01-14 17:44:34 -0800477 while (getline(&line, &line_alloc, fp.get()) > 0) {
Sandeep Patilfa2d8d52018-12-29 21:05:38 -0800478 switch (line[0]) {
479 case 'P':
480 if (strncmp(line, "Pss:", 4) == 0) {
481 char* c = line + 4;
482 stats->pss += strtoull(c, nullptr, 10);
483 } else if (strncmp(line, "Private_Clean:", 14) == 0) {
484 char* c = line + 14;
485 uint64_t prcl = strtoull(c, nullptr, 10);
486 stats->private_clean += prcl;
487 stats->uss += prcl;
488 } else if (strncmp(line, "Private_Dirty:", 14) == 0) {
489 char* c = line + 14;
490 uint64_t prdi = strtoull(c, nullptr, 10);
491 stats->private_dirty += prdi;
492 stats->uss += prdi;
493 }
494 break;
495 case 'R':
496 if (strncmp(line, "Rss:", 4) == 0) {
497 char* c = line + 4;
498 stats->rss += strtoull(c, nullptr, 10);
499 }
500 break;
501 case 'S':
502 if (strncmp(line, "SwapPss:", 8) == 0) {
503 char* c = line + 8;
504 stats->swap_pss += strtoull(c, nullptr, 10);
505 }
506 break;
507 }
508 }
509
Sandeep Patil3fcb44b2019-01-14 17:44:34 -0800510 // free getline() managed buffer
511 free(line);
Sandeep Patilfa2d8d52018-12-29 21:05:38 -0800512 return true;
513}
514
Sandeep Patil8871e7e2019-01-13 16:47:20 -0800515bool SmapsOrRollupPssFromFile(const std::string& path, uint64_t* pss) {
516 auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
517 if (fp == nullptr) {
518 return false;
519 }
520 *pss = 0;
Sandeep Patil3fcb44b2019-01-14 17:44:34 -0800521 char* line = nullptr;
522 size_t line_alloc = 0;
523 while (getline(&line, &line_alloc, fp.get()) > 0) {
Sandeep Patil8871e7e2019-01-13 16:47:20 -0800524 uint64_t v;
525 if (sscanf(line, "Pss: %" SCNu64 " kB", &v) == 1) {
526 *pss += v;
527 }
528 }
529
Sandeep Patil3fcb44b2019-01-14 17:44:34 -0800530 // free getline() managed buffer
531 free(line);
Sandeep Patil8871e7e2019-01-13 16:47:20 -0800532 return true;
533}
534
Sandeep Patil54d87212018-08-29 17:10:47 -0700535} // namespace meminfo
536} // namespace android