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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..4b09dd14d3fc03aae2d5dda20a5ac69743a2b706 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,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
« 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