blob: 56b62e8266f3d0f93ca31c3a1870a39cf214b88a [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 Patil549feab2018-11-19 11:38:40 -080056ProcMemInfo::ProcMemInfo(pid_t pid, bool get_wss, uint64_t pgflags, uint64_t pgflags_mask)
57 : pid_(pid), get_wss_(get_wss), pgflags_(pgflags), pgflags_mask_(pgflags_mask) {
Sandeep Patil54d87212018-08-29 17:10:47 -070058 if (!ReadMaps(get_wss_)) {
59 LOG(ERROR) << "Failed to read maps for Process " << pid_;
60 }
61}
62
63const std::vector<Vma>& ProcMemInfo::Maps() {
64 return maps_;
65}
66
67const MemUsage& ProcMemInfo::Usage() {
68 if (get_wss_) {
69 LOG(WARNING) << "Trying to read memory usage from working set object";
70 }
71 return usage_;
72}
73
74const MemUsage& ProcMemInfo::Wss() {
75 if (!get_wss_) {
76 LOG(WARNING) << "Trying to read working set when there is none";
77 }
78
79 return wss_;
80}
81
Sandeep Patil2259fdf2018-11-09 16:42:45 -080082const std::vector<uint16_t>& ProcMemInfo::SwapOffsets() const {
83 return swap_offsets_;
84}
85
Sandeep Patil54d87212018-08-29 17:10:47 -070086bool ProcMemInfo::WssReset() {
87 if (!get_wss_) {
88 LOG(ERROR) << "Trying to reset working set from a memory usage counting object";
89 return false;
90 }
91
92 std::string clear_refs_path = ::android::base::StringPrintf("/proc/%d/clear_refs", pid_);
93 if (!::android::base::WriteStringToFile("1\n", clear_refs_path)) {
94 PLOG(ERROR) << "Failed to write to " << clear_refs_path;
95 return false;
96 }
97
98 wss_.clear();
99 return true;
100}
101
102bool ProcMemInfo::ReadMaps(bool get_wss) {
103 // parse and read /proc/<pid>/maps
104 std::string maps_file = ::android::base::StringPrintf("/proc/%d/maps", pid_);
105 if (!::android::procinfo::ReadMapFile(
106 maps_file, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff,
107 const char* name) {
108 maps_.emplace_back(Vma(start, end, pgoff, flags, name));
109 })) {
110 LOG(ERROR) << "Failed to parse " << maps_file;
111 maps_.clear();
112 return false;
113 }
114
115 std::string pagemap_file = ::android::base::StringPrintf("/proc/%d/pagemap", pid_);
116 ::android::base::unique_fd pagemap_fd(
117 TEMP_FAILURE_RETRY(open(pagemap_file.c_str(), O_RDONLY | O_CLOEXEC)));
118 if (pagemap_fd < 0) {
119 PLOG(ERROR) << "Failed to open " << pagemap_file;
120 return false;
121 }
122
123 for (auto& vma : maps_) {
124 if (!ReadVmaStats(pagemap_fd.get(), vma, get_wss)) {
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800125 LOG(ERROR) << "Failed to read page map for vma " << vma.name << "[" << vma.start << "-"
126 << vma.end << "]";
Sandeep Patil54d87212018-08-29 17:10:47 -0700127 maps_.clear();
128 return false;
129 }
130 if (get_wss) {
131 add_mem_usage(&wss_, vma.wss);
132 } else {
133 add_mem_usage(&usage_, vma.usage);
134 }
135 }
136
137 return true;
138}
139
140bool ProcMemInfo::ReadVmaStats(int pagemap_fd, Vma& vma, bool get_wss) {
141 PageAcct& pinfo = PageAcct::Instance();
142 uint64_t pagesz = getpagesize();
143 uint64_t num_pages = (vma.end - vma.start) / pagesz;
144
145 std::unique_ptr<uint64_t[]> pg_frames(new uint64_t[num_pages]);
146 uint64_t first = vma.start / pagesz;
147 if (pread64(pagemap_fd, pg_frames.get(), num_pages * sizeof(uint64_t),
148 first * sizeof(uint64_t)) < 0) {
149 PLOG(ERROR) << "Failed to read page frames from page map for pid: " << pid_;
150 return false;
151 }
152
153 std::unique_ptr<uint64_t[]> pg_flags(new uint64_t[num_pages]);
154 std::unique_ptr<uint64_t[]> pg_counts(new uint64_t[num_pages]);
155 for (uint64_t i = 0; i < num_pages; ++i) {
156 if (!get_wss) {
157 vma.usage.vss += pagesz;
158 }
159 uint64_t p = pg_frames[i];
160 if (!PAGE_PRESENT(p) && !PAGE_SWAPPED(p)) continue;
161
162 if (PAGE_SWAPPED(p)) {
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800163 vma.usage.swap += pagesz;
164 swap_offsets_.emplace_back(PAGE_SWAP_OFFSET(p));
Sandeep Patil54d87212018-08-29 17:10:47 -0700165 continue;
166 }
167
168 uint64_t page_frame = PAGE_PFN(p);
169 if (!pinfo.PageFlags(page_frame, &pg_flags[i])) {
170 LOG(ERROR) << "Failed to get page flags for " << page_frame << " in process " << pid_;
171 return false;
172 }
173
Sandeep Patil549feab2018-11-19 11:38:40 -0800174 // skip unwanted pages from the count
175 if ((pg_flags[i] & pgflags_mask_) != pgflags_) continue;
176
Sandeep Patil54d87212018-08-29 17:10:47 -0700177 if (!pinfo.PageMapCount(page_frame, &pg_counts[i])) {
178 LOG(ERROR) << "Failed to get page count for " << page_frame << " in process " << pid_;
179 return false;
180 }
181
182 // Page was unmapped between the presence check at the beginning of the loop and here.
183 if (pg_counts[i] == 0) {
184 pg_frames[i] = 0;
185 pg_flags[i] = 0;
186 continue;
187 }
188
189 bool is_dirty = !!(pg_flags[i] & (1 << KPF_DIRTY));
190 bool is_private = (pg_counts[i] == 1);
191 // Working set
192 if (get_wss) {
193 bool is_referenced = !!(pg_flags[i] & (1 << KPF_REFERENCED));
194 if (!is_referenced) {
195 continue;
196 }
197 // This effectively makes vss = rss for the working set is requested.
198 // The libpagemap implementation returns vss > rss for
199 // working set, which doesn't make sense.
200 vma.wss.vss += pagesz;
201 vma.wss.rss += pagesz;
202 vma.wss.uss += is_private ? pagesz : 0;
203 vma.wss.pss += pagesz / pg_counts[i];
204 if (is_private) {
205 vma.wss.private_dirty += is_dirty ? pagesz : 0;
206 vma.wss.private_clean += is_dirty ? 0 : pagesz;
207 } else {
208 vma.wss.shared_dirty += is_dirty ? pagesz : 0;
209 vma.wss.shared_clean += is_dirty ? 0 : pagesz;
210 }
211 } else {
212 vma.usage.rss += pagesz;
213 vma.usage.uss += is_private ? pagesz : 0;
214 vma.usage.pss += pagesz / pg_counts[i];
215 if (is_private) {
216 vma.usage.private_dirty += is_dirty ? pagesz : 0;
217 vma.usage.private_clean += is_dirty ? 0 : pagesz;
218 } else {
219 vma.usage.shared_dirty += is_dirty ? pagesz : 0;
220 vma.usage.shared_clean += is_dirty ? 0 : pagesz;
221 }
222 }
223 }
224
225 return true;
226}
227
228} // namespace meminfo
229} // namespace android