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

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 //
271 // We iterate through each VM region in the task's address map. For shared
Nico 2010/12/21 19:55:07 ubernit: i'd move the implementation description (
sail 2010/12/21 19:58:08 Done.
272 // memory we add up all the pages that are marked as shared.Like libtop we try
Nico 2010/12/21 19:55:07 space after period
sail 2010/12/21 19:58:08 Done.
273 // to avoid counting pages that are also referenced by other tasks. Since we
274 // don't have access to the VM regions of other tasks the only hint we have is
275 // if the address is in the shared region area.
276 //
277 // Private memory is much simpler. We simply count the pages that are marked as
278 // private or copy on write (COW).
279 //
280 // See libtop_update_vm_regions in
281 // http://www.opensource.apple.com/source/top/top-67/libtop.c
282 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes,
283 size_t* shared_bytes) {
284 kern_return_t kr;
285 size_t private_pages_count = 0;
286 size_t shared_pages_count = 0;
287
288 if (!private_bytes && !shared_bytes)
289 return true;
290
291 mach_port_t task = TaskForPid(process_);
292 if (task == MACH_PORT_NULL) {
293 LOG(ERROR) << "Invalid process";
294 return false;
295 }
296
297 cpu_type_t cpu_type;
298 if (!GetCPUTypeForProcess(process_, &cpu_type))
299 return false;
300
301 // The same region can be referenced multiple times. To avoid double counting
302 // we need to keep track of which regions we've already counted.
303 base::hash_set<int> seen_objects;
304
305 mach_vm_size_t size = 0;
306 for (mach_vm_address_t address = MACH_VM_MIN_ADDRESS;; address += size) {
307 vm_region_top_info_data_t info;
308 mach_msg_type_number_t info_count = VM_REGION_TOP_INFO_COUNT;
309 mach_port_t object_name;
310 kr = mach_vm_region(task,
311 &address,
312 &size,
313 VM_REGION_TOP_INFO,
314 (vm_region_info_t)&info,
315 &info_count,
316 &object_name);
317 if (kr == KERN_INVALID_ADDRESS) {
318 // We're at the end of the address space.
319 break;
320 } else if (kr != KERN_SUCCESS) {
321 LOG(ERROR) << "Calling mach_vm_region failed with error: "
322 << mach_error_string(kr);
323 return false;
324 }
325
326 if (IsAddressInSharedRegion(address, cpu_type) &&
327 info.share_mode != SM_PRIVATE)
328 continue;
329
330 if (info.share_mode == SM_COW && info.ref_count == 1)
331 info.share_mode = SM_PRIVATE;
332
333 switch (info.share_mode) {
334 case SM_PRIVATE:
335 private_pages_count += info.private_pages_resident;
336 private_pages_count += info.shared_pages_resident;
337 break;
338 case SM_COW:
339 private_pages_count += info.private_pages_resident;
340 // Fall through
341 case SM_SHARED:
342 if (seen_objects.count(info.obj_id) == 0) {
343 // Only count the first reference to this region.
344 seen_objects.insert(info.obj_id);
345 shared_pages_count += info.shared_pages_resident;
346 }
347 break;
348 default:
349 break;
350 }
351 }
352
353 vm_size_t page_size;
354 kr = host_page_size(task, &page_size);
355 if (kr != KERN_SUCCESS) {
356 LOG(ERROR) << "Failed to fetch host page size, error: "
357 << mach_error_string(kr);
358 return false;
359 }
360
361 if (private_bytes)
362 *private_bytes = private_pages_count * page_size;
363 if (shared_bytes)
364 *shared_bytes = shared_pages_count * page_size;
365
366 return true;
367 }
368
248 void ProcessMetrics::GetCommittedKBytes(CommittedKBytes* usage) const { 369 void ProcessMetrics::GetCommittedKBytes(CommittedKBytes* usage) const {
249 } 370 }
250 371
251 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { 372 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const {
252 size_t priv = GetWorkingSetSize(); 373 size_t priv = GetWorkingSetSize();
253 if (!priv) 374 if (!priv)
254 return false; 375 return false;
255 ws_usage->priv = priv / 1024; 376 ws_usage->priv = priv / 1024;
256 ws_usage->shareable = 0; 377 ws_usage->shareable = 0;
257 ws_usage->shared = 0; 378 ws_usage->shared = 0;
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 @selector(allocWithZone:)); 875 @selector(allocWithZone:));
755 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>( 876 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>(
756 method_getImplementation(orig_method)); 877 method_getImplementation(orig_method));
757 CHECK(g_old_allocWithZone) 878 CHECK(g_old_allocWithZone)
758 << "Failed to get allocWithZone allocation function."; 879 << "Failed to get allocWithZone allocation function.";
759 method_setImplementation(orig_method, 880 method_setImplementation(orig_method,
760 reinterpret_cast<IMP>(oom_killer_allocWithZone)); 881 reinterpret_cast<IMP>(oom_killer_allocWithZone));
761 } 882 }
762 883
763 } // namespace base 884 } // 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