From d03244ab894e4a5d101ee31cc7eef18cdf2a21a6 Mon Sep 17 00:00:00 2001 From: Sandeep Patil Date: Mon, 14 Jan 2019 17:49:50 -0800 Subject: [PATCH 1/2] meminfo: test: delete SmapsOrRollupReturn test The test has become invalid due to addition of IsSmapsRollupSupported() and its internal usage by SmapsOrRollup() method. The method now returns success and returns statistics even if 'smaps_rollup' doesn't exist. It does that by internally checking for it's existence and falls back to using /proc//smaps Bug: 111694435 Test: mma -j Change-Id: I1983a23f1f617aee126bc66a4c1fbd4abb50f565 Signed-off-by: Sandeep Patil --- libmeminfo/libmeminfo_test.cpp | 9 --------- 1 file changed, 9 deletions(-) diff --git a/libmeminfo/libmeminfo_test.cpp b/libmeminfo/libmeminfo_test.cpp index e689a2679..7d85dd239 100644 --- a/libmeminfo/libmeminfo_test.cpp +++ b/libmeminfo/libmeminfo_test.cpp @@ -293,15 +293,6 @@ TEST(TestProcMemInfo, IsSmapsSupportedTest) { EXPECT_EQ(supported, IsSmapsRollupSupported(-1)); } -TEST(TestProcMemInfo, SmapsOrRollupReturn) { - // if /proc//smaps_rollup file exists, .SmapsRollup() must return true; - // false otherwise - std::string path = ::android::base::StringPrintf("/proc/%d/smaps_rollup", pid); - ProcMemInfo proc_mem(pid); - MemUsage stats; - EXPECT_EQ(!access(path.c_str(), F_OK), proc_mem.SmapsOrRollup(&stats)); -} - TEST(TestProcMemInfo, SmapsOrRollupTest) { std::string rollup = R"rollup(12c00000-7fe859e000 ---p 00000000 00:00 0 [rollup] From 3fcb44b90847f47cc0fd3e97f6cf9107fff38e09 Mon Sep 17 00:00:00 2001 From: Sandeep Patil Date: Mon, 14 Jan 2019 17:44:34 -0800 Subject: [PATCH 2/2] procmeminfo: use getline() instead of fgets() everywhere Bug: 111694435 Test: libmeminfo_test 1 --gtest_filter=TestProcMemInfo.* Change-Id: Idfc797aa65f45e0152765605c14622e2110dfdc1 Signed-off-by: Sandeep Patil --- libmeminfo/procmeminfo.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/libmeminfo/procmeminfo.cpp b/libmeminfo/procmeminfo.cpp index 347a293a9..d6332a32e 100644 --- a/libmeminfo/procmeminfo.cpp +++ b/libmeminfo/procmeminfo.cpp @@ -406,9 +406,10 @@ bool SmapsOrRollupFromFile(const std::string& path, MemUsage* stats) { return false; } - char line[1024]; + char* line = nullptr; + size_t line_alloc = 0; stats->clear(); - while (fgets(line, sizeof(line), fp.get()) != nullptr) { + while (getline(&line, &line_alloc, fp.get()) > 0) { switch (line[0]) { case 'P': if (strncmp(line, "Pss:", 4) == 0) { @@ -441,6 +442,8 @@ bool SmapsOrRollupFromFile(const std::string& path, MemUsage* stats) { } } + // free getline() managed buffer + free(line); return true; } @@ -450,14 +453,17 @@ bool SmapsOrRollupPssFromFile(const std::string& path, uint64_t* pss) { return false; } *pss = 0; - char line[1024]; - while (fgets(line, sizeof(line), fp.get()) != nullptr) { + char* line = nullptr; + size_t line_alloc = 0; + while (getline(&line, &line_alloc, fp.get()) > 0) { uint64_t v; if (sscanf(line, "Pss: %" SCNu64 " kB", &v) == 1) { *pss += v; } } + // free getline() managed buffer + free(line); return true; }