Chromium Code Reviews| Index: chrome/browser/extensions/api/system_info_cpu/cpu_info_provider_linux.cc |
| diff --git a/chrome/browser/extensions/api/system_info_cpu/cpu_info_provider_linux.cc b/chrome/browser/extensions/api/system_info_cpu/cpu_info_provider_linux.cc |
| index 74b71637d8a60295d80222e4c7d0d87c6777912f..b2e2ec5ada425621036a1478856689891c16227a 100644 |
| --- a/chrome/browser/extensions/api/system_info_cpu/cpu_info_provider_linux.cc |
| +++ b/chrome/browser/extensions/api/system_info_cpu/cpu_info_provider_linux.cc |
| @@ -4,11 +4,49 @@ |
| #include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h" |
| +#include <cstdio> |
| +#include <iostream> |
| + |
| +#include "base/file_util.h" |
| +#include "base/format_macros.h" |
| + |
| namespace extensions { |
| +namespace { |
| + |
| +const char kProcStat[] = "/proc/stat"; |
| + |
| +} // namespace |
| + |
| bool CpuInfoProvider::QueryCpuTimePerProcessor(std::vector<CpuTime>* times) { |
|
benwells
2012/09/21 07:10:10
DCHECK times here. This provides
(a) a statement o
Hongbo Min
2012/09/21 08:30:56
Done.
|
| - // TODO(hongbo): Query the cpu time from /proc/stat. |
| - return false; |
| + std::vector<CpuTime> results; |
|
benwells
2012/09/21 07:10:10
Move results down to just before while loop / afte
Hongbo Min
2012/09/21 08:30:56
It is just a coding preference. See http://coderev
|
| + |
| + std::string contents; |
| + if (!file_util::ReadFileToString(FilePath(kProcStat), &contents)) |
| + return false; |
| + |
| + std::istringstream iss(contents); |
| + int count = 0; |
|
benwells
2012/09/21 07:10:10
Replace int count with bool first, since you just
Hongbo Min
2012/09/21 08:30:56
Remove it and call std::getline before the loop to
|
| + uint64 user = 0, nice = 0, sys = 0, idle = 0; |
| + std::string line; |
| + while (std::getline(iss, line)) { |
| + ++count; |
| + // Ignore the first line because it is just an aggregated number of |
|
benwells
2012/09/21 07:10:10
Mihai had asked for a comment in cpu_info_provider
benwells
2012/09/21 08:09:14
Oh, I see it in the next review.
|
| + // all cpuN lines, or the line not starting 'cpu' string. |
| + if (count == 1 || line.compare(0, 3, "cpu") != 0) |
| + continue; |
| + |
| + sscanf(line.c_str(), "%*s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, |
| + &user, &nice, &sys, &idle); |
| + |
| + CpuTime time; |
| + time.kernel = sys; |
| + time.user = user + nice; |
| + time.idle = idle; |
| + results.push_back(time); |
| + } |
| + times->swap(results); |
| + return true; |
| } |
| } // namespace extensions |