| OLD | NEW |
| 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> |
| 11 #include <stdint.h> | 11 #include <stdint.h> |
| 12 #include <sys/sysctl.h> | 12 #include <sys/sysctl.h> |
| 13 | 13 |
| 14 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/mac/mach_logging.h" | 16 #include "base/mac/mach_logging.h" |
| 17 #include "base/mac/scoped_mach_port.h" | 17 #include "base/mac/scoped_mach_port.h" |
| 18 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
| 19 #include "base/numerics/safe_conversions.h" |
| 19 #include "base/sys_info.h" | 20 #include "base/sys_info.h" |
| 20 | 21 |
| 21 #if !defined(TASK_POWER_INFO) | 22 #if !defined(TASK_POWER_INFO) |
| 22 // Doesn't exist in the 10.6 or 10.7 SDKs. | 23 // Doesn't exist in the 10.6 or 10.7 SDKs. |
| 23 #define TASK_POWER_INFO 21 | 24 #define TASK_POWER_INFO 21 |
| 24 struct task_power_info { | 25 struct task_power_info { |
| 25 uint64_t total_user; | 26 uint64_t total_user; |
| 26 uint64_t total_system; | 27 uint64_t total_system; |
| 27 uint64_t task_interrupt_wakeups; | 28 uint64_t task_interrupt_wakeups; |
| 28 uint64_t task_platform_idle_wakeups; | 29 uint64_t task_platform_idle_wakeups; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 } else if (type == CPU_TYPE_X86_64) { | 74 } else if (type == CPU_TYPE_X86_64) { |
| 74 return addr >= SHARED_REGION_BASE_X86_64 && | 75 return addr >= SHARED_REGION_BASE_X86_64 && |
| 75 addr < (SHARED_REGION_BASE_X86_64 + SHARED_REGION_SIZE_X86_64); | 76 addr < (SHARED_REGION_BASE_X86_64 + SHARED_REGION_SIZE_X86_64); |
| 76 } else { | 77 } else { |
| 77 return false; | 78 return false; |
| 78 } | 79 } |
| 79 } | 80 } |
| 80 | 81 |
| 81 } // namespace | 82 } // namespace |
| 82 | 83 |
| 83 SystemMemoryInfoKB::SystemMemoryInfoKB() : total(0), free(0) {} | |
| 84 | |
| 85 SystemMemoryInfoKB::SystemMemoryInfoKB(const SystemMemoryInfoKB& other) = | |
| 86 default; | |
| 87 | |
| 88 // Getting a mach task from a pid for another process requires permissions in | 84 // Getting a mach task from a pid for another process requires permissions in |
| 89 // general, so there doesn't really seem to be a way to do these (and spinning | 85 // general, so there doesn't really seem to be a way to do these (and spinning |
| 90 // up ps to fetch each stats seems dangerous to put in a base api for anyone to | 86 // up ps to fetch each stats seems dangerous to put in a base api for anyone to |
| 91 // call). Child processes ipc their port, so return something if available, | 87 // call). Child processes ipc their port, so return something if available, |
| 92 // otherwise return 0. | 88 // otherwise return 0. |
| 93 | 89 |
| 94 // static | 90 // static |
| 95 std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics( | 91 std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics( |
| 96 ProcessHandle process, | 92 ProcessHandle process, |
| 97 PortProvider* port_provider) { | 93 PortProvider* port_provider) { |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 reinterpret_cast<host_info_t>(&data), | 366 reinterpret_cast<host_info_t>(&data), |
| 371 &count); | 367 &count); |
| 372 if (kr != KERN_SUCCESS) { | 368 if (kr != KERN_SUCCESS) { |
| 373 MACH_DLOG(WARNING, kr) << "host_statistics"; | 369 MACH_DLOG(WARNING, kr) << "host_statistics"; |
| 374 return 0; | 370 return 0; |
| 375 } | 371 } |
| 376 | 372 |
| 377 return (data.active_count * PAGE_SIZE) / 1024; | 373 return (data.active_count * PAGE_SIZE) / 1024; |
| 378 } | 374 } |
| 379 | 375 |
| 380 // On Mac, We only get total memory and free memory from the system. | |
| 381 bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) { | 376 bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) { |
| 382 struct host_basic_info hostinfo; | 377 struct host_basic_info hostinfo; |
| 383 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; | 378 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; |
| 384 base::mac::ScopedMachSendRight host(mach_host_self()); | 379 base::mac::ScopedMachSendRight host(mach_host_self()); |
| 385 int result = host_info(host.get(), HOST_BASIC_INFO, | 380 int result = host_info(host.get(), HOST_BASIC_INFO, |
| 386 reinterpret_cast<host_info_t>(&hostinfo), &count); | 381 reinterpret_cast<host_info_t>(&hostinfo), &count); |
| 387 if (result != KERN_SUCCESS) | 382 if (result != KERN_SUCCESS) |
| 388 return false; | 383 return false; |
| 389 | 384 |
| 390 DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); | 385 DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); |
| 391 meminfo->total = static_cast<int>(hostinfo.max_mem / 1024); | 386 meminfo->total = static_cast<int>(hostinfo.max_mem / 1024); |
| 392 | 387 |
| 393 vm_statistics_data_t vm_info; | 388 vm_statistics64_data_t vm_info; |
| 394 count = HOST_VM_INFO_COUNT; | 389 count = HOST_VM_INFO64_COUNT; |
| 395 | 390 |
| 396 if (host_statistics(host.get(), HOST_VM_INFO, | 391 if (host_statistics64(host.get(), HOST_VM_INFO64, |
| 397 reinterpret_cast<host_info_t>(&vm_info), | 392 reinterpret_cast<host_info64_t>(&vm_info), |
| 398 &count) != KERN_SUCCESS) { | 393 &count) != KERN_SUCCESS) { |
| 399 return false; | 394 return false; |
| 400 } | 395 } |
| 396 DCHECK_EQ(HOST_VM_INFO64_COUNT, count); |
| 401 | 397 |
| 402 meminfo->free = static_cast<int>( | 398 static_assert(PAGE_SIZE % 1024 == 0, "Invalid page size"); |
| 403 (vm_info.free_count - vm_info.speculative_count) * PAGE_SIZE / 1024); | 399 meminfo->free = saturated_cast<int>( |
| 400 PAGE_SIZE / 1024 * (vm_info.free_count - vm_info.speculative_count)); |
| 401 meminfo->speculative = |
| 402 saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.speculative_count); |
| 403 meminfo->file_backed = |
| 404 saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.external_page_count); |
| 405 meminfo->purgeable = |
| 406 saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.purgeable_count); |
| 404 | 407 |
| 405 return true; | 408 return true; |
| 406 } | 409 } |
| 407 | 410 |
| 408 } // namespace base | 411 } // namespace base |
| OLD | NEW |