Commit 597a8517 authored by afakhry's avatar afakhry Committed by Commit bot

ProcessMetrics::GetCPUUsage() is Slow

Instead of parsing the whole proc stats just to extract two values, we
skim through it just to extract the two values we're interested in.

BUG=453871
TEST=base_unittests --gtest_filter=ProcessMetricsTest.*

Review URL: https://codereview.chromium.org/888193002

Cr-Commit-Position: refs/heads/master@{#314691}
parent f9dffe3f
...@@ -399,17 +399,36 @@ size_t GetSystemCommitCharge() { ...@@ -399,17 +399,36 @@ size_t GetSystemCommitCharge() {
return meminfo.total - meminfo.free - meminfo.buffers - meminfo.cached; return meminfo.total - meminfo.free - meminfo.buffers - meminfo.cached;
} }
// Exposed for testing.
int ParseProcStatCPU(const std::string& input) { int ParseProcStatCPU(const std::string& input) {
std::vector<std::string> proc_stats; // |input| may be empty if the process disappeared somehow.
if (!internal::ParseProcStats(input, &proc_stats)) // e.g. http://crbug.com/145811.
if (input.empty())
return -1; return -1;
if (proc_stats.size() <= internal::VM_STIME) size_t start = input.find_last_of(')');
if (start == input.npos)
return -1; 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
return utime + stime; // last ')'.
int num_spaces_remaining = internal::VM_UTIME - 1;
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) {
int utime = 0;
int stime = 0;
if (sscanf(&input.data()[i], "%d %d", &utime, &stime) != 2)
return -1;
return utime + stime;
}
}
return -1;
} }
const char kProcSelfExe[] = "/proc/self/exe"; const char kProcSelfExe[] = "/proc/self/exe";
......
...@@ -323,6 +323,17 @@ TEST(ProcessMetricsTest, ParseProcStatCPU) { ...@@ -323,6 +323,17 @@ TEST(ProcessMetricsTest, ParseProcStatCPU) {
"3221224832 3221224344 3086339742 0 0 0 0 0 0 0 17 0 0 0"; "3221224832 3221224344 3086339742 0 0 0 0 0 0 0 17 0 0 0";
EXPECT_EQ(0, base::ParseProcStatCPU(kSelfStat)); EXPECT_EQ(0, base::ParseProcStatCPU(kSelfStat));
// Some weird long-running process with a weird name that I created for the
// purposes of this test.
const char kWeirdNameStat[] = "26115 (Hello) You ())) ) R 24614 26115 24614"
" 34839 26115 4218880 227 0 0 0 "
"5186 11 0 0 "
"20 0 1 0 36933953 4296704 90 18446744073709551615 4194304 4196116 "
"140735857761568 140735857761160 4195644 0 0 0 0 0 0 0 17 14 0 0 0 0 0 "
"6295056 6295616 16519168 140735857770710 140735857770737 "
"140735857770737 140735857774557 0";
EXPECT_EQ(5186 + 11, base::ParseProcStatCPU(kWeirdNameStat));
} }
#endif // defined(OS_LINUX) || defined(OS_ANDROID) #endif // defined(OS_LINUX) || defined(OS_ANDROID)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment