-2

I would like to report how much memory is being used (Flash as well as RAM) on the fly (embedded environment). Does anyone knows a technique to do this? I've heard about using a "watermark" but really don't get it.

The embedded system is a 16 bit microcontroller from NXP's MagniV family, the approach of the application is bare-metal.

Alexis
  • 23
  • 1
  • 4

2 Answers2

3

There is probably no need to report such information "on the fly", since the memory usage is statically determined when the code is linked. The link-map generated by your linker will provide this information.

What may be useful to determine at run-time is the heap and stack usage. Heap usage reporting, if possible will be entirely down to the C library you are using. For example using ARM's library, the __heapstats() function will provide that.

"Water-marking" of stack memory involves filling it with a specific bit pattern and then scanning the stack to see where that pattern has been changed - that is indicative of maximum stack usage (the high water mark).

Some linkers are capable of performing static analysis of stack usage, and can report the worst case stack usage and call-path for any particular function. The analysis is however worst case and the call-path for which it is determined my never occur in real execution. Also such analysis cannot provide stack usage with calls made through function pointer variables or recursive functions.

Advice on stack usage analysis for various common scenarios (bare-metal, RTOS, memory-map and toolchain combinations) can be found in this article

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • The first sentence is true for the flash but the ram scenario is different, The water-marking explanation was really good and the resource that you shared helped me a lot now i have it clear. Thank you. – Alexis Feb 18 '18 at 15:24
  • 2
    @Kahjiit : You are incorrect. The link map will include statically allocated RAM usage, including the heap and stack allocations (though not the stack and heap _usage_). – Clifford Feb 18 '18 at 15:31
  • @Cliddord: As you can see, the question never mentioned nothing about allocation. I know that i can get (and configure) that from the linker. The question is about usage. – Alexis Feb 18 '18 at 15:50
  • 1
    @Kahjiit : That is clearly a matter of point-of-view. From the point-of-view of the linker _all_ memory allocated is _used_. From the point-of-view of the _application_ the _usage_ of both stack and heap pools is variable. So there is nothing false about the first statement. Since in your question you conflated flash memory usage with RAM, it would not be unreasonable to assume a linker PoV, but since it was ambiguated by your desire to determine usage "on the fly", I clearly covered both possibilities. An answer covering aspects you did not expect or require does not make it false. – Clifford Feb 18 '18 at 17:31
0

If you are using gcc linker you can use -Wl,--print-memory-usage as linker parameter as shown in this question.

  • 1
    That only shows static usage, but it is a start. It could be combined with printing out runtime usage such as the stack pointer and effective heap size, though the stack at least will vary in different parts of the program. Initializing memory to a unique value and then dumping it all after some operation could show a high water record. – Chris Stratton Feb 17 '18 at 20:06