android之ION内存储器管理器(1)

为什么需要ION

回顾2011年末[2],LWN审查了android kernel patch[3],以期望将这些patch合并到kernel主线中。但是PMEM(android实现的 一个内存分配器)使这个愿望破灭了。为什么PMEM不被linux 社区接受的原因在[3]中有讲到。从那开始,,PMEM很明确会被完全抛弃,取而代之的是ION内存管理器。ION是google在Android4.0 ICS为了解决内存碎片管理而引入的通用内存管理器,它会更加融合kernel。目前QCOM MSM, NVDIA Tegra, TI OMAP, MRVL PXA都用ION替换PMEM。

如何获取source code

ION codes reside in drivers/gpu/ion

Specific usage examples on omap4:

ION 框架[1]

ION 定义了四种不同的heap,实现不同的内存分配策略。

下图是两个client共享内存的示意图。图中有2个heap(每种heap都有自己的内存分配策略),每个heap中分配了若干个buffer。client的handle管理到对应的buffer。两个client是通过文件描述符fd来实现内存共享的。

ION APIs用户空间 API

定义了6种 ioctl 接口,可以与用户应用程序交互。

ION_IOC_SHARE 及ION_IOC_IMPORT是基于DMABUF实现的,所以当共享进程获取文件描述符后,可以直接调用mmap来操作共享内存。mmap实现由DMABUF子系统调用ION子系统中mmap回调函数完成。

内核空间 API

内核驱动也可以注册为一个ION的客户端(client),可以选择使用哪种类型的heap来申请内存。

ion_client_create: 分配一个客户端。ion_client_destroy: 释放一个客户端及绑定在它上面的所有ion handle.

ion handle: 这里每个ion handle映射到一个buffer中,每个buffer关联一个heap。也就是说一个客户端可以操作多块buffer。

Buffer 申请及释放函数:

ion_alloc: 申请ion内存,返回ion handleion_free: 释放ion handle

ION 通过handle来管理buffer,驱动需要可以访问到buffer的地址。ION通过下面的函数来达到这个目的

ION是通过handle而非buffer地址来实现驱动间共享内存,用户空间共享内存也是利用同样原理。

Heap API

Heap 接口定义 [drivers/gpu/ion/ion_priv.h]

这些接口不是暴露给驱动或者用户应用程序的。

/** * struct ion_heap_ops – ops to operate on a given heap * @allocate:allocate memory * @free:free memory * @physget physical address of a buffer (only define on physically contiguous heaps) * @map_dmamap the memory for dma to a scatterlist * @unmap_dmaunmap the memory for dma * @map_kernelmap memory to the kernel * @unmap_kernelunmap memory to the kernel * @map_usermap memory to userspace */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 *mapper, struct ion_buffer *buffer, struct vm_area_struct *vma);};ION debug

ION 在/sys/kernel/debug/ion/ 提供一个debugfs 接口。

每个heap都有自己的debugfs目录,client内存使用状况显示在/sys/kernel/debug/ion/<<heap name>>

$cat /sys/kernel/debug/ion/ion-heap-1clientpidsizetest_ion289016384

每个由pid标识的client也有一个debugfs目录/sys/kernel/debug/ion/<<pid>>

$cat /sys/kernel/debug/ion/2890heap_name: size_in_bytesion-heap-1: 40960 11

参考文献

1.https://wiki.linaro.org/BenjaminGaignard/ion

2.

3.

所有的失败,与失去自己的失败比起来,更是微不足道

android之ION内存储器管理器(1)

相关文章:

你感兴趣的文章:

标签云: