| 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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 if (!GetTaskInfo(TaskForPid(process_), &task_info_data)) | 103 if (!GetTaskInfo(TaskForPid(process_), &task_info_data)) |
| 104 return 0; | 104 return 0; |
| 105 return task_info_data.virtual_size; | 105 return task_info_data.virtual_size; |
| 106 } | 106 } |
| 107 | 107 |
| 108 size_t ProcessMetrics::GetPeakPagefileUsage() const { | 108 size_t ProcessMetrics::GetPeakPagefileUsage() const { |
| 109 return 0; | 109 return 0; |
| 110 } | 110 } |
| 111 | 111 |
| 112 size_t ProcessMetrics::GetWorkingSetSize() const { | 112 size_t ProcessMetrics::GetWorkingSetSize() const { |
| 113 task_basic_info_64 task_info_data; | 113 size_t private_bytes = 0; |
| 114 if (!GetTaskInfo(TaskForPid(process_), &task_info_data)) | 114 size_t shared_bytes = 0; |
| 115 size_t resident_bytes = 0; |
| 116 if (!GetMemoryBytes(&private_bytes, &shared_bytes, &resident_bytes)) |
| 115 return 0; | 117 return 0; |
| 116 return task_info_data.resident_size; | 118 return resident_bytes; |
| 117 } | 119 } |
| 118 | 120 |
| 119 size_t ProcessMetrics::GetPeakWorkingSetSize() const { | 121 size_t ProcessMetrics::GetPeakWorkingSetSize() const { |
| 120 return 0; | 122 return 0; |
| 121 } | 123 } |
| 122 | 124 |
| 123 // This is a rough approximation of the algorithm that libtop uses. | 125 // This is a rough approximation of the algorithm that libtop uses. |
| 124 // private_bytes is the size of private resident memory. | 126 // private_bytes is the size of private resident memory. |
| 125 // shared_bytes is the size of shared resident memory. | 127 // shared_bytes is the size of shared resident memory. |
| 126 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, | 128 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, |
| 127 size_t* shared_bytes) { | 129 size_t* shared_bytes) const { |
| 128 size_t private_pages_count = 0; | 130 size_t private_pages_count = 0; |
| 129 size_t shared_pages_count = 0; | 131 size_t shared_pages_count = 0; |
| 130 | 132 |
| 131 if (!private_bytes && !shared_bytes) | 133 if (!private_bytes && !shared_bytes) |
| 132 return true; | 134 return true; |
| 133 | 135 |
| 134 mach_port_t task = TaskForPid(process_); | 136 mach_port_t task = TaskForPid(process_); |
| 135 if (task == MACH_PORT_NULL) { | 137 if (task == MACH_PORT_NULL) { |
| 136 DLOG(ERROR) << "Invalid process"; | 138 DLOG(ERROR) << "Invalid process"; |
| 137 return false; | 139 return false; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 mach_port_deallocate(mach_task_self(), object_name); | 184 mach_port_deallocate(mach_task_self(), object_name); |
| 183 | 185 |
| 184 if (IsAddressInSharedRegion(address, cpu_type) && | 186 if (IsAddressInSharedRegion(address, cpu_type) && |
| 185 info.share_mode != SM_PRIVATE) | 187 info.share_mode != SM_PRIVATE) |
| 186 continue; | 188 continue; |
| 187 | 189 |
| 188 if (info.share_mode == SM_COW && info.ref_count == 1) | 190 if (info.share_mode == SM_COW && info.ref_count == 1) |
| 189 info.share_mode = SM_PRIVATE; | 191 info.share_mode = SM_PRIVATE; |
| 190 | 192 |
| 191 switch (info.share_mode) { | 193 switch (info.share_mode) { |
| 194 case SM_LARGE_PAGE: |
| 192 case SM_PRIVATE: | 195 case SM_PRIVATE: |
| 193 private_pages_count += info.private_pages_resident; | 196 private_pages_count += info.private_pages_resident; |
| 194 private_pages_count += info.shared_pages_resident; | 197 private_pages_count += info.shared_pages_resident; |
| 195 break; | 198 break; |
| 196 case SM_COW: | 199 case SM_COW: |
| 197 private_pages_count += info.private_pages_resident; | 200 private_pages_count += info.private_pages_resident; |
| 198 // Fall through | 201 // Fall through |
| 199 case SM_SHARED: | 202 case SM_SHARED: |
| 203 case SM_PRIVATE_ALIASED: |
| 204 case SM_TRUESHARED: |
| 205 case SM_SHARED_ALIASED: |
| 200 if (seen_objects.count(info.obj_id) == 0) { | 206 if (seen_objects.count(info.obj_id) == 0) { |
| 201 // Only count the first reference to this region. | 207 // Only count the first reference to this region. |
| 202 seen_objects.insert(info.obj_id); | 208 seen_objects.insert(info.obj_id); |
| 203 shared_pages_count += info.shared_pages_resident; | 209 shared_pages_count += info.shared_pages_resident; |
| 204 } | 210 } |
| 205 break; | 211 break; |
| 206 default: | 212 default: |
| 207 break; | 213 break; |
| 208 } | 214 } |
| 209 } | 215 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 239 usage->mapped = 0; | 245 usage->mapped = 0; |
| 240 usage->image = 0; | 246 usage->image = 0; |
| 241 | 247 |
| 242 ws_usage->priv = task_info_data.resident_size / 1024; | 248 ws_usage->priv = task_info_data.resident_size / 1024; |
| 243 ws_usage->shareable = 0; | 249 ws_usage->shareable = 0; |
| 244 ws_usage->shared = 0; | 250 ws_usage->shared = 0; |
| 245 | 251 |
| 246 return true; | 252 return true; |
| 247 } | 253 } |
| 248 | 254 |
| 255 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, |
| 256 size_t* shared_bytes, |
| 257 size_t* resident_bytes) const { |
| 258 if (!GetMemoryBytes(private_bytes, shared_bytes)) |
| 259 return false; |
| 260 *resident_bytes = *private_bytes + *shared_bytes; |
| 261 return true; |
| 262 } |
| 263 |
| 249 #define TIME_VALUE_TO_TIMEVAL(a, r) do { \ | 264 #define TIME_VALUE_TO_TIMEVAL(a, r) do { \ |
| 250 (r)->tv_sec = (a)->seconds; \ | 265 (r)->tv_sec = (a)->seconds; \ |
| 251 (r)->tv_usec = (a)->microseconds; \ | 266 (r)->tv_usec = (a)->microseconds; \ |
| 252 } while (0) | 267 } while (0) |
| 253 | 268 |
| 254 double ProcessMetrics::GetCPUUsage() { | 269 double ProcessMetrics::GetCPUUsage() { |
| 255 mach_port_t task = TaskForPid(process_); | 270 mach_port_t task = TaskForPid(process_); |
| 256 if (task == MACH_PORT_NULL) | 271 if (task == MACH_PORT_NULL) |
| 257 return 0; | 272 return 0; |
| 258 | 273 |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 return false; | 414 return false; |
| 400 } | 415 } |
| 401 | 416 |
| 402 meminfo->free = static_cast<int>( | 417 meminfo->free = static_cast<int>( |
| 403 (vm_info.free_count - vm_info.speculative_count) * PAGE_SIZE / 1024); | 418 (vm_info.free_count - vm_info.speculative_count) * PAGE_SIZE / 1024); |
| 404 | 419 |
| 405 return true; | 420 return true; |
| 406 } | 421 } |
| 407 | 422 |
| 408 } // namespace base | 423 } // namespace base |
| OLD | NEW |