3.4. Memory Management

3.4.1. Introduction

EGOS embeds a memory allocator based on TLSF algorithm (Two-Level Segregated Fit). It is designed to meet real-time requirements: short and bounded response time, and efficient memory use (low fragmentation). When some memory blocks are free, they can used then to do a new allocation.

Each allocated blocks (for example: when a process is created, a message is sent, ...) is owned by its creator. In the case of an entity's creation, the entity is the owner of the new allocation.

When an entity is created, a stack is allocated. For dimensioning the size of the stack, it should be good to analyse the memory consumption requirement. Its stack will allow to access quickly to free memory instead of dynamic allocation. When a block memory has the vocation to be released then, a dynamic allocation should be better.

l_pnNewData
= malloc(sizeof(nU32));

Then the memory shall be free with the returned pointer at the creation:

free(l_pnNewData);

3.4.2. Debug memory use

The developer can get the status of the used memory during execution on the debug port of the board. The input parameter allows to display periodically the status (the value is in seconds, 0 for a one-shot display).

The output debug port can be change in 1_BSPC/SRC/inc/bspc_oss/bspc_oss_dbg__cfg.h (be aware of the ouput debug port available on the board). To use a specific port, the corresponding line needs to be uncommented:

#define dOSS_DBG__CFG__USE_UART
//#define dOSS_DBG__CFG__USE_SWO
//#define dOSS_DBG__CFG__USE_NONE
egos_oss_dbg_StatsDisp( .i_nPeriod = 0);

The following display will appear on the defined debug port:

Pool Size: 91152  Used: 36312 (39/100)  Max use: 36872 (40/100)

Entity name  / Allocs size (var)

egOS           0           (0)
timr           3136        (0)
app            0           (0)
user           5248        (0)
event          5248        (0)
drv            0           (0)
imu            2104        (0)
acc            4288        (0)
hmi            2104        (0)
button         3264        (0)
led            2832        (0)
com            2104        (0)
lpwan          2112        (0)
lora           3872        (0)
  • Pool size: all available memory (free and allocated)
  • Used: the allocated memory size (ratio of memory used)
  • Max use: the highest value of memory allocation during this running time
  • (var): variation of memory between two stat displays
  • Allocs size: contains entity structure, stack, chest, dynamic allocation, messages

This display should help during the debug step and to see if there is a memory leak.

If the profiler is enabled in your configuration, it is also possible to dipslay in addition the consumption of your stack. To check if the profiler is enabled in your environment, you need to access to :

  • 1_BSPC/SRC/inc/bspc_oss/bspc_oss_exe_task__cfg.h

The following line needs to be uncommented:

#define dOSS_EXE_TASK__CFG__USE_PROFILER        (1)
  • 2_USER/SRC/inc/egos_cfg/BOARDS/"board_name"/kernel_cfg

The following line needs to be uncommented:

#define dEGOS__CFG__USE_OSS_PROC

When the profiler is enabled, it is possible to see the bigger stack size used during the running time. It shall allow to optimize the stack size of each process. Be aware to not reduce to the stack size to the displayed stack size, it may need additional memory to do some stuff.

Pool Size: 91152  Used: 39712 (43/100)  Max use: 40280 (44/100)

Entity name  / Allocs size (var)   / Stack size  (used)

egOS           0           (0)       4096        3772
timr           3152        (0)       2048        512
app            0           (0)       1024        120
user           5264        (0)       4096        1400
event          5264        (0)       4096        1180
drv            0           (0)       1024        684
imu            2128        (0)       1024        552
acc            4304        (0)       3072        804
hmi            2128        (0)       1024        632
button         3280        (0)       1536        952
led            2848        (0)       1536        1144
com            2128        (0)       1024        552
lpwan          2128        (0)       1024        552
lora           3872        (0)       2048        1224
oss            0           (0)       1024        112
prof           3216        (0)       2048        308