ION 概述


原文转载自http://blog.csdn.net/g_salamander/article/details/8424334

增加了Gralloc模块的平台背景和功能概述部分。

对原文针对msm8960 Android display做了修正。

增加了Surfaceflinger初始化FrameBufferNativeWindow的代码部分。

平台中内存有ashmen、PMEM等多种内存类型,为了Video、Graphics、GPU内存访问的需要,android引入Gralloc模块实现内存的管理。Gralloc把FrameBuffer的分配也纳入了其中,并且新引入ION做为Gralloc的非FrameBuffer内存的分配器。ION对于内核态内存在用户进程之间的访问和硬件平台模块之间数据流转提供了高效的解决方案。

Android 中 lcd 是一个帧缓冲设备,驱动程序通过处理器的 lcd 控制器将物理内存的一段区域设置为显存,如果向这段内存区域写入数据就会马上在 lcd 上显示出来。Android 在 HAL 中提供了gralloc 模块,封装了用户层对帧缓冲设备的所有操作接口,并通过 SurfaceFlinger 服务向应用提供显示支持。在启动过程中系统会加载 gralloc 模块,然后打开帧缓冲设备,获取设备的各种参数并完成 gralloc 模块的初始化。当应用程序需要把内容显示到 lcd 上时,需要通过 gralloc 模块申请一块图形缓冲区,然后将这块图形缓冲区映射到自己的地址空间并写入内容即可。当应用程序不再需要这块图形缓冲区时需要通过 gralloc 模块释放掉,然后解除对缓冲区的映射。


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.

 

 
ION_HEAP_TYPE_SYSTEM : 通过vmalloc分配内存
 ION_HEAP_TYPE_SYSTEM_CONTIG: 通过kmalloc分配内存
 ION_HEAP_TYPE_CARVEOUT: 在保留内存块中(reserve memory)分配内存
 ION_HEAP_TYPE_CUSTOM: 由客户自己定义
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值