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 <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 
24 #include <atomic>
25 #include <fstream>
26 #include <iostream>
27 #include <memory>
28 #include <string>
29 #include <utility>
30 #include <vector>
31 
32 #include <android-base/file.h>
33 #include <android-base/logging.h>
34 #include <android-base/stringprintf.h>
35 #include <android-base/unique_fd.h>
36 #include <procinfo/process_map.h>
37 
38 #include "meminfo_private.h"
39 
40 namespace android {
41 namespace meminfo {
42 
43 // List of VMA names that we don't want to process:
44 //   - On ARM32, [vectors] is a special VMA that is outside of pagemap range.
45 //   - On x86, [vsyscall] is a kernel memory that is outside of pagemap range.
46 static const std::vector<std::string> g_excluded_vmas = {
47     "[vectors]",
48 #ifdef __x86_64__
49     "[vsyscall]"
50 #endif
51 };
52 
add_mem_usage(MemUsage * to,const MemUsage & from)53 static void add_mem_usage(MemUsage* to, const MemUsage& from) {
54     to->vss += from.vss;
55     to->rss += from.rss;
56     to->pss += from.pss;
57     to->uss += from.uss;
58 
59     to->swap += from.swap;
60 
61     to->private_clean += from.private_clean;
62     to->private_dirty += from.private_dirty;
63 
64     to->shared_clean += from.shared_clean;
65     to->shared_dirty += from.shared_dirty;
66 }
67 
68 // Converts MemUsage stats from KB to B in case usage is expected in bytes.
convert_usage_kb_to_b(MemUsage & usage)69 static void convert_usage_kb_to_b(MemUsage& usage) {
70     // These stats are only populated if /proc/<pid>/smaps is read, so they are excluded:
71     // swap_pss, anon_huge_pages, shmem_pmdmapped, file_pmd_mapped, shared_hugetlb, private_hugetlb.
72     constexpr int conversion_factor = 1024;
73     usage.vss *= conversion_factor;
74     usage.rss *= conversion_factor;
75     usage.pss *= conversion_factor;
76     usage.uss *= conversion_factor;
77 
78     usage.swap *= conversion_factor;
79 
80     usage.private_clean *= conversion_factor;
81     usage.private_dirty *= conversion_factor;
82 
83     usage.shared_clean *= conversion_factor;
84     usage.shared_dirty *= conversion_factor;
85 
86     usage.thp *= conversion_factor;
87 }
88 
89 // Returns true if the line was valid smaps stats line false otherwise.
parse_smaps_field(const char * line,MemUsage * stats)90 static bool parse_smaps_field(const char* line, MemUsage* stats) {
91     const char *end = line;
92 
93     // https://lore.kernel.org/patchwork/patch/1088579/ introduced tabs. Handle this case as well.
94     while (*end && !isspace(*end)) end++;
95     if (*end && end > line && *(end - 1) == ':') {
96         const char* c = end;
97         while (isspace(*c)) c++;
98         switch (line[0]) {
99             case 'P':
100                 if (strncmp(line, "Pss:", 4) == 0) {
101                     stats->pss = strtoull(c, nullptr, 10);
102                 } else if (strncmp(line, "Private_Clean:", 14) == 0) {
103                     uint64_t prcl = strtoull(c, nullptr, 10);
104                     stats->private_clean = prcl;
105                     stats->uss += prcl;
106                 } else if (strncmp(line, "Private_Dirty:", 14) == 0) {
107                     uint64_t prdi = strtoull(c, nullptr, 10);
108                     stats->private_dirty = prdi;
109                     stats->uss += prdi;
110                 } else if (strncmp(line, "Private_Hugetlb:", 16) == 0) {
111                     stats->private_hugetlb = strtoull(c, nullptr, 10);
112                 }
113                 break;
114             case 'S':
115                 if (strncmp(line, "Size:", 5) == 0) {
116                     stats->vss = strtoull(c, nullptr, 10);
117                 } else if (strncmp(line, "Shared_Clean:", 13) == 0) {
118                     stats->shared_clean = strtoull(c, nullptr, 10);
119                 } else if (strncmp(line, "Shared_Dirty:", 13) == 0) {
120                     stats->shared_dirty = strtoull(c, nullptr, 10);
121                 } else if (strncmp(line, "Swap:", 5) == 0) {
122                     stats->swap = strtoull(c, nullptr, 10);
123                 } else if (strncmp(line, "SwapPss:", 8) == 0) {
124                     stats->swap_pss = strtoull(c, nullptr, 10);
125                 } else if (strncmp(line, "ShmemPmdMapped:", 15) == 0) {
126                     stats->shmem_pmd_mapped = strtoull(c, nullptr, 10);
127                 } else if (strncmp(line, "Shared_Hugetlb:", 15) == 0) {
128                     stats->shared_hugetlb = strtoull(c, nullptr, 10);
129                 }
130                 break;
131             case 'R':
132                 if (strncmp(line, "Rss:", 4) == 0) {
133                     stats->rss = strtoull(c, nullptr, 10);
134                 }
135                 break;
136             case 'A':
137                 if (strncmp(line, "AnonHugePages:", 14) == 0) {
138                     stats->anon_huge_pages = strtoull(c, nullptr, 10);
139                 }
140                 break;
141             case 'F':
142                 if (strncmp(line, "FilePmdMapped:", 14) == 0) {
143                     stats->file_pmd_mapped = strtoull(c, nullptr, 10);
144                 }
145                 break;
146             case 'L':
147                 if (strncmp(line, "Locked:", 7) == 0) {
148                     stats->locked = strtoull(c, nullptr, 10);
149                 }
150                 break;
151         }
152         return true;
153     }
154 
155     return false;
156 }
157 
ResetWorkingSet(pid_t pid)158 bool ProcMemInfo::ResetWorkingSet(pid_t pid) {
159     std::string clear_refs_path = ::android::base::StringPrintf("/proc/%d/clear_refs", pid);
160     if (!::android::base::WriteStringToFile("1\n", clear_refs_path)) {
161         PLOG(ERROR) << "Failed to write to " << clear_refs_path;
162         return false;
163     }
164 
165     return true;
166 }
167 
ProcMemInfo(pid_t pid,bool get_wss,uint64_t pgflags,uint64_t pgflags_mask)168 ProcMemInfo::ProcMemInfo(pid_t pid, bool get_wss, uint64_t pgflags, uint64_t pgflags_mask)
169     : pid_(pid), get_wss_(get_wss), pgflags_(pgflags), pgflags_mask_(pgflags_mask) {}
170 
Maps()171 const std::vector<Vma>& ProcMemInfo::Maps() {
172     if (maps_.empty() && !ReadMaps(get_wss_)) {
173         LOG(ERROR) << "Failed to read maps for Process " << pid_;
174     }
175 
176     return maps_;
177 }
178 
MapsWithPageIdle()179 const std::vector<Vma>& ProcMemInfo::MapsWithPageIdle() {
180     if (maps_.empty() && !ReadMaps(get_wss_, true)) {
181         LOG(ERROR) << "Failed to read maps with page idle for Process " << pid_;
182     }
183 
184     return maps_;
185 }
186 
MapsWithoutUsageStats()187 const std::vector<Vma>& ProcMemInfo::MapsWithoutUsageStats() {
188     if (maps_.empty() && !ReadMaps(get_wss_, false, false)) {
189         LOG(ERROR) << "Failed to read maps for Process " << pid_;
190     }
191 
192     return maps_;
193 }
194 
GetPagemapFd(pid_t pid)195 static int GetPagemapFd(pid_t pid) {
196     std::string pagemap_file = ::android::base::StringPrintf("/proc/%d/pagemap", pid);
197     int fd = TEMP_FAILURE_RETRY(open(pagemap_file.c_str(), O_RDONLY | O_CLOEXEC));
198     if (fd == -1) {
199         PLOG(ERROR) << "Failed to open " << pagemap_file;
200     }
201     return fd;
202 }
203 
Smaps(const std::string & path,bool collect_usage,bool collect_swap_offsets)204 const std::vector<Vma>& ProcMemInfo::Smaps(const std::string& path, bool collect_usage,
205                                            bool collect_swap_offsets) {
206     if (!maps_.empty()) {
207         return maps_;
208     }
209 
210     ::android::base::unique_fd pagemap_fd;
211     if (collect_swap_offsets) {
212         pagemap_fd = ::android::base::unique_fd(GetPagemapFd(pid_));
213         if (pagemap_fd == -1) {
214             LOG(ERROR) << "Failed to open pagemap for pid " << pid_ << " during Smaps()";
215             return maps_;
216         }
217     }
218 
219     auto collect_vmas = [&](Vma& vma) {
220         if (std::find(g_excluded_vmas.begin(), g_excluded_vmas.end(), vma.name) ==
221                 g_excluded_vmas.end()) {
222             maps_.emplace_back(vma);
223             if (collect_usage) {
224                 add_mem_usage(&usage_, vma.usage);
225             }
226             if (collect_swap_offsets &&
227                 !ReadVmaStats(pagemap_fd.get(), vma, false, false, false, false)) {
228                 LOG(ERROR) << "Failed to read page map for vma " << vma.name << "[" << vma.start
229                            << "-" << vma.end << "]";
230                 return false;
231             }
232         }
233         return true;
234     };
235 
236     if (path.empty() && !ForEachVma(collect_vmas)) {
237         LOG(ERROR) << "Failed to read smaps for Process " << pid_;
238         maps_.clear();
239     }
240 
241     if (!path.empty() && !ForEachVmaFromFile(path, collect_vmas)) {
242         LOG(ERROR) << "Failed to read smaps from file " << path;
243         maps_.clear();
244     }
245 
246     return maps_;
247 }
248 
Usage()249 const MemUsage& ProcMemInfo::Usage() {
250     if (get_wss_) {
251         LOG(WARNING) << "Trying to read process memory usage for " << pid_
252                      << " using invalid object";
253         return usage_;
254     }
255 
256     if (maps_.empty() && !ReadMaps(get_wss_)) {
257         LOG(ERROR) << "Failed to get memory usage for Process " << pid_;
258     }
259 
260     return usage_;
261 }
262 
Wss()263 const MemUsage& ProcMemInfo::Wss() {
264     if (!get_wss_) {
265         LOG(WARNING) << "Trying to read process working set for " << pid_
266                      << " using invalid object";
267         return usage_;
268     }
269 
270     if (maps_.empty() && !ReadMaps(get_wss_)) {
271         LOG(ERROR) << "Failed to get working set for Process " << pid_;
272     }
273 
274     return usage_;
275 }
276 
ForEachVma(const VmaCallback & callback,bool use_smaps)277 bool ProcMemInfo::ForEachVma(const VmaCallback& callback, bool use_smaps) {
278     std::string path =
279             ::android::base::StringPrintf("/proc/%d/%s", pid_, use_smaps ? "smaps" : "maps");
280     return ForEachVmaFromFile(path, callback, use_smaps);
281 }
282 
ForEachExistingVma(const VmaCallback & callback)283 bool ProcMemInfo::ForEachExistingVma(const VmaCallback& callback) {
284     if (maps_.empty()) {
285         return false;
286     }
287     for (auto& vma : maps_) {
288         if (!callback(vma)) {
289             return false;
290         }
291     }
292     return true;
293 }
294 
ForEachVmaFromMaps(const VmaCallback & callback)295 bool ProcMemInfo::ForEachVmaFromMaps(const VmaCallback& callback) {
296     Vma vma;
297     auto vmaCollect = [&callback,&vma](const uint64_t start, uint64_t end, uint16_t flags,
298                             uint64_t pgoff, ino_t inode, const char* name, bool shared) {
299         vma.start = start;
300         vma.end = end;
301         vma.flags = flags;
302         vma.offset = pgoff;
303         vma.name = name;
304         vma.inode = inode;
305         vma.is_shared = shared;
306         callback(vma);
307     };
308 
309     bool success = ::android::procinfo::ReadProcessMaps(pid_, vmaCollect);
310 
311     return success;
312 }
313 
ForEachVmaFromMaps(const VmaCallback & callback,std::string & mapsBuffer)314 bool ProcMemInfo::ForEachVmaFromMaps(const VmaCallback& callback, std::string& mapsBuffer) {
315     Vma vma;
316     vma.name.reserve(256);
317     auto vmaCollect = [&callback,&vma](const uint64_t start, uint64_t end, uint16_t flags,
318                             uint64_t pgoff, ino_t inode, const char* name, bool shared) {
319         vma.start = start;
320         vma.end = end;
321         vma.flags = flags;
322         vma.offset = pgoff;
323         vma.name = name;
324         vma.inode = inode;
325         vma.is_shared = shared;
326         callback(vma);
327     };
328 
329     bool success = ::android::procinfo::ReadProcessMaps(pid_, vmaCollect, mapsBuffer);
330 
331     return success;
332 }
333 
SmapsOrRollup(MemUsage * stats) const334 bool ProcMemInfo::SmapsOrRollup(MemUsage* stats) const {
335     std::string path = ::android::base::StringPrintf(
336             "/proc/%d/%s", pid_, IsSmapsRollupSupported() ? "smaps_rollup" : "smaps");
337     return SmapsOrRollupFromFile(path, stats);
338 }
339 
SmapsOrRollupPss(uint64_t * pss) const340 bool ProcMemInfo::SmapsOrRollupPss(uint64_t* pss) const {
341     std::string path = ::android::base::StringPrintf(
342             "/proc/%d/%s", pid_, IsSmapsRollupSupported() ? "smaps_rollup" : "smaps");
343     return SmapsOrRollupPssFromFile(path, pss);
344 }
345 
StatusVmRSS(uint64_t * rss) const346 bool ProcMemInfo::StatusVmRSS(uint64_t* rss) const {
347     std::string path = ::android::base::StringPrintf("/proc/%d/status", pid_);
348     return StatusVmRSSFromFile(path, rss);
349 }
350 
SwapOffsets()351 const std::vector<uint64_t>& ProcMemInfo::SwapOffsets() {
352     if (get_wss_) {
353         LOG(WARNING) << "Trying to read process swap offsets for " << pid_
354                      << " using invalid object";
355         return swap_offsets_;
356     }
357 
358     if (maps_.empty() && !ReadMaps(get_wss_, false, true, false)) {
359         LOG(ERROR) << "Failed to get swap offsets for Process " << pid_;
360     }
361 
362     return swap_offsets_;
363 }
364 
PageMap(const Vma & vma,std::vector<uint64_t> * pagemap)365 bool ProcMemInfo::PageMap(const Vma& vma, std::vector<uint64_t>* pagemap) {
366     pagemap->clear();
367     std::string pagemap_file = ::android::base::StringPrintf("/proc/%d/pagemap", pid_);
368     ::android::base::unique_fd pagemap_fd(
369             TEMP_FAILURE_RETRY(open(pagemap_file.c_str(), O_RDONLY | O_CLOEXEC)));
370     if (pagemap_fd == -1) {
371         PLOG(ERROR) << "Failed to open " << pagemap_file;
372         return false;
373     }
374 
375     uint64_t nr_pages = (vma.end - vma.start) / getpagesize();
376     pagemap->resize(nr_pages);
377 
378     size_t bytes_to_read = sizeof(uint64_t) * nr_pages;
379     off64_t start_addr = (vma.start / getpagesize()) * sizeof(uint64_t);
380     ssize_t bytes_read = pread64(pagemap_fd, pagemap->data(), bytes_to_read, start_addr);
381     if (bytes_read == -1) {
382         PLOG(ERROR) << "Failed to read page frames from page map for pid: " << pid_;
383         return false;
384     } else if (static_cast<size_t>(bytes_read) != bytes_to_read) {
385         LOG(ERROR) << "Failed to read page frames from page map for pid: " << pid_
386                    << ": read bytes " << bytes_read << " expected bytes " << bytes_to_read;
387         return false;
388     }
389 
390     return true;
391 }
392 
ReadMaps(bool get_wss,bool use_pageidle,bool get_usage_stats,bool update_mem_usage)393 bool ProcMemInfo::ReadMaps(bool get_wss, bool use_pageidle, bool get_usage_stats,
394                            bool update_mem_usage) {
395     // Each object reads /proc/<pid>/maps only once. This is done to make sure programs that are
396     // running for the lifetime of the system can recycle the objects and don't have to
397     // unnecessarily retain and update this object in memory (which can get significantly large).
398     // E.g. A program that only needs to reset the working set will never all ->Maps(), ->Usage().
399     // E.g. A program that is monitoring smaps_rollup, may never call ->maps(), Usage(), so it
400     // doesn't make sense for us to parse and retain unnecessary memory accounting stats by default.
401     if (!maps_.empty()) return true;
402 
403     // parse and read /proc/<pid>/maps
404     std::string maps_file = ::android::base::StringPrintf("/proc/%d/maps", pid_);
405     if (!::android::procinfo::ReadMapFile(
406                 maps_file, [&](const android::procinfo::MapInfo& mapinfo) {
407                     if (std::find(g_excluded_vmas.begin(), g_excluded_vmas.end(), mapinfo.name) ==
408                             g_excluded_vmas.end()) {
409                       maps_.emplace_back(Vma(mapinfo.start, mapinfo.end,
410                                              mapinfo.pgoff, mapinfo.flags,
411                                              mapinfo.name,
412                                              mapinfo.inode, mapinfo.shared));
413                     }
414                 })) {
415         LOG(ERROR) << "Failed to parse " << maps_file;
416         maps_.clear();
417         return false;
418     }
419 
420     if (!get_usage_stats) {
421         return true;
422     }
423 
424     if (!GetUsageStats(get_wss, use_pageidle, update_mem_usage)) {
425         maps_.clear();
426         return false;
427     }
428     return true;
429 }
430 
GetUsageStats(bool get_wss,bool use_pageidle,bool update_mem_usage)431 bool ProcMemInfo::GetUsageStats(bool get_wss, bool use_pageidle, bool update_mem_usage) {
432     ::android::base::unique_fd pagemap_fd(GetPagemapFd(pid_));
433     if (pagemap_fd == -1) {
434         return false;
435     }
436 
437     for (auto& vma : maps_) {
438         if (!ReadVmaStats(pagemap_fd.get(), vma, get_wss, use_pageidle, update_mem_usage, true)) {
439             LOG(ERROR) << "Failed to read page map for vma " << vma.name << "[" << vma.start << "-"
440                        << vma.end << "]";
441             return false;
442         }
443         add_mem_usage(&usage_, vma.usage);
444     }
445 
446     return true;
447 }
448 
FillInVmaStats(Vma & vma,bool use_kb)449 bool ProcMemInfo::FillInVmaStats(Vma& vma, bool use_kb) {
450     ::android::base::unique_fd pagemap_fd(GetPagemapFd(pid_));
451     if (pagemap_fd == -1) {
452         return false;
453     }
454 
455     if (!ReadVmaStats(pagemap_fd.get(), vma, get_wss_, false, true, true)) {
456         LOG(ERROR) << "Failed to read page map for vma " << vma.name << "[" << vma.start << "-"
457                    << vma.end << "]";
458         return false;
459     }
460     if (!use_kb) {
461         convert_usage_kb_to_b(vma.usage);
462     }
463     return true;
464 }
465 
ReadVmaStats(int pagemap_fd,Vma & vma,bool get_wss,bool use_pageidle,bool update_mem_usage,bool update_swap_usage)466 bool ProcMemInfo::ReadVmaStats(int pagemap_fd, Vma& vma, bool get_wss, bool use_pageidle,
467                                bool update_mem_usage, bool update_swap_usage) {
468     PageAcct& pinfo = PageAcct::Instance();
469     if (get_wss && use_pageidle && !pinfo.InitPageAcct(true)) {
470         LOG(ERROR) << "Failed to init idle page accounting";
471         return false;
472     }
473 
474     uint64_t pagesz_kb = getpagesize() / 1024;
475     size_t num_pages = (vma.end - vma.start) / getpagesize();
476     size_t first_page = vma.start / getpagesize();
477 
478     std::vector<uint64_t> page_cache;
479     size_t cur_page_cache_index = 0;
480     size_t num_in_page_cache = 0;
481     size_t num_leftover_pages = num_pages;
482     for (size_t cur_page = first_page; cur_page < first_page + num_pages; ++cur_page) {
483         // Cache page map data.
484         if (cur_page_cache_index == num_in_page_cache) {
485             static constexpr size_t kMaxPages = 2048;
486             num_leftover_pages -= num_in_page_cache;
487             if (num_leftover_pages > kMaxPages) {
488                 num_in_page_cache = kMaxPages;
489             } else {
490                 num_in_page_cache = num_leftover_pages;
491             }
492             page_cache.resize(num_in_page_cache);
493             size_t total_bytes = page_cache.size() * sizeof(uint64_t);
494             ssize_t bytes = pread64(pagemap_fd, page_cache.data(), total_bytes,
495                                     cur_page * sizeof(uint64_t));
496             if (bytes != total_bytes) {
497                 if (bytes == -1) {
498                     PLOG(ERROR) << "Failed to read page data at offset 0x" << std::hex
499                                 << cur_page * sizeof(uint64_t);
500                 } else {
501                     LOG(ERROR) << "Failed to read page data at offset 0x" << std::hex
502                                << cur_page * sizeof(uint64_t) << std::dec << " read bytes " << bytes
503                                << " expected bytes " << total_bytes;
504                 }
505                 return false;
506             }
507             cur_page_cache_index = 0;
508         }
509 
510         uint64_t page_info = page_cache[cur_page_cache_index++];
511         if (!PAGE_PRESENT(page_info) && !PAGE_SWAPPED(page_info)) continue;
512 
513         if (PAGE_SWAPPED(page_info)) {
514             if (update_swap_usage) {
515                 vma.usage.swap += pagesz_kb;
516             }
517             swap_offsets_.emplace_back(PAGE_SWAP_OFFSET(page_info));
518             continue;
519         }
520 
521         if (!update_mem_usage) continue;
522 
523         uint64_t page_frame = PAGE_PFN(page_info);
524         uint64_t cur_page_flags;
525         if (!pinfo.PageFlags(page_frame, &cur_page_flags)) {
526             LOG(ERROR) << "Failed to get page flags for " << page_frame << " in process " << pid_;
527             swap_offsets_.clear();
528             return false;
529         }
530 
531         if (KPAGEFLAG_THP(cur_page_flags)) {
532             vma.usage.thp += pagesz_kb;
533         }
534 
535         // skip unwanted pages from the count
536         if ((cur_page_flags & pgflags_mask_) != pgflags_) continue;
537 
538         uint64_t cur_page_counts;
539         if (!pinfo.PageMapCount(page_frame, &cur_page_counts)) {
540             LOG(ERROR) << "Failed to get page count for " << page_frame << " in process " << pid_;
541             swap_offsets_.clear();
542             return false;
543         }
544 
545         // Page was unmapped between the presence check at the beginning of the loop and here.
546         if (cur_page_counts == 0) {
547             continue;
548         }
549 
550         bool is_dirty = !!(cur_page_flags & (1 << KPF_DIRTY));
551         bool is_private = (cur_page_counts == 1);
552         // Working set
553         if (get_wss) {
554             bool is_referenced = use_pageidle ? (pinfo.IsPageIdle(page_frame) == 1)
555                                               : !!(cur_page_flags & (1 << KPF_REFERENCED));
556             if (!is_referenced) {
557                 continue;
558             }
559             // This effectively makes vss = rss for the working set is requested.
560             // The libpagemap implementation returns vss > rss for
561             // working set, which doesn't make sense.
562             vma.usage.vss += pagesz_kb;
563         }
564 
565         vma.usage.rss += pagesz_kb;
566         vma.usage.uss += is_private ? pagesz_kb : 0;
567         vma.usage.pss += pagesz_kb / cur_page_counts;
568         if (is_private) {
569             vma.usage.private_dirty += is_dirty ? pagesz_kb : 0;
570             vma.usage.private_clean += is_dirty ? 0 : pagesz_kb;
571         } else {
572             vma.usage.shared_dirty += is_dirty ? pagesz_kb : 0;
573             vma.usage.shared_clean += is_dirty ? 0 : pagesz_kb;
574         }
575     }
576     if (!get_wss) {
577         vma.usage.vss += pagesz_kb * num_pages;
578     }
579     return true;
580 }
581 
582 // Public APIs
ForEachVmaFromFile(const std::string & path,const VmaCallback & callback,bool read_smaps_fields)583 bool ForEachVmaFromFile(const std::string& path, const VmaCallback& callback,
584                         bool read_smaps_fields) {
585     auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
586     if (fp == nullptr) {
587         return false;
588     }
589 
590     char* line = nullptr;
591     bool parsing_vma = false;
592     ssize_t line_len;
593     size_t line_alloc = 0;
594     Vma vma;
595     while ((line_len = getline(&line, &line_alloc, fp.get())) > 0) {
596         // Make sure the line buffer terminates like a C string for ReadMapFile
597         line[line_len] = '\0';
598 
599         if (parsing_vma) {
600             if (parse_smaps_field(line, &vma.usage)) {
601                 // This was a stats field
602                 continue;
603             }
604 
605             // Done collecting stats, make the call back
606             if (!callback(vma)) {
607                 free(line);
608                 return false;
609             }
610             parsing_vma = false;
611         }
612 
613         vma.clear();
614         // If it has, we are looking for the vma stats
615         // 00400000-00409000 r-xp 00000000 fc:00 426998  /usr/lib/gvfs/gvfsd-http
616         if (!::android::procinfo::ReadMapFileContent(
617                     line, [&](const android::procinfo::MapInfo& mapinfo) {
618                         vma.start = mapinfo.start;
619                         vma.end = mapinfo.end;
620                         vma.flags = mapinfo.flags;
621                         vma.offset = mapinfo.pgoff;
622                         vma.name = mapinfo.name;
623                         vma.inode = mapinfo.inode;
624                         vma.is_shared = mapinfo.shared;
625                     })) {
626             // free getline() managed buffer
627             free(line);
628             LOG(ERROR) << "Failed to parse " << path;
629             return false;
630         }
631         if (read_smaps_fields) {
632             parsing_vma = true;
633         } else {
634             // Done collecting stats, make the call back
635             if (!callback(vma)) {
636                 free(line);
637                 return false;
638             }
639         }
640     }
641 
642     // free getline() managed buffer
643     free(line);
644 
645     if (parsing_vma) {
646         if (!callback(vma)) {
647             return false;
648         }
649     }
650 
651     return true;
652 }
653 
654 enum smaps_rollup_support { UNTRIED, SUPPORTED, UNSUPPORTED };
655 
656 static std::atomic<smaps_rollup_support> g_rollup_support = UNTRIED;
657 
IsSmapsRollupSupported()658 bool IsSmapsRollupSupported() {
659     // Similar to OpenSmapsOrRollup checks from android_os_Debug.cpp, except
660     // the method only checks if rollup is supported and returns the status
661     // right away.
662     enum smaps_rollup_support rollup_support = g_rollup_support.load(std::memory_order_relaxed);
663     if (rollup_support != UNTRIED) {
664         return rollup_support == SUPPORTED;
665     }
666 
667     // Check the calling process for smaps_rollup since it is guaranteed to be alive
668     if (access("/proc/self/smaps_rollup", F_OK | R_OK)) {
669         g_rollup_support.store(UNSUPPORTED, std::memory_order_relaxed);
670         return false;
671     }
672 
673     g_rollup_support.store(SUPPORTED, std::memory_order_relaxed);
674     LOG(INFO) << "Using smaps_rollup for pss collection";
675     return true;
676 }
677 
SmapsOrRollupFromFile(const std::string & path,MemUsage * stats)678 bool SmapsOrRollupFromFile(const std::string& path, MemUsage* stats) {
679     auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
680     if (fp == nullptr) {
681         return false;
682     }
683 
684     char* line = nullptr;
685     size_t line_alloc = 0;
686     stats->clear();
687     while (getline(&line, &line_alloc, fp.get()) > 0) {
688         switch (line[0]) {
689             case 'P':
690                 if (strncmp(line, "Pss:", 4) == 0) {
691                     char* c = line + 4;
692                     stats->pss += strtoull(c, nullptr, 10);
693                 } else if (strncmp(line, "Private_Clean:", 14) == 0) {
694                     char* c = line + 14;
695                     uint64_t prcl = strtoull(c, nullptr, 10);
696                     stats->private_clean += prcl;
697                     stats->uss += prcl;
698                 } else if (strncmp(line, "Private_Dirty:", 14) == 0) {
699                     char* c = line + 14;
700                     uint64_t prdi = strtoull(c, nullptr, 10);
701                     stats->private_dirty += prdi;
702                     stats->uss += prdi;
703                 }
704                 break;
705             case 'R':
706                 if (strncmp(line, "Rss:", 4) == 0) {
707                     char* c = line + 4;
708                     stats->rss += strtoull(c, nullptr, 10);
709                 }
710                 break;
711             case 'S':
712                 if (strncmp(line, "SwapPss:", 8) == 0) {
713                     char* c = line + 8;
714                     stats->swap_pss += strtoull(c, nullptr, 10);
715                 }
716                 break;
717         }
718     }
719 
720     // free getline() managed buffer
721     free(line);
722     return true;
723 }
724 
SmapsOrRollupPssFromFile(const std::string & path,uint64_t * pss)725 bool SmapsOrRollupPssFromFile(const std::string& path, uint64_t* pss) {
726     auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
727     if (fp == nullptr) {
728         return false;
729     }
730     *pss = 0;
731     char* line = nullptr;
732     size_t line_alloc = 0;
733     while (getline(&line, &line_alloc, fp.get()) > 0) {
734         uint64_t v;
735         if (sscanf(line, "Pss: %" SCNu64 " kB", &v) == 1) {
736             *pss += v;
737         }
738     }
739 
740     // free getline() managed buffer
741     free(line);
742     return true;
743 }
744 
StatusVmRSSFromFile(const std::string & path,uint64_t * rss)745 bool StatusVmRSSFromFile(const std::string& path, uint64_t* rss) {
746     auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
747     if (fp == nullptr) {
748         return false;
749     }
750 
751     // We use this bool because -1 as an "invalid" value for RSS will wrap
752     // around to a positive number.
753     bool success = false;
754 
755     *rss = 0;
756     char* line = nullptr;
757     size_t line_alloc = 0;
758     while (getline(&line, &line_alloc, fp.get()) > 0) {
759         uint64_t v;
760         if (sscanf(line, "VmRSS: %" SCNu64 " kB", &v) == 1) {
761             *rss = v;
762             success = true;
763             // Break because there is only one VmRSS field in status.
764             break;
765         }
766     }
767 
768     // free getline() managed buffer
769     free(line);
770     return success;
771 }
772 
GetFormat(std::string_view arg)773 Format GetFormat(std::string_view arg) {
774     if (arg == "json") {
775         return Format::JSON;
776     }
777     if (arg == "csv") {
778         return Format::CSV;
779     }
780     if (arg == "raw") {
781         return Format::RAW;
782     }
783     return Format::INVALID;
784 }
785 
EscapeCsvString(const std::string & raw)786 std::string EscapeCsvString(const std::string& raw) {
787     std::string ret;
788     for (auto it = raw.cbegin(); it != raw.cend(); it++) {
789         switch (*it) {
790             case '"':
791                 ret += "\"";
792                 break;
793             default:
794                 ret += *it;
795                 break;
796         }
797     }
798     return '"' + ret + '"';
799 }
800 
EscapeJsonString(const std::string & raw)801 std::string EscapeJsonString(const std::string& raw) {
802     std::string ret;
803     for (auto it = raw.cbegin(); it != raw.cend(); it++) {
804         switch (*it) {
805             case '\\':
806                 ret += "\\\\";
807                 break;
808             case '"':
809                 ret += "\\\"";
810                 break;
811             case '/':
812                 ret += "\\/";
813                 break;
814             case '\b':
815                 ret += "\\b";
816                 break;
817             case '\f':
818                 ret += "\\f";
819                 break;
820             case '\n':
821                 ret += "\\n";
822                 break;
823             case '\r':
824                 ret += "\\r";
825                 break;
826             case '\t':
827                 ret += "\\t";
828                 break;
829             default:
830                 ret += *it;
831                 break;
832         }
833     }
834     return '"' + ret + '"';
835 }
836 
837 }  // namespace meminfo
838 }  // namespace android
839