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

Side by Side Diff: base/process_util_mac.mm

Issue 5992005: Mac: Shared and Private memory for Task Manager... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | 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) 2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2008 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 5
6 #include "base/process_util.h" 6 #include "base/process_util.h"
7 7
8 #import <Cocoa/Cocoa.h> 8 #import <Cocoa/Cocoa.h>
9 #include <crt_externs.h> 9 #include <crt_externs.h>
10 #include <dlfcn.h> 10 #include <dlfcn.h>
11 #include <mach/mach.h> 11 #include <mach/mach.h>
12 #include <mach/mach_init.h> 12 #include <mach/mach_init.h>
13 #include <mach/mach_vm.h>
14 #include <mach/shared_region.h>
13 #include <mach/task.h> 15 #include <mach/task.h>
14 #include <malloc/malloc.h> 16 #include <malloc/malloc.h>
15 #import <objc/runtime.h> 17 #import <objc/runtime.h>
16 #include <spawn.h> 18 #include <spawn.h>
17 #include <sys/mman.h> 19 #include <sys/mman.h>
18 #include <sys/sysctl.h> 20 #include <sys/sysctl.h>
19 #include <sys/types.h> 21 #include <sys/types.h>
20 #include <sys/utsname.h> 22 #include <sys/utsname.h>
21 #include <sys/wait.h> 23 #include <sys/wait.h>
22 24
23 #include <new> 25 #include <new>
24 #include <string> 26 #include <string>
25 27
26 #include "base/debug/debugger.h" 28 #include "base/debug/debugger.h"
27 #include "base/eintr_wrapper.h" 29 #include "base/eintr_wrapper.h"
30 #include "base/hash_tables.h"
28 #include "base/logging.h" 31 #include "base/logging.h"
29 #include "base/string_util.h" 32 #include "base/string_util.h"
30 #include "base/sys_info.h" 33 #include "base/sys_info.h"
31 #include "base/sys_string_conversions.h" 34 #include "base/sys_string_conversions.h"
32 #include "base/time.h" 35 #include "base/time.h"
33 #include "third_party/apple_apsl/CFBase.h" 36 #include "third_party/apple_apsl/CFBase.h"
34 #include "third_party/apple_apsl/malloc.h" 37 #include "third_party/apple_apsl/malloc.h"
35 38
36 namespace base { 39 namespace base {
37 40
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 task_basic_info_64 task_info_data; 231 task_basic_info_64 task_info_data;
229 if (!GetTaskInfo(TaskForPid(process_), &task_info_data)) 232 if (!GetTaskInfo(TaskForPid(process_), &task_info_data))
230 return 0; 233 return 0;
231 return task_info_data.resident_size; 234 return task_info_data.resident_size;
232 } 235 }
233 236
234 size_t ProcessMetrics::GetPeakWorkingSetSize() const { 237 size_t ProcessMetrics::GetPeakWorkingSetSize() const {
235 return 0; 238 return 0;
236 } 239 }
237 240
238 // OSX appears to use a different system to get its memory. 241 static bool GetCPUTypeForProcess(pid_t pid, cpu_type_t* cpu_type) {
239 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, 242 size_t len = sizeof(*cpu_type);
240 size_t* shared_bytes) { 243 int result = sysctlbyname("sysctl.proc_cputype",
241 if (private_bytes) 244 cpu_type,
242 *private_bytes = 0; 245 &len,
243 if (shared_bytes) 246 NULL,
244 *shared_bytes = 0; 247 0);
248 if (result != 0) {
249 LOG(ERROR) << "Call to sysctlbyname failed with error "
Nico 2010/12/21 18:30:06 use PLOG instead (it appends strerror(errno) autom
sail 2010/12/21 19:31:43 Done.
250 << strerror(errno);
251 return false;
252 }
253
245 return true; 254 return true;
246 } 255 }
247 256
257 static bool IsAddressInSharedRegion(mach_vm_address_t addr, cpu_type_t type) {
258 if (type == CPU_TYPE_I386)
259 return addr >= SHARED_REGION_BASE_I386 &&
260 addr < (SHARED_REGION_BASE_I386 + SHARED_REGION_SIZE_I386);
261 else if (type == CPU_TYPE_I386)
Nico 2010/12/21 18:30:06 This probably should be CPU_TYPE_X86_64? (Does gc
sail 2010/12/21 19:31:43 Ouch, good catch. Fixed.
262 return addr >= SHARED_REGION_BASE_X86_64 &&
263 addr < (SHARED_REGION_BASE_X86_64 + SHARED_REGION_SIZE_X86_64);
264 else
265 return false;
266 }
267
268 // This is a rough approximation of the algorithm that libtop uses.
269 // private_bytes is the size of private resident memory.
270 // shared_bytes is the size of shared resident memory.
271 // Note, libtop doesn't include shared memory that is referenced by other
272 // process but we do since we only look at a single process at a time.
273 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes,
274 size_t* shared_bytes) {
Nico 2010/12/21 18:30:06 Can you add a comment with a link to the libtop fi
sail 2010/12/21 19:31:43 Done.
275 kern_return_t kr;
276 size_t private_pages_count = 0;
277 size_t shared_pages_count = 0;
278
279 mach_port_t task = TaskForPid(process_);
280 if (task == MACH_PORT_NULL) {
281 LOG(ERROR) << "Invalid process";
282 return false;
283 }
284
285 cpu_type_t cpu_type;
286 if (!GetCPUTypeForProcess(process_, &cpu_type))
287 return false;
288
289 // The same region can be referenced multiple times. To avoid double counting
290 // we need to keep track of which regions we've already counted.
291 base::hash_set<int> seen_objects;
292
293 mach_vm_size_t size = 0;
294 for (mach_vm_address_t address = MACH_VM_MIN_ADDRESS;; address += size) {
295 vm_region_top_info_data_t info;
296 mach_msg_type_number_t info_count = VM_REGION_TOP_INFO_COUNT;
297 mach_port_t object_name;
298 kr = mach_vm_region(task,
299 &address,
300 &size,
301 VM_REGION_TOP_INFO,
302 (vm_region_info_t)&info,
303 &info_count,
304 &object_name);
305 if (kr == KERN_INVALID_ADDRESS) {
306 // We're at the end of the address space.
307 break;
308 } else if (kr != KERN_SUCCESS) {
309 LOG(ERROR) << "Calling mach_vm_region failed with error: "
310 << mach_error_string(kr);
311 return false;
312 }
313
314 if (IsAddressInSharedRegion(address, cpu_type) &&
315 info.share_mode != SM_PRIVATE)
316 continue;
317
318 if (info.share_mode == SM_COW && info.ref_count == 1)
319 info.share_mode = SM_PRIVATE;
320
321 switch (info.share_mode) {
322 case SM_PRIVATE:
323 private_pages_count += info.private_pages_resident;
324 private_pages_count += info.shared_pages_resident;
325 break;
326 case SM_COW:
327 private_pages_count += info.private_pages_resident;
328 // Fall through
329 case SM_SHARED:
330 if (seen_objects.find(info.obj_id) == seen_objects.end()) {
Nico 2010/12/21 18:30:06 Nit: I believe `if (seen_objects.count(info.obj_id
sail 2010/12/21 19:31:43 Done.
331 // Only count the first reference to this region.
332 seen_objects.insert(info.obj_id);
333 shared_pages_count += info.shared_pages_resident;
334 }
335 break;
336 default:
337 break;
338 }
339 }
340
341 vm_size_t page_size;
342 kr = host_page_size(task, &page_size);
343 if (kr != KERN_SUCCESS) {
344 LOG(ERROR) << "Failed to fetch host page size, error: "
345 << mach_error_string(kr);
346 return false;
347 }
348
349 if (private_bytes)
350 *private_bytes = private_pages_count * page_size;
351 if (shared_bytes)
352 *shared_bytes = shared_pages_count * page_size;
353
354 return true;
355 }
356
248 void ProcessMetrics::GetCommittedKBytes(CommittedKBytes* usage) const { 357 void ProcessMetrics::GetCommittedKBytes(CommittedKBytes* usage) const {
249 } 358 }
250 359
251 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { 360 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const {
252 size_t priv = GetWorkingSetSize(); 361 size_t priv = GetWorkingSetSize();
253 if (!priv) 362 if (!priv)
254 return false; 363 return false;
255 ws_usage->priv = priv / 1024; 364 ws_usage->priv = priv / 1024;
256 ws_usage->shareable = 0; 365 ws_usage->shareable = 0;
257 ws_usage->shared = 0; 366 ws_usage->shared = 0;
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 @selector(allocWithZone:)); 863 @selector(allocWithZone:));
755 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>( 864 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>(
756 method_getImplementation(orig_method)); 865 method_getImplementation(orig_method));
757 CHECK(g_old_allocWithZone) 866 CHECK(g_old_allocWithZone)
758 << "Failed to get allocWithZone allocation function."; 867 << "Failed to get allocWithZone allocation function.";
759 method_setImplementation(orig_method, 868 method_setImplementation(orig_method,
760 reinterpret_cast<IMP>(oom_killer_allocWithZone)); 869 reinterpret_cast<IMP>(oom_killer_allocWithZone));
761 } 870 }
762 871
763 } // namespace base 872 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698