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

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

Issue 2740423002: mac: Fix calulation for number of resident pages in a process. (Closed)
Patch Set: Comments from mark, primiano. Created 3 years, 9 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
« no previous file with comments | « base/process/process_metrics_linux.cc ('k') | base/process/process_metrics_openbsd.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 if (!GetTaskInfo(TaskForPid(process_), &task_info_data)) 103 if (!GetTaskInfo(TaskForPid(process_), &task_info_data))
104 return 0; 104 return 0;
105 return task_info_data.virtual_size; 105 return task_info_data.virtual_size;
106 } 106 }
107 107
108 size_t ProcessMetrics::GetPeakPagefileUsage() const { 108 size_t ProcessMetrics::GetPeakPagefileUsage() const {
109 return 0; 109 return 0;
110 } 110 }
111 111
112 size_t ProcessMetrics::GetWorkingSetSize() const { 112 size_t ProcessMetrics::GetWorkingSetSize() const {
113 task_basic_info_64 task_info_data; 113 size_t private_bytes = 0;
114 if (!GetTaskInfo(TaskForPid(process_), &task_info_data)) 114 size_t shared_bytes = 0;
115 if (!GetMemoryBytes(&private_bytes, &shared_bytes))
115 return 0; 116 return 0;
116 return task_info_data.resident_size; 117 return private_bytes + shared_bytes;
Mark Mentovai 2017/03/13 21:05:08 Call the 3-arg GetMemoryBytes() and return the thi
erikchen 2017/03/13 21:19:04 Done.
117 } 118 }
118 119
119 size_t ProcessMetrics::GetPeakWorkingSetSize() const { 120 size_t ProcessMetrics::GetPeakWorkingSetSize() const {
120 return 0; 121 return 0;
121 } 122 }
122 123
123 // This is a rough approximation of the algorithm that libtop uses. 124 // This is a rough approximation of the algorithm that libtop uses.
124 // private_bytes is the size of private resident memory. 125 // private_bytes is the size of private resident memory.
125 // shared_bytes is the size of shared resident memory. 126 // shared_bytes is the size of shared resident memory.
126 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, 127 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes,
127 size_t* shared_bytes) { 128 size_t* shared_bytes) const {
128 size_t private_pages_count = 0; 129 size_t private_pages_count = 0;
129 size_t shared_pages_count = 0; 130 size_t shared_pages_count = 0;
130 131
131 if (!private_bytes && !shared_bytes) 132 if (!private_bytes && !shared_bytes)
132 return true; 133 return true;
133 134
134 mach_port_t task = TaskForPid(process_); 135 mach_port_t task = TaskForPid(process_);
135 if (task == MACH_PORT_NULL) { 136 if (task == MACH_PORT_NULL) {
136 DLOG(ERROR) << "Invalid process"; 137 DLOG(ERROR) << "Invalid process";
137 return false; 138 return false;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 mach_port_deallocate(mach_task_self(), object_name); 183 mach_port_deallocate(mach_task_self(), object_name);
183 184
184 if (IsAddressInSharedRegion(address, cpu_type) && 185 if (IsAddressInSharedRegion(address, cpu_type) &&
185 info.share_mode != SM_PRIVATE) 186 info.share_mode != SM_PRIVATE)
186 continue; 187 continue;
187 188
188 if (info.share_mode == SM_COW && info.ref_count == 1) 189 if (info.share_mode == SM_COW && info.ref_count == 1)
189 info.share_mode = SM_PRIVATE; 190 info.share_mode = SM_PRIVATE;
190 191
191 switch (info.share_mode) { 192 switch (info.share_mode) {
193 case SM_LARGE_PAGE:
192 case SM_PRIVATE: 194 case SM_PRIVATE:
193 private_pages_count += info.private_pages_resident; 195 private_pages_count += info.private_pages_resident;
194 private_pages_count += info.shared_pages_resident; 196 private_pages_count += info.shared_pages_resident;
195 break; 197 break;
196 case SM_COW: 198 case SM_COW:
197 private_pages_count += info.private_pages_resident; 199 private_pages_count += info.private_pages_resident;
198 // Fall through 200 // Fall through
199 case SM_SHARED: 201 case SM_SHARED:
202 case SM_PRIVATE_ALIASED:
203 case SM_TRUESHARED:
204 case SM_SHARED_ALIASED:
200 if (seen_objects.count(info.obj_id) == 0) { 205 if (seen_objects.count(info.obj_id) == 0) {
201 // Only count the first reference to this region. 206 // Only count the first reference to this region.
202 seen_objects.insert(info.obj_id); 207 seen_objects.insert(info.obj_id);
203 shared_pages_count += info.shared_pages_resident; 208 shared_pages_count += info.shared_pages_resident;
204 } 209 }
205 break; 210 break;
206 default: 211 default:
207 break; 212 break;
208 } 213 }
209 } 214 }
(...skipping 29 matching lines...) Expand all
239 usage->mapped = 0; 244 usage->mapped = 0;
240 usage->image = 0; 245 usage->image = 0;
241 246
242 ws_usage->priv = task_info_data.resident_size / 1024; 247 ws_usage->priv = task_info_data.resident_size / 1024;
243 ws_usage->shareable = 0; 248 ws_usage->shareable = 0;
244 ws_usage->shared = 0; 249 ws_usage->shared = 0;
245 250
246 return true; 251 return true;
247 } 252 }
248 253
254 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes,
255 size_t* shared_bytes,
256 size_t* resident_bytes) const {
257 if (!GetMemoryBytes(private_bytes, shared_bytes))
258 return false;
259 *resident_bytes = *private_bytes + *shared_bytes;
260 return true;
261 }
262
249 #define TIME_VALUE_TO_TIMEVAL(a, r) do { \ 263 #define TIME_VALUE_TO_TIMEVAL(a, r) do { \
250 (r)->tv_sec = (a)->seconds; \ 264 (r)->tv_sec = (a)->seconds; \
251 (r)->tv_usec = (a)->microseconds; \ 265 (r)->tv_usec = (a)->microseconds; \
252 } while (0) 266 } while (0)
253 267
254 double ProcessMetrics::GetCPUUsage() { 268 double ProcessMetrics::GetCPUUsage() {
255 mach_port_t task = TaskForPid(process_); 269 mach_port_t task = TaskForPid(process_);
256 if (task == MACH_PORT_NULL) 270 if (task == MACH_PORT_NULL)
257 return 0; 271 return 0;
258 272
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 return false; 413 return false;
400 } 414 }
401 415
402 meminfo->free = static_cast<int>( 416 meminfo->free = static_cast<int>(
403 (vm_info.free_count - vm_info.speculative_count) * PAGE_SIZE / 1024); 417 (vm_info.free_count - vm_info.speculative_count) * PAGE_SIZE / 1024);
404 418
405 return true; 419 return true;
406 } 420 }
407 421
408 } // namespace base 422 } // namespace base
OLDNEW
« no previous file with comments | « base/process/process_metrics_linux.cc ('k') | base/process/process_metrics_openbsd.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698