DO NOT MERGE - Skip qt-dev-plus-aosp-without-vendor (5713463) in stage-aosp-master
Bug: 134405016
Change-Id: Id2cfb74df8b3329588237c1f735f0e52722f6a69
diff --git a/Android.bp b/Android.bp
index fc022bd..8dcc77b 100644
--- a/Android.bp
+++ b/Android.bp
@@ -26,10 +26,17 @@
"liblog",
"libprocinfo",
],
+ target: {
+ darwin: {
+ enabled: false,
+ },
+
+ },
}
cc_library {
name: "libmeminfo",
+ host_supported: true,
defaults: ["libmeminfo_defaults"],
export_include_dirs: ["include"],
export_shared_lib_headers: ["libbase"],
diff --git a/include/meminfo/procmeminfo.h b/include/meminfo/procmeminfo.h
index 1fb4151..f782ec5 100644
--- a/include/meminfo/procmeminfo.h
+++ b/include/meminfo/procmeminfo.h
@@ -45,6 +45,9 @@
// vector.
const std::vector<Vma>& MapsWithPageIdle();
+ // Same as Maps() except, do not read the usage stats for each map.
+ const std::vector<Vma>& MapsWithoutUsageStats();
+
// Collect all 'vma' or 'maps' from /proc/<pid>/smaps and store them in 'maps_'. Returns a
// constant reference to the vma vector after the collection is done.
//
@@ -88,7 +91,7 @@
~ProcMemInfo() = default;
private:
- bool ReadMaps(bool get_wss, bool use_pageidle = false);
+ bool ReadMaps(bool get_wss, bool use_pageidle = false, bool get_usage_stats = true);
bool ReadVmaStats(int pagemap_fd, Vma& vma, bool get_wss, bool use_pageidle);
pid_t pid_;
diff --git a/libdmabufinfo/dmabufinfo_test.cpp b/libdmabufinfo/dmabufinfo_test.cpp
index eb53e57..7bba599 100644
--- a/libdmabufinfo/dmabufinfo_test.cpp
+++ b/libdmabufinfo/dmabufinfo_test.cpp
@@ -17,6 +17,7 @@
#include <inttypes.h>
#include <linux/dma-buf.h>
#include <poll.h>
+#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
@@ -230,7 +231,7 @@
DmaBufTester() : ion_fd(ion_open()), ion_heap_mask(get_ion_heap_mask()) {}
~DmaBufTester() {
- if (is_valid()) {
+ if (ion_fd >= 0) {
ion_close(ion_fd);
}
}
@@ -241,12 +242,16 @@
int fd;
int err = ion_alloc_fd(ion_fd, size, 0, ion_heap_mask, 0, &fd);
if (err < 0) {
- return unique_fd{err};
+ printf("Failed ion_alloc_fd, return value: %d\n", err);
+ return unique_fd{};
}
if (!name.empty()) {
- err = ioctl(fd, DMA_BUF_SET_NAME, name.c_str());
- if (err < 0) return unique_fd{-errno};
+ if (ioctl(fd, DMA_BUF_SET_NAME, name.c_str()) == -1) {
+ printf("Failed ioctl(DMA_BUF_SET_NAME): %s\n", strerror(errno));
+ close(fd);
+ return unique_fd{};
+ }
}
return unique_fd{fd};
@@ -306,7 +311,7 @@
return ret;
}
- unique_fd ion_fd;
+ int ion_fd;
const int ion_heap_mask;
};
diff --git a/libmeminfo_test.cpp b/libmeminfo_test.cpp
index 5451ca3..4c2be91 100644
--- a/libmeminfo_test.cpp
+++ b/libmeminfo_test.cpp
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
@@ -60,6 +61,103 @@
EXPECT_FALSE(maps.empty());
}
+TEST(ProcMemInfo, MapsUsageNotEmpty) {
+ ProcMemInfo proc_mem(pid);
+ const std::vector<Vma>& maps = proc_mem.Maps();
+ EXPECT_FALSE(maps.empty());
+ uint64_t total_pss = 0;
+ uint64_t total_rss = 0;
+ uint64_t total_uss = 0;
+ for (auto& map : maps) {
+ ASSERT_NE(0, map.usage.vss);
+ total_rss += map.usage.rss;
+ total_pss += map.usage.pss;
+ total_uss += map.usage.uss;
+ }
+
+ // Crude check that stats are actually being read.
+ EXPECT_NE(0, total_rss) << "RSS zero for all maps, that is not possible.";
+ EXPECT_NE(0, total_pss) << "PSS zero for all maps, that is not possible.";
+ EXPECT_NE(0, total_uss) << "USS zero for all maps, that is not possible.";
+}
+
+TEST(ProcMemInfo, MapsUsageEmpty) {
+ ProcMemInfo proc_mem(pid);
+ const std::vector<Vma>& maps = proc_mem.MapsWithoutUsageStats();
+ EXPECT_FALSE(maps.empty());
+ // Verify that all usage stats are zero in every map.
+ for (auto& map : maps) {
+ ASSERT_EQ(0, map.usage.vss);
+ ASSERT_EQ(0, map.usage.rss);
+ ASSERT_EQ(0, map.usage.pss);
+ ASSERT_EQ(0, map.usage.uss);
+ ASSERT_EQ(0, map.usage.swap);
+ ASSERT_EQ(0, map.usage.swap_pss);
+ ASSERT_EQ(0, map.usage.private_clean);
+ ASSERT_EQ(0, map.usage.private_dirty);
+ ASSERT_EQ(0, map.usage.shared_clean);
+ ASSERT_EQ(0, map.usage.shared_dirty);
+ }
+}
+
+TEST(ProcMemInfo, PageMapPresent) {
+ static constexpr size_t kNumPages = 20;
+ size_t pagesize = getpagesize();
+ void* ptr = mmap(nullptr, pagesize * (kNumPages + 2), PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ ASSERT_NE(MAP_FAILED, ptr);
+
+ // Unmap the first page and the last page so that we guarantee this
+ // map is in a map by itself.
+ ASSERT_EQ(0, munmap(ptr, pagesize));
+ uintptr_t addr = reinterpret_cast<uintptr_t>(ptr) + pagesize;
+ ASSERT_EQ(0, munmap(reinterpret_cast<void*>(addr + kNumPages * pagesize), pagesize));
+
+ ProcMemInfo proc_mem(getpid());
+ const std::vector<Vma>& maps = proc_mem.MapsWithoutUsageStats();
+ ASSERT_FALSE(maps.empty());
+
+ // Find the vma associated with our previously created map.
+ const Vma* test_vma = nullptr;
+ for (const Vma& vma : maps) {
+ if (vma.start == addr) {
+ test_vma = &vma;
+ break;
+ }
+ }
+ ASSERT_TRUE(test_vma != nullptr) << "Cannot find test map.";
+
+ // Verify that none of the pages are listed as present.
+ std::vector<uint64_t> pagemap;
+ ASSERT_TRUE(proc_mem.PageMap(*test_vma, &pagemap));
+ ASSERT_EQ(kNumPages, pagemap.size());
+ for (size_t i = 0; i < pagemap.size(); i++) {
+ EXPECT_FALSE(android::meminfo::page_present(pagemap[i]))
+ << "Page " << i << " is present and it should not be.";
+ }
+
+ // Make some of the pages present and verify that we see them
+ // as present.
+ uint8_t* data = reinterpret_cast<uint8_t*>(addr);
+ data[0] = 1;
+ data[pagesize * 5] = 1;
+ data[pagesize * 11] = 1;
+
+ ASSERT_TRUE(proc_mem.PageMap(*test_vma, &pagemap));
+ ASSERT_EQ(kNumPages, pagemap.size());
+ for (size_t i = 0; i < pagemap.size(); i++) {
+ if (i == 0 || i == 5 || i == 11) {
+ EXPECT_TRUE(android::meminfo::page_present(pagemap[i]))
+ << "Page " << i << " is not present and it should be.";
+ } else {
+ EXPECT_FALSE(android::meminfo::page_present(pagemap[i]))
+ << "Page " << i << " is present and it should not be.";
+ }
+ }
+
+ ASSERT_EQ(0, munmap(reinterpret_cast<void*>(addr), kNumPages * pagesize));
+}
+
TEST(ProcMemInfo, WssEmpty) {
// If we created the object for getting usage,
// the working set must be empty
diff --git a/procmeminfo.cpp b/procmeminfo.cpp
index a8b43c1..6f68ab4 100644
--- a/procmeminfo.cpp
+++ b/procmeminfo.cpp
@@ -130,6 +130,14 @@
return maps_;
}
+const std::vector<Vma>& ProcMemInfo::MapsWithoutUsageStats() {
+ if (maps_.empty() && !ReadMaps(get_wss_, false, false)) {
+ LOG(ERROR) << "Failed to read maps for Process " << pid_;
+ }
+
+ return maps_;
+}
+
const std::vector<Vma>& ProcMemInfo::Smaps(const std::string& path) {
if (!maps_.empty()) {
return maps_;
@@ -213,29 +221,30 @@
std::string pagemap_file = ::android::base::StringPrintf("/proc/%d/pagemap", pid_);
::android::base::unique_fd pagemap_fd(
TEMP_FAILURE_RETRY(open(pagemap_file.c_str(), O_RDONLY | O_CLOEXEC)));
- if (pagemap_fd < 0) {
+ if (pagemap_fd == -1) {
PLOG(ERROR) << "Failed to open " << pagemap_file;
return false;
}
uint64_t nr_pages = (vma.end - vma.start) / getpagesize();
- pagemap->reserve(nr_pages);
+ pagemap->resize(nr_pages);
- uint64_t idx = vma.start / getpagesize();
- uint64_t last = idx + nr_pages;
- uint64_t val;
- for (; idx < last; idx++) {
- if (pread64(pagemap_fd, &val, sizeof(uint64_t), idx * sizeof(uint64_t)) < 0) {
- PLOG(ERROR) << "Failed to read page frames from page map for pid: " << pid_;
- return false;
- }
- pagemap->emplace_back(val);
+ size_t bytes_to_read = sizeof(uint64_t) * nr_pages;
+ off64_t start_addr = (vma.start / getpagesize()) * sizeof(uint64_t);
+ ssize_t bytes_read = pread64(pagemap_fd, pagemap->data(), bytes_to_read, start_addr);
+ if (bytes_read == -1) {
+ PLOG(ERROR) << "Failed to read page frames from page map for pid: " << pid_;
+ return false;
+ } else if (static_cast<size_t>(bytes_read) != bytes_to_read) {
+ LOG(ERROR) << "Failed to read page frames from page map for pid: " << pid_
+ << ": read bytes " << bytes_read << " expected bytes " << bytes_to_read;
+ return false;
}
return true;
}
-bool ProcMemInfo::ReadMaps(bool get_wss, bool use_pageidle) {
+bool ProcMemInfo::ReadMaps(bool get_wss, bool use_pageidle, bool get_usage_stats) {
// Each object reads /proc/<pid>/maps only once. This is done to make sure programs that are
// running for the lifetime of the system can recycle the objects and don't have to
// unnecessarily retain and update this object in memory (which can get significantly large).
@@ -256,6 +265,10 @@
return false;
}
+ if (!get_usage_stats) {
+ return true;
+ }
+
std::string pagemap_file = ::android::base::StringPrintf("/proc/%d/pagemap", pid_);
::android::base::unique_fd pagemap_fd(
TEMP_FAILURE_RETRY(open(pagemap_file.c_str(), O_RDONLY | O_CLOEXEC)));
diff --git a/tools/Android.bp b/tools/Android.bp
index 2e89c41..3968c09 100644
--- a/tools/Android.bp
+++ b/tools/Android.bp
@@ -56,6 +56,7 @@
cc_binary {
name: "showmap",
+ host_supported: true,
cflags: [
"-Wall",
"-Werror",
@@ -66,6 +67,12 @@
"libbase",
"libmeminfo",
],
+
+ target: {
+ darwin: {
+ enabled: false,
+ },
+ },
}
cc_binary {
diff --git a/tools/showmap.cpp b/tools/showmap.cpp
index a80fa76..8ea2108 100644
--- a/tools/showmap.cpp
+++ b/tools/showmap.cpp
@@ -18,6 +18,7 @@
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
+#include <sys/signal.h>
#include <sys/types.h>
#include <unistd.h>
@@ -56,7 +57,7 @@
static VmaInfo g_total;
static std::vector<VmaInfo> g_vmas;
-[[noreturn]] static void usage(int exit_status) {
+[[noreturn]] static void usage(const char* progname, int exit_status) {
fprintf(stderr,
"%s [-aqtv] [-f FILE] PID\n"
"-a\taddresses (show virtual memory map)\n"
@@ -64,7 +65,7 @@
"-t\tterse (show only items with private pages)\n"
"-v\tverbose (don't coalesce maps with the same name)\n"
"-f\tFILE (read from input from FILE instead of PID)\n",
- getprogname());
+ progname);
exit(exit_status);
}
@@ -239,22 +240,22 @@
g_filename = optarg;
break;
case 'h':
- usage(EXIT_SUCCESS);
+ usage(argv[0], EXIT_SUCCESS);
default:
- usage(EXIT_FAILURE);
+ usage(argv[0], EXIT_FAILURE);
}
}
if (g_filename.empty()) {
if ((argc - 1) < optind) {
fprintf(stderr, "Invalid arguments: Must provide <pid> at the end\n");
- usage(EXIT_FAILURE);
+ usage(argv[0], EXIT_FAILURE);
}
g_pid = atoi(argv[optind]);
if (g_pid <= 0) {
fprintf(stderr, "Invalid process id %s\n", argv[optind]);
- usage(EXIT_FAILURE);
+ usage(argv[0], EXIT_FAILURE);
}
g_filename = ::android::base::StringPrintf("/proc/%d/smaps", g_pid);
diff --git a/vts/AndroidTest.xml b/vts/AndroidTest.xml
index 530d16e..9614025 100644
--- a/vts/AndroidTest.xml
+++ b/vts/AndroidTest.xml
@@ -24,6 +24,7 @@
<option name="binary-test-source" value="_32bit::DATA/nativetest/vts_meminfo_test/vts_meminfo_test" />
<option name="binary-test-source" value="_64bit::DATA/nativetest64/vts_meminfo_test/vts_meminfo_test" />
<option name="binary-test-type" value="gtest"/>
+ <option name="precondition-first-api-level" value="29" />
<option name="test-timeout" value="10m"/>
</test>
</configuration>