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; |
} |