From ea448b829ef3c5eefcadcd50fdf74c873eeadd86 Mon Sep 17 00:00:00 2001 From: Android Partner Docs Date: Wed, 8 Aug 2018 13:50:33 -0700 Subject: Docs: Changes to source.android.com - 207940242 One more fix: add header for Television Requirements. by Gina Dimino - 207936055 Update health README link. by Android Partner Docs - 207929938 Errata run for Android 9 CDD. by Gina Dimino - 207897850 Fix typos in Develop and Configure index pages by Kenneth Lau - 207813977 Devsite localized content from translation request 958913. by Android Partner Docs - 207813949 Devsite localized content from translation request 953118. by Android Partner Docs - 207813941 Devsite localized content from translation request 961934. by Android Partner Docs - 207813934 Devsite localized content from translation request 552632. by Android Partner Docs - 207813463 Update line numbers in links by Kenneth Lau - 207796341 Fixing the URL for the CDD link in versions file. by Gina Dimino - 207779392 Fix incorrect link by Kenneth Lau - 207777680 Update build numbers for 2018/08 releases by Android Partner Docs - 207775888 Update links to AOSP by Kenneth Lau - 207769948 Update links to AOSP by Kenneth Lau - 207763826 Clarify system for HIDL passthrough loading. by Android Partner Docs - 207733156 Fixing malformed links in html for kernel patches, adding... by Heidi von Markham - 207650104 Remove link by Heidi von Markham - 207640627 Tags for Android P. by Android Partner Docs - 207626815 Making link absolute by Clay Murphy - 207611166 Add Background Restrictions into Release Notes. by Christina Nguyen - 207606267 Fixing unclosed tag, reformatting for clarity by Heidi von Markham - 207604244 Fix malformed link by Clay Murphy - 207598416 adding subscript by Heidi von Markham - 207595049 Fix link in section 3.5.1. by Gina Dimino - 207590813 Fix broken link due to file path change by Christina Nguyen - 207588930 Update Power nav to include changes to mgmt page (broken ... by Christina Nguyen - 207588102 Separate out the Power Management article into "Applicati... by Christina Nguyen - 207583000 Fix broken links in HAL interface section by Kenneth Lau - 207582699 Put index files in place as redirects are not taking hold by Clay Murphy - 207575443 P release notes: fix bad links, remove "P release" by Mark Hecomovich - 207574657 Fix link typo from release notes to Carrier ID page by Christina Nguyen - 207559561 Integrate SAC next branch into mainline for Android P/9 p... by Mark Hecomovich - 207559252 Publish links to July localized versions within Japanese ... by Clay Murphy - 207122872 Devsite localized content from translation request 958912. by Android Partner Docs - 207122854 Devsite localized content from translation request 961384. by Android Partner Docs - 207007888 Add blurb about the SystemUpdateSampler app on SAC so use... by Christina Nguyen - 206862073 Update Camera HAL testing page by Kenneth Lau - 206805870 Devsite localized content from translation request 960240. by Android Partner Docs - 206805861 Devsite localized content from translation request 954945. by Android Partner Docs PiperOrigin-RevId: 207940242 Change-Id: I3dee204c744e2e6062ac56810b88aefabf84636a --- en/devices/architecture/hidl/memoryblock.md | 302 ++++++++++++++++++++++++++++ 1 file changed, 302 insertions(+) create mode 100644 en/devices/architecture/hidl/memoryblock.md (limited to 'en/devices/architecture/hidl') diff --git a/en/devices/architecture/hidl/memoryblock.md b/en/devices/architecture/hidl/memoryblock.md new file mode 100644 index 00000000..de2b3493 --- /dev/null +++ b/en/devices/architecture/hidl/memoryblock.md @@ -0,0 +1,302 @@ +Project: /_project.yaml +Book: /_book.yaml + +{% include "_versions.html" %} + + + +# HIDL Memory Block + +The HIDL MemoryBlock is an abstract layer built on `hidl_memory`, `HIDL +@1.0::IAllocator`, and `HIDL @1.0::IMapper`. It is designed for HIDL services +that have multiple memory blocks to share a single memory heap. + + +## Performance improvements + +Using MemoryBlock in applications can significantly reduce the number of +`mmap`/`munmap` and user space segmentation faults, thus improving performance. +For example: + +* Using per `hidl_memory` for each buffer allocation averages 238 us/1 allocation. +* Using `MemoryBlock` and sharing a single `hidl_memory` averages 2.82 us/1 allocation. + + +## Architecture + +The HIDL MemoryBlock architecture includes HIDL services with multiple memory +blocks sharing a single memory heap: + +![HIDL MemoryBlock](/devices/architecture/images/hidl_memoryblock_arch.png) + +**Figure 1.** HIDL MemoryBlock architecture + + +## Normal usage + +This section provides an example of using MemoryBlock by first declaring the HAL +then implementing the HAL. + + +### Declaring the HAL + +For the following example IFoo HAL: + +``` +import android.hidl.memory.block@1.0::MemoryBlock; + +interface IFoo { + getSome() generates(MemoryBlock block); + giveBack(MemoryBlock block); +}; +``` + +The `Android.bp` is as follows: + +``` +hidl_interface { + ... + srcs: [ + "IFoo.hal", + ], + interfaces: [ + "android.hidl.memory.block@1.0", + ... +}; +``` + +### Implementing the HAL + +To implement the example HAL: + +1. Get the `hidl_memory` (for details, refer to [HIDL C++](/devices/architecture/hidl-cpp/)). + + + ``` + #include + + using ::android::hidl::allocator::V1_0::IAllocator; + using ::android::hardware::hidl_memory; + ... + sp allocator = IAllocator::getService("ashmem"); + allocator->allocate(2048, [&](bool success, const hidl_memory& mem) + { + if (!success) { /* error */ } + // you can now use the hidl_memory object 'mem' or pass it + })); + ``` + +1. Make a `HidlMemoryDealer` with the acquired `hidl_memory`: + + ``` + #include + + using ::android::hardware::HidlMemoryDealer + /* The mem argument is acquired in the Step1, returned by the ashmemAllocator->allocate */ + sp memory_dealer = HidlMemoryDealer::getInstance(mem); + ``` + +1. Allocate `MemoryBlock`, which is a struct defined with HIDL. + + Example `MemoryBlock`: + + ``` + struct MemoryBlock { + IMemoryToken token; + uint64_t size; + uint64_t offset; + }; + ``` + + Example using the `MemoryDealer` to allocate a `MemoryBlock`: + + + ``` + #include + + using ::android::hidl::memory::block::V1_0::MemoryBlock; + + Return Foo::getSome(getSome_cb _hidl_cb) { + MemoryBlock block = memory_dealer->allocate(1024); + if(HidlMemoryDealer::isOk(block)){ + _hidl_cb(block); + ... + ``` + +1. Deallocate `MemoryBlock`: + + ``` + Return Foo::giveBack(const MemoryBlock& block) { + memory_dealer->deallocate(block.offset); + ... + ``` + +1. Manipulate the data: + + ``` + #include + #include + + using ::android::hidl::memory::V1_0::IMemory; + + sp memory = mapMemory(block); + uint8_t* data = + + static_cast(static_cast(memory->getPointer())); + ``` + +1. Config `Android.bp`: + + ``` + shared_libs: [ + "android.hidl.memory@1.0", + + "android.hidl.memory.block@1.0" + + "android.hidl.memory.token@1.0", + "libhidlbase", + "libhidlmemory", + ``` + +1. Review the flow to determine if you need to `lockMemory`. + + Normally, the MemoryBlock uses reference count to maintain the shared + `hidl_memory` which is `mmap()`-ed the first time one of its `MemoryBlock`s gets + mapped and is `munmap()`-ed when nothing refers to it. To keep the `hidl_memory` + always mapped, you can use `lockMemory`, a RAII style object that keeps the + corresponding `hidl_memory` mapped throughout the lock life cycle. Example: + + ``` + #include + + sp lockMemory(const sp key); + ``` + +## Extended usage + +This section provides details about the extended usage of `MemoryBlock`. + + +### Using reference count to manage Memoryblock + +In most situations, the most efficient way to use MemoryBlock is to explicitly +allocate/deallocate. However, in complicated applications using reference count +for garbage collection might be a better idea. To have reference count on +MemoryBlock, you can bind MemoryBlock with a binder object, which helps to count +the references and deallocate the MemoryBlock when the count decreases to zero. + + +### Declaring the HAL + +When declaring the HAL, describe a HIDL struct that contains a MemoryBlock and a +IBase: + + +``` +import android.hidl.memory.block@1.0::MemoryBlock; + +struct MemoryBlockAllocation { + MemoryBlock block; + IBase refcnt; +}; +``` + +Use the `MemoryBlockAllocation` to replace `MemoryBlock` and remove the method +to give back the `MemoryBlock`. It will be deallocated by reference counting +with the `MemoryBlockAllocation`. Example: + + +``` +interface IFoo { + allocateSome() generates(MemoryBlockAllocation allocation); +}; +``` + +### Implementing the HAL + +Example of the service side implementation of the HAL: + + +``` +class MemoryBlockRefCnt: public virtual IBase { + MemoryBlockRefCnt(uint64_t offset, sp dealer) + : mOffset(offset), mDealer(dealer) {} + ~MemoryBlockRefCnt() { + mDealer->deallocate(mOffset); + } + private: + uint64_t mOffset; + sp mDealer; +}; + +Return Foo::allocateSome(allocateSome_cb _hidl_cb) { + MemoryBlockAllocation allocation; + allocation.block = memory_dealer->allocate(1024); + if(HidlMemoryDealer::isOk(block)){ + allocation.refcnt= new MemoryBlockRefCnt(...); + _hidl_cb(allocation); +``` + + +Example of the client side implementation of the HAL: + + +``` +ifoo->allocateSome([&](const MemoryBlockAllocation& allocation){ + ... +); +``` + +### Attaching/retrieving metadata + +Some applications need additional data to bind with the allocated `MemoryBlock`. +You can append/retrieve metadata using two methods: + + +* If the application accesses the metadata as often as the block itself, + append the metadata and pass them all in a struct. Example: + + ``` + import android.hidl.memory.block@1.0::MemoryBlock; + + struct MemoryBlockWithMetaData{ + MemoryBlock block; + MetaDataStruct metaData; + }; + ``` + +* If the application accesses the metadata much less frequently than the + block, it is more efficient to pass the metadata passively with an + interface. Example: + + ``` + import android.hidl.memory.block@1.0::MemoryBlock; + + struct MemoryBlockWithMetaData{ + MemoryBlock block; + IMetaData metaData; + }; + ``` + + Next, bind the metadata with the MemoryBlock using the Memory Dealer. Example: + + ``` + MemoryBlockWithMetaData memory_block; + memory_block.block = dealer->allocate(size); + if(HidlMemoryDealer::isOk(block)){ + memory_block.metaData = new MetaData(...); + ``` -- cgit v1.2.3