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..e72b8b4e424e5bcc13ecd83fc675f977a5247f54 100644 |
| --- a/base/process/process_metrics_linux.cc |
| +++ b/base/process/process_metrics_linux.cc |
| @@ -144,7 +144,7 @@ int GetProcessCPU(pid_t pid) { |
| FilePath stat_path = |
| task_path.Append(ent->d_name).Append(internal::kStatFile); |
| if (ReadFileToString(stat_path, &stat)) { |
| - int cpu = ParseProcStatCPU(stat); |
| + int cpu = GetCpuFromProcStats(stat); |
| if (cpu > 0) |
| total_cpu += cpu; |
| } |
| @@ -412,6 +412,46 @@ int ParseProcStatCPU(const std::string& input) { |
| return utime + stime; |
| } |
| +// Exposed for testing. |
|
danakj
2015/02/03 18:51:09
this comment is in a weird place, itd expect it in
afakhry
2015/02/03 20:23:54
I was following the same style of the original Par
|
| +int GetCpuFromProcStats(const std::string& input) { |
| + if (input.empty()) |
| + return -1; |
| + |
| + int j = 0; |
| + int last_space_index = 0; |
| + int utime_start_index = 0; |
| + bool prevWasSpace = false; |
|
danakj
2015/02/03 18:51:09
fix style
afakhry
2015/02/03 20:23:54
Done.
|
| + for (auto& ch : input) { |
| + if (ch == ' ') { |
| + if (!prevWasSpace) |
| + ++last_space_index; |
| + |
| + prevWasSpace = true; |
| + } else { |
| + if (ch == ')') { |
| + // Restart the counter, we're looking to count from the last ')' we |
| + // encounter. |
| + last_space_index = 0; |
| + } else if (prevWasSpace && last_space_index == internal::VM_UTIME - 2) { |
| + utime_start_index = j + 1; |
| + break; |
| + } |
| + prevWasSpace = false; |
| + } |
| + ++j; |
| + } |
| + |
| + 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; |
| + |
| + return utime + stime; |
| +} |
| + |
| const char kProcSelfExe[] = "/proc/self/exe"; |
| int GetNumberOfThreads(ProcessHandle process) { |