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..5e911c1064a5745156ae2728cfdb355b55f0711c 100644 |
| --- a/base/process/process_metrics_linux.cc |
| +++ b/base/process/process_metrics_linux.cc |
| @@ -399,16 +399,38 @@ 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 last_space_index = 0; |
| + int utime_start_index = 0; |
| + bool prev_was_space = false; |
| + auto start = input.rfind(')'); |
|
danakj
2015/02/04 00:19:11
Thanks. Did you consider a regular expression for
|
| + for (auto j = start; j < input.size(); ++j) { |
| + if (input[j] == ' ') { |
| + if (!prev_was_space) |
| + ++last_space_index; |
| + |
| + prev_was_space = true; |
| + } else { |
| + if (prev_was_space && last_space_index == internal::VM_UTIME - 2) { |
| + utime_start_index = j + 1; |
| + break; |
| + } |
| + |
| + prev_was_space = false; |
| + } |
| + } |
| + |
| + if (last_space_index < internal::VM_UTIME - 2) |
| + return -1; |
| + |
| + int utime = 0; |
| + int stime = 0; |
| + if (sscanf(&input.data()[utime_start_index], "%d %d", &utime, &stime) != 2) |
| return -1; |
| - int utime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_UTIME); |
| - int stime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_STIME); |
| + |
| return utime + stime; |
| } |