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

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 PLOG(ERROR) << "sysctlbyname(""sysctl.proc_cputype"")";
250 return false;
251 }
252
245 return true; 253 return true;
246 } 254 }
247 255
256 static bool IsAddressInSharedRegion(mach_vm_address_t addr, cpu_type_t type) {
257 if (type == CPU_TYPE_I386)
258 return addr >= SHARED_REGION_BASE_I386 &&
259 addr < (SHARED_REGION_BASE_I386 + SHARED_REGION_SIZE_I386);
260 else if (type == CPU_TYPE_X86_64)
261 return addr >= SHARED_REGION_BASE_X86_64 &&
262 addr < (SHARED_REGION_BASE_X86_64 + SHARED_REGION_SIZE_X86_64);
263 else
264 return false;
265 }
266
267 // This is a rough approximation of the algorithm that libtop uses.
268 // private_bytes is the size of private resident memory.
269 // shared_bytes is the size of shared resident memory.
270 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes,
271 size_t* shared_bytes) {
272 kern_return_t kr;
273 size_t private_pages_count = 0;
274 size_t shared_pages_count = 0;
275
276 if (!private_bytes && !shared_bytes)
277 return true;
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 // We iterate through each VM region in the task's address map. For shared
294 // memory we add up all the pages that are marked as shared. Like libtop we
295 // try to avoid counting pages that are also referenced by other tasks. Since
296 // we don't have access to the VM regions of other tasks the only hint we have
297 // is if the address is in the shared region area.
298 //
299 // Private memory is much simpler. We simply count the pages that are marked
300 // as private or copy on write (COW).
301 //
302 // See libtop_update_vm_regions in
303 // http://www.opensource.apple.com/source/top/top-67/libtop.c
304 mach_vm_size_t size = 0;
305 for (mach_vm_address_t address = MACH_VM_MIN_ADDRESS;; address += size) {
306 vm_region_top_info_data_t info;
307 mach_msg_type_number_t info_count = VM_REGION_TOP_INFO_COUNT;
308 mach_port_t object_name;
309 kr = mach_vm_region(task,
310 &address,
311 &size,
312 VM_REGION_TOP_INFO,
313 (vm_region_info_t)&info,
314 &info_count,
315 &object_name);
316 if (kr == KERN_INVALID_ADDRESS) {
317 // We're at the end of the address space.
318 break;
319 } else if (kr != KERN_SUCCESS) {
320 LOG(ERROR) << "Calling mach_vm_region failed with error: "
321 << mach_error_string(kr);
322 return false;
323 }
324
325 if (IsAddressInSharedRegion(address, cpu_type) &&
326 info.share_mode != SM_PRIVATE)
327 continue;
328
329 if (info.share_mode == SM_COW && info.ref_count == 1)
330 info.share_mode = SM_PRIVATE;
331
332 switch (info.share_mode) {
333 case SM_PRIVATE:
334 private_pages_count += info.private_pages_resident;
335 private_pages_count += info.shared_pages_resident;
336 break;
337 case SM_COW:
338 private_pages_count += info.private_pages_resident;
339 // Fall through
340 case SM_SHARED:
341 if (seen_objects.count(info.obj_id) == 0) {
342 // Only count the first reference to this region.
343 seen_objects.insert(info.obj_id);
344 shared_pages_count += info.shared_pages_resident;
345 }
346 break;
347 default:
348 break;
349 }
350 }
351
352 vm_size_t page_size;
353 kr = host_page_size(task, &page_size);
354 if (kr != KERN_SUCCESS) {
355 LOG(ERROR) << "Failed to fetch host page size, error: "
356 << mach_error_string(kr);
357 return false;
358 }
359
360 if (private_bytes)
361 *private_bytes = private_pages_count * page_size;
362 if (shared_bytes)
363 *shared_bytes = shared_pages_count * page_size;
364
365 return true;
366 }
367
248 void ProcessMetrics::GetCommittedKBytes(CommittedKBytes* usage) const { 368 void ProcessMetrics::GetCommittedKBytes(CommittedKBytes* usage) const {
249 } 369 }
250 370
251 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { 371 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const {
252 size_t priv = GetWorkingSetSize(); 372 size_t priv = GetWorkingSetSize();
253 if (!priv) 373 if (!priv)
254 return false; 374 return false;
255 ws_usage->priv = priv / 1024; 375 ws_usage->priv = priv / 1024;
256 ws_usage->shareable = 0; 376 ws_usage->shareable = 0;
257 ws_usage->shared = 0; 377 ws_usage->shared = 0;
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 @selector(allocWithZone:)); 874 @selector(allocWithZone:));
755 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>( 875 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>(
756 method_getImplementation(orig_method)); 876 method_getImplementation(orig_method));
757 CHECK(g_old_allocWithZone) 877 CHECK(g_old_allocWithZone)
758 << "Failed to get allocWithZone allocation function."; 878 << "Failed to get allocWithZone allocation function.";
759 method_setImplementation(orig_method, 879 method_setImplementation(orig_method,
760 reinterpret_cast<IMP>(oom_killer_allocWithZone)); 880 reinterpret_cast<IMP>(oom_killer_allocWithZone));
761 } 881 }
762 882
763 } // namespace base 883 } // 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