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/sys_info.h" | 19 #include "base/sys_info.h" |
19 | 20 |
20 #if !defined(TASK_POWER_INFO) | 21 #if !defined(TASK_POWER_INFO) |
21 // Doesn't exist in the 10.6 or 10.7 SDKs. | 22 // Doesn't exist in the 10.6 or 10.7 SDKs. |
22 #define TASK_POWER_INFO 21 | 23 #define TASK_POWER_INFO 21 |
23 struct task_power_info { | 24 struct task_power_info { |
24 uint64_t total_user; | 25 uint64_t total_user; |
25 uint64_t total_system; | 26 uint64_t total_system; |
26 uint64_t task_interrupt_wakeups; | 27 uint64_t task_interrupt_wakeups; |
27 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... |
72 } else if (type == CPU_TYPE_X86_64) { | 73 } else if (type == CPU_TYPE_X86_64) { |
73 return addr >= SHARED_REGION_BASE_X86_64 && | 74 return addr >= SHARED_REGION_BASE_X86_64 && |
74 addr < (SHARED_REGION_BASE_X86_64 + SHARED_REGION_SIZE_X86_64); | 75 addr < (SHARED_REGION_BASE_X86_64 + SHARED_REGION_SIZE_X86_64); |
75 } else { | 76 } else { |
76 return false; | 77 return false; |
77 } | 78 } |
78 } | 79 } |
79 | 80 |
80 } // namespace | 81 } // namespace |
81 | 82 |
82 SystemMemoryInfoKB::SystemMemoryInfoKB() { | 83 SystemMemoryInfoKB::SystemMemoryInfoKB() : total(0), free(0) {} |
83 total = 0; | |
84 free = 0; | |
85 } | |
86 | 84 |
87 SystemMemoryInfoKB::SystemMemoryInfoKB(const SystemMemoryInfoKB& other) = | 85 SystemMemoryInfoKB::SystemMemoryInfoKB(const SystemMemoryInfoKB& other) = |
88 default; | 86 default; |
89 | 87 |
90 // 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 |
91 // 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 |
92 // 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 |
93 // call). Child processes ipc their port, so return something if available, | 91 // call). Child processes ipc their port, so return something if available, |
94 // otherwise return 0. | 92 // otherwise return 0. |
95 | 93 |
96 // static | 94 // static |
97 ProcessMetrics* ProcessMetrics::CreateProcessMetrics( | 95 std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics( |
98 ProcessHandle process, | 96 ProcessHandle process, |
99 PortProvider* port_provider) { | 97 PortProvider* port_provider) { |
100 return new ProcessMetrics(process, port_provider); | 98 return WrapUnique(new ProcessMetrics(process, port_provider)); |
101 } | 99 } |
102 | 100 |
103 size_t ProcessMetrics::GetPagefileUsage() const { | 101 size_t ProcessMetrics::GetPagefileUsage() const { |
104 task_basic_info_64 task_info_data; | 102 task_basic_info_64 task_info_data; |
105 if (!GetTaskInfo(TaskForPid(process_), &task_info_data)) | 103 if (!GetTaskInfo(TaskForPid(process_), &task_info_data)) |
106 return 0; | 104 return 0; |
107 return task_info_data.virtual_size; | 105 return task_info_data.virtual_size; |
108 } | 106 } |
109 | 107 |
110 size_t ProcessMetrics::GetPeakPagefileUsage() const { | 108 size_t ProcessMetrics::GetPeakPagefileUsage() const { |
(...skipping 27 matching lines...) Expand all Loading... |
138 DLOG(ERROR) << "Invalid process"; | 136 DLOG(ERROR) << "Invalid process"; |
139 return false; | 137 return false; |
140 } | 138 } |
141 | 139 |
142 cpu_type_t cpu_type; | 140 cpu_type_t cpu_type; |
143 if (!GetCPUTypeForProcess(process_, &cpu_type)) | 141 if (!GetCPUTypeForProcess(process_, &cpu_type)) |
144 return false; | 142 return false; |
145 | 143 |
146 // The same region can be referenced multiple times. To avoid double counting | 144 // The same region can be referenced multiple times. To avoid double counting |
147 // we need to keep track of which regions we've already counted. | 145 // we need to keep track of which regions we've already counted. |
148 base::hash_set<int> seen_objects; | 146 hash_set<int> seen_objects; |
149 | 147 |
150 // We iterate through each VM region in the task's address map. For shared | 148 // We iterate through each VM region in the task's address map. For shared |
151 // memory we add up all the pages that are marked as shared. Like libtop we | 149 // memory we add up all the pages that are marked as shared. Like libtop we |
152 // try to avoid counting pages that are also referenced by other tasks. Since | 150 // try to avoid counting pages that are also referenced by other tasks. Since |
153 // we don't have access to the VM regions of other tasks the only hint we have | 151 // we don't have access to the VM regions of other tasks the only hint we have |
154 // is if the address is in the shared region area. | 152 // is if the address is in the shared region area. |
155 // | 153 // |
156 // Private memory is much simpler. We simply count the pages that are marked | 154 // Private memory is much simpler. We simply count the pages that are marked |
157 // as private or copy on write (COW). | 155 // as private or copy on write (COW). |
158 // | 156 // |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 return false; | 388 return false; |
391 } | 389 } |
392 | 390 |
393 meminfo->free = static_cast<int>( | 391 meminfo->free = static_cast<int>( |
394 (vm_info.free_count - vm_info.speculative_count) * PAGE_SIZE / 1024); | 392 (vm_info.free_count - vm_info.speculative_count) * PAGE_SIZE / 1024); |
395 | 393 |
396 return true; | 394 return true; |
397 } | 395 } |
398 | 396 |
399 } // namespace base | 397 } // namespace base |
OLD | NEW |