android之ION内存管理器(1)-- 简介
2015-09-21 18:05
651 查看
为什么需要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, TIOMAP, MRVL PXA都用ION替换PMEM。
如何获取source code
http://android.googlesource.com/kernel/common.gitION codes reside in drivers/gpu/ion
Specific usage examples on omap4: http://android.googlesource.com/kernel/omap.git
ION 框架[1]
ION 定义了四种不同的heap,实现不同的内存分配策略。ION_HEAP_TYPE_SYSTEM : 通过vmalloc分配内存
ION_HEAP_TYPE_SYSTEM_CONTIG: 通过kmalloc分配内存
ION_HEAP_TYPE_CARVEOUT: 在保留内存块中(reserve memory)分配内存
ION_HEAP_TYPE_CUSTOM: 由客户自己定义
下图是两个client共享内存的示意图。图中有2个heap(每种heap都有自己的内存分配策略),每个heap中分配了若干个buffer。client的handle管理到对应的buffer。两个client是通过文件描述符fd来实现内存共享的。
ION APIs
用户空间 API
定义了6种 ioctl 接口,可以与用户应用程序交互。ION_IOC_ALLOC: 分配内存
ION_IOC_FREE: 释放内存
ION_IOC_MAP: 获取文件描述符进行mmap (? 在code中未使用这个定义)
ION_IOC_SHARE: 创建文件描述符来实现共享内存
ION_IOC_IMPORT: 获取文件描述符
ION_IOC_CUSTOM: 调用用户自定义的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 handle
ion_free: 释放ion handle
ION 通过handle来管理buffer,驱动需要可以访问到buffer的地址。ION通过下面的函数来达到这个目的
ion_phys: 返回buffer的物理地址(address)及大小(size)
ion_map_kernel: 给指定的buffer创建内核内存映射
ion_unmap_kernel: 销毁指定buffer的内核内存映射
ion_map_dma: 为指定buffer创建dma 映射,返回sglist(scatter/gather list)
ion_unmap_dma: 销毁指定buffer的dma映射
ION是通过handle而非buffer地址来实现驱动间共享内存,用户空间共享内存也是利用同样原理。
ion_share: given a handle, obtain a buffer to pass to other clients
ion_import: given an buffer in another client, import it
ion_import_fd: given an fd obtained via ION_IOC_SHARE ioctl, import it
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 * @phys get physical address of a buffer (only define on physically contiguous heaps) * @map_dma map the memory for dma to a scatterlist * @unmap_dma unmap the memory for dma * @map_kernel map memory to the kernel * @unmap_kernel unmap memory to the kernel * @map_user map 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-1 client pid size test_ion 2890 16384
每个由pid标识的client也有一个debugfs目录/sys/kernel/debug/ion/<<pid>>
$cat /sys/kernel/debug/ion/2890 heap_name: size_in_bytes ion-heap-1: 40960 11
参考文献
1. https://wiki.linaro.org/BenjaminGaignard/ion
2. http://lwn.net/Articles/480055/
3. http://lwn.net/Articles/472984/
相关文章推荐
- AndroidManifest.xml 配置权限大全
- Android 点击空白处自动隐藏输入法
- android studio修改项目名,模块名,包名
- 移动环境下push技术开发(01-androidpn环境的配置)
- Android获取通话记录
- androidpn的学习研究(二)androidpn-server服务端启动过程的理解分析
- android vector
- Android 无缝轮播实现——本地加载图片轮播(一)
- android中跨进程通讯的4种方式
- Android应用集成支付宝接口的简化
- 【Material Design】Material Design 开发利器:Android Design Support Library 介绍
- Android程序中启动另一个activity
- Android中使用Bitmap会出现OutOfMemory的原因(转)
- Ubuntu 12.04(64位)安装Android Studio
- 【Android Studio】制作清晰Gif截图以作为项目的运行效果
- Android——启动过程详解
- Android 自定义TitleBar替换ActionBar的另类方式
- android string.xml中%1$s、%1$d等的用法
- 【内存优化】(MDCC技术大咖秀)Android内存优化之OOM
- Android——4.2.2 源码目录结构分析