Chromium Code Reviews| 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..5a60c5c2f953f633814f9cc5624be5f550b3bb83 100644 |
| --- a/base/process/process_metrics_linux.cc |
| +++ b/base/process/process_metrics_linux.cc |
| @@ -399,16 +399,40 @@ 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)) |
| + // |input| may be empty if the process disappeared somehow. |
| + // e.g. http://crbug.com/145811. |
| + if (input.empty()) |
| return -1; |
| - if (proc_stats.size() <= internal::VM_STIME) |
| + size_t start = input.find_last_of(')'); |
| + if (start == input.npos) |
| return -1; |
| - int utime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_UTIME); |
| - int stime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_STIME); |
| + |
| + // Number of spaces remaining until reaching utime's index starting after the |
| + // last ')'. |
| + int num_spaces_remaining = internal::VM_UTIME - 1; |
| + int utime_start_index = 0; |
| + |
| + size_t i = start; |
| + while ((i = input.find(' ', i + 1)) != input.npos) { |
| + // Validate the assumption that there aren't any contiguous spaces |
| + // in |input| before utime. |
| + DCHECK_NE(input[i - 1], ' '); |
| + if (--num_spaces_remaining == 0) { |
| + utime_start_index = i; |
|
danakj
2015/02/04 19:52:44
cool thanks. one more comment, i think we could sa
afakhry
2015/02/04 20:07:05
I like that! :) Done!
|
| + break; |
| + } |
| + } |
| + |
| + if (utime_start_index == 0) |
| + 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; |
| } |