Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: chrome/browser/extensions/api/system_info_cpu/cpu_info_provider_linux.cc

Issue 10907139: Implement querying CPU time from /proc/stat on Linux for systemInfo.cpu API (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698