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..86ddfa3f8dea4bcbd26e37db5f2b84c8c4eabf67 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,43 @@ |
| #include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h" |
| +#include "base/file_util.h" |
| + |
| namespace extensions { |
| bool CpuInfoProvider::QueryCpuTimePerProcessor(std::vector<CpuTime>* times) { |
| - // TODO(hongbo): Query the cpu time from /proc/stat. |
| - return false; |
| + if (!times) return false; |
|
Mihai Parparita -not on Chrome
2012/09/16 06:01:22
Why the soft failure when times is null? A CHECK s
Hongbo Min
2012/09/16 15:14:43
No CHECK is used since access violation can help d
|
| + |
| + times->clear(); |
| + |
| + const char* kProcStat = "/proc/stat"; |
|
Mihai Parparita -not on Chrome
2012/09/16 06:01:22
Move this to an anonymous namespace at the head of
Hongbo Min
2012/09/16 15:14:43
Done.
|
| + std::string contents, line; |
|
Mihai Parparita -not on Chrome
2012/09/16 06:01:22
"line" isn't used until later, its declaration sho
Hongbo Min
2012/09/16 15:14:43
Done.
|
| + if (!file_util::ReadFileToString(FilePath(kProcStat), &contents)) |
| + return false; |
| + |
| + std::istringstream iss(contents); |
|
Mihai Parparita -not on Chrome
2012/09/16 06:01:22
Needs an #include.
Hongbo Min
2012/09/16 15:14:43
Done.
|
| + int count = 0; |
| + |
| + unsigned long long user, nice, sys, idle; |
|
Mihai Parparita -not on Chrome
2012/09/16 06:01:22
Use uint64 (see http://google-styleguide.googlecod
Hongbo Min
2012/09/16 15:14:43
Done.
|
| + user = nice = sys = idle = 0; |
| + |
| + while (std::getline(iss, line)) { |
| + // Ignore the first line because it is just an aggregated number of |
|
Mihai Parparita -not on Chrome
2012/09/16 06:07:47
Also, given this behavior, you may want to remove
Hongbo Min
2012/09/16 15:14:43
Done. Correct the comment in the cpu_info_provider
|
| + // all cpuN lines. |
| + if (count++ == 0) continue; |
| + if (line.compare(0, 3, "cpu") == 0) { |
|
Mihai Parparita -not on Chrome
2012/09/16 06:01:22
Turn this into a "if (line.compare(0, 3, "cpu") !=
Hongbo Min
2012/09/16 15:14:43
Done.
Hongbo Min
2012/09/16 15:14:43
Done.
|
| + sscanf(line.c_str(), "%*s %llu %llu %llu %llu", |
|
Mihai Parparita -not on Chrome
2012/09/16 06:01:22
Needs an #include
Hongbo Min
2012/09/16 15:14:43
Done.
|
| + &user, &nice, &sys, &idle); |
| + |
| + CpuTime time; |
| + time.kernel = sys; |
| + time.user = user + nice; |
| + time.idle = idle; |
| + times->push_back(time); |
| + } |
| + } |
| + |
| + return true; |
| } |
| } // namespace extensions |