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

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: 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
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;
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 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 return false; 404 return false;
400 } 405 }
401 406
402 meminfo->free = static_cast<int>( 407 meminfo->free = static_cast<int>(
403 (vm_info.free_count - vm_info.speculative_count) * PAGE_SIZE / 1024); 408 (vm_info.free_count - vm_info.speculative_count) * PAGE_SIZE / 1024);
404 409
405 return true; 410 return true;
406 } 411 }
407 412
408 } // namespace base 413 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698