blob: 378f923b32e75439782ed4fb23dba52491b52568 [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)
67 : pid_(pid), get_wss_(get_wss), pgflags_(pgflags), pgflags_mask_(pgflags_mask) {
Sandeep Patil54d87212018-08-29 17:10:47 -070068 if (!ReadMaps(get_wss_)) {
69 LOG(ERROR) << "Failed to read maps for Process " << pid_;
70 }
71}
72
73const std::vector<Vma>& ProcMemInfo::Maps() {
74 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 Patil54d87212018-08-29 17:10:47 -070081 }
82 return usage_;
83}
84
85const MemUsage& ProcMemInfo::Wss() {
86 if (!get_wss_) {
Sandeep Patilf1291992018-11-19 15:25:18 -080087 LOG(WARNING) << "Trying to read process working set for " << pid_
88 << " using invalid object";
Sandeep Patil54d87212018-08-29 17:10:47 -070089 }
90
91 return wss_;
92}
93
Sandeep Patil2259fdf2018-11-09 16:42:45 -080094const std::vector<uint16_t>& ProcMemInfo::SwapOffsets() const {
95 return swap_offsets_;
96}
97
Sandeep Patil54d87212018-08-29 17:10:47 -070098bool ProcMemInfo::ReadMaps(bool get_wss) {
99 // parse and read /proc/<pid>/maps
100 std::string maps_file = ::android::base::StringPrintf("/proc/%d/maps", pid_);
101 if (!::android::procinfo::ReadMapFile(
102 maps_file, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff,
103 const char* name) {
104 maps_.emplace_back(Vma(start, end, pgoff, flags, name));
105 })) {
106 LOG(ERROR) << "Failed to parse " << maps_file;
107 maps_.clear();
108 return false;
109 }
110
111 std::string pagemap_file = ::android::base::StringPrintf("/proc/%d/pagemap", pid_);
112 ::android::base::unique_fd pagemap_fd(
113 TEMP_FAILURE_RETRY(open(pagemap_file.c_str(), O_RDONLY | O_CLOEXEC)));
114 if (pagemap_fd < 0) {
115 PLOG(ERROR) << "Failed to open " << pagemap_file;
116 return false;
117 }
118
119 for (auto& vma : maps_) {
120 if (!ReadVmaStats(pagemap_fd.get(), vma, get_wss)) {
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800121 LOG(ERROR) << "Failed to read page map for vma " << vma.name << "[" << vma.start << "-"
122 << vma.end << "]";
Sandeep Patil54d87212018-08-29 17:10:47 -0700123 maps_.clear();
124 return false;
125 }
126 if (get_wss) {
127 add_mem_usage(&wss_, vma.wss);
128 } else {
129 add_mem_usage(&usage_, vma.usage);
130 }
131 }
132
133 return true;
134}
135
136bool ProcMemInfo::ReadVmaStats(int pagemap_fd, Vma& vma, bool get_wss) {
137 PageAcct& pinfo = PageAcct::Instance();
138 uint64_t pagesz = getpagesize();
139 uint64_t num_pages = (vma.end - vma.start) / pagesz;
140
141 std::unique_ptr<uint64_t[]> pg_frames(new uint64_t[num_pages]);
142 uint64_t first = vma.start / pagesz;
143 if (pread64(pagemap_fd, pg_frames.get(), num_pages * sizeof(uint64_t),
144 first * sizeof(uint64_t)) < 0) {
145 PLOG(ERROR) << "Failed to read page frames from page map for pid: " << pid_;
146 return false;
147 }
148
149 std::unique_ptr<uint64_t[]> pg_flags(new uint64_t[num_pages]);
150 std::unique_ptr<uint64_t[]> pg_counts(new uint64_t[num_pages]);
151 for (uint64_t i = 0; i < num_pages; ++i) {
152 if (!get_wss) {
153 vma.usage.vss += pagesz;
154 }
155 uint64_t p = pg_frames[i];
156 if (!PAGE_PRESENT(p) && !PAGE_SWAPPED(p)) continue;
157
158 if (PAGE_SWAPPED(p)) {
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800159 vma.usage.swap += pagesz;
160 swap_offsets_.emplace_back(PAGE_SWAP_OFFSET(p));
Sandeep Patil54d87212018-08-29 17:10:47 -0700161 continue;
162 }
163
164 uint64_t page_frame = PAGE_PFN(p);
165 if (!pinfo.PageFlags(page_frame, &pg_flags[i])) {
166 LOG(ERROR) << "Failed to get page flags for " << page_frame << " in process " << pid_;
167 return false;
168 }
169
Sandeep Patil549feab2018-11-19 11:38:40 -0800170 // skip unwanted pages from the count
171 if ((pg_flags[i] & pgflags_mask_) != pgflags_) continue;
172
Sandeep Patil54d87212018-08-29 17:10:47 -0700173 if (!pinfo.PageMapCount(page_frame, &pg_counts[i])) {
174 LOG(ERROR) << "Failed to get page count for " << page_frame << " in process " << pid_;
175 return false;
176 }
177
178 // Page was unmapped between the presence check at the beginning of the loop and here.
179 if (pg_counts[i] == 0) {
180 pg_frames[i] = 0;
181 pg_flags[i] = 0;
182 continue;
183 }
184
185 bool is_dirty = !!(pg_flags[i] & (1 << KPF_DIRTY));
186 bool is_private = (pg_counts[i] == 1);
187 // Working set
188 if (get_wss) {
189 bool is_referenced = !!(pg_flags[i] & (1 << KPF_REFERENCED));
190 if (!is_referenced) {
191 continue;
192 }
193 // This effectively makes vss = rss for the working set is requested.
194 // The libpagemap implementation returns vss > rss for
195 // working set, which doesn't make sense.
196 vma.wss.vss += pagesz;
197 vma.wss.rss += pagesz;
198 vma.wss.uss += is_private ? pagesz : 0;
199 vma.wss.pss += pagesz / pg_counts[i];
200 if (is_private) {
201 vma.wss.private_dirty += is_dirty ? pagesz : 0;
202 vma.wss.private_clean += is_dirty ? 0 : pagesz;
203 } else {
204 vma.wss.shared_dirty += is_dirty ? pagesz : 0;
205 vma.wss.shared_clean += is_dirty ? 0 : pagesz;
206 }
207 } else {
208 vma.usage.rss += pagesz;
209 vma.usage.uss += is_private ? pagesz : 0;
210 vma.usage.pss += pagesz / pg_counts[i];
211 if (is_private) {
212 vma.usage.private_dirty += is_dirty ? pagesz : 0;
213 vma.usage.private_clean += is_dirty ? 0 : pagesz;
214 } else {
215 vma.usage.shared_dirty += is_dirty ? pagesz : 0;
216 vma.usage.shared_clean += is_dirty ? 0 : pagesz;
217 }
218 }
219 }
220
221 return true;
222}
223
224} // namespace meminfo
225} // namespace android