Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1380)

Side by Side Diff: base/process/process_metrics_mac.cc

Issue 2782503002: Add new wired memory metric to memory-infra dumps. (Closed)
Patch Set: compile error. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/process/process_metrics.h" 5 #include "base/process/process_metrics.h"
6 6
7 #include <mach/mach.h> 7 #include <mach/mach.h>
8 #include <mach/mach_vm.h> 8 #include <mach/mach_vm.h>
9 #include <mach/shared_region.h> 9 #include <mach/shared_region.h>
10 #include <stddef.h> 10 #include <stddef.h>
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 return addr >= SHARED_REGION_BASE_I386 && 72 return addr >= SHARED_REGION_BASE_I386 &&
73 addr < (SHARED_REGION_BASE_I386 + SHARED_REGION_SIZE_I386); 73 addr < (SHARED_REGION_BASE_I386 + SHARED_REGION_SIZE_I386);
74 } else if (type == CPU_TYPE_X86_64) { 74 } else if (type == CPU_TYPE_X86_64) {
75 return addr >= SHARED_REGION_BASE_X86_64 && 75 return addr >= SHARED_REGION_BASE_X86_64 &&
76 addr < (SHARED_REGION_BASE_X86_64 + SHARED_REGION_SIZE_X86_64); 76 addr < (SHARED_REGION_BASE_X86_64 + SHARED_REGION_SIZE_X86_64);
77 } else { 77 } else {
78 return false; 78 return false;
79 } 79 }
80 } 80 }
81 81
82 enum MachVMRegionResult { Finished, Error, Success };
83
84 // Both |size| and |address| are in-out parameters.
85 // |info| is an output parameter, only valid on Success.
86 MachVMRegionResult GetTopInfo(mach_port_t task,
87 mach_vm_size_t* size,
88 mach_vm_address_t* address,
89 vm_region_top_info_data_t* info) {
90 mach_msg_type_number_t info_count = VM_REGION_TOP_INFO_COUNT;
91 mach_port_t object_name;
92 kern_return_t kr = mach_vm_region(task, address, size, VM_REGION_TOP_INFO,
93 reinterpret_cast<vm_region_info_t>(info),
94 &info_count, &object_name);
95 if (kr == KERN_INVALID_ADDRESS) {
96 // We're at the end of the address space.
97 return Finished;
98 } else if (kr != KERN_SUCCESS) {
99 return Error;
100 }
101
102 // The kernel always returns a null object for VM_REGION_TOP_INFO, but
103 // balance it with a deallocate in case this ever changes. See 10.9.2
104 // xnu-2422.90.20/osfmk/vm/vm_map.c vm_map_region.
105 mach_port_deallocate(task, object_name);
106 return Success;
107 }
108
109 MachVMRegionResult GetBasicInfo(mach_port_t task,
110 mach_vm_size_t* size,
111 mach_vm_address_t* address,
112 vm_region_basic_info_64* info) {
113 mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT_64;
114 mach_port_t object_name;
115 kern_return_t kr = mach_vm_region(
116 task, address, size, VM_REGION_BASIC_INFO_64,
117 reinterpret_cast<vm_region_info_t>(info), &info_count, &object_name);
118 if (kr == KERN_INVALID_ADDRESS) {
119 // We're at the end of the address space.
120 return Finished;
121 } else if (kr != KERN_SUCCESS) {
122 return Error;
123 }
124
125 // The kernel always returns a null object for VM_REGION_BASIC_INFO_64, but
126 // balance it with a deallocate in case this ever changes. See 10.9.2
127 // xnu-2422.90.20/osfmk/vm/vm_map.c vm_map_region.
128 mach_port_deallocate(task, object_name);
129 return Success;
130 }
131
82 } // namespace 132 } // namespace
83 133
84 // Getting a mach task from a pid for another process requires permissions in 134 // Getting a mach task from a pid for another process requires permissions in
85 // general, so there doesn't really seem to be a way to do these (and spinning 135 // general, so there doesn't really seem to be a way to do these (and spinning
86 // up ps to fetch each stats seems dangerous to put in a base api for anyone to 136 // up ps to fetch each stats seems dangerous to put in a base api for anyone to
87 // call). Child processes ipc their port, so return something if available, 137 // call). Child processes ipc their port, so return something if available,
88 // otherwise return 0. 138 // otherwise return 0.
89 139
90 // static 140 // static
91 std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics( 141 std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics(
92 ProcessHandle process, 142 ProcessHandle process,
93 PortProvider* port_provider) { 143 PortProvider* port_provider) {
94 return WrapUnique(new ProcessMetrics(process, port_provider)); 144 return WrapUnique(new ProcessMetrics(process, port_provider));
95 } 145 }
96 146
97 size_t ProcessMetrics::GetPagefileUsage() const { 147 size_t ProcessMetrics::GetPagefileUsage() const {
98 task_basic_info_64 task_info_data; 148 task_basic_info_64 task_info_data;
99 if (!GetTaskInfo(TaskForPid(process_), &task_info_data)) 149 if (!GetTaskInfo(TaskForPid(process_), &task_info_data))
100 return 0; 150 return 0;
101 return task_info_data.virtual_size; 151 return task_info_data.virtual_size;
102 } 152 }
103 153
104 size_t ProcessMetrics::GetPeakPagefileUsage() const { 154 size_t ProcessMetrics::GetPeakPagefileUsage() const {
105 return 0; 155 return 0;
106 } 156 }
107 157
108 size_t ProcessMetrics::GetWorkingSetSize() const { 158 size_t ProcessMetrics::GetWorkingSetSize() const {
109 size_t private_bytes = 0;
110 size_t shared_bytes = 0;
111 size_t resident_bytes = 0; 159 size_t resident_bytes = 0;
112 if (!GetMemoryBytes(&private_bytes, &shared_bytes, &resident_bytes)) 160 if (!GetMemoryBytes(nullptr, nullptr, &resident_bytes, nullptr))
113 return 0; 161 return 0;
114 return resident_bytes; 162 return resident_bytes;
115 } 163 }
116 164
117 size_t ProcessMetrics::GetPeakWorkingSetSize() const { 165 size_t ProcessMetrics::GetPeakWorkingSetSize() const {
118 return 0; 166 return 0;
119 } 167 }
120 168
169 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes,
170 size_t* shared_bytes) const {
171 if (!private_bytes && !shared_bytes)
Primiano Tucci (use gerrit) 2017/03/28 20:32:54 is this ever the case? If not I'd drop this at it
erikchen 2017/03/30 00:03:14 Done.
172 return true;
173
174 return GetMemoryBytes(private_bytes, shared_bytes, nullptr, nullptr);
175 }
176
121 // This is a rough approximation of the algorithm that libtop uses. 177 // This is a rough approximation of the algorithm that libtop uses.
122 // private_bytes is the size of private resident memory. 178 // private_bytes is the size of private resident memory.
123 // shared_bytes is the size of shared resident memory. 179 // shared_bytes is the size of shared resident memory.
124 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, 180 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes,
125 size_t* shared_bytes) const { 181 size_t* shared_bytes,
182 size_t* resident_bytes,
183 size_t* wired_bytes) const {
126 size_t private_pages_count = 0; 184 size_t private_pages_count = 0;
127 size_t shared_pages_count = 0; 185 size_t shared_pages_count = 0;
128 186 size_t wired_pages_count = 0;
129 if (!private_bytes && !shared_bytes)
130 return true;
131 187
132 mach_port_t task = TaskForPid(process_); 188 mach_port_t task = TaskForPid(process_);
133 if (task == MACH_PORT_NULL) { 189 if (task == MACH_PORT_NULL) {
134 DLOG(ERROR) << "Invalid process"; 190 DLOG(ERROR) << "Invalid process";
135 return false; 191 return false;
136 } 192 }
137 193
138 cpu_type_t cpu_type; 194 cpu_type_t cpu_type;
139 if (!GetCPUTypeForProcess(process_, &cpu_type)) 195 if (!GetCPUTypeForProcess(process_, &cpu_type))
140 return false; 196 return false;
141 197
142 // The same region can be referenced multiple times. To avoid double counting 198 // The same region can be referenced multiple times. To avoid double counting
143 // we need to keep track of which regions we've already counted. 199 // we need to keep track of which regions we've already counted.
144 hash_set<int> seen_objects; 200 hash_set<int> seen_objects;
145 201
146 // We iterate through each VM region in the task's address map. For shared 202 // We iterate through each VM region in the task's address map. For shared
147 // memory we add up all the pages that are marked as shared. Like libtop we 203 // memory we add up all the pages that are marked as shared. Like libtop we
148 // try to avoid counting pages that are also referenced by other tasks. Since 204 // try to avoid counting pages that are also referenced by other tasks. Since
149 // we don't have access to the VM regions of other tasks the only hint we have 205 // we don't have access to the VM regions of other tasks the only hint we have
150 // is if the address is in the shared region area. 206 // is if the address is in the shared region area.
151 // 207 //
152 // Private memory is much simpler. We simply count the pages that are marked 208 // Private memory is much simpler. We simply count the pages that are marked
153 // as private or copy on write (COW). 209 // as private or copy on write (COW).
154 // 210 //
155 // See libtop_update_vm_regions in 211 // See libtop_update_vm_regions in
156 // http://www.opensource.apple.com/source/top/top-67/libtop.c 212 // http://www.opensource.apple.com/source/top/top-67/libtop.c
157 mach_vm_size_t size = 0; 213 mach_vm_size_t size = 0;
158 for (mach_vm_address_t address = MACH_VM_MIN_ADDRESS;; address += size) { 214 for (mach_vm_address_t address = MACH_VM_MIN_ADDRESS;; address += size) {
215 mach_vm_size_t size_copy = size;
216 mach_vm_address_t address_copy = address;
217
159 vm_region_top_info_data_t info; 218 vm_region_top_info_data_t info;
160 mach_msg_type_number_t info_count = VM_REGION_TOP_INFO_COUNT; 219 MachVMRegionResult result = GetTopInfo(task, &size, &address, &info);
161 mach_port_t object_name; 220 bool finished = false;
162 kern_return_t kr = mach_vm_region(task, 221 switch (result) {
163 &address, 222 case Finished:
164 &size, 223 finished = true;
165 VM_REGION_TOP_INFO, 224 break;
166 reinterpret_cast<vm_region_info_t>(&info), 225 case Error:
167 &info_count, 226 return false;
168 &object_name); 227 case Success:
169 if (kr == KERN_INVALID_ADDRESS) { 228 break;
170 // We're at the end of the address space.
171 break;
172 } else if (kr != KERN_SUCCESS) {
173 MACH_DLOG(ERROR, kr) << "mach_vm_region";
174 return false;
175 } 229 }
176 230
177 // The kernel always returns a null object for VM_REGION_TOP_INFO, but 231 if (finished)
178 // balance it with a deallocate in case this ever changes. See 10.9.2 232 break;
179 // xnu-2422.90.20/osfmk/vm/vm_map.c vm_map_region. 233
180 mach_port_deallocate(mach_task_self(), object_name); 234 vm_region_basic_info_64 basic_info;
235 result = GetBasicInfo(task, &size_copy, &address_copy, &basic_info);
236 switch (result) {
237 case Finished:
238 case Error:
239 return false;
240 case Success:
241 break;
242 }
243 bool is_wired = basic_info.user_wired_count > 0;
181 244
182 if (IsAddressInSharedRegion(address, cpu_type) && 245 if (IsAddressInSharedRegion(address, cpu_type) &&
183 info.share_mode != SM_PRIVATE) 246 info.share_mode != SM_PRIVATE)
184 continue; 247 continue;
185 248
186 if (info.share_mode == SM_COW && info.ref_count == 1) 249 if (info.share_mode == SM_COW && info.ref_count == 1)
187 info.share_mode = SM_PRIVATE; 250 info.share_mode = SM_PRIVATE;
188 251
189 switch (info.share_mode) { 252 switch (info.share_mode) {
190 case SM_LARGE_PAGE: 253 case SM_LARGE_PAGE:
(...skipping 10 matching lines...) Expand all
201 case SM_SHARED_ALIASED: 264 case SM_SHARED_ALIASED:
202 if (seen_objects.count(info.obj_id) == 0) { 265 if (seen_objects.count(info.obj_id) == 0) {
203 // Only count the first reference to this region. 266 // Only count the first reference to this region.
204 seen_objects.insert(info.obj_id); 267 seen_objects.insert(info.obj_id);
205 shared_pages_count += info.shared_pages_resident; 268 shared_pages_count += info.shared_pages_resident;
206 } 269 }
207 break; 270 break;
208 default: 271 default:
209 break; 272 break;
210 } 273 }
274 if (is_wired)
275 wired_pages_count +=
276 info.private_pages_resident + info.shared_pages_resident;
211 } 277 }
212 278
213 if (private_bytes) 279 if (private_bytes)
214 *private_bytes = private_pages_count * PAGE_SIZE; 280 *private_bytes = private_pages_count * PAGE_SIZE;
215 if (shared_bytes) 281 if (shared_bytes)
216 *shared_bytes = shared_pages_count * PAGE_SIZE; 282 *shared_bytes = shared_pages_count * PAGE_SIZE;
283 if (resident_bytes)
284 *resident_bytes = *private_bytes + *shared_bytes;
285 if (wired_bytes)
286 *wired_bytes = wired_pages_count * PAGE_SIZE;
217 287
218 return true; 288 return true;
219 } 289 }
220 290
221 void ProcessMetrics::GetCommittedKBytes(CommittedKBytes* usage) const { 291 void ProcessMetrics::GetCommittedKBytes(CommittedKBytes* usage) const {
222 WorkingSetKBytes unused; 292 WorkingSetKBytes unused;
223 if (!GetCommittedAndWorkingSetKBytes(usage, &unused)) { 293 if (!GetCommittedAndWorkingSetKBytes(usage, &unused)) {
224 *usage = CommittedKBytes(); 294 *usage = CommittedKBytes();
225 } 295 }
226 } 296 }
(...skipping 14 matching lines...) Expand all
241 usage->mapped = 0; 311 usage->mapped = 0;
242 usage->image = 0; 312 usage->image = 0;
243 313
244 ws_usage->priv = task_info_data.resident_size / 1024; 314 ws_usage->priv = task_info_data.resident_size / 1024;
245 ws_usage->shareable = 0; 315 ws_usage->shareable = 0;
246 ws_usage->shared = 0; 316 ws_usage->shared = 0;
247 317
248 return true; 318 return true;
249 } 319 }
250 320
251 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes,
252 size_t* shared_bytes,
253 size_t* resident_bytes) const {
254 if (!GetMemoryBytes(private_bytes, shared_bytes))
255 return false;
256 *resident_bytes = *private_bytes + *shared_bytes;
257 return true;
258 }
259
260 #define TIME_VALUE_TO_TIMEVAL(a, r) do { \ 321 #define TIME_VALUE_TO_TIMEVAL(a, r) do { \
261 (r)->tv_sec = (a)->seconds; \ 322 (r)->tv_sec = (a)->seconds; \
262 (r)->tv_usec = (a)->microseconds; \ 323 (r)->tv_usec = (a)->microseconds; \
263 } while (0) 324 } while (0)
264 325
265 double ProcessMetrics::GetCPUUsage() { 326 double ProcessMetrics::GetCPUUsage() {
266 mach_port_t task = TaskForPid(process_); 327 mach_port_t task = TaskForPid(process_);
267 if (task == MACH_PORT_NULL) 328 if (task == MACH_PORT_NULL)
268 return 0; 329 return 0;
269 330
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.speculative_count); 478 saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.speculative_count);
418 meminfo->file_backed = 479 meminfo->file_backed =
419 saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.external_page_count); 480 saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.external_page_count);
420 meminfo->purgeable = 481 meminfo->purgeable =
421 saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.purgeable_count); 482 saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.purgeable_count);
422 483
423 return true; 484 return true;
424 } 485 }
425 486
426 } // namespace base 487 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698