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的比较:

FeatureIONDMABUF

Memory Manager RoleION 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 ControlION 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 DatabaseION 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 UsageION 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 SynchronizationION 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 AllocationION 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.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,695评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,569评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,130评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,648评论 1 297
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,655评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,268评论 1 309
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,835评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,740评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,286评论 1 318
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,375评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,505评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,185评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,873评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,357评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,466评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,921评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,515评论 2 359

推荐阅读更多精彩内容