Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h" | 5 #include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | |
| 8 | |
| 7 namespace extensions { | 9 namespace extensions { |
| 8 | 10 |
| 9 bool CpuInfoProvider::QueryCpuTimePerProcessor(std::vector<CpuTime>* times) { | 11 bool CpuInfoProvider::QueryCpuTimePerProcessor(std::vector<CpuTime>* times) { |
| 10 // TODO(hongbo): Query the cpu time from /proc/stat. | 12 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
| |
| 11 return false; | 13 |
| 14 times->clear(); | |
| 15 | |
| 16 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.
| |
| 17 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.
| |
| 18 if (!file_util::ReadFileToString(FilePath(kProcStat), &contents)) | |
| 19 return false; | |
| 20 | |
| 21 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.
| |
| 22 int count = 0; | |
| 23 | |
| 24 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.
| |
| 25 user = nice = sys = idle = 0; | |
| 26 | |
| 27 while (std::getline(iss, line)) { | |
| 28 // 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
| |
| 29 // all cpuN lines. | |
| 30 if (count++ == 0) continue; | |
| 31 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.
| |
| 32 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.
| |
| 33 &user, &nice, &sys, &idle); | |
| 34 | |
| 35 CpuTime time; | |
| 36 time.kernel = sys; | |
| 37 time.user = user + nice; | |
| 38 time.idle = idle; | |
| 39 times->push_back(time); | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 return true; | |
| 12 } | 44 } |
| 13 | 45 |
| 14 } // namespace extensions | 46 } // namespace extensions |
| OLD | NEW |