blob: e3839346a908e0e3931545bdb3cf223b2b6a66c6 [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
56ProcMemInfo::ProcMemInfo(pid_t pid, bool get_wss) : pid_(pid), get_wss_(get_wss) {
57 if (!ReadMaps(get_wss_)) {
58 LOG(ERROR) << "Failed to read maps for Process " << pid_;
59 }
60}
61
62const std::vector<Vma>& ProcMemInfo::Maps() {
63 return maps_;
64}
65
66const MemUsage& ProcMemInfo::Usage() {
67 if (get_wss_) {
68 LOG(WARNING) << "Trying to read memory usage from working set object";
69 }
70 return usage_;
71}
72
73const MemUsage& ProcMemInfo::Wss() {
74 if (!get_wss_) {
75 LOG(WARNING) << "Trying to read working set when there is none";
76 }
77
78 return wss_;
79}
80
Sandeep Patil2259fdf2018-11-09 16:42:45 -080081const std::vector<uint16_t>& ProcMemInfo::SwapOffsets() const {
82 return swap_offsets_;
83}
84
Sandeep Patil54d87212018-08-29 17:10:47 -070085bool ProcMemInfo::WssReset() {
86 if (!get_wss_) {
87 LOG(ERROR) << "Trying to reset working set from a memory usage counting object";
88 return false;
89 }
90
91 std::string clear_refs_path = ::android::base::StringPrintf("/proc/%d/clear_refs", pid_);
92 if (!::android::base::WriteStringToFile("1\n", clear_refs_path)) {
93 PLOG(ERROR) << "Failed to write to " << clear_refs_path;
94 return false;
95 }
96
97 wss_.clear();
98 return true;
99}
100
101bool ProcMemInfo::ReadMaps(bool get_wss) {
102 // parse and read /proc/<pid>/maps
103 std::string maps_file = ::android::base::StringPrintf("/proc/%d/maps", pid_);
104 if (!::android::procinfo::ReadMapFile(
105 maps_file, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff,
106 const char* name) {
107 maps_.emplace_back(Vma(start, end, pgoff, flags, name));
108 })) {
109 LOG(ERROR) << "Failed to parse " << maps_file;
110 maps_.clear();
111 return false;
112 }
113
114 std::string pagemap_file = ::android::base::StringPrintf("/proc/%d/pagemap", pid_);
115 ::android::base::unique_fd pagemap_fd(
116 TEMP_FAILURE_RETRY(open(pagemap_file.c_str(), O_RDONLY | O_CLOEXEC)));
117 if (pagemap_fd < 0) {
118 PLOG(ERROR) << "Failed to open " << pagemap_file;
119 return false;
120 }
121
122 for (auto& vma : maps_) {
123 if (!ReadVmaStats(pagemap_fd.get(), vma, get_wss)) {
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800124 LOG(ERROR) << "Failed to read page map for vma " << vma.name << "[" << vma.start << "-"
125 << vma.end << "]";
Sandeep Patil54d87212018-08-29 17:10:47 -0700126 maps_.clear();
127 return false;
128 }
129 if (get_wss) {
130 add_mem_usage(&wss_, vma.wss);
131 } else {
132 add_mem_usage(&usage_, vma.usage);
133 }
134 }
135
136 return true;
137}
138
139bool ProcMemInfo::ReadVmaStats(int pagemap_fd, Vma& vma, bool get_wss) {
140 PageAcct& pinfo = PageAcct::Instance();
141 uint64_t pagesz = getpagesize();
142 uint64_t num_pages = (vma.end - vma.start) / pagesz;
143
144 std::unique_ptr<uint64_t[]> pg_frames(new uint64_t[num_pages]);
145 uint64_t first = vma.start / pagesz;
146 if (pread64(pagemap_fd, pg_frames.get(), num_pages * sizeof(uint64_t),
147 first * sizeof(uint64_t)) < 0) {
148 PLOG(ERROR) << "Failed to read page frames from page map for pid: " << pid_;
149 return false;
150 }
151
152 std::unique_ptr<uint64_t[]> pg_flags(new uint64_t[num_pages]);
153 std::unique_ptr<uint64_t[]> pg_counts(new uint64_t[num_pages]);
154 for (uint64_t i = 0; i < num_pages; ++i) {
155 if (!get_wss) {
156 vma.usage.vss += pagesz;
157 }
158 uint64_t p = pg_frames[i];
159 if (!PAGE_PRESENT(p) && !PAGE_SWAPPED(p)) continue;
160
161 if (PAGE_SWAPPED(p)) {
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800162 vma.usage.swap += pagesz;
163 swap_offsets_.emplace_back(PAGE_SWAP_OFFSET(p));
Sandeep Patil54d87212018-08-29 17:10:47 -0700164 continue;
165 }
166
167 uint64_t page_frame = PAGE_PFN(p);
168 if (!pinfo.PageFlags(page_frame, &pg_flags[i])) {
169 LOG(ERROR) << "Failed to get page flags for " << page_frame << " in process " << pid_;
170 return false;
171 }
172
173 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