天天看點

ION 記憶體管理

what is ION?

  • ION  記憶體管理從android4.0開始被引入
  • ION子產品是可擴充的(API都是統一的),支援各種形式的記憶體配置設定方式,可以表述不同的硬體資源和他們的一些限制
  • ION 支援連續與不連續記憶體的配置設定
  • ION 給Kernel and User space processes提供了相應的APIs

 目前支援的memory type

  • ION_HEAP_TYPE_CARVEOUT - memory (PMEM style) for larger physically contiguous allocations
  • ION_HEAP_TYPE_SYSTEM_CONTIG - physically contiguous for small physical allocations
  • ION_HEAP_TYPE_SYSTEM - virtually contiguous but physically discontiguous memory
  • ION_HEAP_TYPE_IOMMU - memory region allocated through IOMMU API.

ION heap 的大小根據每個裝置自身的記憶體情況而定,但是都要實作下面的回調:

   struct ion_heap_ops {      
        int (*allocate) (struct ion_heap *heap,      
                        struct ion_buffer *buffer, unsigned long len,      
                        unsigned long align, unsigned long flags);      
        void (*free) (struct ion_buffer *buffer);      
        int (*phys) (struct ion_heap *heap, struct ion_buffer *buffer,      
                    ion_phys_addr_t *addr, size_t *len);      
        struct scatterlist *(*map_dma) (struct ion_heap *heap,      
                        struct ion_buffer *buffer);      
        void (*unmap_dma) (struct ion_heap *heap,       
                 struct ion_buffer *buffer);      
        void * (*map_kernel) (struct ion_heap *heap,       
                 struct ion_buffer *buffer);      
        void (*unmap_kernel) (struct ion_heap *heap,       
                 struct ion_buffer *buffer);      
        int (*map_user) (struct ion_heap *heap, struct ion_buffer *buffer,      
                        struct vm_area_struct *vma);      
   };      
方法phys(),傳回的是的實體位址和buffer的長度,但必須是連續的實體buffer,如果沒有連續的實體buffer,是不能提供改回調。      
map_kernel() and unmap_kernel(),把實體記憶體映射到kernel virtual address space.      
map_user(),映射實體記憶體到使用者空間,為啥沒有unmap_user()方法呢,因為映射到使用者空間,是以FD(檔案描述符)的形式映射的,當FD close了就自動unmap。      

ION可以釋放記憶體嘛?

答案是否定的。它主要的是提供給applications間共享記憶體。

ION和PMem可以共存嘛?

可以,但是不能共享buffers.

userspace是如何使用ION?

1:open ION device-------open("/dev/ion", O_RDONLY),傳回一個FD(相當于ION client)

2:  用戶端要填充如下資料結構,除了handle,也就是你要申請的data:

struct ion_allocation_data {      
        size_t len;      
        size_t align;      
        unsigned int flags;      
        struct ion_handle *handle;      
   }      

3:  user space clients 用ioctl跟ION通信

int ioctl(int client_fd, ION_IOC_ALLOC, struct ion_allocation_data *allocation_data)

傳回的FD的buffer。

4:FD可以通過Binder機制進行程序間的share

如何檢視ION的使用量

for example:

>adb shell

 #mount -t debugfs NONE /d

 #cd /d/ion/

 #ls

 922

 vmalloc

 ...

 # cat vmalloc

 cat vmalloc

 client pid size

 total bytes currently allocated: 0

 # cat 922

 cat 922

 heap_name: size_in_bytes : handle refcount : buffer

 client refcount: 1

ION 和DMABUF的比較:

Feature ION DMABUF
Memory Manager Role ION replaces PMEM as the manager of provisioned memory pools. The list of ION heaps can be extended per device. DMABUF is a buffer sharing framework, designed to integrate with the memory allocators in DMA mapping frameworks, like the work-in-progress DMA-contiguous allocator, also known as theContiguous Memory Allocator (CMA). DMABUF exporters have the option to implement custom allocators.
User Space Access Control ION offers the /dev/ion interface for user-space programs to allocate and share buffers. Any user program with ION access can cripple the system by depleting the ION heaps. Android checks user and group IDs to block unauthorized access to ION heaps. DMABUF offers only kernel APIs. Access control is a function of the permissions on the devices using the DMABUF feature.
Global Client and Buffer Database ION contains a device driver associated with /dev/ion. The device structure contains a database that tracks the allocated ION buffers, handles and file descriptors, all grouped by user clients and kernel clients. ION validates all client calls according to the rules of the database. For example, there is a rule that a client cannot have two handles to the same buffer. The DMA debug facility implements a global hashtable,dma_entry_hash, to track DMA buffers, but only when the kernel was built with theCONFIG_DMA_API_DEBUG option.
Cross-architecture Usage ION usage today is limited to architectures that run the Android kernel. DMABUF usage is cross-architecture. The DMA mapping redesign preparation patchset modified the DMA mapping code in 9 architectures besides the ARM architecture.
Buffer Synchronization ION considers buffer synchronization to be an orthogonal problem. DMABUF provides a pair of APIs for synchronization. The buffer-user callsdma_buf_map_attachment() whenever it wants to use the buffer for DMA . Once the DMA for the current buffer-user is over, it signals 'end-of-DMA' to the exporter via a call todma_buf_unmap_attachment().
Delayed Buffer Allocation ION allocates the physical memory before the buffer is shared. DMABUF can defer the allocation until the first call todma_buf_map_attachment(). The exporter of DMA buffer has the opportunity to scan all client attachments, collate their buffer constraints, then choose the appropriate backing storage.
下一篇: 報告四

繼續閱讀