OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ios/chrome/browser/memory/memory_metrics.h" | |
6 | |
7 #include <mach/mach.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/process/process_handle.h" | |
11 #include "base/process/process_metrics.h" | |
12 | |
13 #ifdef ARCH_CPU_64_BITS | |
14 #define cr_vm_region vm_region_64 | |
15 #else | |
16 #define cr_vm_region vm_region | |
17 #endif | |
18 | |
19 namespace { | |
20 // The number of pages returned by host_statistics and vm_region are a count | |
21 // of pages of 4096 bytes even when running on arm64 but the constants that | |
22 // are exposed (vm_page_size, VM_PAGE_SIZE, host_page_size) are all equals to | |
23 // 16384 bytes. So we define our own constant here to convert from page count | |
24 // to bytes. | |
25 const uint64_t kVMPageSize = 4096; | |
26 } | |
27 | |
28 namespace memory_util { | |
29 | |
30 uint64 GetFreePhysicalBytes() { | |
31 vm_statistics_data_t vmstat; | |
32 mach_msg_type_number_t count = HOST_VM_INFO_COUNT; | |
33 kern_return_t result = host_statistics(mach_host_self(), HOST_VM_INFO, | |
34 (host_info_t)&vmstat, &count); | |
sdefresne
2015/04/03 08:21:49
style: C-style cast are forbidden, use static_cast
droger
2015/04/03 09:20:22
I had to use reinterpret_cast everywhere, as the p
| |
35 if (result != KERN_SUCCESS) { | |
36 LOG(ERROR) << "Calling host_statistics failed."; | |
37 return 0; | |
38 } | |
39 return vmstat.free_count * kVMPageSize; | |
40 } | |
41 | |
42 uint64 GetRealMemoryUsedInBytes() { | |
43 base::ProcessHandle process_handle = base::GetCurrentProcessHandle(); | |
44 scoped_ptr<base::ProcessMetrics> process_metrics( | |
45 base::ProcessMetrics::CreateProcessMetrics(process_handle)); | |
46 return static_cast<uint64>(process_metrics->GetWorkingSetSize()); | |
47 } | |
48 | |
49 uint64 GetDirtyVMBytes() { | |
50 // Iterate over all VM regions and sum their dirty pages. | |
51 unsigned int total_dirty_pages = 0; | |
52 vm_size_t vm_size = 0; | |
53 kern_return_t result; | |
54 for (vm_address_t address = MACH_VM_MIN_ADDRESS;; address += vm_size) { | |
55 vm_region_extended_info_data_t info; | |
56 mach_msg_type_number_t info_count = VM_REGION_EXTENDED_INFO_COUNT; | |
57 mach_port_t object_name; | |
58 result = cr_vm_region(mach_task_self(), &address, &vm_size, | |
59 VM_REGION_EXTENDED_INFO, (vm_region_info_t)&info, | |
sdefresne
2015/04/03 08:21:49
style: C-style cast are forbidden, use static_cast
| |
60 &info_count, &object_name); | |
61 if (result == KERN_INVALID_ADDRESS) { | |
62 // The end of the address space has been reached. | |
63 break; | |
64 } else if (result != KERN_SUCCESS) { | |
65 LOG(ERROR) << "Calling vm_region failed with code: " << result; | |
66 break; | |
67 } else { | |
68 total_dirty_pages += info.pages_dirtied; | |
69 } | |
70 } | |
71 return total_dirty_pages * kVMPageSize; | |
72 } | |
73 | |
74 uint64 GetInternalVMBytes() { | |
75 task_vm_info_data_t task_vm_info; | |
76 mach_msg_type_number_t count = TASK_VM_INFO_COUNT; | |
77 kern_return_t result = task_info(mach_task_self(), TASK_VM_INFO, | |
78 (task_info_t)&task_vm_info, &count); | |
sdefresne
2015/04/03 08:21:49
style: C-style cast are forbidden, use static_cast
| |
79 if (result != KERN_SUCCESS) { | |
80 LOG(ERROR) << "Calling task_info failed."; | |
81 return 0; | |
82 } | |
83 | |
84 return static_cast<uint64>(task_vm_info.internal); | |
85 } | |
86 | |
87 } // namespace memory_util | |
OLD | NEW |