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