| 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 <sys/sysctl.h> | 10 #include <sys/sysctl.h> |
| 11 | 11 |
| 12 #include "base/containers/hash_tables.h" | 12 #include "base/containers/hash_tables.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/mac/mach_logging.h" |
| 14 #include "base/mac/scoped_mach_port.h" | 15 #include "base/mac/scoped_mach_port.h" |
| 15 #include "base/sys_info.h" | 16 #include "base/sys_info.h" |
| 16 | 17 |
| 17 #if !defined(TASK_POWER_INFO) | 18 #if !defined(TASK_POWER_INFO) |
| 18 // Doesn't exist in the 10.6 or 10.7 SDKs. | 19 // Doesn't exist in the 10.6 or 10.7 SDKs. |
| 19 #define TASK_POWER_INFO 21 | 20 #define TASK_POWER_INFO 21 |
| 20 struct task_power_info { | 21 struct task_power_info { |
| 21 uint64_t total_user; | 22 uint64_t total_user; |
| 22 uint64_t total_system; | 23 uint64_t total_system; |
| 23 uint64_t task_interrupt_wakeups; | 24 uint64_t task_interrupt_wakeups; |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 | 110 |
| 110 size_t ProcessMetrics::GetPeakWorkingSetSize() const { | 111 size_t ProcessMetrics::GetPeakWorkingSetSize() const { |
| 111 return 0; | 112 return 0; |
| 112 } | 113 } |
| 113 | 114 |
| 114 // This is a rough approximation of the algorithm that libtop uses. | 115 // This is a rough approximation of the algorithm that libtop uses. |
| 115 // private_bytes is the size of private resident memory. | 116 // private_bytes is the size of private resident memory. |
| 116 // shared_bytes is the size of shared resident memory. | 117 // shared_bytes is the size of shared resident memory. |
| 117 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, | 118 bool ProcessMetrics::GetMemoryBytes(size_t* private_bytes, |
| 118 size_t* shared_bytes) { | 119 size_t* shared_bytes) { |
| 119 kern_return_t kr; | |
| 120 size_t private_pages_count = 0; | 120 size_t private_pages_count = 0; |
| 121 size_t shared_pages_count = 0; | 121 size_t shared_pages_count = 0; |
| 122 | 122 |
| 123 if (!private_bytes && !shared_bytes) | 123 if (!private_bytes && !shared_bytes) |
| 124 return true; | 124 return true; |
| 125 | 125 |
| 126 mach_port_t task = TaskForPid(process_); | 126 mach_port_t task = TaskForPid(process_); |
| 127 if (task == MACH_PORT_NULL) { | 127 if (task == MACH_PORT_NULL) { |
| 128 DLOG(ERROR) << "Invalid process"; | 128 DLOG(ERROR) << "Invalid process"; |
| 129 return false; | 129 return false; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 146 // Private memory is much simpler. We simply count the pages that are marked | 146 // Private memory is much simpler. We simply count the pages that are marked |
| 147 // as private or copy on write (COW). | 147 // as private or copy on write (COW). |
| 148 // | 148 // |
| 149 // See libtop_update_vm_regions in | 149 // See libtop_update_vm_regions in |
| 150 // http://www.opensource.apple.com/source/top/top-67/libtop.c | 150 // http://www.opensource.apple.com/source/top/top-67/libtop.c |
| 151 mach_vm_size_t size = 0; | 151 mach_vm_size_t size = 0; |
| 152 for (mach_vm_address_t address = MACH_VM_MIN_ADDRESS;; address += size) { | 152 for (mach_vm_address_t address = MACH_VM_MIN_ADDRESS;; address += size) { |
| 153 vm_region_top_info_data_t info; | 153 vm_region_top_info_data_t info; |
| 154 mach_msg_type_number_t info_count = VM_REGION_TOP_INFO_COUNT; | 154 mach_msg_type_number_t info_count = VM_REGION_TOP_INFO_COUNT; |
| 155 mach_port_t object_name; | 155 mach_port_t object_name; |
| 156 kr = mach_vm_region(task, | 156 kern_return_t kr = mach_vm_region(task, |
| 157 &address, | 157 &address, |
| 158 &size, | 158 &size, |
| 159 VM_REGION_TOP_INFO, | 159 VM_REGION_TOP_INFO, |
| 160 (vm_region_info_t)&info, | 160 reinterpret_cast<vm_region_info_t>(&info), |
| 161 &info_count, | 161 &info_count, |
| 162 &object_name); | 162 &object_name); |
| 163 if (kr == KERN_INVALID_ADDRESS) { | 163 if (kr == KERN_INVALID_ADDRESS) { |
| 164 // We're at the end of the address space. | 164 // We're at the end of the address space. |
| 165 break; | 165 break; |
| 166 } else if (kr != KERN_SUCCESS) { | 166 } else if (kr != KERN_SUCCESS) { |
| 167 DLOG(ERROR) << "Calling mach_vm_region failed with error: " | 167 MACH_DLOG(ERROR, kr) << "mach_vm_region"; |
| 168 << mach_error_string(kr); | |
| 169 return false; | 168 return false; |
| 170 } | 169 } |
| 171 | 170 |
| 171 mach_port_deallocate(mach_task_self(), object_name); |
| 172 |
| 172 if (IsAddressInSharedRegion(address, cpu_type) && | 173 if (IsAddressInSharedRegion(address, cpu_type) && |
| 173 info.share_mode != SM_PRIVATE) | 174 info.share_mode != SM_PRIVATE) |
| 174 continue; | 175 continue; |
| 175 | 176 |
| 176 if (info.share_mode == SM_COW && info.ref_count == 1) | 177 if (info.share_mode == SM_COW && info.ref_count == 1) |
| 177 info.share_mode = SM_PRIVATE; | 178 info.share_mode = SM_PRIVATE; |
| 178 | 179 |
| 179 switch (info.share_mode) { | 180 switch (info.share_mode) { |
| 180 case SM_PRIVATE: | 181 case SM_PRIVATE: |
| 181 private_pages_count += info.private_pages_resident; | 182 private_pages_count += info.private_pages_resident; |
| 182 private_pages_count += info.shared_pages_resident; | 183 private_pages_count += info.shared_pages_resident; |
| 183 break; | 184 break; |
| 184 case SM_COW: | 185 case SM_COW: |
| 185 private_pages_count += info.private_pages_resident; | 186 private_pages_count += info.private_pages_resident; |
| 186 // Fall through | 187 // Fall through |
| 187 case SM_SHARED: | 188 case SM_SHARED: |
| 188 if (seen_objects.count(info.obj_id) == 0) { | 189 if (seen_objects.count(info.obj_id) == 0) { |
| 189 // Only count the first reference to this region. | 190 // Only count the first reference to this region. |
| 190 seen_objects.insert(info.obj_id); | 191 seen_objects.insert(info.obj_id); |
| 191 shared_pages_count += info.shared_pages_resident; | 192 shared_pages_count += info.shared_pages_resident; |
| 192 } | 193 } |
| 193 break; | 194 break; |
| 194 default: | 195 default: |
| 195 break; | 196 break; |
| 196 } | 197 } |
| 197 } | 198 } |
| 198 | 199 |
| 199 vm_size_t page_size; | |
| 200 kr = host_page_size(task, &page_size); | |
| 201 if (kr != KERN_SUCCESS) { | |
| 202 DLOG(ERROR) << "Failed to fetch host page size, error: " | |
| 203 << mach_error_string(kr); | |
| 204 return false; | |
| 205 } | |
| 206 | |
| 207 if (private_bytes) | 200 if (private_bytes) |
| 208 *private_bytes = private_pages_count * page_size; | 201 *private_bytes = private_pages_count * PAGE_SIZE; |
| 209 if (shared_bytes) | 202 if (shared_bytes) |
| 210 *shared_bytes = shared_pages_count * page_size; | 203 *shared_bytes = shared_pages_count * PAGE_SIZE; |
| 211 | 204 |
| 212 return true; | 205 return true; |
| 213 } | 206 } |
| 214 | 207 |
| 215 void ProcessMetrics::GetCommittedKBytes(CommittedKBytes* usage) const { | 208 void ProcessMetrics::GetCommittedKBytes(CommittedKBytes* usage) const { |
| 216 } | 209 } |
| 217 | 210 |
| 218 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { | 211 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { |
| 219 size_t priv = GetWorkingSetSize(); | 212 size_t priv = GetWorkingSetSize(); |
| 220 if (!priv) | 213 if (!priv) |
| 221 return false; | 214 return false; |
| 222 ws_usage->priv = priv / 1024; | 215 ws_usage->priv = priv / 1024; |
| 223 ws_usage->shareable = 0; | 216 ws_usage->shareable = 0; |
| 224 ws_usage->shared = 0; | 217 ws_usage->shared = 0; |
| 225 return true; | 218 return true; |
| 226 } | 219 } |
| 227 | 220 |
| 228 #define TIME_VALUE_TO_TIMEVAL(a, r) do { \ | 221 #define TIME_VALUE_TO_TIMEVAL(a, r) do { \ |
| 229 (r)->tv_sec = (a)->seconds; \ | 222 (r)->tv_sec = (a)->seconds; \ |
| 230 (r)->tv_usec = (a)->microseconds; \ | 223 (r)->tv_usec = (a)->microseconds; \ |
| 231 } while (0) | 224 } while (0) |
| 232 | 225 |
| 233 double ProcessMetrics::GetCPUUsage() { | 226 double ProcessMetrics::GetCPUUsage() { |
| 234 mach_port_t task = TaskForPid(process_); | 227 mach_port_t task = TaskForPid(process_); |
| 235 if (task == MACH_PORT_NULL) | 228 if (task == MACH_PORT_NULL) |
| 236 return 0; | 229 return 0; |
| 237 | 230 |
| 238 kern_return_t kr; | |
| 239 | |
| 240 // Libtop explicitly loops over the threads (libtop_pinfo_update_cpu_usage() | 231 // Libtop explicitly loops over the threads (libtop_pinfo_update_cpu_usage() |
| 241 // in libtop.c), but this is more concise and gives the same results: | 232 // in libtop.c), but this is more concise and gives the same results: |
| 242 task_thread_times_info thread_info_data; | 233 task_thread_times_info thread_info_data; |
| 243 mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT; | 234 mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT; |
| 244 kr = task_info(task, | 235 kern_return_t kr = task_info(task, |
| 245 TASK_THREAD_TIMES_INFO, | 236 TASK_THREAD_TIMES_INFO, |
| 246 reinterpret_cast<task_info_t>(&thread_info_data), | 237 reinterpret_cast<task_info_t>(&thread_info_data), |
| 247 &thread_info_count); | 238 &thread_info_count); |
| 248 if (kr != KERN_SUCCESS) { | 239 if (kr != KERN_SUCCESS) { |
| 249 // Most likely cause: |task| is a zombie. | 240 // Most likely cause: |task| is a zombie. |
| 250 return 0; | 241 return 0; |
| 251 } | 242 } |
| 252 | 243 |
| 253 task_basic_info_64 task_info_data; | 244 task_basic_info_64 task_info_data; |
| 254 if (!GetTaskInfo(task, &task_info_data)) | 245 if (!GetTaskInfo(task, &task_info_data)) |
| 255 return 0; | 246 return 0; |
| 256 | 247 |
| 257 /* Set total_time. */ | 248 /* Set total_time. */ |
| (...skipping 29 matching lines...) Expand all Loading... |
| 287 last_system_time_ = task_time; | 278 last_system_time_ = task_time; |
| 288 | 279 |
| 289 return static_cast<double>(system_time_delta * 100.0) / time_delta; | 280 return static_cast<double>(system_time_delta * 100.0) / time_delta; |
| 290 } | 281 } |
| 291 | 282 |
| 292 int ProcessMetrics::GetIdleWakeupsPerSecond() { | 283 int ProcessMetrics::GetIdleWakeupsPerSecond() { |
| 293 mach_port_t task = TaskForPid(process_); | 284 mach_port_t task = TaskForPid(process_); |
| 294 if (task == MACH_PORT_NULL) | 285 if (task == MACH_PORT_NULL) |
| 295 return 0; | 286 return 0; |
| 296 | 287 |
| 297 kern_return_t kr; | |
| 298 | |
| 299 task_power_info power_info_data; | 288 task_power_info power_info_data; |
| 300 mach_msg_type_number_t power_info_count = TASK_POWER_INFO_COUNT; | 289 mach_msg_type_number_t power_info_count = TASK_POWER_INFO_COUNT; |
| 301 kr = task_info(task, | 290 kern_return_t kr = task_info(task, |
| 302 TASK_POWER_INFO, | 291 TASK_POWER_INFO, |
| 303 reinterpret_cast<task_info_t>(&power_info_data), | 292 reinterpret_cast<task_info_t>(&power_info_data), |
| 304 &power_info_count); | 293 &power_info_count); |
| 305 if (kr != KERN_SUCCESS) { | 294 if (kr != KERN_SUCCESS) { |
| 306 // Most likely cause: |task| is a zombie, or this is on a pre-10.8.4 system | 295 // Most likely cause: |task| is a zombie, or this is on a pre-10.8.4 system |
| 307 // where TASK_POWER_INFO isn't supported yet. | 296 // where TASK_POWER_INFO isn't supported yet. |
| 308 return 0; | 297 return 0; |
| 309 } | 298 } |
| 310 uint64_t absolute_idle_wakeups = power_info_data.task_platform_idle_wakeups; | 299 uint64_t absolute_idle_wakeups = power_info_data.task_platform_idle_wakeups; |
| 311 | 300 |
| 312 TimeTicks time = TimeTicks::Now(); | 301 TimeTicks time = TimeTicks::Now(); |
| 313 | 302 |
| 314 if (last_absolute_idle_wakeups_ == 0) { | 303 if (last_absolute_idle_wakeups_ == 0) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 } | 344 } |
| 356 | 345 |
| 357 // Bytes committed by the system. | 346 // Bytes committed by the system. |
| 358 size_t GetSystemCommitCharge() { | 347 size_t GetSystemCommitCharge() { |
| 359 base::mac::ScopedMachPort host(mach_host_self()); | 348 base::mac::ScopedMachPort host(mach_host_self()); |
| 360 mach_msg_type_number_t count = HOST_VM_INFO_COUNT; | 349 mach_msg_type_number_t count = HOST_VM_INFO_COUNT; |
| 361 vm_statistics_data_t data; | 350 vm_statistics_data_t data; |
| 362 kern_return_t kr = host_statistics(host, HOST_VM_INFO, | 351 kern_return_t kr = host_statistics(host, HOST_VM_INFO, |
| 363 reinterpret_cast<host_info_t>(&data), | 352 reinterpret_cast<host_info_t>(&data), |
| 364 &count); | 353 &count); |
| 365 if (kr) { | 354 if (kr != KERN_SUCCESS) { |
| 366 DLOG(WARNING) << "Failed to fetch host statistics."; | 355 MACH_DLOG(WARNING, kr) << "host_statistics"; |
| 367 return 0; | 356 return 0; |
| 368 } | 357 } |
| 369 | 358 |
| 370 vm_size_t page_size; | 359 return (data.active_count * PAGE_SIZE) / 1024; |
| 371 kr = host_page_size(host, &page_size); | |
| 372 if (kr) { | |
| 373 DLOG(ERROR) << "Failed to fetch host page size."; | |
| 374 return 0; | |
| 375 } | |
| 376 | |
| 377 return (data.active_count * page_size) / 1024; | |
| 378 } | 360 } |
| 379 | 361 |
| 380 } // namespace base | 362 } // namespace base |
| OLD | NEW |