diff options
author | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-06 21:25:01 +0000 |
---|---|---|
committer | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-06 21:25:01 +0000 |
commit | e21c33248f3bc1c048fe2e12d452f63d90a03e57 (patch) | |
tree | 7336b4707b5fd453461f5f59aa7721a8e58199d4 /base/process | |
parent | 160abc9575362e8d9ebe818211b02f410f236c03 (diff) | |
download | chromium_src-e21c33248f3bc1c048fe2e12d452f63d90a03e57.zip chromium_src-e21c33248f3bc1c048fe2e12d452f63d90a03e57.tar.gz chromium_src-e21c33248f3bc1c048fe2e12d452f63d90a03e57.tar.bz2 |
Use TimeTicks instead of gettimeofday in ProcessMetrics.
No real behavior change, but if the user changes their system time there
shouldn't be a jump in %cpu for one call.
BUG=none
Review URL: https://codereview.chromium.org/139103007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@262059 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/process')
-rw-r--r-- | base/process/process_metrics.h | 4 | ||||
-rw-r--r-- | base/process/process_metrics_freebsd.cc | 6 | ||||
-rw-r--r-- | base/process/process_metrics_linux.cc | 11 | ||||
-rw-r--r-- | base/process/process_metrics_mac.cc | 24 | ||||
-rw-r--r-- | base/process/process_metrics_openbsd.cc | 13 | ||||
-rw-r--r-- | base/process/process_metrics_win.cc | 11 |
6 files changed, 18 insertions, 51 deletions
diff --git a/base/process/process_metrics.h b/base/process/process_metrics.h index 45f2ef9..840f933 100644 --- a/base/process/process_metrics.h +++ b/base/process/process_metrics.h @@ -205,11 +205,11 @@ class BASE_EXPORT ProcessMetrics { // Used to store the previous times and CPU usage counts so we can // compute the CPU usage between calls. - int64 last_cpu_time_; + TimeTicks last_cpu_time_; int64 last_system_time_; // Same thing for idle wakeups. - int64 last_idle_wakeups_time_; + TimeTicks last_idle_wakeups_time_; int64 last_absolute_idle_wakeups_; #if !defined(OS_IOS) diff --git a/base/process/process_metrics_freebsd.cc b/base/process/process_metrics_freebsd.cc index b0c20d2e..9d4149d 100644 --- a/base/process/process_metrics_freebsd.cc +++ b/base/process/process_metrics_freebsd.cc @@ -14,7 +14,6 @@ namespace base { ProcessMetrics::ProcessMetrics(ProcessHandle process) : process_(process), - last_cpu_time_(0), last_system_time_(0), last_cpu_(0) { processor_count_ = base::SysInfo::NumberOfProcessors(); @@ -87,11 +86,6 @@ double ProcessMetrics::GetCPUUsage() { int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process_ }; size_t length = sizeof(info); - struct timeval now; - int retval = gettimeofday(&now, NULL); - if (retval) - return 0; - if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0) return 0; diff --git a/base/process/process_metrics_linux.cc b/base/process/process_metrics_linux.cc index 7fa5092..0e12595 100644 --- a/base/process/process_metrics_linux.cc +++ b/base/process/process_metrics_linux.cc @@ -182,20 +182,16 @@ bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { } double ProcessMetrics::GetCPUUsage() { - struct timeval now; - int retval = gettimeofday(&now, NULL); - if (retval) - return 0; - int64 time = TimeValToMicroseconds(now); + TimeTicks time = TimeTicks::Now(); - if (last_cpu_time_ == 0) { + if (last_cpu_ == 0) { // First call, just set the last values. last_cpu_time_ = time; last_cpu_ = GetProcessCPU(process_); return 0; } - int64 time_delta = time - last_cpu_time_; + int64 time_delta = (time - last_cpu_time_).InMicroseconds(); DCHECK_NE(time_delta, 0); if (time_delta == 0) return 0; @@ -263,7 +259,6 @@ bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { ProcessMetrics::ProcessMetrics(ProcessHandle process) : process_(process), - last_cpu_time_(0), last_system_time_(0), last_cpu_(0) { processor_count_ = base::SysInfo::NumberOfProcessors(); diff --git a/base/process/process_metrics_mac.cc b/base/process/process_metrics_mac.cc index 8566861..1b2e61b 100644 --- a/base/process/process_metrics_mac.cc +++ b/base/process/process_metrics_mac.cc @@ -267,15 +267,10 @@ double ProcessMetrics::GetCPUUsage() { timeradd(&user_timeval, &task_timeval, &task_timeval); timeradd(&system_timeval, &task_timeval, &task_timeval); - struct timeval now; - int retval = gettimeofday(&now, NULL); - if (retval) - return 0; - - int64 time = TimeValToMicroseconds(now); + TimeTicks time = TimeTicks::Now(); int64 task_time = TimeValToMicroseconds(task_timeval); - if (last_cpu_time_ == 0) { + if (last_system_time_ == 0) { // First call, just set the last values. last_cpu_time_ = time; last_system_time_ = task_time; @@ -283,7 +278,7 @@ double ProcessMetrics::GetCPUUsage() { } int64 system_time_delta = task_time - last_system_time_; - int64 time_delta = time - last_cpu_time_; + int64 time_delta = (time - last_cpu_time_).InMicroseconds(); DCHECK_NE(0U, time_delta); if (time_delta == 0) return 0; @@ -314,14 +309,9 @@ int ProcessMetrics::GetIdleWakeupsPerSecond() { } uint64_t absolute_idle_wakeups = power_info_data.task_platform_idle_wakeups; - struct timeval now; - int retval = gettimeofday(&now, NULL); - if (retval) - return 0; - - int64 time = TimeValToMicroseconds(now); + TimeTicks time = TimeTicks::Now(); - if (last_idle_wakeups_time_ == 0) { + if (last_absolute_idle_wakeups_ == 0) { // First call, just set the last values. last_idle_wakeups_time_ = time; last_absolute_idle_wakeups_ = absolute_idle_wakeups; @@ -329,7 +319,7 @@ int ProcessMetrics::GetIdleWakeupsPerSecond() { } int64 wakeups_delta = absolute_idle_wakeups - last_absolute_idle_wakeups_; - int64 time_delta = time - last_idle_wakeups_time_; + int64 time_delta = (time - last_idle_wakeups_time_).InMicroseconds(); DCHECK_NE(0U, time_delta); if (time_delta == 0) return 0; @@ -349,9 +339,7 @@ bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { ProcessMetrics::ProcessMetrics(ProcessHandle process, ProcessMetrics::PortProvider* port_provider) : process_(process), - last_cpu_time_(0), last_system_time_(0), - last_idle_wakeups_time_(0), last_absolute_idle_wakeups_(0), port_provider_(port_provider) { processor_count_ = SysInfo::NumberOfProcessors(); diff --git a/base/process/process_metrics_openbsd.cc b/base/process/process_metrics_openbsd.cc index a374362..72927a1 100644 --- a/base/process/process_metrics_openbsd.cc +++ b/base/process/process_metrics_openbsd.cc @@ -106,22 +106,16 @@ static int GetProcessCPU(pid_t pid) { } double ProcessMetrics::GetCPUUsage() { - struct timeval now; + TimeTicks time = TimeTicks::Now(); - int retval = gettimeofday(&now, NULL); - if (retval) - return 0; - - int64 time = TimeValToMicroseconds(now); - - if (last_cpu_time_ == 0) { + if (last_cpu_ == 0) { // First call, just set the last values. last_cpu_time_ = time; last_cpu_ = GetProcessCPU(process_); return 0; } - int64 time_delta = time - last_cpu_time_; + int64 time_delta = (time - last_cpu_time_).InMicroseconds(); DCHECK_NE(time_delta, 0); if (time_delta == 0) @@ -139,7 +133,6 @@ double ProcessMetrics::GetCPUUsage() { ProcessMetrics::ProcessMetrics(ProcessHandle process) : process_(process), - last_cpu_time_(0), last_system_time_(0), last_cpu_(0) { diff --git a/base/process/process_metrics_win.cc b/base/process/process_metrics_win.cc index 0dd19cb..b1810b4 100644 --- a/base/process/process_metrics_win.cc +++ b/base/process/process_metrics_win.cc @@ -195,14 +195,11 @@ static uint64 FileTimeToUTC(const FILETIME& ftime) { } double ProcessMetrics::GetCPUUsage() { - FILETIME now; FILETIME creation_time; FILETIME exit_time; FILETIME kernel_time; FILETIME user_time; - GetSystemTimeAsFileTime(&now); - if (!GetProcessTimes(process_, &creation_time, &exit_time, &kernel_time, &user_time)) { // We don't assert here because in some cases (such as in the Task Manager) @@ -212,9 +209,9 @@ double ProcessMetrics::GetCPUUsage() { } int64 system_time = (FileTimeToUTC(kernel_time) + FileTimeToUTC(user_time)) / processor_count_; - int64 time = FileTimeToUTC(now); + TimeTicks time = TimeTicks::Now(); - if ((last_system_time_ == 0) || (last_cpu_time_ == 0)) { + if (last_system_time_ == 0) { // First call, just set the last values. last_system_time_ = system_time; last_cpu_time_ = time; @@ -222,7 +219,8 @@ double ProcessMetrics::GetCPUUsage() { } int64 system_time_delta = system_time - last_system_time_; - int64 time_delta = time - last_cpu_time_; + // FILETIME is in 100-nanosecond units, so this needs microseconds times 10. + int64 time_delta = (time - last_cpu_time_).InMicroseconds() * 10; DCHECK_NE(0U, time_delta); if (time_delta == 0) return 0; @@ -269,7 +267,6 @@ bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { ProcessMetrics::ProcessMetrics(ProcessHandle process) : process_(process), processor_count_(base::SysInfo::NumberOfProcessors()), - last_cpu_time_(0), last_system_time_(0) { } |