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

Unified Diff: base/process/process_metrics_linux.cc

Issue 888193002: ProcessMetrics::GetCPUUsage() is Slow (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 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
Index: base/process/process_metrics_linux.cc
diff --git a/base/process/process_metrics_linux.cc b/base/process/process_metrics_linux.cc
index e8db571a317b380b17e7efc5c0595bce35faffb8..e72b8b4e424e5bcc13ecd83fc675f977a5247f54 100644
--- a/base/process/process_metrics_linux.cc
+++ b/base/process/process_metrics_linux.cc
@@ -144,7 +144,7 @@ int GetProcessCPU(pid_t pid) {
FilePath stat_path =
task_path.Append(ent->d_name).Append(internal::kStatFile);
if (ReadFileToString(stat_path, &stat)) {
- int cpu = ParseProcStatCPU(stat);
+ int cpu = GetCpuFromProcStats(stat);
if (cpu > 0)
total_cpu += cpu;
}
@@ -412,6 +412,46 @@ int ParseProcStatCPU(const std::string& input) {
return utime + stime;
}
+// Exposed for testing.
danakj 2015/02/03 18:51:09 this comment is in a weird place, itd expect it in
afakhry 2015/02/03 20:23:54 I was following the same style of the original Par
+int GetCpuFromProcStats(const std::string& input) {
+ if (input.empty())
+ return -1;
+
+ int j = 0;
+ int last_space_index = 0;
+ int utime_start_index = 0;
+ bool prevWasSpace = false;
danakj 2015/02/03 18:51:09 fix style
afakhry 2015/02/03 20:23:54 Done.
+ for (auto& ch : input) {
+ if (ch == ' ') {
+ if (!prevWasSpace)
+ ++last_space_index;
+
+ prevWasSpace = true;
+ } else {
+ if (ch == ')') {
+ // Restart the counter, we're looking to count from the last ')' we
+ // encounter.
+ last_space_index = 0;
+ } else if (prevWasSpace && last_space_index == internal::VM_UTIME - 2) {
+ utime_start_index = j + 1;
+ break;
+ }
+ prevWasSpace = false;
+ }
+ ++j;
+ }
+
+ if (last_space_index < internal::VM_UTIME - 2)
+ return -1;
+
+ int utime = 0;
+ int stime = 0;
+ if (sscanf(&input.data()[utime_start_index], "%d %d", &utime, &stime) != 2)
+ return -1;
+
+ return utime + stime;
+}
+
const char kProcSelfExe[] = "/proc/self/exe";
int GetNumberOfThreads(ProcessHandle process) {

Powered by Google App Engine
This is Rietveld 408576698