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