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 #pragma once 18 19 #include <sys/types.h> 20 21 #include <string> 22 #include <vector> 23 24 #include "meminfo.h" 25 26 namespace android { 27 namespace meminfo { 28 29 using VmaCallback = std::function<void(const Vma&)>; 30 31 class ProcMemInfo final { 32 // Per-process memory accounting 33 public: 34 // Reset the working set accounting of the process via /proc/<pid>/clear_refs 35 static bool ResetWorkingSet(pid_t pid); 36 37 ProcMemInfo(pid_t pid, bool get_wss = false, uint64_t pgflags = 0, uint64_t pgflags_mask = 0); 38 39 const std::vector<Vma>& Maps(); 40 const MemUsage& Usage(); 41 const MemUsage& Wss(); 42 43 // Same as Maps() except, only valid for reading working set using CONFIG_IDLE_PAGE_TRACKING 44 // support in kernel. If the kernel support doesn't exist, the function will return an empty 45 // vector. 46 const std::vector<Vma>& MapsWithPageIdle(); 47 48 // Same as Maps() except, do not read the usage stats for each map. 49 const std::vector<Vma>& MapsWithoutUsageStats(); 50 51 // If MapsWithoutUsageStats was called, this function will fill in 52 // usage stats for this single vma. 53 bool FillInVmaStats(Vma& vma); 54 55 // Collect all 'vma' or 'maps' from /proc/<pid>/smaps and store them in 'maps_'. Returns a 56 // constant reference to the vma vector after the collection is done. 57 // 58 // Each 'struct Vma' is *fully* populated by this method (unlike SmapsOrRollup). 59 const std::vector<Vma>& Smaps(const std::string& path = ""); 60 61 // This method reads /proc/<pid>/smaps and calls the callback() for each 62 // vma or map that it finds. The map is converted to 'struct Vma' object which is then 63 // passed to the callback. 64 // Returns 'false' if the file is malformed. 65 bool ForEachVma(const VmaCallback& callback); 66 67 // Used to parse either of /proc/<pid>/{smaps, smaps_rollup} and record the process's 68 // Pss and Private memory usage in 'stats'. In particular, the method only populates the fields 69 // of the MemUsage structure that are intended to be used by Android's periodic Pss collection. 70 // 71 // The method populates the following statistics in order to be fast an efficient. 72 // Pss 73 // Rss 74 // Uss 75 // private_clean 76 // private_dirty 77 // SwapPss 78 // All other fields of MemUsage are zeroed. 79 bool SmapsOrRollup(MemUsage* stats) const; 80 81 // Used to parse either of /proc/<pid>/{smaps, smaps_rollup} and record the process's 82 // Pss. 83 // Returns 'true' on success and the value of Pss in the out parameter. 84 bool SmapsOrRollupPss(uint64_t* pss) const; 85 86 const std::vector<uint64_t>& SwapOffsets(); 87 88 // Reads /proc/<pid>/pagemap for this process for each page within 89 // the 'vma' and stores that in 'pagemap'. It is assumed that the 'vma' 90 // is obtained by calling Maps() or 'ForEachVma' for the same object. No special checks 91 // are made to see if 'vma' is *valid*. 92 // Returns false if anything goes wrong, 'true' otherwise. 93 bool PageMap(const Vma& vma, std::vector<uint64_t>* pagemap); 94 95 ~ProcMemInfo() = default; 96 97 private: 98 bool ReadMaps(bool get_wss, bool use_pageidle = false, bool get_usage_stats = true); 99 bool ReadVmaStats(int pagemap_fd, Vma& vma, bool get_wss, bool use_pageidle); 100 101 pid_t pid_; 102 bool get_wss_; 103 uint64_t pgflags_; 104 uint64_t pgflags_mask_; 105 106 std::vector<Vma> maps_; 107 108 MemUsage usage_; 109 std::vector<uint64_t> swap_offsets_; 110 }; 111 112 // Makes callback for each 'vma' or 'map' found in file provided. The file is expected to be in the 113 // same format as /proc/<pid>/smaps. Returns 'false' if the file is malformed. 114 bool ForEachVmaFromFile(const std::string& path, const VmaCallback& callback); 115 116 // Returns if the kernel supports /proc/<pid>/smaps_rollup. Assumes that the 117 // calling process has access to the /proc/<pid>/smaps_rollup. 118 // Returns 'false' if the calling process has no permission to read the file if it exists 119 // of if the file doesn't exist. 120 bool IsSmapsRollupSupported(pid_t pid); 121 122 // Same as ProcMemInfo::SmapsOrRollup but reads the statistics directly 123 // from a file. The file MUST be in the same format as /proc/<pid>/smaps 124 // or /proc/<pid>/smaps_rollup 125 bool SmapsOrRollupFromFile(const std::string& path, MemUsage* stats); 126 127 // Same as ProcMemInfo::SmapsOrRollupPss but reads the statistics directly 128 // from a file and returns total Pss in kB. The file MUST be in the same format 129 // as /proc/<pid>/smaps or /proc/<pid>/smaps_rollup 130 bool SmapsOrRollupPssFromFile(const std::string& path, uint64_t* pss); 131 132 } // namespace meminfo 133 } // namespace android 134