| 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 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 | 175 |
| 176 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { | 176 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { |
| 177 #if defined(OS_CHROMEOS) | 177 #if defined(OS_CHROMEOS) |
| 178 if (GetWorkingSetKBytesTotmaps(ws_usage)) | 178 if (GetWorkingSetKBytesTotmaps(ws_usage)) |
| 179 return true; | 179 return true; |
| 180 #endif | 180 #endif |
| 181 return GetWorkingSetKBytesStatm(ws_usage); | 181 return GetWorkingSetKBytesStatm(ws_usage); |
| 182 } | 182 } |
| 183 | 183 |
| 184 double ProcessMetrics::GetCPUUsage() { | 184 double ProcessMetrics::GetCPUUsage() { |
| 185 struct timeval now; | 185 TimeTicks time = TimeTicks::Now(); |
| 186 int retval = gettimeofday(&now, NULL); | |
| 187 if (retval) | |
| 188 return 0; | |
| 189 int64 time = TimeValToMicroseconds(now); | |
| 190 | 186 |
| 191 if (last_cpu_time_ == 0) { | 187 if (last_cpu_ == 0) { |
| 192 // First call, just set the last values. | 188 // First call, just set the last values. |
| 193 last_cpu_time_ = time; | 189 last_cpu_time_ = time; |
| 194 last_cpu_ = GetProcessCPU(process_); | 190 last_cpu_ = GetProcessCPU(process_); |
| 195 return 0; | 191 return 0; |
| 196 } | 192 } |
| 197 | 193 |
| 198 int64 time_delta = time - last_cpu_time_; | 194 int64 time_delta = (time - last_cpu_time_).InMicroseconds(); |
| 199 DCHECK_NE(time_delta, 0); | 195 DCHECK_NE(time_delta, 0); |
| 200 if (time_delta == 0) | 196 if (time_delta == 0) |
| 201 return 0; | 197 return 0; |
| 202 | 198 |
| 203 int cpu = GetProcessCPU(process_); | 199 int cpu = GetProcessCPU(process_); |
| 204 | 200 |
| 205 // We have the number of jiffies in the time period. Convert to percentage. | 201 // We have the number of jiffies in the time period. Convert to percentage. |
| 206 // Note this means we will go *over* 100 in the case where multiple threads | 202 // Note this means we will go *over* 100 in the case where multiple threads |
| 207 // are together adding to more than one CPU's worth. | 203 // are together adding to more than one CPU's worth. |
| 208 TimeDelta cpu_time = internal::ClockTicksToTimeDelta(cpu); | 204 TimeDelta cpu_time = internal::ClockTicksToTimeDelta(cpu); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 } | 252 } |
| 257 state = KEY_NAME; | 253 state = KEY_NAME; |
| 258 break; | 254 break; |
| 259 } | 255 } |
| 260 } | 256 } |
| 261 return true; | 257 return true; |
| 262 } | 258 } |
| 263 | 259 |
| 264 ProcessMetrics::ProcessMetrics(ProcessHandle process) | 260 ProcessMetrics::ProcessMetrics(ProcessHandle process) |
| 265 : process_(process), | 261 : process_(process), |
| 266 last_cpu_time_(0), | |
| 267 last_system_time_(0), | 262 last_system_time_(0), |
| 268 last_cpu_(0) { | 263 last_cpu_(0) { |
| 269 processor_count_ = base::SysInfo::NumberOfProcessors(); | 264 processor_count_ = base::SysInfo::NumberOfProcessors(); |
| 270 } | 265 } |
| 271 | 266 |
| 272 #if defined(OS_CHROMEOS) | 267 #if defined(OS_CHROMEOS) |
| 273 // Private, Shared and Proportional working set sizes are obtained from | 268 // Private, Shared and Proportional working set sizes are obtained from |
| 274 // /proc/<pid>/totmaps | 269 // /proc/<pid>/totmaps |
| 275 bool ProcessMetrics::GetWorkingSetKBytesTotmaps(WorkingSetKBytes *ws_usage) | 270 bool ProcessMetrics::GetWorkingSetKBytesTotmaps(WorkingSetKBytes *ws_usage) |
| 276 const { | 271 const { |
| (...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 875 swap_info->num_reads = ReadFileToUint64(zram_path.Append("num_reads")); | 870 swap_info->num_reads = ReadFileToUint64(zram_path.Append("num_reads")); |
| 876 swap_info->num_writes = ReadFileToUint64(zram_path.Append("num_writes")); | 871 swap_info->num_writes = ReadFileToUint64(zram_path.Append("num_writes")); |
| 877 swap_info->compr_data_size = | 872 swap_info->compr_data_size = |
| 878 ReadFileToUint64(zram_path.Append("compr_data_size")); | 873 ReadFileToUint64(zram_path.Append("compr_data_size")); |
| 879 swap_info->mem_used_total = | 874 swap_info->mem_used_total = |
| 880 ReadFileToUint64(zram_path.Append("mem_used_total")); | 875 ReadFileToUint64(zram_path.Append("mem_used_total")); |
| 881 } | 876 } |
| 882 #endif // defined(OS_CHROMEOS) | 877 #endif // defined(OS_CHROMEOS) |
| 883 | 878 |
| 884 } // namespace base | 879 } // namespace base |
| OLD | NEW |