meminfo: Add IsSmapsRollupSupported Api

Consolidate the checking of /proc/<pid>/smaps_rollup support
in libmeminfo and do it in a thread safe way.

Use the API in ProcMemInfo as well to eliminate the extra
parameters passed to SmapsOrRollup* methods.

Bug: 111694435
Test: libmeminfo_test 1 --gtest_filter=TestProcMemInfo.IsSmapsSupportedTest
Test: Tested with and without the smaps_rollup support in kernel.

Change-Id: I992057f06b54569025fa0cdade9618da2675d1de
Merged-In: I992057f06b54569025fa0cdade9618da2675d1de
Signed-off-by: Sandeep Patil <sspatil@google.com>
diff --git a/libmeminfo/libmeminfo_test.cpp b/libmeminfo/libmeminfo_test.cpp
index 796a7d0..e689a26 100644
--- a/libmeminfo/libmeminfo_test.cpp
+++ b/libmeminfo/libmeminfo_test.cpp
@@ -284,13 +284,22 @@
     EXPECT_EQ(swap_offsets.size(), 0);
 }
 
+TEST(TestProcMemInfo, IsSmapsSupportedTest) {
+    std::string path = ::android::base::StringPrintf("/proc/%d/smaps_rollup", pid);
+    bool supported = IsSmapsRollupSupported(pid);
+    EXPECT_EQ(!access(path.c_str(), F_OK | R_OK), supported);
+    // Second call must return what the first one returned regardless of the pid parameter.
+    // So, deliberately pass invalid pid.
+    EXPECT_EQ(supported, IsSmapsRollupSupported(-1));
+}
+
 TEST(TestProcMemInfo, SmapsOrRollupReturn) {
     // if /proc/<pid>/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(true, &stats));
+    EXPECT_EQ(!access(path.c_str(), F_OK), proc_mem.SmapsOrRollup(&stats));
 }
 
 TEST(TestProcMemInfo, SmapsOrRollupTest) {