diff options
author | Michael Butler <butlermichael@google.com> | 2019-08-02 15:04:00 -0700 |
---|---|---|
committer | Michael Butler <butlermichael@google.com> | 2019-08-26 14:41:04 -0700 |
commit | 6a7acfeb079a5c02e958f5938db4ac96657a5b44 (patch) | |
tree | 1a588b93c7af99008360506b7945a5883c27d956 /nn/runtime/test/TestPartitioningRandom.cpp | |
parent | f4333ba55cff91db17279aa39cfe4fcc8a239263 (diff) | |
download | ml-6a7acfeb079a5c02e958f5938db4ac96657a5b44.tar.gz |
Cleanup NNAPI runtime Memory objects
Prior to this CL, runtime Memory* objects were default constructed and
set to a value later. Rebinding the value led to multiple bugs in the
past and made the Memory* objects prone to data races. This CL addresses
these issues by determining all values in the Memory* objects' factory
methods, and making the Memory* objects immutable after construction.
This CL also untangles some of the inheritance hierarchy. Prior to this
CL, all MemoryFd and MemoryAHWB inherited from Memory, which was
effectively ashmem-based memory. This CL separates MemoryAshmem into its
own, unique class type, and has a common base Memory class. This
reorganization also uncovered that getPointer was only used in the
runtime on ashmem-based memory, and removes the unused getPointer
methods.
Finally, this CL improves documentation of NeuralNetworks.h in two ways:
1) Fixes the typo "ANeuralNetworksMemory_createFromAHardwarBuffer"
2) Documents the missing lifetime constraints of
ANeuralNetworksMemory_createFromAHardwareBuffer
Fixes: 138852228
Fixes: 69632863
Fixes: 69633035
Fixes: 129572123
Fixes: 132323765
Fixes: 139213289
Test: mma
Test: atest NeuralNetworksTest_static
Test: atest CtsNNAPITestCases
Change-Id: I49a2356d6b8fc38e501d8e37ba8a8893f3e91395
Merged-In: I49a2356d6b8fc38e501d8e37ba8a8893f3e91395
(cherry picked from commit 008caeab69787b8978e874f8fb3811e2400f5d55)
Diffstat (limited to 'nn/runtime/test/TestPartitioningRandom.cpp')
-rw-r--r-- | nn/runtime/test/TestPartitioningRandom.cpp | 32 |
1 files changed, 11 insertions, 21 deletions
diff --git a/nn/runtime/test/TestPartitioningRandom.cpp b/nn/runtime/test/TestPartitioningRandom.cpp index b7326e562..e591f1697 100644 --- a/nn/runtime/test/TestPartitioningRandom.cpp +++ b/nn/runtime/test/TestPartitioningRandom.cpp @@ -30,6 +30,7 @@ #include <cassert> #include <cstdio> #include <iterator> +#include <memory> #include <random> #include <set> #include <tuple> @@ -39,7 +40,6 @@ #include <unistd.h> #include <android-base/logging.h> -#include <android/sharedmem.h> #include <gtest/gtest.h> // Uncomment the following line to generate some debugging output that @@ -98,7 +98,6 @@ using ExecutionPlan = nn::ExecutionPlan; using HalVersion = nn::HalVersion; using HidlModel = V1_2::Model; using HidlToken = hidl_array<uint8_t, ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN>; -using MemoryBuilder = nn::Memory; using ModelBuilder = nn::ModelBuilder; using Result = nn::test_wrapper::Result; using SampleDriver = nn::sample_driver::SampleDriver; @@ -252,7 +251,6 @@ class TestCompilation : public WrapperCompilation { class TestMemories { public: TestMemories() = default; - ~TestMemories(); TestMemories(const TestMemories&) = delete; TestMemories& operator=(const TestMemories&) = delete; @@ -282,15 +280,12 @@ class TestMemories { CHECK(mLayoutDone); CHECK(regionIndex < regionCount()); const auto& regionDescriptor = mRegions[regionIndex]; - const WrapperMemory* memory = &mMemorys[std::get<0>(regionDescriptor)]; + const WrapperMemory* memory = &mMemories[std::get<0>(regionDescriptor)]; uint32_t offset = std::get<1>(regionDescriptor); uint32_t length = std::get<2>(regionDescriptor); - uint8_t* buffer; - if (reinterpret_cast<MemoryBuilder*>(memory->get())->getPointer(&buffer) != - ANEURALNETWORKS_NO_ERROR) { - CHECK(0); - } + uint8_t* buffer = reinterpret_cast<nn::MemoryAshmem*>(memory->get())->getPointer(); + CHECK(buffer != nullptr); if (pMemory) *pMemory = memory; if (pOffset) *pOffset = offset; @@ -309,8 +304,7 @@ class TestMemories { std::vector<uint32_t> mMemorySizes; // Index is the memory index. - std::vector<WrapperMemory> mMemorys; - std::vector<int> mFDs; + std::vector<WrapperMemory> mMemories; // Index is the region index; tuple represents memory index, // region offset within memory, region length. @@ -323,18 +317,14 @@ class TestMemories { void TestMemories::layout() { CHECK(!mLayoutDone); for (uint32_t memorySize : mMemorySizes) { - const int fd = ASharedMemory_create(nullptr, memorySize); - CHECK(fd >= 0); - mMemorys.emplace_back(memorySize, PROT_READ | PROT_WRITE, fd, 0); - mFDs.push_back(fd); - } - mLayoutDone = true; -} + auto [n, ashmem] = nn::MemoryAshmem::create(memorySize); + CHECK_EQ(n, ANEURALNETWORKS_NO_ERROR); + CHECK(ashmem != nullptr); -TestMemories::~TestMemories() { - for (int fd : mFDs) { - close(fd); + ANeuralNetworksMemory* memory = reinterpret_cast<ANeuralNetworksMemory*>(ashmem.release()); + mMemories.emplace_back(memory); } + mLayoutDone = true; } class RandomPartitioningTest : public ::testing::TestWithParam<unsigned> { |