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

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
« no previous file with comments | « no previous file | base/process/process_metrics_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..76df1f49e385b017b946c8b2d54e1fc5ace5b00c 100644
--- a/base/process/process_metrics_linux.cc
+++ b/base/process/process_metrics_linux.cc
@@ -399,16 +399,42 @@ size_t GetSystemCommitCharge() {
return meminfo.total - meminfo.free - meminfo.buffers - meminfo.cached;
}
-// Exposed for testing.
int ParseProcStatCPU(const std::string& input) {
- std::vector<std::string> proc_stats;
- if (!internal::ParseProcStats(input, &proc_stats))
+ if (input.empty())
return -1;
- if (proc_stats.size() <= internal::VM_STIME)
+ int j = 0;
+ int last_space_index = 0;
+ int utime_start_index = 0;
+ bool prev_was_space = false;
+ for (auto& ch : input) {
+ if (ch == ' ') {
+ if (!prev_was_space)
+ ++last_space_index;
+
+ prev_was_space = true;
+ } else {
+ if (ch == ')') {
+ // Restart the counter, we're looking to count from the last ')' we
danakj 2015/02/03 20:35:51 Can you use input.rfind to get the last ')'? This
afakhry 2015/02/03 21:21:59 I understand the need for readability. But this is
+ // encounter.
+ last_space_index = 0;
+ } else if (prev_was_space && last_space_index == internal::VM_UTIME - 2) {
+ utime_start_index = j + 1;
+ break;
+ }
+ prev_was_space = false;
+ }
+ ++j;
+ }
+
+ if (last_space_index < internal::VM_UTIME - 2)
return -1;
- int utime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_UTIME);
- int stime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_STIME);
+
+ int utime = 0;
+ int stime = 0;
+ if (sscanf(&input.data()[utime_start_index], "%d %d", &utime, &stime) != 2)
+ return -1;
+
return utime + stime;
}
« no previous file with comments | « no previous file | base/process/process_metrics_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698