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..0d923af50e3d1112680f585f7832a3492b6dc560 100644 |
| --- a/base/process/process_metrics_linux.cc |
| +++ b/base/process/process_metrics_linux.cc |
| @@ -399,16 +399,37 @@ 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) { |
| + if (input[i - 1] != ' ' && --num_spaces_remaining == 0) { |
|
danakj
2015/02/04 18:33:06
can you leave a comment like // Skips past contigu
afakhry
2015/02/04 19:05:20
It's unlikey but it's better be safe. The original
|
| + utime_start_index = i; |
| + break; |
| + } |
| + } |
| + |
| + if (num_spaces_remaining != 0) |
|
danakj
2015/02/04 18:33:06
nit: maybe slightly more obvious to check utime_st
afakhry
2015/02/04 19:05:20
Done.
|
| + 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; |
| } |