| 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 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 | 160 |
| 161 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { | 161 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { |
| 162 #if defined(OS_CHROMEOS) | 162 #if defined(OS_CHROMEOS) |
| 163 if (GetWorkingSetKBytesTotmaps(ws_usage)) | 163 if (GetWorkingSetKBytesTotmaps(ws_usage)) |
| 164 return true; | 164 return true; |
| 165 #endif | 165 #endif |
| 166 return GetWorkingSetKBytesStatm(ws_usage); | 166 return GetWorkingSetKBytesStatm(ws_usage); |
| 167 } | 167 } |
| 168 | 168 |
| 169 double ProcessMetrics::GetCPUUsage() { | 169 double ProcessMetrics::GetCPUUsage() { |
| 170 // This queries the /proc-specific scaling factor which is | |
| 171 // conceptually the system hertz. To dump this value on another | |
| 172 // system, try | |
| 173 // od -t dL /proc/self/auxv | |
| 174 // and look for the number after 17 in the output; mine is | |
| 175 // 0000040 17 100 3 134512692 | |
| 176 // which means the answer is 100. | |
| 177 // It may be the case that this value is always 100. | |
| 178 static const int kHertz = sysconf(_SC_CLK_TCK); | |
| 179 | |
| 180 struct timeval now; | 170 struct timeval now; |
| 181 int retval = gettimeofday(&now, NULL); | 171 int retval = gettimeofday(&now, NULL); |
| 182 if (retval) | 172 if (retval) |
| 183 return 0; | 173 return 0; |
| 184 int64 time = TimeValToMicroseconds(now); | 174 int64 time = TimeValToMicroseconds(now); |
| 185 | 175 |
| 186 if (last_time_ == 0) { | 176 if (last_time_ == 0) { |
| 187 // First call, just set the last values. | 177 // First call, just set the last values. |
| 188 last_time_ = time; | 178 last_time_ = time; |
| 189 last_cpu_ = GetProcessCPU(process_); | 179 last_cpu_ = GetProcessCPU(process_); |
| 190 return 0; | 180 return 0; |
| 191 } | 181 } |
| 192 | 182 |
| 193 int64 time_delta = time - last_time_; | 183 int64 time_delta = time - last_time_; |
| 194 DCHECK_NE(time_delta, 0); | 184 DCHECK_NE(time_delta, 0); |
| 195 if (time_delta == 0) | 185 if (time_delta == 0) |
| 196 return 0; | 186 return 0; |
| 197 | 187 |
| 198 int cpu = GetProcessCPU(process_); | 188 int cpu = GetProcessCPU(process_); |
| 199 | 189 |
| 200 // We have the number of jiffies in the time period. Convert to percentage. | 190 // We have the number of jiffies in the time period. Convert to percentage. |
| 201 // Note this means we will go *over* 100 in the case where multiple threads | 191 // Note this means we will go *over* 100 in the case where multiple threads |
| 202 // are together adding to more than one CPU's worth. | 192 // are together adding to more than one CPU's worth. |
| 203 int percentage = 100 * (cpu - last_cpu_) / | 193 TimeDelta cpu_time = internal::ClockTicksToTimeDelta(cpu); |
| 204 (kHertz * TimeDelta::FromMicroseconds(time_delta).InSecondsF()); | 194 TimeDelta last_cpu_time = internal::ClockTicksToTimeDelta(last_cpu_); |
| 195 int percentage = 100 * (cpu_time - last_cpu_time).InSecondsF() / |
| 196 TimeDelta::FromMicroseconds(time_delta).InSecondsF(); |
| 205 | 197 |
| 206 last_time_ = time; | 198 last_time_ = time; |
| 207 last_cpu_ = cpu; | 199 last_cpu_ = cpu; |
| 208 | 200 |
| 209 return percentage; | 201 return percentage; |
| 210 } | 202 } |
| 211 | 203 |
| 212 // To have /proc/self/io file you must enable CONFIG_TASK_IO_ACCOUNTING | 204 // To have /proc/self/io file you must enable CONFIG_TASK_IO_ACCOUNTING |
| 213 // in your kernel configuration. | 205 // in your kernel configuration. |
| 214 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { | 206 bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const { |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 } | 507 } |
| 516 | 508 |
| 517 const char kProcSelfExe[] = "/proc/self/exe"; | 509 const char kProcSelfExe[] = "/proc/self/exe"; |
| 518 | 510 |
| 519 int GetNumberOfThreads(ProcessHandle process) { | 511 int GetNumberOfThreads(ProcessHandle process) { |
| 520 return internal::ReadProcStatsAndGetFieldAsInt(process, | 512 return internal::ReadProcStatsAndGetFieldAsInt(process, |
| 521 internal::VM_NUMTHREADS); | 513 internal::VM_NUMTHREADS); |
| 522 } | 514 } |
| 523 | 515 |
| 524 } // namespace base | 516 } // namespace base |
| OLD | NEW |