You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now that we've just released version 1.0 of this project, I think it would be useful to take some time to analyze the memory (RAM and FLASH) usage.
Indeed, both memory are nearly full right now : firmware is ~410KB out of the 460KB available, and it's becoming harder and harder to find RAM memory to allocate buffers when needed.
FLASH memory usage should be fairly easy to map using the .map file.
There is a global stack and heap regions allocated by the linker script. I think both are 8KB. How full are they? Can we shrink or are they already full?
FreeRTOS use a 16KB buffer as a heap for its own use (memory allocation, stack for tasks,...)
NimBLE probably use a good amount of RAM memory too. How much? And where is it allocated?
Mapping memory usage will allow us to see what's need to be optimized and which solutions we can implement to be able to continue to add new features and applications.
I uploaded the results of running linkermapviz vs develop: Full and without nimble and lvgl (they take 50% of space by themselves and skew perspective)
About NimBLE , we could again try replacing parts of it with our own custom made variant or we could try to completely ditch it and make our own stack based on it.(It does seem to use a WHOLE lot of memory)
it is possible that we are linking the entire static libs while we only use part of them.
i spent a while playing with LTO to try and shake the unused bits off, but i did not succeed (managed to get linking working but the resulting file was larger, so probably it both mangled LTO and disabled linker GC pass)
it is possible that we are linking the entire static libs while we only use part of them -- i spent a long while playing with LTO to try and shake the unused bits off, but it did not work (linking worked but the resulting bin file was larger)
True , is there anyway to find out which static libs are being used?
On Tue, 4 May 2021 at 13:06, MysteriousLog6 ***@***.***> wrote:
it is possible that we are linking the entire static libs while we only
use part of them -- i spent a long while playing with LTO to try and shake
the unused bits off, but it did not work (linking worked but the resulting
bin file was *larger*)
True , is there anyway to find out which static libs are being used?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#313 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA33I3TO26S2G5P6LBNMXATTL7WMNANCNFSM43PEJMVQ>
.
--
*Stack* is the new term for "I have no idea what I'm actually using".
About NimBLE , we could again try replacing parts of it with our own custom made variant or we could try to completely ditch it and make our own stack based on it.(It does seem to use a WHOLE lot of memory)
Absolutely not, I'm very against this. This is not a maintenance burden that's reasonable to take.
it is possible that we are linking the entire static libs while we only use part of them -- i spent a long while playing with LTO to try and shake the unused bits off, but it did not work (linking worked but the resulting bin file was larger)
As discussed, incorrect linker/compiler flags or their combinations, but yes it is difficult to achieve correct results. Correct LTO doesn't add size.
We could try to remake the heavy hitters (The Navigation and AnalogWatchface) from scratch and see if it helps.
No, that's unlikely to help in any significant way. Just store the used resources on external storage. Code is very very dense and ultra rarely the actual issue (that automated tools can't optimize).
it is possible that we are linking the entire static libs while we only use part of them -- i spent a long while playing with LTO to try and shake the unused bits off, but it did not work (linking worked but the resulting bin file was larger)
As discussed, incorrect linker/compiler flags or their combinations, but yes it is difficult to achieve correct results. Correct LTO doesn't add size.
yea that was implied in my message - now i see it was unclear; sorry for the noise. will update the comment
We could try to remake the heavy hitters (The Navigation and AnalogWatchface) from scratch and see if it helps.
No, that's unlikely to help in any significant way. Just store the used resources on external storage. Code is very very dense and ultra rarely the actual issue (that automated tools can't optimize).
About NimBLE , we could again try replacing parts of it with our own custom made variant or we could try to completely ditch it and make our own stack based on it.(It does seem to use a WHOLE lot of memory)
Absolutely not, I'm very against this. This is not a maintenance burden that's reasonable to take.
Well , is there anyway we can reduce the memory usage of NimBLE? I do understand why your against an entirely new Bluetooth stack , removing parts of it might work.
Compiling with size optimization (-Os) brings ~80KB savings (see this comment for details), down to ~320KB. I can't notice any performance difference when using the watch.
Another 35-40KB can be moved to flash (fonts + analog clock bg), although adding LittleFS support would bring ~20KB making net gains ~20KB
Compiling with size optimization (-Os) brings ~80KB savings (see this comment for details), down to ~320KB. I can't notice any performance difference when using the watch.
Another 35-40KB can be moved to flash (fonts + analog clock bg), although adding LittleFS support would bring ~20KB making net gains ~20KB
So if we move it to the SPI flash we save 30-40 (Let's say 35Kb) and then for LittleFS we have to take another 20kb so about 15Kb.
I did a deep analysis of the usage of the buffer dedicated for lvgl (managed by lv_mem).
This buffer is used by lvgl to allocated memory for drivers (display/touch), screens, themes, and all widgets created by the apps.
The usage of this buffer can be monitored using this code :
The most interesting metric is mon.max_used which specifies the maximum number of bytes that were used from this buffer since the initialization of lvgl.
According to my measurements, initializing the theme, display/touch driver and screens cost 4752 bytes!
Then, initializing the digital clock face costs 1541 bytes.
For example a simple lv_label needs ~140 bytes of memory.
I tried to monitor this max value while going through all the apps of InfiniTime 1.1 : the max value I've seen is 5660 bytes. It means that we could probably reduce the size of the buffer from 14KB to 6 - 10 KB (we have to take the fragmentation of the memory into account).
I also noticed that LVGL allows to specify a custom memory manager instead of the default one implemented in lv_mem.
That would allow us to use the FreeRTOS memory manager for LVGL. This way, we would have to allocate only 1 memory buffer for the RTOS and LVGL, thus reducing the overhead (both buffers are a bit bigger than actually needed).
I also noticed that LVGL allows to specify a custom memory manager instead of the default one implemented in lv_mem.
That would allow us to use the FreeRTOS memory manager for LVGL. This way, we would have to allocate only 1 memory buffer for the RTOS and LVGL, thus reducing the overhead (both buffers are a bit bigger than actually needed).
But would we have to increase the buffer size or remain the same.
We would probably have to increase the size of the buffer allocated to freertos, but also remove the buffer allocated to lvgl.
For now, both buffer are allocated with a size probably a bit larger than necessary (= overhead). If we use only one buffer, maybe we can reduce that overhead. To be analyzed :)
But would we have to increase the buffer size or remain the same.
We would probably have to increase the size of the buffer allocated to freertos, but also remove the buffer allocated to lvgl.
For now, both buffer are allocated with a size probably a bit larger than necessary (= overhead). If we use only one buffer, maybe we can reduce that overhead. To be analyzed :)
Is the overhead necessary in the future let's say while updating to LVGL 8?
So it would be nice to still keep a small overhead to be safe.
This stack will be used for everything except tasks, which have their own stack allocated by FreeRTOS. The stack is 8192B and is allocated in the linker script.
An easy way to monitor its usage is by filling the section with a known pattern at boot time, then use the firmware and dump the memory. You can then check the maximum stack usage by checking the address from the beginning of the stack that were overwritten.
Fill the stack section by a known pattern:
Edit /modules/nrfx/mdk/gcc_startup_nrf52.S and add the following code after the copy of the data from read only memory to RAM at around line 243:
/* Loop to copy data from read only memory to RAM.
* The ranges of copy from/to are specified by following symbols:
* __etext: LMA of start of the section to copy from. Usually end of text
* __data_start__: VMA of start of the section to copy to.
* __bss_start__: VMA of end of the section to copy to. Normally __data_end__ is used, but by using __bss_start__
* the user can add their own initialized data section before BSS section with the INTERT AFTER command.
*
* All addresses must be aligned to 4 bytes boundary.
*/
ldr r1, =__etext
ldr r2, =__data_start__
ldr r3, =__bss_start__
subs r3, r3, r2
ble .L_loop1_done
.L_loop1:
subs r3, r3, #4
ldr r0, [r1,r3]
str r0, [r2,r3]
bgt .L_loop1
.L_loop1_done:
/* Add this code to fill the stack section with 0xFFEEDDBB */
ldr r0, =__StackLimit
ldr r1, =8192
ldr r2, =0xFFEEDDBB
.L_fill:
str r2, [r0]
adds r0, 4
subs r1, 4
bne .L_fill
/* -- */
Dump RAM memory and check usage
Dumping the content of the ram is easy using JLink debugger and nrfjprog:
nrfjprog --readram ram.bin
You can then display the file using objdump:
hexdump ram.bin -v |less
The stack is positionned at the end of the RAM -> 0xFFFF. Its size is 8192 bytes, so the end of the stack is at 0xE000.
On the following dump, the maximum stack usage is 520 bytes (0xFFFF - 0xFDF8):
The heap is declared in the linker script and its current size is 8192 bytes. The heap is used for dynamic memory allocation(malloc(), new,...).
Heap monitoring is not easy, but it seems that we can use the following code to know the current usage of the heap:
auto m = mallinfo();
NRF_LOG_INFO("heap : %d", m.uordblks);
According to my experimentation, InfiniTime uses ~6000bytes of heap most of the time. Except when the Navigation app is launched, where the heap usage increases to... more than 9500 bytes (meaning that the heap overflows and could potentially corrupt the stack!!!). This is a bug that should be fixed in #362.
To know exactly what's consuming heap memory, you can wrap functions like malloc() into your own functions. In this wrapper, you can add logging code or put breakpoints:
Add -Wl,-wrap,malloc to the cmake variable LINK_FLAGS of the target you want to debug (pinetime-app, most probably)
The heap is declared in the linker script and its current size is 8192 bytes. The heap is used for dynamic memory allocation(malloc(), new,...).
Heap monitoring is not easy, but it seems that we can use the following code to know the current usage of the heap:
auto m = mallinfo();
NRF_LOG_INFO("heap : %d", m.uordblks);
According to my experimentation, InfiniTime uses ~6000bytes of heap most of the time. Except when the Navigation app is launched, where the heap usage increases to... more than 9500 bytes (meaning that the heap overflows and could potentially corrupt the stack!!!). This is a bug that should be fixed in #362.
To know exactly what's consuming heap memory, you can wrap functions like malloc() into your own functions. In this wrapper, you can add logging code or put breakpoints:
Add -Wl,-wrap,malloc to the cmake variable LINK_FLAGS of the target you want to debug (pinetime-app, most probably)
About the overflow of stack is this somehow related to #327 , this could probably solve two problems at once.
Yes, it could be the cause of the issue, indeed. Any other memory issue could also cause that kind of error, though...
I've just tried to open/close SystemInfo 20-30x on current develop without any crash.
I also noticed that LVGL allows to specify a custom memory manager instead of the default one implemented in lv_mem. That would allow us to use the FreeRTOS memory manager for LVGL. This way, we would have to allocate only 1 memory buffer for the RTOS and LVGL, thus reducing the overhead (both buffers are a bit bigger than actually needed).
I did a deep analysis of the usage of the buffer dedicated for lvgl (managed by lv_mem). This buffer is used by lvgl to allocated memory for drivers (display/touch), screens, themes, and all widgets created by the apps.
The usage of this buffer can be monitored using this code :
The most interesting metric is mon.max_used which specifies the maximum number of bytes that were used from this buffer since the initialization of lvgl. According to my measurements, initializing the theme, display/touch driver and screens cost 4752 bytes! Then, initializing the digital clock face costs 1541 bytes. For example a simple lv_label needs ~140 bytes of memory.
I tried to monitor this max value while going through all the apps of InfiniTime 1.1 : the max value I've seen is 5660 bytes. It means that we could probably reduce the size of the buffer from 14KB to 6 - 10 KB (we have to take the fragmentation of the memory into account).
Hey, I am sorry to respond on a closed thread, but we are working on an embedded system project which utilizes the LVGL library for UI rendering and input handling. Similar to your analysis, I was able to perform memory profiling for objects using the LVGL memory monitors. For our project, it came out around 5KiB during general usage. Therefore, we allocated a static buffer to the library and are happy with the configuration.
However, we are unsure about stack depth caused by the lv_task_handler. Do you have any estimate as to how much stack each call to this function grows? LVGL has plenty of callbacks (signal_cb, event_cb), and therefore we are not able to accurately measure the stack usage. We were wondering if you have had some analysis in this direction before? It would be great to learn about the same.
We are looking at revamping our code architecture at this stage, therefore are looking to do some ground work around memory profiling. I stumbled upon your project while I was digging issues on the LVGL repository.
PS: We currently don't use any RTOS framework and don't have any plans to integrate it soon.
Thanks in advance! Looking forward to your response.
@kartikCypherock Don't worry, that's fine to respond to this closed thread! Memory management is a complex topic, and that's exactly why I take the time to document all my findings! I'm glad to see that this can be useful to other developers :)
FYI I continue this analysis in a new post here and opened a new PR to unify all heaps here.
Regarding your question about the stack usage, I wrote this comment about the "global" stack analysis. In this context, I call the Global stack the main stack of the application (as opposed to the stack of the various FreeRTOS tasks that are running in InfiniTime). I can't remember any way to monitor the stack usage at runtime, but you can use those techniques to ensure that your application does not overflow when running specific use-cases.
LVGL is integrated in a FreeRTOS task so we can use tools from the OS to get info about the stack usage at runtime : current stack usage and the minimum amount of memory available in the stack since the beginning of the execution. This is very useful to find the best size for all the stacks.
Activity
ObiKeahloa commentedon Apr 30, 2021
Can we try reducing the FreeRTOS heap size or is it already at its smallest.
We could also try to remake NimBLE but that would require a lot of handwork and isn't guaranteed to succeed.
kieranc commentedon May 2, 2021
This shows the reduction in the DFU file size that I saw when disabling each of the following components:
DavidVentura commentedon May 2, 2021
I did some quick analysis using a fork of linkermapviz
I uploaded the results of running linkermapviz vs develop: Full and without nimble and lvgl (they take 50% of space by themselves and skew perspective)
ObiKeahloa commentedon May 4, 2021
We could try to remake the heavy hitters (The Navigation and AnalogWatchface) from scratch and see if it helps.
ObiKeahloa commentedon May 4, 2021
About NimBLE , we could again try replacing parts of it with our own custom made variant or we could try to completely ditch it and make our own stack based on it.(It does seem to use a WHOLE lot of memory)
DavidVentura commentedon May 4, 2021
it is possible that we are linking the entire static libs while we only use part of them.
i spent a while playing with LTO to try and shake the unused bits off, but i did not succeed (managed to get linking working but the resulting file was larger, so probably it both mangled LTO and disabled linker GC pass)
ObiKeahloa commentedon May 4, 2021
True , is there anyway to find out which static libs are being used?
DavidVentura commentedon May 4, 2021
Avamander commentedon May 4, 2021
Absolutely not, I'm very against this. This is not a maintenance burden that's reasonable to take.
Avamander commentedon May 4, 2021
As discussed, incorrect linker/compiler flags or their combinations, but yes it is difficult to achieve correct results. Correct LTO doesn't add size.
Avamander commentedon May 4, 2021
No, that's unlikely to help in any significant way. Just store the used resources on external storage. Code is very very dense and ultra rarely the actual issue (that automated tools can't optimize).
DavidVentura commentedon May 4, 2021
yea that was implied in my message - now i see it was unclear; sorry for the noise. will update the comment
ObiKeahloa commentedon May 5, 2021
Well , is there anyway we can reduce the memory usage of NimBLE? I do understand why your against an entirely new Bluetooth stack , removing parts of it might work.
DavidVentura commentedon May 5, 2021
Compiling with size optimization (
-Os) brings ~80KB savings (see this comment for details), down to ~320KB. I can't notice any performance difference when using the watch.Another 35-40KB can be moved to flash (fonts + analog clock bg), although adding LittleFS support would bring ~20KB making net gains ~20KB
ObiKeahloa commentedon May 6, 2021
So if we move it to the SPI flash we save 30-40 (Let's say 35Kb) and then for LittleFS we have to take another 20kb so about 15Kb.
5 remaining items
JF002 commentedon May 29, 2021
I did a deep analysis of the usage of the buffer dedicated for lvgl (managed by lv_mem).
This buffer is used by lvgl to allocated memory for drivers (display/touch), screens, themes, and all widgets created by the apps.
The usage of this buffer can be monitored using this code :
The most interesting metric is
mon.max_usedwhich specifies the maximum number of bytes that were used from this buffer since the initialization of lvgl.According to my measurements, initializing the theme, display/touch driver and screens cost 4752 bytes!
Then, initializing the digital clock face costs 1541 bytes.
For example a simple lv_label needs ~140 bytes of memory.
I tried to monitor this max value while going through all the apps of InfiniTime 1.1 : the max value I've seen is 5660 bytes. It means that we could probably reduce the size of the buffer from 14KB to 6 - 10 KB (we have to take the fragmentation of the memory into account).
JF002 commentedon May 30, 2021
I also noticed that LVGL allows to specify a custom memory manager instead of the default one implemented in lv_mem.
That would allow us to use the FreeRTOS memory manager for LVGL. This way, we would have to allocate only 1 memory buffer for the RTOS and LVGL, thus reducing the overhead (both buffers are a bit bigger than actually needed).
https://github.com/JF002/InfiniTime/blob/develop/src/libs/lv_conf.h#L74
ObiKeahloa commentedon May 30, 2021
But would we have to increase the buffer size or remain the same.
JF002 commentedon May 30, 2021
We would probably have to increase the size of the buffer allocated to freertos, but also remove the buffer allocated to lvgl.
For now, both buffer are allocated with a size probably a bit larger than necessary (= overhead). If we use only one buffer, maybe we can reduce that overhead. To be analyzed :)
ObiKeahloa commentedon May 30, 2021
Is the overhead necessary in the future let's say while updating to LVGL 8?
So it would be nice to still keep a small overhead to be safe.
JF002 commentedon May 30, 2021
Yes, of course... but a (one) small overhead is better than 2 :)
Anyway, this is just an idea, I don't know if that'll work.
And we will probably have to review the memory allocation as we add features and update components and libraries, this is not written in stone.
The issue we are facing right now is that it's really difficult to add anything in the firmware because there's almost no RAM/Flash memory available.
JF002 commentedon May 30, 2021
"Global" stack analysis:
This stack will be used for everything except tasks, which have their own stack allocated by FreeRTOS. The stack is 8192B and is allocated in the linker script.
An easy way to monitor its usage is by filling the section with a known pattern at boot time, then use the firmware and dump the memory. You can then check the maximum stack usage by checking the address from the beginning of the stack that were overwritten.
Fill the stack section by a known pattern:
Edit /modules/nrfx/mdk/gcc_startup_nrf52.S and add the following code after the copy of the data from read only memory to RAM at around line 243:
Dump RAM memory and check usage
Dumping the content of the ram is easy using JLink debugger and
nrfjprog:You can then display the file using objdump:
The stack is positionned at the end of the RAM -> 0xFFFF. Its size is 8192 bytes, so the end of the stack is at 0xE000.
On the following dump, the maximum stack usage is 520 bytes (0xFFFF - 0xFDF8):
According to my experimentations, we don't use the stack that much, and 8192 bytes is probably way too big for InfiniTime!
JF002 commentedon May 30, 2021
"Global" heap usage
The heap is declared in the linker script and its current size is 8192 bytes. The heap is used for dynamic memory allocation(
malloc(),new,...).Heap monitoring is not easy, but it seems that we can use the following code to know the current usage of the heap:
According to my experimentation, InfiniTime uses ~6000bytes of heap most of the time. Except when the Navigation app is launched, where the heap usage increases to... more than 9500 bytes (meaning that the heap overflows and could potentially corrupt the stack!!!). This is a bug that should be fixed in #362.
To know exactly what's consuming heap memory, you can
wrapfunctions likemalloc()into your own functions. In this wrapper, you can add logging code or put breakpoints:-Wl,-wrap,mallocto the cmake variableLINK_FLAGSof the target you want to debug (pinetime-app, most probably)main.cppNow, your function
__wrap_malloc()will be called instead ofmalloc(). You can call the actual malloc from the stdlib by calling__real_malloc().Using this technique, I was able to trace all malloc calls at boot (boot -> digital watchface):
Interesting articles :
https://www.embedded.com/mastering-stack-and-heap-for-system-reliability-part-1-calculating-stack-size/
https://www.embedded.com/mastering-stack-and-heap-for-system-reliability-part-2-properly-allocating-stacks/
https://www.embedded.com/mastering-stack-and-heap-for-system-reliability-part-3-avoiding-heap-errors/
ObiKeahloa commentedon May 31, 2021
About the overflow of stack is this somehow related to #327 , this could probably solve two problems at once.
JF002 commentedon Jun 1, 2021
@MysteriousLog6
Yes, it could be the cause of the issue, indeed. Any other memory issue could also cause that kind of error, though...
I've just tried to open/close SystemInfo 20-30x on current develop without any crash.
JF002 commentedon Jun 3, 2021
I summarized this analysis in the following PR : #411
kartikCypherock commentedon Mar 23, 2023
`
Hey, I am sorry to respond on a closed thread, but we are working on an embedded system project which utilizes the LVGL library for UI rendering and input handling. Similar to your analysis, I was able to perform memory profiling for objects using the LVGL memory monitors. For our project, it came out around 5KiB during general usage. Therefore, we allocated a
staticbuffer to the library and are happy with the configuration.However, we are unsure about stack depth caused by the
lv_task_handler. Do you have any estimate as to how much stack each call to this function grows? LVGL has plenty of callbacks (signal_cb,event_cb), and therefore we are not able to accurately measure the stack usage. We were wondering if you have had some analysis in this direction before? It would be great to learn about the same.We are looking at revamping our code architecture at this stage, therefore are looking to do some ground work around memory profiling. I stumbled upon your project while I was digging issues on the LVGL repository.
PS: We currently don't use any RTOS framework and don't have any plans to integrate it soon.
Thanks in advance! Looking forward to your response.
JF002 commentedon Mar 26, 2023
@kartikCypherock Don't worry, that's fine to respond to this closed thread! Memory management is a complex topic, and that's exactly why I take the time to document all my findings! I'm glad to see that this can be useful to other developers :)
FYI I continue this analysis in a new post here and opened a new PR to unify all heaps here.
Regarding your question about the stack usage, I wrote this comment about the "global" stack analysis. In this context, I call the Global stack the main stack of the application (as opposed to the stack of the various FreeRTOS tasks that are running in InfiniTime). I can't remember any way to monitor the stack usage at runtime, but you can use those techniques to ensure that your application does not overflow when running specific use-cases.
LVGL is integrated in a FreeRTOS task so we can use tools from the OS to get info about the stack usage at runtime : current stack usage and the minimum amount of memory available in the stack since the beginning of the execution. This is very useful to find the best size for all the stacks.