• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Memory Information Statistics
2
3
4## Basic Concepts
5
6Memory information includes the memory pool size, memory usage, remaining memory size, maximum free memory, memory waterline, number of memory nodes, and fragmentation rate.
7
8- The memory waterline indicates the maximum memory used in a memory pool. The waterline value is updated each time the memory is allocated or released. The memory pool size can be optimized based on this value.
9
10- The 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:<br>Fragmentation rate = 100 – 100 x Maximum free memory block size/Remaining memory size
11
12- You can use **LOS_MemInfoGet()** to scan the node information in the memory pool and collect the related statistics.
13
14
15## Function Configuration
16
17**LOSCFG_MEM_WATERLINE** specifies the setting of the memory information statistics function. This function is disabled by default. If you want to obtain the memory waterline, enable it in **Debug-&gt; Enable MEM Debug-&gt; Enable memory pool waterline or not**.
18
19
20## Development Guidelines
21
22
23### How to Develop
24
25Key structure:
26
27
28```c
29typedef struct {
30    UINT32 totalUsedSize;       // Memory usage of the memory pool.
31    UINT32 totalFreeSize;       // Remaining size of the memory pool.
32    UINT32 maxFreeNodeSize;     // Maximum size of the free memory block in the memory pool.
33    UINT32 usedNodeNum;         // Number of non-free memory blocks in the memory pool.
34    UINT32 freeNodeNum;         // Number of free memory blocks in the memory pool.
35#if (LOSCFG_MEM_WATERLINE == 1) // This function is disabled by default and can be enabled using the **menuconfig** tool.
36    UINT32 usageWaterLine;      // Waterline of the memory pool.
37#endif
38} LOS_MEM_POOL_STATUS;
39```
40
41To obtain the memory waterline, call **LOS_MemInfoGet(VOID *pool, LOS_MEM_POOL_STATUS *poolStatus)**. The first parameter specifies the start address of the memory pool, and the second parameter specifies the handle of the **LOS_MEM_POOL_STATUS** type. The **usageWaterLine** field indicates the waterline.
42
43To 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:
44
45Fragmentation rate = 100 – 100 x Maximum free memory block size/Remaining memory size
46
47
48### Development Example
49
50This example implements the following:
51
521. Create a monitoring task to obtain information about the memory pool.
53
542. Call **LOS_MemInfoGet** to obtain the basic information about the memory pool.
55
563. Calculate the memory usage and fragmentation rate.
57
58**Sample Code**
59
60You can compile and verify the sample code in **kernel/liteos_a/testsuites/kernel/src/osTest.c**. The **MemTest()** function is called in **TestTaskEntry**.
61
62The sample code is as follows:
63
64```c
65#include <stdio.h>
66#include <string.h>
67#include "los_task.h"
68#include "los_memory.h"
69#include "los_config.h"
70
71void MemInfoTaskFunc(void)
72{
73    LOS_MEM_POOL_STATUS poolStatus = {0};
74
75    /* pool is the memory address of the information to be collected. OS_SYS_MEM_ADDR is used as an example. */
76    void *pool = OS_SYS_MEM_ADDR;
77    LOS_MemInfoGet(pool, &poolStatus);
78    /* Calculate the fragmentation rate of the memory pool. */
79    unsigned char fragment = 100 - poolStatus.maxFreeNodeSize * 100 / poolStatus.totalFreeSize;
80    /* Calculate the memory usage of the memory pool. */
81    unsigned char usage = LOS_MemTotalUsedGet(pool) * 100 / LOS_MemPoolSizeGet(pool);
82    dprintf("usage = %d, fragment = %d, maxFreeSize = %d, totalFreeSize = %d, waterLine = %d\n", usage, fragment,                            poolStatus.maxFreeNodeSize, poolStatus.totalFreeSize, poolStatus.usageWaterLine);
83}
84
85int MemTest(void)
86{
87    unsigned int ret;
88    unsigned int taskID;
89    TSK_INIT_PARAM_S taskStatus = {0};
90    taskStatus.pfnTaskEntry = (TSK_ENTRY_FUNC)MemInfoTaskFunc;
91    taskStatus.uwStackSize  = 0x1000;
92    taskStatus.pcName       = "memInfo";
93    taskStatus.usTaskPrio   = 10;
94    ret = LOS_TaskCreate(&taskID, &taskStatus);
95    if (ret != LOS_OK) {
96        dprintf("task create failed\n");
97        return LOS_NOK;
98    }
99    return LOS_OK;
100}
101```
102
103**Verification**
104
105
106The result is as follows:
107
108The data may vary depending on the running environment.
109
110```
111usage = 22, fragment = 3, maxFreeSize = 49056, totalFreeSize = 50132, waterLine = 1414
112```
113