summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHridya Valsaraju <hridya@google.com>2021-03-23 23:54:28 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2021-03-23 23:54:28 +0000
commit5ed731bd38f09e1add6e22cde3bca245a141000b (patch)
tree82458f154de917312f9e70c1c7a7b68f3234d217
parentdf7dea6a1a0cc2f9d21ef2cec17a728b41aff961 (diff)
parent53d09380cdf648a4205f40d33b6548be6a6c81f1 (diff)
downloadlibdmabufheap-5ed731bd38f09e1add6e22cde3bca245a141000b.tar.gz
Add unit test cases for multithreaded access to BufferAllocator am: 5524cb7451 am: 53d09380cd
Original change: https://android-review.googlesource.com/c/platform/system/memory/libdmabufheap/+/1634148 Change-Id: I0186cd9d57f9aaaec9fc4ec40884edbf3e21d0fe
-rw-r--r--tests/Android.bp28
-rw-r--r--tests/dmabuf_heap_test.cpp107
-rw-r--r--tests/dmabufheap-vts-unit-tests.xml (renamed from tests/dmabufheap-unit-tests.xml)6
3 files changed, 115 insertions, 26 deletions
diff --git a/tests/Android.bp b/tests/Android.bp
index 25eec5c..c5a9e81 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -18,8 +18,8 @@ package {
default_applicable_licenses: ["Android-Apache-2.0"],
}
-cc_test {
- name: "dmabufheap-unit-tests",
+cc_defaults {
+ name: "dmabufheap-unit-tests-default",
cflags: [
"-Wall",
"-Werror",
@@ -34,13 +34,27 @@ cc_test {
"libvintf",
],
srcs: [
- "dmabuf_heap_test.cpp",
+ "dmabuf_heap_test.cpp",
],
+}
+
+cc_test {
+ name: "dmabufheap-vts-unit-tests",
+ defaults: ["dmabufheap-unit-tests-default"],
+ test_config: "dmabufheap-vts-unit-tests.xml",
test_suites: [
"general-tests",
- "vts"
+ "vts",
],
- test_config: "dmabufheap-unit-tests.xml",
+}
+
+cc_test {
+ name: "dmabufheap-unit-tests",
+ defaults: ["dmabufheap-unit-tests-default"],
+ test_suites: [
+ "device-tests",
+ ],
+ require_root: true,
}
cc_test {
@@ -50,7 +64,7 @@ cc_test {
"libion",
"libdmabufheap",
"liblog",
- "libbase"
+ "libbase",
],
cflags: [
"-Werror",
@@ -65,7 +79,7 @@ cc_test {
static_libs: [
"libion",
"liblog",
- "libbase"
+ "libbase",
],
cflags: [
"-Werror",
diff --git a/tests/dmabuf_heap_test.cpp b/tests/dmabuf_heap_test.cpp
index 6c5dd77..2b387ca 100644
--- a/tests/dmabuf_heap_test.cpp
+++ b/tests/dmabuf_heap_test.cpp
@@ -27,6 +27,87 @@
#include <android-base/unique_fd.h>
#include <vintf/VintfObject.h>
+#include <thread>
+
+class DmaBufHeapConcurrentAccessTest : public ::testing::Test {
+ public:
+ virtual void SetUp() { allocator = new BufferAllocator(); }
+
+ void DoAlloc(const std::string& heap_name) {
+ static const size_t kAllocSizeInBytes = 4096;
+ int map_fd = allocator->Alloc(heap_name, kAllocSizeInBytes);
+ ASSERT_GE(map_fd, 0);
+
+ void* ptr = mmap(NULL, kAllocSizeInBytes, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
+ ASSERT_TRUE(ptr != MAP_FAILED);
+
+ int ret = allocator->CpuSyncStart(map_fd, kSyncReadWrite);
+ ASSERT_EQ(0, ret);
+
+ ret = allocator->CpuSyncEnd(map_fd, kSyncReadWrite);
+ ASSERT_EQ(0, ret);
+
+ ASSERT_EQ(0, munmap(ptr, kAllocSizeInBytes));
+ ASSERT_EQ(0, close(map_fd));
+ }
+
+ void DoConcurrentAlloc() {
+ DoAlloc(kDmabufSystemHeapName);
+ DoAlloc(kDmabufSystemUncachedHeapName);
+ }
+
+ void DoConcurrentAllocWithMapName() {
+ allocator->MapNameToIonHeap(kDmabufSystemHeapName, "" /* no mapping for non-legacy */,
+ 0 /* no mapping for non-legacy ion */,
+ ~0 /* legacy ion heap mask */, ION_FLAG_CACHED);
+ DoAlloc(kDmabufSystemHeapName);
+ allocator->MapNameToIonHeap(
+ kDmabufSystemUncachedHeapName, "" /* no mapping for non-legacy */,
+ 0 /* no mapping for non-legacy ion */, ~0 /* legacy ion heap mask */);
+ DoAlloc(kDmabufSystemUncachedHeapName);
+ }
+
+ virtual void TearDown() { delete allocator; }
+
+ BufferAllocator* allocator = nullptr;
+};
+
+static constexpr size_t NUM_CONCURRENT_THREADS = 100;
+
+TEST_F(DmaBufHeapConcurrentAccessTest, ConcurrentAllocTest) {
+ using android::vintf::KernelVersion;
+
+ KernelVersion min_kernel_version = KernelVersion(5, 10, 0);
+ KernelVersion kernel_version =
+ android::vintf::VintfObject::GetInstance()
+ ->getRuntimeInfo(android::vintf::RuntimeInfo::FetchFlag::CPU_VERSION)
+ ->kernelVersion();
+ if (kernel_version < min_kernel_version) {
+ GTEST_SKIP();
+ }
+
+ std::vector<std::thread> threads(NUM_CONCURRENT_THREADS);
+ for (int i = 0; i < NUM_CONCURRENT_THREADS; i++) {
+ threads[i] = std::thread(&DmaBufHeapConcurrentAccessTest::DoConcurrentAlloc, this);
+ }
+
+ for (auto& thread : threads) {
+ thread.join();
+ }
+}
+
+TEST_F(DmaBufHeapConcurrentAccessTest, ConcurrentAllocWithMapNameTest) {
+ std::vector<std::thread> threads(NUM_CONCURRENT_THREADS);
+ for (int i = 0; i < NUM_CONCURRENT_THREADS; i++) {
+ threads[i] =
+ std::thread(&DmaBufHeapConcurrentAccessTest::DoConcurrentAllocWithMapName, this);
+ }
+
+ for (auto& thread : threads) {
+ thread.join();
+ }
+}
+
DmaBufHeapTest::DmaBufHeapTest() : allocator(new BufferAllocator()) {
/*
* Legacy ion devices may have hardcoded heap IDs that do not
@@ -120,10 +201,8 @@ TEST_F(DmaBufHeapTest, Zeroed) {
map_fd = allocator->Alloc(kDmabufSystemHeapName, kAllocSizeInBytes);
ASSERT_GE(map_fd, 0);
- void* ptr = NULL;
-
- ptr = mmap(NULL, kAllocSizeInBytes, PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
+ void* ptr = mmap(NULL, kAllocSizeInBytes, PROT_WRITE, MAP_SHARED, map_fd, 0);
+ ASSERT_TRUE(ptr != MAP_FAILED);
ret = allocator->CpuSyncStart(map_fd, kSyncWrite);
ASSERT_EQ(0, ret);
@@ -144,9 +223,8 @@ TEST_F(DmaBufHeapTest, Zeroed) {
map_fd = allocator->Alloc(kDmabufSystemHeapName, kAllocSizeInBytes);
ASSERT_GE(map_fd, 0);
- void* ptr = NULL;
- ptr = mmap(NULL, kAllocSizeInBytes, PROT_READ, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
+ void* ptr = mmap(NULL, kAllocSizeInBytes, PROT_READ, MAP_SHARED, map_fd, 0);
+ ASSERT_TRUE(ptr != MAP_FAILED);
ret = allocator->CpuSyncStart(map_fd);
ASSERT_EQ(0, ret);
@@ -167,9 +245,8 @@ TEST_F(DmaBufHeapTest, TestCpuSync) {
int map_fd = allocator->Alloc(kDmabufSystemHeapName, kAllocSizeInBytes);
ASSERT_GE(map_fd, 0);
- void* ptr;
- ptr = mmap(NULL, kAllocSizeInBytes, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
+ void* ptr = mmap(NULL, kAllocSizeInBytes, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
+ ASSERT_TRUE(ptr != MAP_FAILED);
int ret = allocator->CpuSyncStart(map_fd, sync_type);
ASSERT_EQ(0, ret);
@@ -203,9 +280,8 @@ TEST_F(DmaBufHeapTest, TestCustomLegacyIonSyncCallback) {
int map_fd = allocator->Alloc(kDmabufSystemHeapName, size);
ASSERT_GE(map_fd, 0);
- void* ptr;
- ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
+ void* ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
+ ASSERT_TRUE(ptr != MAP_FAILED);
int ret = allocator->CpuSyncStart(map_fd, kSyncWrite, CustomCpuSyncStart);
ASSERT_EQ(0, ret);
@@ -249,9 +325,8 @@ TEST_F(DmaBufHeapTest, TestDmabufSystemHeapCompliance) {
/*
* Test that system heap can be mmapped by the CPU.
*/
- void* ptr;
- ptr = mmap(NULL, kAllocSizeInBytes, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
- ASSERT_TRUE(ptr != NULL);
+ void* ptr = mmap(NULL, kAllocSizeInBytes, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
+ ASSERT_TRUE(ptr != MAP_FAILED);
/*
* Test that the allocated memory is zeroed.
diff --git a/tests/dmabufheap-unit-tests.xml b/tests/dmabufheap-vts-unit-tests.xml
index 3cfa319..4aa6701 100644
--- a/tests/dmabufheap-unit-tests.xml
+++ b/tests/dmabufheap-vts-unit-tests.xml
@@ -13,18 +13,18 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
-<configuration description="Config for dmabufheap-unit-tests">
+<configuration description="Config for dmabufheap-vts-unit-tests">
<object type="module_controller" class="com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController">
<option name="min-api-level" value="30" />
</object>
<target_preparer class="com.android.tradefed.targetprep.RootTargetPreparer"/>
<target_preparer class="com.android.tradefed.targetprep.PushFilePreparer">
<option name="cleanup" value="true" />
- <option name="push" value="dmabufheap-unit-tests->/data/local/tmp/dmabufheap-unit-tests" />
+ <option name="push" value="dmabufheap-vts-unit-tests->/data/local/tmp/dmabufheap-vts-unit-tests" />
</target_preparer>
<test class="com.android.tradefed.testtype.GTest" >
<option name="native-test-device-path" value="/data/local/tmp" />
- <option name="module-name" value="dmabufheap-unit-tests" />
+ <option name="module-name" value="dmabufheap-vts-unit-tests" />
<option name="include-filter" value="*TestDmabufSystemHeapCompliance*" />
</test>
</configuration>