| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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 | 5 |
| 6 #include "base/process_util.h" | 6 #include "base/process_util.h" |
| 7 | 7 |
| 8 #import <Cocoa/Cocoa.h> | 8 #import <Cocoa/Cocoa.h> |
| 9 #include <crt_externs.h> | 9 #include <crt_externs.h> |
| 10 #include <mach/mach.h> | 10 #include <mach/mach.h> |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 (r)->tv_usec = (a)->microseconds; \ | 248 (r)->tv_usec = (a)->microseconds; \ |
| 249 } while (0) | 249 } while (0) |
| 250 | 250 |
| 251 double ProcessMetrics::GetCPUUsage() { | 251 double ProcessMetrics::GetCPUUsage() { |
| 252 mach_port_t task = TaskForPid(process_); | 252 mach_port_t task = TaskForPid(process_); |
| 253 if (task == MACH_PORT_NULL) | 253 if (task == MACH_PORT_NULL) |
| 254 return 0; | 254 return 0; |
| 255 | 255 |
| 256 kern_return_t kr; | 256 kern_return_t kr; |
| 257 | 257 |
| 258 // TODO(thakis): Libtop doesn't use thread info. How can they get away | 258 // Libtop explicitly loops over the threads (libtop_pinfo_update_cpu_usage() |
| 259 // without it? | 259 // in libtop.c), but this is more concise and gives the same results: |
| 260 task_thread_times_info thread_info_data; | 260 task_thread_times_info thread_info_data; |
| 261 mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT; | 261 mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT; |
| 262 kr = task_info(task, | 262 kr = task_info(task, |
| 263 TASK_THREAD_TIMES_INFO, | 263 TASK_THREAD_TIMES_INFO, |
| 264 reinterpret_cast<task_info_t>(&thread_info_data), | 264 reinterpret_cast<task_info_t>(&thread_info_data), |
| 265 &thread_info_count); | 265 &thread_info_count); |
| 266 if (kr != KERN_SUCCESS) { | 266 if (kr != KERN_SUCCESS) { |
| 267 // Most likely cause: |task| is a zombie. | 267 // Most likely cause: |task| is a zombie. |
| 268 return 0; | 268 return 0; |
| 269 } | 269 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 300 return 0; | 300 return 0; |
| 301 } | 301 } |
| 302 | 302 |
| 303 int64 system_time_delta = task_time - last_system_time_; | 303 int64 system_time_delta = task_time - last_system_time_; |
| 304 int64 time_delta = time - last_time_; | 304 int64 time_delta = time - last_time_; |
| 305 DCHECK(time_delta != 0); | 305 DCHECK(time_delta != 0); |
| 306 if (time_delta == 0) | 306 if (time_delta == 0) |
| 307 return 0; | 307 return 0; |
| 308 | 308 |
| 309 // We add time_delta / 2 so the result is rounded. | 309 // We add time_delta / 2 so the result is rounded. |
| 310 double cpu = static_cast<double>( | 310 double cpu = static_cast<double>((system_time_delta * 100.0) / time_delta); |
| 311 (system_time_delta * 100.0 + time_delta / 2.0) / time_delta); | |
| 312 | 311 |
| 313 last_system_time_ = task_time; | 312 last_system_time_ = task_time; |
| 314 last_time_ = time; | 313 last_time_ = time; |
| 315 | 314 |
| 316 return cpu; | 315 return cpu; |
| 317 } | 316 } |
| 318 | 317 |
| 319 mach_port_t ProcessMetrics::TaskForPid(ProcessHandle process) const { | 318 mach_port_t ProcessMetrics::TaskForPid(ProcessHandle process) const { |
| 320 mach_port_t task = MACH_PORT_NULL; | 319 mach_port_t task = MACH_PORT_NULL; |
| 321 if (port_provider_) | 320 if (port_provider_) |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 CHECK(g_old_malloc && g_old_calloc && g_old_valloc && g_old_realloc) | 431 CHECK(g_old_malloc && g_old_calloc && g_old_valloc && g_old_realloc) |
| 433 << "Failed to get system allocation functions."; | 432 << "Failed to get system allocation functions."; |
| 434 | 433 |
| 435 default_zone->malloc = oom_killer_malloc; | 434 default_zone->malloc = oom_killer_malloc; |
| 436 default_zone->calloc = oom_killer_calloc; | 435 default_zone->calloc = oom_killer_calloc; |
| 437 default_zone->valloc = oom_killer_valloc; | 436 default_zone->valloc = oom_killer_valloc; |
| 438 default_zone->realloc = oom_killer_realloc; | 437 default_zone->realloc = oom_killer_realloc; |
| 439 } | 438 } |
| 440 | 439 |
| 441 } // namespace base | 440 } // namespace base |
| OLD | NEW |