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