• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Memory Information Statistics
2
3## Basic Concepts<a name="section52691565235"></a>
4
5Memory information includes the memory pool size, memory usage, remaining memory size, maximum free memory, memory waterline, number of memory nodes, and fragmentation rate.
6
7-   Memory waterline: indicates the maximum memory used in a memory pool. The waterline value is updated upon each memory allocation and release. The memory pool size can be optimized based on this value.
8
9-   Fragmentation rate: indicates the fragmentation degree of the memory pool. If the fragmentation rate is high, there are a large number of free memory blocks in the memory pool but each block is small. You can use the following formula to calculate the fragmentation rate:
10
11    Fragmentation rate = 100 – 100 x Maximum free memory block size/Remaining memory size
12
13
14-   Other statistics: When  **LOS\_MemInfoGet**  is called, the node information in the memory pool is scanned and related statistics are collected.
15
16## Function Configuration<a name="section470611682411"></a>
17
18**LOSCFG\_MEM\_WATERLINE**: specifies the setting of the memory information statistics function. This function is disabled by default. To enable this function, configure it in  **Debug-\> Enable memory pool waterline or not in the configuration item**. If you want to obtain the memory waterline, you must enable this macro.
19
20## Development Guidelines<a name="section9368374243"></a>
21
22### How to Develop<a name="section679912407257"></a>
23
24Key structure:
25
26```
27typedef struct {
28    UINT32 totalUsedSize; // Memory usage of the memory pool
29    UINT32 totalFreeSize; // Remaining memory in the memory pool
30    UINT32 maxFreeNodeSize; // Maximum size of the free memory block in the memory pool
31    UINT32 usedNodeNum; // Number of non-free memory blocks in the memory pool
32    UINT32 freeNodeNum; // Number of free memory blocks in the memory pool
33#if (LOSCFG_MEM_WATERLINE == 1) // This function is disabled by default and can be enabled using the menuconfig tool.
34    UINT32 usageWaterLine; // Waterline of the memory pool
35#endif
36} LOS_MEM_POOL_STATUS;
37```
38
39-   To obtain the memory waterline, call  **LOS\_MemInfoGet**. The first parameter in the API is the start address of the memory pool, and the second parameter is the handle of the  **LOS\_MEM\_POOL\_STATUS**  type. The  **usageWaterLine**  field indicates the waterline.
40
41-   To calculate the memory fragmentation rate, call  **LOS\_MemInfoGet**  to obtain the remaining memory size and the maximum free memory block size in the memory pool, and then calculate the fragmentation rate of the dynamic memory pool as follows:
42
43    Fragmentation rate = 100 – 100 x Maximum free memory block size/Remaining memory size
44
45
46### Development Example<a name="section1025453412611"></a>
47
48This example implements the following:
49
501.  Creates a monitoring task to obtain information about the memory pool.
512.  Calls  **LOS\_MemInfoGet**  to obtain the basic information about the memory pool.
523.  Calculates the memory usage and fragmentation rate.
53
54**Sample Code**
55
56The sample code is as follows:
57
58```
59#include <stdio.h>
60#include <string.h>
61#include "los_task.h"
62#include "los_memory.h"
63#include "los_config.h"
64
65void MemInfoTaskFunc(void)
66{
67    LOS_MEM_POOL_STATUS poolStatus = {0};
68
69    /* pool is the memory address of the information to be collected. OS_SYS_MEM_ADDR is used as an example.*/
70    void *pool = OS_SYS_MEM_ADDR;
71    LOS_MemInfoGet(pool, &poolStatus);
72    /* Calculate the fragmentation rate of the memory pool. */
73    unsigned char fragment = 100 - poolStatus.maxFreeNodeSize * 100 / poolStatus.totalFreeSize;
74    /* Calculate the memory usage of the memory pool. */
75    unsigned char usage = LOS_MemTotalUsedGet(pool) * 100 / LOS_MemPoolSizeGet(pool);
76    printf("usage = %d, fragment = %d, maxFreeSize = %d, totalFreeSize = %d, waterLine = %d\n", usage, fragment, poolStatus.maxFreeNodeSize,
77           poolStatus.totalFreeSize, poolStatus.usageWaterLine);
78}
79
80int MemTest(void)
81{
82    unsigned int ret;
83    unsigned int taskID;
84    TSK_INIT_PARAM_S taskStatus = {0};
85    taskStatus.pfnTaskEntry = (TSK_ENTRY_FUNC)MemInfoTaskFunc;
86    taskStatus.uwStackSize  = 0x1000;
87    taskStatus.pcName       = "memInfo";
88    taskStatus.usTaskPrio   = 10;
89    ret = LOS_TaskCreate(&taskID, &taskStatus);
90    if (ret != LOS_OK) {
91        printf("task create failed\n");
92        return -1;
93    }
94    return 0;
95}
96```
97
98**Verification**
99
100The result is as follows:
101
102```
103usage = 22, fragment = 3, maxFreeSize = 49056, totalFreeSize = 50132, waterLine = 1414
104```
105
106