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..76df1f49e385b017b946c8b2d54e1fc5ace5b00c 100644 |
| --- a/base/process/process_metrics_linux.cc |
| +++ b/base/process/process_metrics_linux.cc |
| @@ -399,16 +399,42 @@ 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 j = 0; |
| + int last_space_index = 0; |
| + int utime_start_index = 0; |
| + bool prev_was_space = false; |
| + for (auto& ch : input) { |
| + if (ch == ' ') { |
| + if (!prev_was_space) |
| + ++last_space_index; |
| + |
| + prev_was_space = true; |
| + } else { |
| + if (ch == ')') { |
| + // Restart the counter, we're looking to count from the last ')' we |
|
danakj
2015/02/03 20:35:51
Can you use input.rfind to get the last ')'?
This
afakhry
2015/02/03 21:21:59
I understand the need for readability. But this is
|
| + // encounter. |
| + last_space_index = 0; |
| + } else if (prev_was_space && last_space_index == internal::VM_UTIME - 2) { |
| + utime_start_index = j + 1; |
| + break; |
| + } |
| + prev_was_space = false; |
| + } |
| + ++j; |
| + } |
| + |
| + if (last_space_index < internal::VM_UTIME - 2) |
| return -1; |
| - int utime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_UTIME); |
| - int stime = GetProcStatsFieldAsInt64(proc_stats, internal::VM_STIME); |
| + |
| + int utime = 0; |
| + int stime = 0; |
| + if (sscanf(&input.data()[utime_start_index], "%d %d", &utime, &stime) != 2) |
| + return -1; |
| + |
| return utime + stime; |
| } |