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 <cstdio> | |
| 8 #include <iostream> | |
| 9 | |
| 10 #include "base/file_util.h" | |
| 11 #include "base/format_macros.h" | |
| 12 | |
| 7 namespace extensions { | 13 namespace extensions { |
| 8 | 14 |
| 15 namespace { | |
| 16 | |
| 17 const char kProcStat[] = "/proc/stat"; | |
| 18 | |
| 19 } // namespace | |
| 20 | |
| 9 bool CpuInfoProvider::QueryCpuTimePerProcessor(std::vector<CpuTime>* times) { | 21 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.
| |
| 10 // TODO(hongbo): Query the cpu time from /proc/stat. | 22 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
| |
| 11 return false; | 23 |
| 24 std::string contents; | |
| 25 if (!file_util::ReadFileToString(FilePath(kProcStat), &contents)) | |
| 26 return false; | |
| 27 | |
| 28 std::istringstream iss(contents); | |
| 29 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
| |
| 30 uint64 user = 0, nice = 0, sys = 0, idle = 0; | |
| 31 std::string line; | |
| 32 while (std::getline(iss, line)) { | |
| 33 ++count; | |
| 34 // 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.
| |
| 35 // all cpuN lines, or the line not starting 'cpu' string. | |
| 36 if (count == 1 || line.compare(0, 3, "cpu") != 0) | |
| 37 continue; | |
| 38 | |
| 39 sscanf(line.c_str(), "%*s %"PRIu64" %"PRIu64" %"PRIu64" %"PRIu64, | |
| 40 &user, &nice, &sys, &idle); | |
| 41 | |
| 42 CpuTime time; | |
| 43 time.kernel = sys; | |
| 44 time.user = user + nice; | |
| 45 time.idle = idle; | |
| 46 results.push_back(time); | |
| 47 } | |
| 48 times->swap(results); | |
| 49 return true; | |
| 12 } | 50 } |
| 13 | 51 |
| 14 } // namespace extensions | 52 } // namespace extensions |
| OLD | NEW |