android memory management -ION / PMEM [turn]

Transfer: https://www.jianshu.com/p/0eac3d3ff6bb

ION debug

ION in / sys / kernel / debug / ion / a debugfs interface.

Each has its own heap debugfs directory, client memory usage appears in / sys / kernel / debug / ion / << heap name >>

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

      client              pid             size
    test_ion             2890            16384

Each identified by a client pid debugfs also has a directory / SYS / Kernel / Debug / Ion / >> << PID
$ CAT / SYS / Kernel / Debug / Ion / 2890
heap_name: SIZE_IN_BYTES
Ion--heap. 1: 40960. 11

Why ION

Recalling the end of 2011 [2], LWN examined the android kernel patch [3], to expect these patch merged into the mainline kernel. But PMEM (android realization of a memory allocator) offers the hope dashed. Why PMEM reason linux is not accepted in the community [3] has talked about. Since then, PMEM is clear is completely abandoned and replaced by ION memory manager. ION is google Android4.0 ICS in order to solve the memory fragmentation management and the introduction of universal memory manager, it will be more integration kernel. Currently QCOM MSM, NVDIA Tegra, TI OMAP, MRVL PXA PMEM are replaced with ION.

How to obtain the source code

http://android.googlesource.com/kernel/common.git

ION codes reside in drivers/gpu/ion

Specific usage examples on omap4:

http://android.googlesource.com/kernel/omap.git

ION frame [1]

ION defines four different heap, implement different memory allocation strategy.

  • ION_HEAP_TYPE_SYSTEM: allocate memory by vmalloc
  • ION_HEAP_TYPE_SYSTEM_CONTIG: allocate memory by kmalloc
  • ION_HEAP_TYPE_CARVEOUT: allocating memory in the reserved memory block (reserve memory)
  • ION_HEAP_TYPE_CUSTOM: defined by the customers themselves
 
image

The figure is a schematic view of two shared memory client. Figure, there are two heap (each has its own memory heap allocation strategy), each heap allocated a number of buffer. The handle management client to the corresponding buffer. This Two client is achieved through shared memory file descriptor fd.

 
image

ION APIs

User-space API

Defines six ioctl interface, you can interact with the user application.

  • ION_IOC_ALLOC: allocate memory
  • ION_IOC_FREE: release memory
  • ION_IOC_MAP: get file descriptor mmap (not used in this definition in the code?)
  • ION_IOC_SHARE: Create a file descriptor to implement shared memory
  • ION_IOC_IMPORT: obtaining a file descriptor
  • ION_IOC_CUSTOM: ioctl call user-defined

ION_IOC_SHARE and ION_IOC_IMPORT is DMABUF achieve, so when the process gets shared file descriptor, you can directly call mmap to operate based on shared memory. mmap call subsystem is implemented by DMABUF ION subsystem mmap callback function complete.

Kernel space API

Kernel driver can also be registered as a ION client (client), you can choose which type of application heap memory to use.

  • ion_client_create: assign a client.
  • ion_client_destroy: the release of a client and binds all ion handle on it.

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

Guess you like

Origin www.cnblogs.com/sky-heaven/p/11506275.html