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> |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 return addr >= SHARED_REGION_BASE_I386 && | 72 return addr >= SHARED_REGION_BASE_I386 && |
73 addr < (SHARED_REGION_BASE_I386 + SHARED_REGION_SIZE_I386); | 73 addr < (SHARED_REGION_BASE_I386 + SHARED_REGION_SIZE_I386); |
74 } else if (type == CPU_TYPE_X86_64) { | 74 } else if (type == CPU_TYPE_X86_64) { |
75 return addr >= SHARED_REGION_BASE_X86_64 && | 75 return addr >= SHARED_REGION_BASE_X86_64 && |
76 addr < (SHARED_REGION_BASE_X86_64 + SHARED_REGION_SIZE_X86_64); | 76 addr < (SHARED_REGION_BASE_X86_64 + SHARED_REGION_SIZE_X86_64); |
77 } else { | 77 } else { |
78 return false; | 78 return false; |
79 } | 79 } |
80 } | 80 } |
81 | 81 |
| 82 enum MachVMRegionResult { Finished, Error, Success }; |
| 83 |
| 84 // Both |size| and |address| are in-out parameters. |
| 85 // |info| is an output parameter, only valid on Success. |
| 86 MachVMRegionResult GetTopInfo(mach_port_t task, |
| 87 mach_vm_size_t* size, |
| 88 mach_vm_address_t* address, |
| 89 vm_region_top_info_data_t* info) { |
| 90 mach_msg_type_number_t info_count = VM_REGION_TOP_INFO_COUNT; |
| 91 mach_port_t object_name; |
| 92 kern_return_t kr = mach_vm_region(task, address, size, VM_REGION_TOP_INFO, |
| 93 reinterpret_cast<vm_region_info_t>(info), |
| 94 &info_count, &object_name); |
| 95 // We're at the end of the address space. |
| 96 if (kr == KERN_INVALID_ADDRESS) |
| 97 return Finished; |
| 98 |
| 99 if (kr != KERN_SUCCESS) |
| 100 return Error; |
| 101 |
| 102 // The kernel always returns a null object for VM_REGION_TOP_INFO, but |
| 103 // balance it with a deallocate in case this ever changes. See 10.9.2 |
| 104 // xnu-2422.90.20/osfmk/vm/vm_map.c vm_map_region. |
| 105 mach_port_deallocate(task, object_name); |
| 106 return Success; |
| 107 } |
| 108 |
| 109 MachVMRegionResult GetBasicInfo(mach_port_t task, |
| 110 mach_vm_size_t* size, |
| 111 mach_vm_address_t* address, |
| 112 vm_region_basic_info_64* info) { |
| 113 mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT_64; |
| 114 mach_port_t object_name; |
| 115 kern_return_t kr = mach_vm_region( |
| 116 task, address, size, VM_REGION_BASIC_INFO_64, |
| 117 reinterpret_cast<vm_region_info_t>(info), &info_count, &object_name); |
| 118 if (kr == KERN_INVALID_ADDRESS) { |
| 119 // We're at the end of the address space. |
| 120 return Finished; |
| 121 } else if (kr != KERN_SUCCESS) { |
| 122 return Error; |
| 123 } |
| 124 |
| 125 // The kernel always returns a null object for VM_REGION_BASIC_INFO_64, but |
| 126 // balance it with a deallocate in case this ever changes. See 10.9.2 |
| 127 // xnu-2422.90.20/osfmk/vm/vm_map.c vm_map_region. |
| 128 mach_port_deallocate(task, object_name); |
| 129 return Success; |
| 130 } |
| 131 |
82 } // namespace | 132 } // namespace |
83 | 133 |
84 // Getting a mach task from a pid for another process requires permissions in | 134 // 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 | 135 // 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 | 136 // 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, | 137 // call). Child processes ipc their port, so return something if available, |
88 // otherwise return 0. | 138 // otherwise return 0. |
89 | 139 |
90 // static | 140 // static |
91 std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics( | 141 std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics( |
92 ProcessHandle process, | 142 ProcessHandle process, |
93 PortProvider* port_provider) { | 143 PortProvider* port_provider) { |
94 return WrapUnique(new ProcessMetrics(process, port_provider)); | 144 return WrapUnique(new ProcessMetrics(process, port_provider)); |
95 } | 145 } |
96 | 146 |
97 size_t ProcessMetrics::GetPagefileUsage() const { | 147 size_t ProcessMetrics::GetPagefileUsage() const { |
98 task_basic_info_64 task_info_data; | 148 task_basic_info_64 task_info_data; |
99 if (!GetTaskInfo(TaskForPid(process_), &task_info_data)) | 149 if (!GetTaskInfo(TaskForPid(process_), &task_info_data)) |
100 return 0; | 150 return 0; |
101 return task_info_data.virtual_size; | 151 return task_info_data.virtual_size; |
102 } | 152 } |
103 | 153 |
104 size_t ProcessMetrics::GetPeakPagefileUsage() const { | 154 size_t ProcessMetrics::GetPeakPagefileUsage() const { |
105 return 0; | 155 return 0; |
106 } | 156 } |
107 | 157 |
108 size_t ProcessMetrics::GetWorkingSetSize() const { | 158 size_t ProcessMetrics::GetWorkingSetSize() const { |
109 size_t private_bytes = 0; | |
110 size_t shared_bytes = 0; | |
111 size_t resident_bytes = 0; | 159 size_t resident_bytes = 0; |
112 if (!GetMemoryBytes(&private_bytes, &shared_bytes, &resident_bytes)) | 160 if (!GetMemoryBytes(nullptr, nullptr, &resident_bytes, nullptr)) |
113 return 0; | 161 return 0; |
114 return resident_bytes; | 162 return resident_bytes; |
115 } | 163 } |
116 | 164 |
117 size_t ProcessMetrics::GetPeakWorkingSetSize() const { | 165 size_t ProcessMetrics::GetPeakWorkingSetSize() const { |
118 return 0; | 166 return 0; |
119 } | 167 } |
120 | 168 |
| 169 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, |
| 170 size_t* shared_bytes) const { |
| 171 return GetMemoryBytes(private_bytes, shared_bytes, nullptr, nullptr); |
| 172 } |
| 173 |
121 // This is a rough approximation of the algorithm that libtop uses. | 174 // This is a rough approximation of the algorithm that libtop uses. |
122 // private_bytes is the size of private resident memory. | 175 // private_bytes is the size of private resident memory. |
123 // shared_bytes is the size of shared resident memory. | 176 // shared_bytes is the size of shared resident memory. |
124 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, | 177 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, |
125 size_t* shared_bytes) const { | 178 size_t* shared_bytes, |
| 179 size_t* resident_bytes, |
| 180 size_t* locked_bytes) const { |
126 size_t private_pages_count = 0; | 181 size_t private_pages_count = 0; |
127 size_t shared_pages_count = 0; | 182 size_t shared_pages_count = 0; |
128 | 183 size_t wired_pages_count = 0; |
129 if (!private_bytes && !shared_bytes) | |
130 return true; | |
131 | 184 |
132 mach_port_t task = TaskForPid(process_); | 185 mach_port_t task = TaskForPid(process_); |
133 if (task == MACH_PORT_NULL) { | 186 if (task == MACH_PORT_NULL) { |
134 DLOG(ERROR) << "Invalid process"; | 187 DLOG(ERROR) << "Invalid process"; |
135 return false; | 188 return false; |
136 } | 189 } |
137 | 190 |
138 cpu_type_t cpu_type; | 191 cpu_type_t cpu_type; |
139 if (!GetCPUTypeForProcess(process_, &cpu_type)) | 192 if (!GetCPUTypeForProcess(process_, &cpu_type)) |
140 return false; | 193 return false; |
141 | 194 |
142 // The same region can be referenced multiple times. To avoid double counting | 195 // The same region can be referenced multiple times. To avoid double counting |
143 // we need to keep track of which regions we've already counted. | 196 // we need to keep track of which regions we've already counted. |
144 hash_set<int> seen_objects; | 197 hash_set<int> seen_objects; |
145 | 198 |
146 // We iterate through each VM region in the task's address map. For shared | 199 // We iterate through each VM region in the task's address map. For shared |
147 // memory we add up all the pages that are marked as shared. Like libtop we | 200 // memory we add up all the pages that are marked as shared. Like libtop we |
148 // try to avoid counting pages that are also referenced by other tasks. Since | 201 // try to avoid counting pages that are also referenced by other tasks. Since |
149 // we don't have access to the VM regions of other tasks the only hint we have | 202 // we don't have access to the VM regions of other tasks the only hint we have |
150 // is if the address is in the shared region area. | 203 // is if the address is in the shared region area. |
151 // | 204 // |
152 // Private memory is much simpler. We simply count the pages that are marked | 205 // Private memory is much simpler. We simply count the pages that are marked |
153 // as private or copy on write (COW). | 206 // as private or copy on write (COW). |
154 // | 207 // |
155 // See libtop_update_vm_regions in | 208 // See libtop_update_vm_regions in |
156 // http://www.opensource.apple.com/source/top/top-67/libtop.c | 209 // http://www.opensource.apple.com/source/top/top-67/libtop.c |
157 mach_vm_size_t size = 0; | 210 mach_vm_size_t size = 0; |
158 for (mach_vm_address_t address = MACH_VM_MIN_ADDRESS;; address += size) { | 211 for (mach_vm_address_t address = MACH_VM_MIN_ADDRESS;; address += size) { |
| 212 mach_vm_size_t size_copy = size; |
| 213 mach_vm_address_t address_copy = address; |
| 214 |
159 vm_region_top_info_data_t info; | 215 vm_region_top_info_data_t info; |
160 mach_msg_type_number_t info_count = VM_REGION_TOP_INFO_COUNT; | 216 MachVMRegionResult result = GetTopInfo(task, &size, &address, &info); |
161 mach_port_t object_name; | 217 if (result == Error) |
162 kern_return_t kr = mach_vm_region(task, | 218 return false; |
163 &address, | 219 if (result == Finished) |
164 &size, | |
165 VM_REGION_TOP_INFO, | |
166 reinterpret_cast<vm_region_info_t>(&info), | |
167 &info_count, | |
168 &object_name); | |
169 if (kr == KERN_INVALID_ADDRESS) { | |
170 // We're at the end of the address space. | |
171 break; | 220 break; |
172 } else if (kr != KERN_SUCCESS) { | 221 |
173 MACH_DLOG(ERROR, kr) << "mach_vm_region"; | 222 vm_region_basic_info_64 basic_info; |
174 return false; | 223 result = GetBasicInfo(task, &size_copy, &address_copy, &basic_info); |
| 224 switch (result) { |
| 225 case Finished: |
| 226 case Error: |
| 227 return false; |
| 228 case Success: |
| 229 break; |
175 } | 230 } |
176 | 231 bool is_wired = basic_info.user_wired_count > 0; |
177 // The kernel always returns a null object for VM_REGION_TOP_INFO, but | |
178 // balance it with a deallocate in case this ever changes. See 10.9.2 | |
179 // xnu-2422.90.20/osfmk/vm/vm_map.c vm_map_region. | |
180 mach_port_deallocate(mach_task_self(), object_name); | |
181 | 232 |
182 if (IsAddressInSharedRegion(address, cpu_type) && | 233 if (IsAddressInSharedRegion(address, cpu_type) && |
183 info.share_mode != SM_PRIVATE) | 234 info.share_mode != SM_PRIVATE) |
184 continue; | 235 continue; |
185 | 236 |
186 if (info.share_mode == SM_COW && info.ref_count == 1) | 237 if (info.share_mode == SM_COW && info.ref_count == 1) |
187 info.share_mode = SM_PRIVATE; | 238 info.share_mode = SM_PRIVATE; |
188 | 239 |
189 switch (info.share_mode) { | 240 switch (info.share_mode) { |
190 case SM_LARGE_PAGE: | 241 case SM_LARGE_PAGE: |
(...skipping 10 matching lines...) Expand all Loading... |
201 case SM_SHARED_ALIASED: | 252 case SM_SHARED_ALIASED: |
202 if (seen_objects.count(info.obj_id) == 0) { | 253 if (seen_objects.count(info.obj_id) == 0) { |
203 // Only count the first reference to this region. | 254 // Only count the first reference to this region. |
204 seen_objects.insert(info.obj_id); | 255 seen_objects.insert(info.obj_id); |
205 shared_pages_count += info.shared_pages_resident; | 256 shared_pages_count += info.shared_pages_resident; |
206 } | 257 } |
207 break; | 258 break; |
208 default: | 259 default: |
209 break; | 260 break; |
210 } | 261 } |
| 262 if (is_wired) { |
| 263 wired_pages_count += |
| 264 info.private_pages_resident + info.shared_pages_resident; |
| 265 } |
211 } | 266 } |
212 | 267 |
213 if (private_bytes) | 268 if (private_bytes) |
214 *private_bytes = private_pages_count * PAGE_SIZE; | 269 *private_bytes = private_pages_count * PAGE_SIZE; |
215 if (shared_bytes) | 270 if (shared_bytes) |
216 *shared_bytes = shared_pages_count * PAGE_SIZE; | 271 *shared_bytes = shared_pages_count * PAGE_SIZE; |
| 272 if (resident_bytes) |
| 273 *resident_bytes = (private_pages_count + shared_pages_count) * PAGE_SIZE; |
| 274 if (locked_bytes) |
| 275 *locked_bytes = wired_pages_count * PAGE_SIZE; |
217 | 276 |
218 return true; | 277 return true; |
219 } | 278 } |
220 | 279 |
221 void ProcessMetrics::GetCommittedKBytes(CommittedKBytes* usage) const { | 280 void ProcessMetrics::GetCommittedKBytes(CommittedKBytes* usage) const { |
222 WorkingSetKBytes unused; | 281 WorkingSetKBytes unused; |
223 if (!GetCommittedAndWorkingSetKBytes(usage, &unused)) { | 282 if (!GetCommittedAndWorkingSetKBytes(usage, &unused)) { |
224 *usage = CommittedKBytes(); | 283 *usage = CommittedKBytes(); |
225 } | 284 } |
226 } | 285 } |
(...skipping 14 matching lines...) Expand all Loading... |
241 usage->mapped = 0; | 300 usage->mapped = 0; |
242 usage->image = 0; | 301 usage->image = 0; |
243 | 302 |
244 ws_usage->priv = task_info_data.resident_size / 1024; | 303 ws_usage->priv = task_info_data.resident_size / 1024; |
245 ws_usage->shareable = 0; | 304 ws_usage->shareable = 0; |
246 ws_usage->shared = 0; | 305 ws_usage->shared = 0; |
247 | 306 |
248 return true; | 307 return true; |
249 } | 308 } |
250 | 309 |
251 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, | |
252 size_t* shared_bytes, | |
253 size_t* resident_bytes) const { | |
254 if (!GetMemoryBytes(private_bytes, shared_bytes)) | |
255 return false; | |
256 *resident_bytes = *private_bytes + *shared_bytes; | |
257 return true; | |
258 } | |
259 | |
260 #define TIME_VALUE_TO_TIMEVAL(a, r) do { \ | 310 #define TIME_VALUE_TO_TIMEVAL(a, r) do { \ |
261 (r)->tv_sec = (a)->seconds; \ | 311 (r)->tv_sec = (a)->seconds; \ |
262 (r)->tv_usec = (a)->microseconds; \ | 312 (r)->tv_usec = (a)->microseconds; \ |
263 } while (0) | 313 } while (0) |
264 | 314 |
265 double ProcessMetrics::GetCPUUsage() { | 315 double ProcessMetrics::GetCPUUsage() { |
266 mach_port_t task = TaskForPid(process_); | 316 mach_port_t task = TaskForPid(process_); |
267 if (task == MACH_PORT_NULL) | 317 if (task == MACH_PORT_NULL) |
268 return 0; | 318 return 0; |
269 | 319 |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.speculative_count); | 467 saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.speculative_count); |
418 meminfo->file_backed = | 468 meminfo->file_backed = |
419 saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.external_page_count); | 469 saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.external_page_count); |
420 meminfo->purgeable = | 470 meminfo->purgeable = |
421 saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.purgeable_count); | 471 saturated_cast<int>(PAGE_SIZE / 1024 * vm_info.purgeable_count); |
422 | 472 |
423 return true; | 473 return true; |
424 } | 474 } |
425 | 475 |
426 } // namespace base | 476 } // namespace base |
OLD | NEW |