// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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) {
  DCHECK(times);

  std::string contents;
  if (!file_util::ReadFileToString(FilePath(kProcStat), &contents))
     return false;

  std::istringstream iss(contents);
  uint64 user = 0, nice = 0, sys = 0, idle = 0;
  std::string line;

  // Skip the first line because it is just an aggregated number of
  // all cpuN lines.
  std::getline(iss, line);
  std::vector<CpuTime> results;
  while (std::getline(iss, line)) {
    if (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

// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h"

namespace extensions {

bool CpuInfoProvider::QueryCpuTimePerProcessor(std::vector<CpuTime>* times) {
  // TODO(hongbo): Query the cpu time from /proc/stat.
  return false;
}

}  // namespace extensions

--- 
+++ 
@@ -4,11 +4,50 @@
 
 #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) {
-  // TODO(hongbo): Query the cpu time from /proc/stat.
-  return false;
+  DCHECK(times);
+
+  std::string contents;
+  if (!file_util::ReadFileToString(FilePath(kProcStat), &contents))
+     return false;
+
+  std::istringstream iss(contents);
+  uint64 user = 0, nice = 0, sys = 0, idle = 0;
+  std::string line;
+
+  // Skip the first line because it is just an aggregated number of
+  // all cpuN lines.
+  std::getline(iss, line);
+  std::vector<CpuTime> results;
+  while (std::getline(iss, line)) {
+    if (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
Implement querying CPU time from /proc/stat on Linux for systemInfo.cpu API


BUG=136519
TEST=None

Review URL: https://chromiumcodereview.appspot.com/10907139

git-svn-id: de016e52bd170d2d4f2344f9bf92d50478b649e0@157984 0039d316-1c4b-4281-b951-d872f2087c98
