| 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 <dirent.h> | 7 #include <dirent.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
| 10 #include <sys/time.h> | 10 #include <sys/time.h> |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 | 174 |
| 175 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { | 175 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { |
| 176 #if defined(OS_CHROMEOS) | 176 #if defined(OS_CHROMEOS) |
| 177 if (GetWorkingSetKBytesTotmaps(ws_usage)) | 177 if (GetWorkingSetKBytesTotmaps(ws_usage)) |
| 178 return true; | 178 return true; |
| 179 #endif | 179 #endif |
| 180 return GetWorkingSetKBytesStatm(ws_usage); | 180 return GetWorkingSetKBytesStatm(ws_usage); |
| 181 } | 181 } |
| 182 | 182 |
| 183 double ProcessMetrics::GetCPUUsage() { | 183 double ProcessMetrics::GetCPUUsage() { |
| 184 struct timeval now; | 184 TimeTicks time = TimeTicks::Now(); |
| 185 int retval = gettimeofday(&now, NULL); | |
| 186 if (retval) | |
| 187 return 0; | |
| 188 int64 time = TimeValToMicroseconds(now); | |
| 189 | 185 |
| 190 if (last_cpu_time_ == 0) { | 186 if (last_cpu_ == 0) { |
| 191 // First call, just set the last values. | 187 // First call, just set the last values. |
| 192 last_cpu_time_ = time; | 188 last_cpu_walltime_ = time; |
| 193 last_cpu_ = GetProcessCPU(process_); | 189 last_cpu_ = GetProcessCPU(process_); |
| 194 return 0; | 190 return 0; |
| 195 } | 191 } |
| 196 | 192 |
| 197 int64 time_delta = time - last_cpu_time_; | 193 int64 time_delta = (time - last_cpu_walltime_).InMicroseconds(); |
| 198 DCHECK_NE(time_delta, 0); | 194 DCHECK_NE(time_delta, 0); |
| 199 if (time_delta == 0) | 195 if (time_delta == 0) |
| 200 return 0; | 196 return 0; |
| 201 | 197 |
| 202 int cpu = GetProcessCPU(process_); | 198 int cpu = GetProcessCPU(process_); |
| 203 | 199 |
| 204 // We have the number of jiffies in the time period. Convert to percentage. | 200 // We have the number of jiffies in the time period. Convert to percentage. |
| 205 // Note this means we will go *over* 100 in the case where multiple threads | 201 // Note this means we will go *over* 100 in the case where multiple threads |
| 206 // are together adding to more than one CPU's worth. | 202 // are together adding to more than one CPU's worth. |
| 207 TimeDelta cpu_time = internal::ClockTicksToTimeDelta(cpu); | 203 TimeDelta cpu_time = internal::ClockTicksToTimeDelta(cpu); |
| 208 TimeDelta last_cpu_time = internal::ClockTicksToTimeDelta(last_cpu_); | 204 TimeDelta last_cpu_time = internal::ClockTicksToTimeDelta(last_cpu_); |
| 209 int percentage = 100 * (cpu_time - last_cpu_time).InSecondsF() / | 205 int percentage = 100 * (cpu_time - last_cpu_time).InSecondsF() / |
| 210 TimeDelta::FromMicroseconds(time_delta).InSecondsF(); | 206 TimeDelta::FromMicroseconds(time_delta).InSecondsF(); |
| 211 | 207 |
| 212 last_cpu_time_ = time; | 208 last_cpu_walltime_ = time; |
| 213 last_cpu_ = cpu; | 209 last_cpu_ = cpu; |
| 214 | 210 |
| 215 return percentage; | 211 return percentage; |
| 216 } | 212 } |
| 217 | 213 |
| 218 // To have /proc/self/io file you must enable CONFIG_TASK_IO_ACCOUNTING | 214 // To have /proc/self/io file you must enable CONFIG_TASK_IO_ACCOUNTING |
| 219 // in your kernel configuration. | 215 // in your kernel configuration. |
| 220 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { | 216 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { |
| 221 // Synchronously reading files in /proc is safe. | 217 // Synchronously reading files in /proc is safe. |
| 222 ThreadRestrictions::ScopedAllowIO allow_io; | 218 ThreadRestrictions::ScopedAllowIO allow_io; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 } | 251 } |
| 256 state = KEY_NAME; | 252 state = KEY_NAME; |
| 257 break; | 253 break; |
| 258 } | 254 } |
| 259 } | 255 } |
| 260 return true; | 256 return true; |
| 261 } | 257 } |
| 262 | 258 |
| 263 ProcessMetrics::ProcessMetrics(ProcessHandle process) | 259 ProcessMetrics::ProcessMetrics(ProcessHandle process) |
| 264 : process_(process), | 260 : process_(process), |
| 265 last_cpu_time_(0), | |
| 266 last_system_time_(0), | 261 last_system_time_(0), |
| 267 last_cpu_(0) { | 262 last_cpu_(0) { |
| 268 processor_count_ = base::SysInfo::NumberOfProcessors(); | 263 processor_count_ = base::SysInfo::NumberOfProcessors(); |
| 269 } | 264 } |
| 270 | 265 |
| 271 #if defined(OS_CHROMEOS) | 266 #if defined(OS_CHROMEOS) |
| 272 // Private, Shared and Proportional working set sizes are obtained from | 267 // Private, Shared and Proportional working set sizes are obtained from |
| 273 // /proc/<pid>/totmaps | 268 // /proc/<pid>/totmaps |
| 274 bool ProcessMetrics::GetWorkingSetKBytesTotmaps(WorkingSetKBytes *ws_usage) | 269 bool ProcessMetrics::GetWorkingSetKBytesTotmaps(WorkingSetKBytes *ws_usage) |
| 275 const { | 270 const { |
| (...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 874 swap_info->num_reads = ReadFileToUint64(zram_path.Append("num_reads")); | 869 swap_info->num_reads = ReadFileToUint64(zram_path.Append("num_reads")); |
| 875 swap_info->num_writes = ReadFileToUint64(zram_path.Append("num_writes")); | 870 swap_info->num_writes = ReadFileToUint64(zram_path.Append("num_writes")); |
| 876 swap_info->compr_data_size = | 871 swap_info->compr_data_size = |
| 877 ReadFileToUint64(zram_path.Append("compr_data_size")); | 872 ReadFileToUint64(zram_path.Append("compr_data_size")); |
| 878 swap_info->mem_used_total = | 873 swap_info->mem_used_total = |
| 879 ReadFileToUint64(zram_path.Append("mem_used_total")); | 874 ReadFileToUint64(zram_path.Append("mem_used_total")); |
| 880 } | 875 } |
| 881 #endif // defined(OS_CHROMEOS) | 876 #endif // defined(OS_CHROMEOS) |
| 882 | 877 |
| 883 } // namespace base | 878 } // namespace base |
| OLD | NEW |