| 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> |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 TIME_VALUE_TO_TIMEVAL(&thread_info_data.user_time, &user_timeval); | 260 TIME_VALUE_TO_TIMEVAL(&thread_info_data.user_time, &user_timeval); |
| 261 TIME_VALUE_TO_TIMEVAL(&thread_info_data.system_time, &system_timeval); | 261 TIME_VALUE_TO_TIMEVAL(&thread_info_data.system_time, &system_timeval); |
| 262 timeradd(&user_timeval, &system_timeval, &task_timeval); | 262 timeradd(&user_timeval, &system_timeval, &task_timeval); |
| 263 | 263 |
| 264 // ... task info contains terminated time. | 264 // ... task info contains terminated time. |
| 265 TIME_VALUE_TO_TIMEVAL(&task_info_data.user_time, &user_timeval); | 265 TIME_VALUE_TO_TIMEVAL(&task_info_data.user_time, &user_timeval); |
| 266 TIME_VALUE_TO_TIMEVAL(&task_info_data.system_time, &system_timeval); | 266 TIME_VALUE_TO_TIMEVAL(&task_info_data.system_time, &system_timeval); |
| 267 timeradd(&user_timeval, &task_timeval, &task_timeval); | 267 timeradd(&user_timeval, &task_timeval, &task_timeval); |
| 268 timeradd(&system_timeval, &task_timeval, &task_timeval); | 268 timeradd(&system_timeval, &task_timeval, &task_timeval); |
| 269 | 269 |
| 270 struct timeval now; | 270 TimeTicks time = TimeTicks::Now(); |
| 271 int retval = gettimeofday(&now, NULL); | |
| 272 if (retval) | |
| 273 return 0; | |
| 274 | |
| 275 int64 time = TimeValToMicroseconds(now); | |
| 276 int64 task_time = TimeValToMicroseconds(task_timeval); | 271 int64 task_time = TimeValToMicroseconds(task_timeval); |
| 277 | 272 |
| 278 if (last_cpu_time_ == 0) { | 273 if (last_system_time_ == 0) { |
| 279 // First call, just set the last values. | 274 // First call, just set the last values. |
| 280 last_cpu_time_ = time; | 275 last_cpu_walltime_ = time; |
| 281 last_system_time_ = task_time; | 276 last_system_time_ = task_time; |
| 282 return 0; | 277 return 0; |
| 283 } | 278 } |
| 284 | 279 |
| 285 int64 system_time_delta = task_time - last_system_time_; | 280 int64 system_time_delta = task_time - last_system_time_; |
| 286 int64 time_delta = time - last_cpu_time_; | 281 int64 walltime_delta = (time - last_cpu_walltime_).InMicroseconds(); |
| 287 DCHECK_NE(0U, time_delta); | 282 DCHECK_NE(0U, walltime_delta); |
| 288 if (time_delta == 0) | 283 if (walltime_delta == 0) |
| 289 return 0; | 284 return 0; |
| 290 | 285 |
| 291 last_cpu_time_ = time; | 286 last_cpu_walltime_ = time; |
| 292 last_system_time_ = task_time; | 287 last_system_time_ = task_time; |
| 293 | 288 |
| 294 return static_cast<double>(system_time_delta * 100.0) / time_delta; | 289 return static_cast<double>(system_time_delta * 100.0) / walltime_delta; |
| 295 } | 290 } |
| 296 | 291 |
| 297 int ProcessMetrics::GetIdleWakeupsPerSecond() { | 292 int ProcessMetrics::GetIdleWakeupsPerSecond() { |
| 298 mach_port_t task = TaskForPid(process_); | 293 mach_port_t task = TaskForPid(process_); |
| 299 if (task == MACH_PORT_NULL) | 294 if (task == MACH_PORT_NULL) |
| 300 return 0; | 295 return 0; |
| 301 | 296 |
| 302 kern_return_t kr; | 297 kern_return_t kr; |
| 303 | 298 |
| 304 task_power_info power_info_data; | 299 task_power_info power_info_data; |
| 305 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; |
| 306 kr = task_info(task, | 301 kr = task_info(task, |
| 307 TASK_POWER_INFO, | 302 TASK_POWER_INFO, |
| 308 reinterpret_cast<task_info_t>(&power_info_data), | 303 reinterpret_cast<task_info_t>(&power_info_data), |
| 309 &power_info_count); | 304 &power_info_count); |
| 310 if (kr != KERN_SUCCESS) { | 305 if (kr != KERN_SUCCESS) { |
| 311 // Most likely cause: |task| is a zombie, or this is on a pre-10.8 system | 306 // Most likely cause: |task| is a zombie, or this is on a pre-10.8 system |
| 312 // where TASK_POWER_INFO isn't supported yet. | 307 // where TASK_POWER_INFO isn't supported yet. |
| 313 return 0; | 308 return 0; |
| 314 } | 309 } |
| 315 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; |
| 316 | 311 |
| 317 struct timeval now; | 312 TimeTicks time = TimeTicks::Now(); |
| 318 int retval = gettimeofday(&now, NULL); | |
| 319 if (retval) | |
| 320 return 0; | |
| 321 | 313 |
| 322 int64 time = TimeValToMicroseconds(now); | 314 if (last_absolute_idle_wakeups_ == 0) { |
| 323 | |
| 324 if (last_idle_wakeups_time_ == 0) { | |
| 325 // First call, just set the last values. | 315 // First call, just set the last values. |
| 326 last_idle_wakeups_time_ = time; | 316 last_idle_wakeups_walltime_ = time; |
| 327 last_absolute_idle_wakeups_ = absolute_idle_wakeups; | 317 last_absolute_idle_wakeups_ = absolute_idle_wakeups; |
| 328 return 0; | 318 return 0; |
| 329 } | 319 } |
| 330 | 320 |
| 331 int64 wakeups_delta = absolute_idle_wakeups - last_absolute_idle_wakeups_; | 321 int64 wakeups_delta = absolute_idle_wakeups - last_absolute_idle_wakeups_; |
| 332 int64 time_delta = time - last_idle_wakeups_time_; | 322 int64 walltime_delta = (time - last_idle_wakeups_walltime_).InMicroseconds(); |
| 333 DCHECK_NE(0U, time_delta); | 323 DCHECK_NE(0U, walltime_delta); |
| 334 if (time_delta == 0) | 324 if (walltime_delta == 0) |
| 335 return 0; | 325 return 0; |
| 336 | 326 |
| 337 last_idle_wakeups_time_ = time; | 327 last_idle_wakeups_walltime_ = time; |
| 338 last_absolute_idle_wakeups_ = absolute_idle_wakeups; | 328 last_absolute_idle_wakeups_ = absolute_idle_wakeups; |
| 339 | 329 |
| 340 // Round to average wakeups per second. | 330 // Round to average wakeups per second. |
| 341 const int kMicrosecondsPerSecond = 1000 * 1000; | 331 const int kMicrosecondsPerSecond = 1000 * 1000; |
| 342 return (wakeups_delta * kMicrosecondsPerSecond + time_delta/2) / time_delta; | 332 return (wakeups_delta * kMicrosecondsPerSecond + walltime_delta / 2) / |
| 333 walltime_delta; |
| 343 } | 334 } |
| 344 | 335 |
| 345 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { | 336 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { |
| 346 return false; | 337 return false; |
| 347 } | 338 } |
| 348 | 339 |
| 349 ProcessMetrics::ProcessMetrics(ProcessHandle process, | 340 ProcessMetrics::ProcessMetrics(ProcessHandle process, |
| 350 ProcessMetrics::PortProvider* port_provider) | 341 ProcessMetrics::PortProvider* port_provider) |
| 351 : process_(process), | 342 : process_(process), |
| 352 last_cpu_time_(0), | |
| 353 last_system_time_(0), | 343 last_system_time_(0), |
| 354 last_idle_wakeups_time_(0), | |
| 355 last_absolute_idle_wakeups_(0), | 344 last_absolute_idle_wakeups_(0), |
| 356 port_provider_(port_provider) { | 345 port_provider_(port_provider) { |
| 357 processor_count_ = SysInfo::NumberOfProcessors(); | 346 processor_count_ = SysInfo::NumberOfProcessors(); |
| 358 } | 347 } |
| 359 | 348 |
| 360 mach_port_t ProcessMetrics::TaskForPid(ProcessHandle process) const { | 349 mach_port_t ProcessMetrics::TaskForPid(ProcessHandle process) const { |
| 361 mach_port_t task = MACH_PORT_NULL; | 350 mach_port_t task = MACH_PORT_NULL; |
| 362 if (port_provider_) | 351 if (port_provider_) |
| 363 task = port_provider_->TaskForPid(process_); | 352 task = port_provider_->TaskForPid(process_); |
| 364 if (task == MACH_PORT_NULL && process_ == getpid()) | 353 if (task == MACH_PORT_NULL && process_ == getpid()) |
| (...skipping 18 matching lines...) Expand all Loading... |
| 383 kr = host_page_size(host, &page_size); | 372 kr = host_page_size(host, &page_size); |
| 384 if (kr) { | 373 if (kr) { |
| 385 DLOG(ERROR) << "Failed to fetch host page size."; | 374 DLOG(ERROR) << "Failed to fetch host page size."; |
| 386 return 0; | 375 return 0; |
| 387 } | 376 } |
| 388 | 377 |
| 389 return (data.active_count * page_size) / 1024; | 378 return (data.active_count * page_size) / 1024; |
| 390 } | 379 } |
| 391 | 380 |
| 392 } // namespace base | 381 } // namespace base |
| OLD | NEW |