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

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: This is probably a more readable version 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..a5b36119653983466175562d063ce1bf7710a54a 100644
--- a/base/process/process_metrics_linux.cc
+++ b/base/process/process_metrics_linux.cc
@@ -399,16 +399,36 @@ 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())
danakj 2015/02/04 01:29:18 Can you leave a comment like ParseProcStats? // |
afakhry 2015/02/04 02:31:22 Done.
return -1;
- if (proc_stats.size() <= internal::VM_STIME)
+ // The index of the last space before 'utime', taking the space after the last
+ // ')' as the beginning.
+ int last_space_index = internal::VM_UTIME - 1;
danakj 2015/02/04 01:29:18 why do you call this index? they seem like counts,
afakhry 2015/02/04 02:31:22 Renamed.
+ int utime_start_index = 0;
oshima 2015/02/04 01:49:46 move these variable after next if block
afakhry 2015/02/04 02:31:22 Done.
+ auto start = input.find_last_of(')');
oshima 2015/02/04 01:49:46 just use size_t. it isn't worth it imo.
afakhry 2015/02/04 02:31:22 Done.
+ if (start == input.npos || start == 0)
danakj 2015/02/04 01:29:18 why do you have to check for 0 here?
return -1;
- int utime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_UTIME);
- int stime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_STIME);
+
+ auto end = input.size() - 1;
danakj 2015/02/04 01:29:18 can you call this last instead of end? end is usua
afakhry 2015/02/04 02:31:22 Done.
+ for (auto j = start + 1; j < end; ++j) {
danakj 2015/02/04 01:29:18 a nit, but why j instead of i? usually we'd use i
+ if (input[j] != ' ' && input[j - 1] == ' ') {
danakj 2015/02/04 01:29:18 could you just string::find(" ") from the current
oshima 2015/02/04 01:49:46 I think he wanted to deal with two space case. I'm
danakj 2015/02/04 01:52:54 Can just skip past contiguous spaces if that can h
oshima 2015/02/04 02:28:37 That's fine too. I don't have much preference.
afakhry 2015/02/04 02:31:22 Done.
+ if (--last_space_index == 0) {
+ utime_start_index = j;
+ break;
+ }
oshima 2015/02/04 01:49:46 optional: you can actually increase j here because
+ }
+ }
+
+ if (last_space_index != 0)
+ return -1;
+
+ int utime = 0;
+ int stime = 0;
+ if (sscanf(&input.data()[utime_start_index], "%d %d", &utime, &stime) != 2)
danakj 2015/02/04 01:29:18 Why not StringToInt instead of sscanf?
oshima 2015/02/04 02:28:37 Is there any issue with sscanf?
afakhry 2015/02/04 02:31:22 Because it's much simpler. No additional parsing i
+ 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