Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/task.h> | 13 #include <mach/task.h> |
| 14 #include <mach/mach_vm.h> | |
|
viettrungluu
2010/12/21 04:21:06
Nit: alphabetical order (see style guide).
sail
2010/12/21 18:15:33
Done.
| |
| 15 #include <mach/shared_region.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> |
| 27 #include <map> | |
|
viettrungluu
2010/12/21 04:21:06
Ditto.
sail
2010/12/21 18:15:33
Done.
| |
| 25 | 28 |
| 26 #include "base/debug/debugger.h" | 29 #include "base/debug/debugger.h" |
| 27 #include "base/eintr_wrapper.h" | 30 #include "base/eintr_wrapper.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" |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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* cpuType) { |
|
viettrungluu
2010/12/21 04:21:06
Nit: This is a C++-ish function, so the parameter
sail
2010/12/21 18:15:33
Done.
| |
| 239 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, | 242 size_t len = sizeof(*cpuType); |
| 240 size_t* shared_bytes) { | 243 int result = sysctlbyname("sysctl.proc_cputype", |
| 241 if (private_bytes) | 244 cpuType, |
| 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 " | |
| 250 << strerror(errno); | |
| 251 return false; | |
| 252 } | |
| 253 | |
| 245 return true; | 254 return true; |
| 246 } | 255 } |
| 247 | 256 |
| 257 // From libtop in_shared_region(). | |
|
viettrungluu
2010/12/21 04:21:06
If this is basically copied, then we need to do so
sail
2010/12/21 18:15:33
Implemented a very simple version that just handle
| |
| 258 static bool IsAddressInSharedRegion(mach_vm_address_t addr, cpu_type_t type) { | |
| 259 mach_vm_address_t base = 0, size = 0; | |
|
viettrungluu
2010/12/21 04:21:06
Nit: We don't declare multiple variables in the sa
| |
| 260 | |
| 261 switch(type) { | |
| 262 case CPU_TYPE_ARM: | |
| 263 base = SHARED_REGION_BASE_ARM; | |
|
viettrungluu
2010/12/21 04:21:06
Nit: Indentation.
sail
2010/12/21 18:15:33
Done.
shaneelaticejohnson
2012/05/26 05:06:43
Done.
| |
| 264 size = SHARED_REGION_SIZE_ARM; | |
| 265 break; | |
| 266 | |
| 267 case CPU_TYPE_X86_64: | |
| 268 base = SHARED_REGION_BASE_X86_64; | |
| 269 size = SHARED_REGION_SIZE_X86_64; | |
| 270 break; | |
| 271 | |
| 272 case CPU_TYPE_I386: | |
| 273 base = SHARED_REGION_BASE_I386; | |
| 274 size = SHARED_REGION_SIZE_I386; | |
| 275 break; | |
| 276 | |
| 277 case CPU_TYPE_POWERPC: | |
| 278 base = SHARED_REGION_BASE_PPC; | |
| 279 size = SHARED_REGION_SIZE_PPC; | |
| 280 break; | |
| 281 | |
| 282 case CPU_TYPE_POWERPC64: | |
| 283 base = SHARED_REGION_BASE_PPC64; | |
| 284 size = SHARED_REGION_SIZE_PPC64; | |
| 285 break; | |
| 286 | |
| 287 default: | |
| 288 // Unknown CPU type. | |
| 289 break; | |
| 290 } | |
| 291 | |
| 292 return(addr >= base && addr < (base + size)); | |
|
viettrungluu
2010/12/21 04:21:06
Nit: No parentheses (and even if you felt they wer
sail
2010/12/21 18:15:33
Done.
| |
| 293 } | |
| 294 | |
| 295 // This is a rough approximation of the algorithm that libtop uses. | |
| 296 // private_bytes is the size of private resident memory. | |
| 297 // shared_bytes is the size of shared resident memory. | |
| 298 // Note, libtop doesn't include shared memory that is referenced by other | |
| 299 // process but we do since we only look at a single process at a time. | |
| 300 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, | |
| 301 size_t* shared_bytes) { | |
| 302 kern_return_t kr; | |
| 303 | |
| 304 mach_port_t task = TaskForPid(process_); | |
| 305 if (task == MACH_PORT_NULL) { | |
| 306 LOG(ERROR) << "Invalid process"; | |
| 307 return false; | |
| 308 } | |
| 309 | |
| 310 vm_size_t pageSize; | |
|
viettrungluu
2010/12/21 04:21:06
Nit: |page_size|.
sail
2010/12/21 18:15:33
Done.
| |
| 311 kr = host_page_size(task, &pageSize); | |
| 312 if (kr != KERN_SUCCESS) { | |
| 313 LOG(ERROR) << "Failed to fetch host page size, error: " | |
| 314 << mach_error_string(kr); | |
| 315 return false; | |
| 316 } | |
| 317 | |
| 318 cpu_type_t cpuType; | |
|
viettrungluu
2010/12/21 04:21:06
Ditto, etc. for other variables.
sail
2010/12/21 18:15:33
Done.
| |
| 319 if (!GetCPUTypeForProcess(process_, &cpuType)) | |
| 320 return false; | |
| 321 | |
| 322 // The same region can be reference multiple times. To avoid double counting | |
|
viettrungluu
2010/12/21 04:21:06
reference*d*
sail
2010/12/21 18:15:33
Done.
| |
| 323 // we need to keep track of which regions we've already counted. | |
| 324 std::map<int, bool> objectMap; | |
|
viettrungluu
2010/12/21 04:21:06
I wonder if you shouldn't be using base::hash_map
sail
2010/12/21 18:15:33
Fixed, used base::hash_set.
| |
| 325 | |
| 326 mach_vm_size_t size = 0; | |
| 327 for (mach_vm_address_t address = MACH_VM_MIN_ADDRESS; ; address += size) { | |
|
viettrungluu
2010/12/21 04:21:06
Nit: No space after first ';', I think.
sail
2010/12/21 18:15:33
Done.
| |
| 328 vm_region_top_info_data_t info; | |
| 329 mach_msg_type_number_t infoCount = VM_REGION_TOP_INFO_COUNT; | |
| 330 mach_port_t object_name; | |
| 331 kr = mach_vm_region( | |
| 332 task, | |
|
viettrungluu
2010/12/21 04:21:06
I believe you can align these with the '(' on the
sail
2010/12/21 18:15:33
Done.
| |
| 333 &address, | |
| 334 &size, | |
| 335 VM_REGION_TOP_INFO, | |
| 336 (vm_region_info_t)&info, | |
| 337 &infoCount, | |
| 338 &object_name); | |
| 339 if (kr == KERN_INVALID_ADDRESS) { | |
| 340 // We're at the end of the address space. | |
| 341 break; | |
| 342 } else if (kr != KERN_SUCCESS) { | |
| 343 LOG(ERROR) << "Calling mach_vm_region failed with error: " | |
| 344 << mach_error_string(kr); | |
| 345 return false; | |
| 346 } | |
| 347 | |
| 348 if (IsAddressInSharedRegion(address, cpuType) && | |
| 349 info.share_mode != SM_PRIVATE) | |
| 350 continue; | |
| 351 | |
| 352 if (info.share_mode == SM_COW && info.ref_count == 1) | |
| 353 info.share_mode = SM_PRIVATE; | |
| 354 | |
| 355 switch (info.share_mode) { | |
| 356 case SM_PRIVATE: | |
| 357 if (private_bytes) { | |
|
viettrungluu
2010/12/21 04:21:06
I think you may as well just calculate these (alwa
sail
2010/12/21 18:15:33
Done.
| |
| 358 *private_bytes += info.private_pages_resident * pageSize; | |
| 359 *private_bytes += info.shared_pages_resident * pageSize; | |
| 360 } | |
| 361 break; | |
| 362 case SM_COW: | |
| 363 case SM_SHARED: | |
| 364 if (objectMap.find(info.obj_id) == objectMap.end()) { | |
| 365 // Only count the first reference to this region. | |
| 366 objectMap[info.obj_id] = true; | |
| 367 if (shared_bytes) | |
|
viettrungluu
2010/12/21 04:21:06
Ditto.
sail
2010/12/21 18:15:33
Done.
| |
| 368 *shared_bytes += info.shared_pages_resident * pageSize; | |
| 369 } | |
| 370 if (info.share_mode == SM_COW) { | |
|
viettrungluu
2010/12/21 04:21:06
This might be nicer using a fall-through (explicit
sail
2010/12/21 18:15:33
Fixed. The C++ style guide doesn't mention fall th
| |
| 371 if (private_bytes) | |
| 372 *private_bytes += info.private_pages_resident * pageSize; | |
| 373 } | |
| 374 break; | |
| 375 default: | |
| 376 break; | |
| 377 } | |
| 378 } | |
| 379 return true; | |
| 380 } | |
| 381 | |
| 248 void ProcessMetrics::GetCommittedKBytes(CommittedKBytes* usage) const { | 382 void ProcessMetrics::GetCommittedKBytes(CommittedKBytes* usage) const { |
| 249 } | 383 } |
| 250 | 384 |
| 251 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { | 385 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { |
| 252 size_t priv = GetWorkingSetSize(); | 386 size_t priv = GetWorkingSetSize(); |
| 253 if (!priv) | 387 if (!priv) |
| 254 return false; | 388 return false; |
| 255 ws_usage->priv = priv / 1024; | 389 ws_usage->priv = priv / 1024; |
| 256 ws_usage->shareable = 0; | 390 ws_usage->shareable = 0; |
| 257 ws_usage->shared = 0; | 391 ws_usage->shared = 0; |
| (...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 754 @selector(allocWithZone:)); | 888 @selector(allocWithZone:)); |
| 755 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>( | 889 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>( |
| 756 method_getImplementation(orig_method)); | 890 method_getImplementation(orig_method)); |
| 757 CHECK(g_old_allocWithZone) | 891 CHECK(g_old_allocWithZone) |
| 758 << "Failed to get allocWithZone allocation function."; | 892 << "Failed to get allocWithZone allocation function."; |
| 759 method_setImplementation(orig_method, | 893 method_setImplementation(orig_method, |
| 760 reinterpret_cast<IMP>(oom_killer_allocWithZone)); | 894 reinterpret_cast<IMP>(oom_killer_allocWithZone)); |
| 761 } | 895 } |
| 762 | 896 |
| 763 } // namespace base | 897 } // namespace base |
| OLD | NEW |