blob: 8867e147f0ed9dfdd0e644060fb3b90334a3f81f [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
24#include <fstream>
25#include <iostream>
26#include <memory>
27#include <string>
28#include <utility>
29
30#include <android-base/file.h>
31#include <android-base/logging.h>
32#include <android-base/stringprintf.h>
33#include <android-base/unique_fd.h>
34#include <procinfo/process_map.h>
35
36#include "meminfo_private.h"
37
38namespace android {
39namespace meminfo {
40
41static void add_mem_usage(MemUsage* to, const MemUsage& from) {
42 to->vss += from.vss;
43 to->rss += from.rss;
44 to->pss += from.pss;
45 to->uss += from.uss;
46
Sandeep Patil2259fdf2018-11-09 16:42:45 -080047 to->swap += from.swap;
48
Sandeep Patil54d87212018-08-29 17:10:47 -070049 to->private_clean += from.private_clean;
50 to->private_dirty += from.private_dirty;
51
52 to->shared_clean += from.shared_clean;
53 to->shared_dirty += from.shared_dirty;
54}
55
Sandeep Patilf1291992018-11-19 15:25:18 -080056bool ProcMemInfo::ResetWorkingSet(pid_t pid) {
57 std::string clear_refs_path = ::android::base::StringPrintf("/proc/%d/clear_refs", pid);
58 if (!::android::base::WriteStringToFile("1\n", clear_refs_path)) {
59 PLOG(ERROR) << "Failed to write to " << clear_refs_path;
60 return false;
61 }
62
63 return true;
64}
65
Sandeep Patil549feab2018-11-19 11:38:40 -080066ProcMemInfo::ProcMemInfo(pid_t pid, bool get_wss, uint64_t pgflags, uint64_t pgflags_mask)
Sandeep Patilc6497eb2018-11-20 09:31:36 -080067 : pid_(pid), get_wss_(get_wss), pgflags_(pgflags), pgflags_mask_(pgflags_mask) {}
Sandeep Patil54d87212018-08-29 17:10:47 -070068
69const std::vector<Vma>& ProcMemInfo::Maps() {
Sandeep Patilc6497eb2018-11-20 09:31:36 -080070 if (maps_.empty() && !ReadMaps(get_wss_)) {
71 LOG(ERROR) << "Failed to read maps for Process " << pid_;
72 }
73
Sandeep Patil54d87212018-08-29 17:10:47 -070074 return maps_;
75}
76
77const MemUsage& ProcMemInfo::Usage() {
78 if (get_wss_) {
Sandeep Patilf1291992018-11-19 15:25:18 -080079 LOG(WARNING) << "Trying to read process memory usage for " << pid_
80 << " using invalid object";
Sandeep Patilc6497eb2018-11-20 09:31:36 -080081 return usage_;
Sandeep Patil54d87212018-08-29 17:10:47 -070082 }
Sandeep Patilc6497eb2018-11-20 09:31:36 -080083
84 if (maps_.empty() && !ReadMaps(get_wss_)) {
85 LOG(ERROR) << "Failed to get memory usage for Process " << pid_;
86 }
87
Sandeep Patil54d87212018-08-29 17:10:47 -070088 return usage_;
89}
90
91const MemUsage& ProcMemInfo::Wss() {
92 if (!get_wss_) {
Sandeep Patilf1291992018-11-19 15:25:18 -080093 LOG(WARNING) << "Trying to read process working set for " << pid_
94 << " using invalid object";
Sandeep Patilc6497eb2018-11-20 09:31:36 -080095 return wss_;
96 }
97
98 if (maps_.empty() && !ReadMaps(get_wss_)) {
99 LOG(ERROR) << "Failed to get working set for Process " << pid_;
Sandeep Patil54d87212018-08-29 17:10:47 -0700100 }
101
102 return wss_;
103}
104
Sandeep Patilc6497eb2018-11-20 09:31:36 -0800105const std::vector<uint16_t>& ProcMemInfo::SwapOffsets() {
106 if (get_wss_) {
107 LOG(WARNING) << "Trying to read process swap offsets for " << pid_
108 << " using invalid object";
109 return swap_offsets_;
110 }
111
112 if (maps_.empty() && !ReadMaps(get_wss_)) {
113 LOG(ERROR) << "Failed to get swap offsets for Process " << pid_;
114 }
115
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800116 return swap_offsets_;
117}
118
Sandeep Patil54d87212018-08-29 17:10:47 -0700119bool ProcMemInfo::ReadMaps(bool get_wss) {
Sandeep Patilc6497eb2018-11-20 09:31:36 -0800120 // Each object reads /proc/<pid>/maps only once. This is done to make sure programs that are
121 // running for the lifetime of the system can recycle the objects and don't have to
122 // unnecessarily retain and update this object in memory (which can get significantly large).
123 // E.g. A program that only needs to reset the working set will never all ->Maps(), ->Usage().
124 // E.g. A program that is monitoring smaps_rollup, may never call ->maps(), Usage(), so it
125 // doesn't make sense for us to parse and retain unnecessary memory accounting stats by default.
126 if (!maps_.empty()) return true;
127
Sandeep Patil54d87212018-08-29 17:10:47 -0700128 // parse and read /proc/<pid>/maps
129 std::string maps_file = ::android::base::StringPrintf("/proc/%d/maps", pid_);
130 if (!::android::procinfo::ReadMapFile(
131 maps_file, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff,
132 const char* name) {
133 maps_.emplace_back(Vma(start, end, pgoff, flags, name));
134 })) {
135 LOG(ERROR) << "Failed to parse " << maps_file;
136 maps_.clear();
137 return false;
138 }
139
140 std::string pagemap_file = ::android::base::StringPrintf("/proc/%d/pagemap", pid_);
141 ::android::base::unique_fd pagemap_fd(
142 TEMP_FAILURE_RETRY(open(pagemap_file.c_str(), O_RDONLY | O_CLOEXEC)));
143 if (pagemap_fd < 0) {
144 PLOG(ERROR) << "Failed to open " << pagemap_file;
145 return false;
146 }
147
148 for (auto& vma : maps_) {
149 if (!ReadVmaStats(pagemap_fd.get(), vma, get_wss)) {
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800150 LOG(ERROR) << "Failed to read page map for vma " << vma.name << "[" << vma.start << "-"
151 << vma.end << "]";
Sandeep Patil54d87212018-08-29 17:10:47 -0700152 maps_.clear();
153 return false;
154 }
155 if (get_wss) {
156 add_mem_usage(&wss_, vma.wss);
157 } else {
158 add_mem_usage(&usage_, vma.usage);
159 }
160 }
161
162 return true;
163}
164
165bool ProcMemInfo::ReadVmaStats(int pagemap_fd, Vma& vma, bool get_wss) {
166 PageAcct& pinfo = PageAcct::Instance();
167 uint64_t pagesz = getpagesize();
168 uint64_t num_pages = (vma.end - vma.start) / pagesz;
169
170 std::unique_ptr<uint64_t[]> pg_frames(new uint64_t[num_pages]);
171 uint64_t first = vma.start / pagesz;
172 if (pread64(pagemap_fd, pg_frames.get(), num_pages * sizeof(uint64_t),
173 first * sizeof(uint64_t)) < 0) {
174 PLOG(ERROR) << "Failed to read page frames from page map for pid: " << pid_;
175 return false;
176 }
177
178 std::unique_ptr<uint64_t[]> pg_flags(new uint64_t[num_pages]);
179 std::unique_ptr<uint64_t[]> pg_counts(new uint64_t[num_pages]);
180 for (uint64_t i = 0; i < num_pages; ++i) {
181 if (!get_wss) {
182 vma.usage.vss += pagesz;
183 }
184 uint64_t p = pg_frames[i];
185 if (!PAGE_PRESENT(p) && !PAGE_SWAPPED(p)) continue;
186
187 if (PAGE_SWAPPED(p)) {
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800188 vma.usage.swap += pagesz;
189 swap_offsets_.emplace_back(PAGE_SWAP_OFFSET(p));
Sandeep Patil54d87212018-08-29 17:10:47 -0700190 continue;
191 }
192
193 uint64_t page_frame = PAGE_PFN(p);
194 if (!pinfo.PageFlags(page_frame, &pg_flags[i])) {
195 LOG(ERROR) << "Failed to get page flags for " << page_frame << " in process " << pid_;
Sandeep Patilc6497eb2018-11-20 09:31:36 -0800196 swap_offsets_.clear();
Sandeep Patil54d87212018-08-29 17:10:47 -0700197 return false;
198 }
199
Sandeep Patil549feab2018-11-19 11:38:40 -0800200 // skip unwanted pages from the count
201 if ((pg_flags[i] & pgflags_mask_) != pgflags_) continue;
202
Sandeep Patil54d87212018-08-29 17:10:47 -0700203 if (!pinfo.PageMapCount(page_frame, &pg_counts[i])) {
204 LOG(ERROR) << "Failed to get page count for " << page_frame << " in process " << pid_;
Sandeep Patilc6497eb2018-11-20 09:31:36 -0800205 swap_offsets_.clear();
Sandeep Patil54d87212018-08-29 17:10:47 -0700206 return false;
207 }
208
209 // Page was unmapped between the presence check at the beginning of the loop and here.
210 if (pg_counts[i] == 0) {
211 pg_frames[i] = 0;
212 pg_flags[i] = 0;
213 continue;
214 }
215
216 bool is_dirty = !!(pg_flags[i] & (1 << KPF_DIRTY));
217 bool is_private = (pg_counts[i] == 1);
218 // Working set
219 if (get_wss) {
220 bool is_referenced = !!(pg_flags[i] & (1 << KPF_REFERENCED));
221 if (!is_referenced) {
222 continue;
223 }
224 // This effectively makes vss = rss for the working set is requested.
225 // The libpagemap implementation returns vss > rss for
226 // working set, which doesn't make sense.
227 vma.wss.vss += pagesz;
228 vma.wss.rss += pagesz;
229 vma.wss.uss += is_private ? pagesz : 0;
230 vma.wss.pss += pagesz / pg_counts[i];
231 if (is_private) {
232 vma.wss.private_dirty += is_dirty ? pagesz : 0;
233 vma.wss.private_clean += is_dirty ? 0 : pagesz;
234 } else {
235 vma.wss.shared_dirty += is_dirty ? pagesz : 0;
236 vma.wss.shared_clean += is_dirty ? 0 : pagesz;
237 }
238 } else {
239 vma.usage.rss += pagesz;
240 vma.usage.uss += is_private ? pagesz : 0;
241 vma.usage.pss += pagesz / pg_counts[i];
242 if (is_private) {
243 vma.usage.private_dirty += is_dirty ? pagesz : 0;
244 vma.usage.private_clean += is_dirty ? 0 : pagesz;
245 } else {
246 vma.usage.shared_dirty += is_dirty ? pagesz : 0;
247 vma.usage.shared_clean += is_dirty ? 0 : pagesz;
248 }
249 }
250 }
251
252 return true;
253}
254
255} // namespace meminfo
256} // namespace android