# sample_hbmem 使用说明


## 功能概述

本说明文档旨在详细介绍 sample_hbmem 的使用方法，包括其主要功能、源码结构、软件架构以及API流程。本示例主要集中在 hbmem API 的应用，特别是涉及 Com Buffer 、Graphic Buffer 和 Share Pool 的创建与使用，以及多进程间的共享机制。

 sample_hbmem 主要功能是提供 hbmem API 的示例，涵盖以下内容：

- Com Buffer 的创建与管理：提供了创建、管理和销毁共享通信缓冲区的能力，这些缓冲区用于多个进程之间的数据交换，有效支持进程间的协作
- Graphic Buffer 的创建与使用：提供了如何创建并管理图形缓冲区，包括对图形数据的配置、操作和转换，能够满足视觉数据处理中对内存的动态需求。
- Share Pool 的使用：实现了共享内存池的分配与释放策略，通过共享内存池优化了资源的利用率，降低了创建和销毁缓冲区时的开销，提高了内存管理的灵活性。

### 软件架构说明

本 sample 基于 libhbmem API 实现，libhbmem 是一套基于 ION 的内存管理库，它提供了内存分配、共享、回收等功能，并提供了统一的 API 接口，使得应用开发者能够更加方便地管理内存，提高内存的利用率，实现不同 buffer 类型的内存申请和进程间共享。具体架构图参见下图软件架构说明。

![hbmem_sample.png](./_static/_images/sample_hbmem/hbmem_sample.png)

### <span id="background-knowledge"/> 背景知识
可以查看 [ION 系统调试指南 ](../linux_development/system_component_development/39-ION_Debug_Guide.html)章节进行了解。

### API 流程说明
libhbmem 提供了一系列的 API 接口，包括：libhbmem 库调用 ION 进行 buffer 分配

![API.png](./_static/_images/sample_hbmem/API.png)

以下是 sample_hbmem 示例中包含了 Alloc Com Buffer 、Alloc Graph Buffer 、 Share Pool 等对 hbmem API 的使用，并展示了各个 API 的分配使用流程图

![com_graph_buf.png](./_static/_images/sample_hbmem/com_graph_buf.png)

- **Alloc Com Buf**：分配共享通信缓冲区，用于多个进程之间的数据交换，有效支持进程间的协作。
- **Alloc Com Buf With Heapmask**：分配共享通信缓冲区，并指定 heapmask，用于指定缓冲区分配到指定的 heap 上。
- **Alloc Graph Buf**：分配图像缓存，包括对图像数据的配置、操作和转换，能够满足视觉数据处理中对内存的动态需求。
- **Alloc Graph Buf With Heapmask**：分配图像缓存，并指定 heapmask，用于指定缓冲区分配到指定的 heap 上。

![com_pool.png](./_static/_images/sample_hbmem/com_pool.png)

- **Share Com Buffer**：共享通信缓冲区，用于多个进程之间的数据交换，有效支持进程间的协作。
- **Share Graph Buffer**：共享图像缓存，包括对图像数据的配置、操作和转换，能够满足视觉数据处理中对内存的动态需求。
- **Share Pool**：共享内存池，实现了共享内存池的分配与释放策略，通过共享内存池优化了资源的利用率，降低了创建和销毁缓冲区时的开销，提高了内存管理的灵活性。

![com_buf_fork.png](./_static/_images/sample_hbmem/com_buf_fork.png)

- **Share Com Buffer Fork Process Scenario**：创建子进程时，共享通信缓冲区的使用。

## 编译部署

### 源码结构

```
1. 代码路径：/app/samples/platform_samples/sample_hbmem
2. 目录结构：
sample_hbmem/
├── .gitignore
├── Makefile
├── sample_alloc.c
├── sample_common.c
├── sample_common.h
├── sample_pool.c
├── sample_queue.c
├── sample_share_pool.c
├── sample_share.c
└── sample.c
```

### 编译说明

本sample主要依赖 libhbmem 提供的头文件：

```
#include <hb_mem_mgr.h>
#include <hb_mem_err.h>
```

编译依赖的库如下：

```
LIBS += -lhbmem -lpthread -lalog -ldl -lstdc++
```

### 编译环境

- 进入 app/samples/platform_samples/sample_hbmem 目录，执行 make 编译

- 编译成功后输出的可执行文件为 sample_hbmem

- 详细程序编译方式请查阅 [ 编译方法 ](overview.html#span-id-sample-build) 章节

把 `sample_hbmem` 上传到开发板之后，运行 `chmod 777 sample_hbmem` 命令给程序赋予可执行权限。

## 运行

部署完 `sample_hbmem` 之后直接执行程序 `./sample_hbmem` 可以获得帮助信息和支持的hbmem列表：

```
# ./sample_hbmem
Options:
  -m                     Specify sample use cases 
  -h                     Show this help message
Usage: ./sample_hbmem -m <index>
***************  Sample Mode Lists  ***************
index:  1       sample_mode:    Alloc Com Buf
index:  2       sample_mode:    Alloc Com Buf With Cache
index:  3       sample_mode:    Alloc Com Buf With Heapmask
index:  4       sample_mode:    Alloc Graph Buf
index:  5       sample_mode:    Alloc Graph Buf With Heapmask
index:  6       sample_mode:    Share Com Buffer
index:  7       sample_mode:    Share Com Buffer Fork Process Scenario
index:  8       sample_mode:    Share Graph Buffer
index:  9       sample_mode:    Share Graph Buffer Fork Process Scenario
index:  10      sample_mode:    Share Pool Fork Process Scenario
index:  11      sample_mode:    Share Pool
index:  12      sample_mode:    Change Com Graph Buf
***************************************************
```

根据提示选择需要使用的 Mode 编号，即可运行对应的示例。以 Alloc Com Buf 为例，执行 `./sample_hbmem -m 1`，即会创建 Alloc Com Buf 示例。

### 命令解释

- -m： 指定sample用例，具体见`运行结果展示`

- -h： 帮助说明

### 测试结果说明

- 如果log最后以 `xxxx done` 结尾，则表示 sample 执行成功。

### 运行结果展示

理解运行命令需要一些基础知识，这里做一个简要的说明：\
`/sys/kernel/debug/ion/clients/` 目录可以查看当前系统申请了 ION 内存的 Client 集合（除了 BPU 子系统）,\
比如有一个 `1114` 的进程申请并使用了 ION 那么该目录下就会显示一个 `1114-0` 的文件节点。而 `cma_reserved`、\
`carveout`、`ion_cma` 等都是针对 ION 不同区域的名字。读者可以先阅读[背景知识](#span-id-background-knowledge)之后，再回来继续查看。

- **创建 Com Buf**

```
# ./sample_hbmem -m 1
sample_mode = 1
=================================================
Ready to sample_alloc_com_buf
alloc com buf, share_id: 7
[1432:1432] Do system command cat /sys/kernel/debug/ion/clients/1432*.
       heap_name:    size_in_bytes :  handle refcount :    handle import :       buffer ptr :  buffer refcount :  buffer share id : buffer share count
         ion_cma:           400000 :                1 :                1 :         d4b2ed6c :                3:                7 :                1

-------------------------------------------------------------------------
          total            400000
-------------------------------------------------------------------------
[1432:1432] Do system command cat /sys/kernel/debug/ion/heaps/ion_cma | grep -w 1432 | grep -w sample_hbmem.
         ion_cma     sample_hbmem             1432          4194304
    sample_hbmem             1432            other          4194304 0
[1432:1432] Result 0.
free com buf
[1432:1432] Do system command cat /sys/kernel/debug/ion/clients/1432*.
       heap_name:    size_in_bytes :  handle refcount :    handle import :       buffer ptr :  buffer refcount :  buffer share id : buffer share count
-------------------------------------------------------------------------
          total                 0
-------------------------------------------------------------------------
[1432:1432] Do system command cat /sys/kernel/debug/ion/heaps/ion_cma | grep -w 1432 | grep -w sample_hbmem.
[1432:1432] Result 256.
sample_alloc_com_buf done
=================================================
```

- **创建 Com Buf With Cache**

```
# ./sample_hbmem -m 2
sample_mode = 2
=================================================
Ready to sample_alloc_com_buf_with_cache
memset uncached buf(size:4194304) use time: 691673
memset   cached buf(size:4194304) use time: 457194
sample_alloc_com_buf_with_cache done
=================================================
```

- **在各个 heaps 里创建 Com Buffer**

```
# ./sample_hbmem -m 3
sample_mode = 3
=================================================
Ready to sample_alloc_com_buf_with_heapmask
alloc com buf form ion_cma, size: 1048576
[1471:1471] Do system command cat /sys/kernel/debug/ion/clients/1471*.
       heap_name:    size_in_bytes :  handle refcount :    handle import :       buffer ptr :  buffer refcount :  buffer share id : buffer share count
         ion_cma:           100000 :                1 :                1 :         27d9b8ed :                3:                7 :                1

-------------------------------------------------------------------------
          total            100000
-------------------------------------------------------------------------
[1471:1471] Do system command cat /sys/kernel/debug/ion/heaps/ion_cma | grep -w 1471 | grep -w sample_hbmem.
         ion_cma     sample_hbmem             1471          1048576
    sample_hbmem             1471            other          1048576 0
[1471:1471] Result 0.
free com buf

alloc com buf form carveout, size: 1048576
[1471:1471] Do system command cat /sys/kernel/debug/ion/clients/1471*.
       heap_name:    size_in_bytes :  handle refcount :    handle import :       buffer ptr :  buffer refcount :  buffer share id : buffer share count
        carveout:           100000 :                1 :                1 :         27d9b8ed :                3:                7 :                1

-------------------------------------------------------------------------
          total            100000
-------------------------------------------------------------------------
[1471:1471] Do system command cat /sys/kernel/debug/ion/heaps/carveout | grep -w 1471 | grep -w sample_hbmem.
        carveout     sample_hbmem             1471          1048576
    sample_hbmem             1471            other          1048576 0
[1471:1471] Result 0.
free com buf

alloc com buf form cma_reserved, size: 1048576
[1471:1471] Do system command cat /sys/kernel/debug/ion/clients/1471*.
       heap_name:    size_in_bytes :  handle refcount :    handle import :       buffer ptr :  buffer refcount :  buffer share id : buffer share count
    cma_reserved:           100000 :                1 :                1 :         27d9b8ed :                3:                7 :                1

-------------------------------------------------------------------------
          total            100000
-------------------------------------------------------------------------
[1471:1471] Do system command cat /sys/kernel/debug/ion/heaps/cma_reserved | grep -w 1471 | grep -w sample_hbmem.
    cma_reserved     sample_hbmem             1471          1048576
    sample_hbmem             1471            other          1048576 0
[1471:1471] Result 0.
free com buf

sample_alloc_com_buf_with_heapmask done
=================================================
```

- **创建 Graph Buffer**

```
# ./sample_hbmem -m 4
sample_mode = 4
=================================================
Ready to sample_alloc_graph_buf
graph_buf.plane_cnt: 2, graph_buf.format: 8, graph_buf.width: 1280, graph_buf.height: 720, graph_buf.stride: 0, graph_buf.vstride: 0, graph_buf.stride: 0, graph_buf.flags: 17
[1487:1487] Do system command cat /sys/kernel/debug/ion/clients/1487*.
       heap_name:    size_in_bytes :  handle refcount :    handle import :       buffer ptr :  buffer refcount :  buffer share id : buffer share count
         ion_cma:            e1000 :                1 :                1 :         27d9b8ed :                3:                7 :                1

         ion_cma:            71000 :                1 :                1 :         1fe4048d :                3:                8 :                1

-------------------------------------------------------------------------
          total            152000
-------------------------------------------------------------------------
[1487:1487] Do system command cat /sys/kernel/debug/ion/heaps/ion_cma | grep -w 1487 | grep -w sample_hbmem.
         ion_cma     sample_hbmem             1487          1384448
    sample_hbmem             1487            other           921600 0
    sample_hbmem             1487            other           462848 0
[1487:1487] Result 0.
sample_alloc_graph_buf done
=================================================
```

- **在各个 heaps 里，创建 Graph Buffer**

```
# ./sample_hbmem -m 5
sample_mode = 5
=================================================
Ready to sample_alloc_graph_buf_with_heapmask
alloc graph buf form ion_cma
graph_buf.plane_cnt: 2, graph_buf.format: 8, graph_buf.width: 1280, graph_buf.height: 720, graph_buf.stride: 0, graph_buf.vstride: 0, graph_buf.stride: 0, graph_buf.flags: 17
[1493:1493] Do system command cat /sys/kernel/debug/ion/clients/1493*.
       heap_name:    size_in_bytes :  handle refcount :    handle import :       buffer ptr :  buffer refcount :  buffer share id : buffer share count
         ion_cma:            71000 :                1 :                1 :         27d9b8ed :                3:                8 :                1

         ion_cma:            e1000 :                1 :                1 :         1fe4048d :                3:                7 :                1

-------------------------------------------------------------------------
          total            152000
-------------------------------------------------------------------------
[1493:1493] Do system command cat /sys/kernel/debug/ion/heaps/ion_cma | grep -w 1493 | grep -w sample_hbmem.
         ion_cma     sample_hbmem             1493          1384448
    sample_hbmem             1493            other           462848 0
    sample_hbmem             1493            other           921600 0
[1493:1493] Result 0.
alloc graph buf form carveout
graph_buf.plane_cnt: 2, graph_buf.format: 8, graph_buf.width: 1280, graph_buf.height: 720, graph_buf.stride: 0, graph_buf.vstride: 0, graph_buf.stride: 0, graph_buf.flags: 4294967313
[1493:1493] Do system command cat /sys/kernel/debug/ion/clients/1493*.
       heap_name:    size_in_bytes :  handle refcount :    handle import :       buffer ptr :  buffer refcount :  buffer share id : buffer share count
        carveout:            e1000 :                1 :                1 :         27d9b8ed :                3:                7 :                1

        carveout:            71000 :                1 :                1 :         1fe4048d :                3:                8 :                1

-------------------------------------------------------------------------
          total            152000
-------------------------------------------------------------------------
[1493:1493] Do system command cat /sys/kernel/debug/ion/heaps/carveout | grep -w 1493 | grep -w sample_hbmem.
        carveout     sample_hbmem             1493          1384448
    sample_hbmem             1493            other           921600 0
    sample_hbmem             1493            other           462848 0
[1493:1493] Result 0.
alloc graph buf form cma_reserved
graph_buf.plane_cnt: 2, graph_buf.format: 8, graph_buf.width: 1280, graph_buf.height: 720, graph_buf.stride: 0, graph_buf.vstride: 0, graph_buf.stride: 0, graph_buf.flags: 17179869201
[1493:1493] Do system command cat /sys/kernel/debug/ion/clients/1493*.
       heap_name:    size_in_bytes :  handle refcount :    handle import :       buffer ptr :  buffer refcount :  buffer share id : buffer share count
    cma_reserved:            71000 :                1 :                1 :         27d9b8ed :                3:                8 :                1

    cma_reserved:            e1000 :                1 :                1 :         1fe4048d :                3:                7 :                1

-------------------------------------------------------------------------
          total            152000
-------------------------------------------------------------------------
[1493:1493] Do system command cat /sys/kernel/debug/ion/heaps/cma_reserved | grep -w 1493 | grep -w sample_hbmem.
    cma_reserved     sample_hbmem             1493          1384448
    sample_hbmem             1493            other           462848 0
    sample_hbmem             1493            other           921600 0
[1493:1493] Result 0.
sample_alloc_graph_buf_with_heapmask done
=================================================
```

- **进程内 Common Buffer 共享**

```
# ./sample_hbmem -m 6
sample_mode = 6
=================================================
Ready to sample_share_com_buffer
alloc com buf, share_id: 7
import com buf, share_id: 7
[1510:1510] Do system command cat /sys/kernel/debug/ion/clients/1510*.
       heap_name:    size_in_bytes :  handle refcount :    handle import :       buffer ptr :  buffer refcount :  buffer share id : buffer share count
         ion_cma:            10000 :                2 :                2 :         df1cdf5a :                4:                7 :                2

-------------------------------------------------------------------------
          total             10000
-------------------------------------------------------------------------
[1510:1510] Do system command cat /sys/kernel/debug/ion/heaps/ion_cma | grep -w 1510 | grep -w sample_hbmem.
         ion_cma     sample_hbmem             1510            65536
    sample_hbmem             1510            other            65536 0
[1510:1510] Result 0.
free com buf
[1510:1510] Do system command cat /sys/kernel/debug/ion/clients/1510*.
       heap_name:    size_in_bytes :  handle refcount :    handle import :       buffer ptr :  buffer refcount :  buffer share id : buffer share count
         ion_cma:            10000 :                1 :                1 :         df1cdf5a :                3:                7 :                1

-------------------------------------------------------------------------
          total             10000
-------------------------------------------------------------------------
[1510:1510] Do system command cat /sys/kernel/debug/ion/heaps/ion_cma | grep -w 1510 | grep -w sample_hbmem.
         ion_cma     sample_hbmem             1510            65536
    sample_hbmem             1510            other            65536 0
[1510:1510] Result 0.
free import buf
[1510:1510] Do system command cat /sys/kernel/debug/ion/clients/1510*.
       heap_name:    size_in_bytes :  handle refcount :    handle import :       buffer ptr :  buffer refcount :  buffer share id : buffer share count
-------------------------------------------------------------------------
          total                 0
-------------------------------------------------------------------------
[1510:1510] Do system command cat /sys/kernel/debug/ion/heaps/ion_cma | grep -w 1510 | grep -w sample_hbmem.
[1510:1510] Result 256.
sample_share_com_buffer done
=================================================
```

- **进程间 Common Buffer 共享**

```
# ./sample_hbmem -m 7
sample_mode = 7
=================================================
Ready to sample_share_com_buffer_fork_process_scenario
alloc graph buf form ion_cma
socketpair: 0
[1527:902] In parent process.
[1528:1527] In child process.
parent write share buf: parent test common buf share.
share_consume_cnt: 0
[1527:902] parent send msg 1
[1528:1527] child recv msg 1
child read share buf: parent test common buf share.
share_consume_cnt: 1
child write share buf: child test common buf share.
[1528:1527] child send msg 2
[1527:902] parent recv msg 2
parent read share buf: child test common buf share.
[1527:902] parent send msg 3
[1528:1527] child recv msg 3
share_consume_cnt: 0
[1527:902] parent quit
[1528:1527] child quit
alloc graph buf form carveout
socketpair: 0
[1527:902] In parent process.
parent write share buf: parent test common buf share.
share_consume_cnt: 0
[1529:1527] In child process.
[1527:902] parent send msg 1
[1529:1527] child recv msg 1
child read share buf: parent test common buf share.
share_consume_cnt: 1
child write share buf: child test common buf share.
[1529:1527] child send msg 2
[1527:902] parent recv msg 2
parent read share buf: child test common buf share.
[1527:902] parent send msg 3
[1529:1527] child recv msg 3
share_consume_cnt: 0
[1527:902] parent quit
[1529:1527] child quit
alloc graph buf form cma_reserved
socketpair: 0
[1527:902] In parent process.
parent write share buf: parent test common buf share.
share_consume_cnt: 0
[1530:1527] In child process.
[1527:902] parent send msg 1
[1530:1527] child recv msg 1
child read share buf: parent test common buf share.
share_consume_cnt: 1
child write share buf: child test common buf share.
[1530:1527] child send msg 2
[1527:902] parent recv msg 2
parent read share buf: child test common buf share.
[1527:902] parent send msg 3
[1530:1527] child recv msg 3
share_consume_cnt: 0
[1527:902] parent quit
[1530:1527] child quit
sample_share_com_buffer_fork_process_scenario done
=================================================
```

- **进程内 Graphic Buffer 共享**

```
# ./sample_hbmem -m 8
sample_mode = 8
=================================================
Ready to sample_share_graph_buffer
alloc graph buf, share_id0: 7, share_id1: 8, share_id2: 8
import graph buf, share_id0: 7, share_id1: 8, share_id2: 8
[1531:1531] Do system command cat /sys/kernel/debug/ion/clients/1531*.
       heap_name:    size_in_bytes :  handle refcount :    handle import :       buffer ptr :  buffer refcount :  buffer share id : buffer share count
         ion_cma:            e1000 :                2 :                2 :         b65c6afe :                4:                7 :                2

         ion_cma:            71000 :                2 :                2 :         d4b2ed6c :                4:                8 :                2

-------------------------------------------------------------------------
          total            152000
-------------------------------------------------------------------------
[1531:1531] Do system command cat /sys/kernel/debug/ion/heaps/ion_cma | grep -w 1531 | grep -w sample_hbmem.
         ion_cma     sample_hbmem             1531          1384448
    sample_hbmem             1531            other           921600 0
    sample_hbmem             1531            other           462848 0
[1531:1531] Result 0.
free graph buf
[1531:1531] Do system command cat /sys/kernel/debug/ion/clients/1531*.
       heap_name:    size_in_bytes :  handle refcount :    handle import :       buffer ptr :  buffer refcount :  buffer share id : buffer share count
         ion_cma:            e1000 :                1 :                1 :         b65c6afe :                3:                7 :                1

         ion_cma:            71000 :                1 :                1 :         d4b2ed6c :                3:                8 :                1

-------------------------------------------------------------------------
          total            152000
-------------------------------------------------------------------------
[1531:1531] Do system command cat /sys/kernel/debug/ion/heaps/ion_cma | grep -w 1531 | grep -w sample_hbmem.
         ion_cma     sample_hbmem             1531          1384448
    sample_hbmem             1531            other           921600 0
    sample_hbmem             1531            other           462848 0
[1531:1531] Result 0.
free import buf
[1531:1531] Do system command cat /sys/kernel/debug/ion/clients/1531*.
       heap_name:    size_in_bytes :  handle refcount :    handle import :       buffer ptr :  buffer refcount :  buffer share id : buffer share count
-------------------------------------------------------------------------
          total                 0
-------------------------------------------------------------------------
[1531:1531] Do system command cat /sys/kernel/debug/ion/heaps/ion_cma | grep -w 1531 | grep -w sample_hbmem.
[1531:1531] Result 256.
sample_share_graph_buffer done
=================================================
```

- **进程间 Graphic Buffer 共享**

```
# ./sample_hbmem -m 9
sample_mode = 9
=================================================
Ready to sample_share_graph_buffer_fork_process_scenario
alloc graph buf form ion_cma
socketpair: 0
[1571:902] In parent process.
[1572:1571] In child process.
parent write share buf: parent test graph buf share.
share_consume_cnt: 0
[1571:902] parent send msg 1
[1572:1571] child recv msg 1
child read share buf: parent test graph buf share.
share_consume_cnt: 1
child write share buf: child test common buf share.
[1572:1571] child send msg 2
[1571:902] parent recv msg 2
parent read share buf: child test common buf share.
[1571:902] parent send msg 3
[1572:1571] child recv msg 3
share_consume_cnt: 0
[1571:902] parent quit
[1572:1571] child quit
alloc graph buf form carveout
socketpair: 0
[1571:902] In parent process.
[1573:1571] In child process.
parent write share buf: parent test graph buf share.
share_consume_cnt: 0
[1571:902] parent send msg 1
[1573:1571] child recv msg 1
child read share buf: parent test graph buf share.
share_consume_cnt: 1
child write share buf: child test common buf share.
[1573:1571] child send msg 2
[1571:902] parent recv msg 2
parent read share buf: child test common buf share.
[1571:902] parent send msg 3
[1573:1571] child recv msg 3
share_consume_cnt: 0
[1571:902] parent quit
[1573:1571] child quit
alloc graph buf form cma_reserved
socketpair: 0
[1571:902] In parent process.
[1574:1571] In child process.
parent write share buf: parent test graph buf share.
share_consume_cnt: 0
[1571:902] parent send msg 1
[1574:1571] child recv msg 1
child read share buf: parent test graph buf share.
share_consume_cnt: 1
child write share buf: child test common buf share.
[1574:1571] child send msg 2
[1571:902] parent recv msg 2
parent read share buf: child test common buf share.
[1571:902] parent send msg 3
[1574:1571] child recv msg 3
share_consume_cnt: 0
[1571:902] parent quit
[1574:1571] child quit
sample_share_graph_buffer_fork_process_scenario done
=================================================
```

- **进程间 Share Pool 共享**

```
# ./sample_hbmem -m 10
sample_mode = 10
=================================================
Ready to sample_share_pool_fork_process_scenario
[1576:902] In parent proces[12121.836077] ion_share_pool_monitor_ref_cnt failed -512
s.
[1577:1576] In child process.
[1576:1576] Do system command cat /sys/kernel/debug/ion/clients/1576*.
       heap_name:    [12121.854779] ion_share_pool_monitor_ref_cnt failed -512
size_in_bytes :  handle refcount :    handle import :       buffer ptr :  buffer refcount :  buffer share id : buffer share count
         ion_cma:            20000 :                1 :                1 :         4b5ef9a2 :                3:                7 :                1

         ion_cma:            20000 :                1 :                1 :         baf4062a :                3:                8 :                1

         ion_cma:            20000 :                1 :                1 :         c865393e :                3:                9 :                1

         ion_cma:            20000 :                1 :                1 :         5db7853d :                3:               10 :                1

         ion_cma:            20000 :                1 :                1 :         d973ee69 :                3:               11 :                1

-------------------------------------------------------------------------
          total             a0000
-------------------------------------------------------------------------
[1576:1576] Do system command cat /sys/kernel/debug/ion/heaps/ion_cma | grep -w 1576 | grep -w sample_hbmem.
         ion_cma     sample_hbmem             1576           655360
    sample_hbmem             1576            other           131072 0
    sample_hbmem             1576            other           131072 0
    sample_hbmem             1576            other           131072 0
    sample_hbmem             1576            other           131072 0
    sample_hbmem             1576            other           131072 0
[1576:1576] Result 0.
hb_mem_share_pool_alloc_buf 0 0xffffa12b0000
hb_mem_share_pool_alloc_buf 1 0xffffa12d0000
hb_mem_share_pool_alloc_buf 2 0xffffa12f0000
hb_mem_share_pool_alloc_buf 3 0xffffa1310000
hb_mem_share_pool_alloc_buf 4 0xffffa1330000
[1576:902] parent send msg 1
[1577:1576] child recv msg 1
[1577:1576] child send msg 2
[1576:902] parent recv msg 2
free 0 0xffffa12b0000
[1576:902] parent pool.avail_buf_cnt: 0
free 1 0xffffa12d0000
[1576:902] parent pool.avail_buf_cnt: 1
free 2 0xffffa12f0000
[1576:902] parent pool.avail_buf_cnt: 2
free 3 0xffffa1310000
[1576:902] parent pool.avail_buf_cnt: 3
free 4 0xffffa1330000
[1576:902] parent pool.avail_buf_cnt: 4
[1576:902] parent send msg 3
[1577:1576] child recv msg 3
[1577:1576] child send msg 4
[1576:902] parent recv msg 4
[1576:902] parent pool.avail_buf_cnt: 5
[1577:1576] child quit
[1576:902] parent quit
sample_share_pool_fork_process_scenario done
=================================================
```

- **使用 Share Pool**

```
# ./sample_hbmem -m 11
sample_mode = 11
=================================================
Ready to sample_share_pool
pool.avail_buf_cnt: 9
pool.avail_buf_cnt: 8
pool.avail_buf_cnt: 7
pool.avail_buf_cnt: 6
pool.avail_buf_cnt: 5
pool.avail_buf_cnt: 4
pool.avail_buf_cnt: 3
pool.avail_buf_cnt: 2
pool.avail_buf_cnt: 1
pool.avail_buf_cnt: 0
pool.avail_buf_cnt: 1
pool.avail_buf_cnt: 2
pool.avail_buf_cnt: 3
pool.avail_buf_cnt: 4
pool.avail_buf_cnt: 5
pool.avail_buf_cnt: 6
pool.avail_buf_cnt: 7
pool.avail_buf_cnt: 8
pool.avail_buf_cnt: 9
pool.avail_buf_cnt: 10
sample_share_pool done
=================================================
```

- **在 Graphic Buffer 与 Common Buffer 之间实现 Buffer 转换**

```
# ./sample_hbmem -m 12
sample_mode = 12
=================================================
Ready to sample_alloc_graph_buf_group_heapmask
----alloc graphic buffer done!----
the data in graphic buffer:
size: 0x1fa400
flags: 0x8000011
fd: 4
share id: 7
virt_addr: 0x0xffffae348000
paddr: 0xe1800000
----change graphic buffer to common buffer done!----
the data in new common buffer:
size: 0x1fa400
flags: 0x8000011
fd: 4
share id: 7
virt_addr: 0x0xffffae348000
paddr: 0xe1800000
----alloc common buffer done!----
the data in common buffer:
size: 0x10000
flags: 0x8000011
fd: 5
share id: 8
virt_addr: 0x0xffffaeb10000
paddr: 0xe1af8000
----change common buffer to graphic buffer done!----
the data in new graphic buffer:
size: 0x10000
flags: 0x8000011
fd: 5
share id: 8
virt_addr: 0x0xffffaeb10000
paddr: 0xe1af8000
sample_alloc_graph_buf_group_heapmask done
=================================================
```


