简体   繁体   English

确定嵌入式C程序的总内存使用量

[英]Determine total memory usage of embedded C program

I would like to be able to debug how much total memory is being used by C program in a limited resource environment of 256 KB memory (currently I am testing in an emulator program). 我希望能够调试C程序在256 KB内存的有限资源环境中使用多少总内存(目前我正在模拟器程序中进行测试)。

I have the ability to print debug statements to a screen, but what method should I use to calculate how much my C program is using (including globals, local variables [from perspective of my main function loop], the program code itself etc..)? 我有能力将调试语句打印到屏幕上,但我应该使用什么方法来计算我的C程序使用了多少(包括全局变量,局部变量[从我的主函数循环的角度来看],程序代码本身等等。 )?

A secondary aspect would be to display the location/ranges of specific variables as opposed to just their size. 第二个方面是显示特定变量的位置/范围,而不仅仅是它们的大小。

-Edit- The CPU is Hitachi SH2, I don't have an IDE that lets me put breakpoints into the program. -Edit- CPU是Hitachi SH2,我没有让我把断点放到程序中的IDE。

Using the IDE options make the proper actions (mark a checkobx, probably) so that the build process (namely, the linker) will generate a map file. 使用IDE选项可以执行适当的操作(可能标记一个checkobx),以便构建过程(即链接器)将生成一个映射文件。 A map file of an embedded system will normally give you the information you need in a detailed fashion: The memory segments, their sizes, how much memory is utilzed in each one, program memory, data memory, etc.. There is usually a lot of data supplied by the map file, and you might need to write a script to calculate exactly what you need, or copy it to Excel. 嵌入式系统的映射文件通常会以详细的方式为您提供所需的信息:内存段,它们的大小,每个内存的使用量,程序内存,数据存储器等。通常有很多地图文件提供的数据,您可能需要编写脚本来准确计算所需内容,或将其复制到Excel。 The map file might also contain summary information for you. 地图文件可能还包含您的摘要信息。

The stack is a bit trickier. 堆栈有点棘手。 If the map file gives that, then there you have it. 如果地图文件给出了那个,那么就有了它。 If not, you need to find it yourself. 如果没有,你需要自己找到它。 Embedded compilers usually let you define the stack location and size. 嵌入式编译器通常允许您定义堆栈的位置和大小。 Put a breakpoint in the start of you program. 在程序的开头放一个断点。 When the application stops there zero the entire stack. 当应用程序停止时,整个堆栈归零。 Resume the application and let it work for a while. 恢复应用程序并让它工作一段时间。 Finally stop it and inspect the stack memory. 最后停下来检查堆栈内存。 You will see non-zero values instead of zeros. 您将看到非零值而不是零。 The used stack goes until the zeros part starts again. 使用的堆栈一直持续到零部分再次启动。

Generally you will have different sections in mmap generated file, where data goes, like : 通常,您将在mmap生成的文件中有不同的部分,其中数据如下:
.intvect .intvect

.intvect_end .intvect_end
.rozdata .rozdata
.robase .robase
.rosdata .rosdata
.rodata .RODATA
.text .... and so on!!! .text ....等等!!!

with other attributes like Base,Size(hex),Size(dec) etc for each section. 与每个部分的其他属性,如基数,大小(十六进制),大小(十进制)等。

While at any time local variables may take up more or less space (as they go in and out of scope), they are instantiated on the stack. 虽然在任何时候局部变量可能占用更多或更少的空间(当它们进出范围时),它们在堆栈上被实例化。 In a single threaded environment, the stack will be a fixed allocation known at link time. 在单线程环境中,堆栈将是链接时已知的固定分配。 The same is true of all statically allocated data. 所有静态分配的数据都是如此。 The only run-time variable part id dynamically allocated data, but even then sich data is allocated from the heap, which in most bare-metal, single-threaded environments is a fixed link-time allocation. 唯一的运行时变量部分id动态分配数据,但即使这样,sich数据也是从堆中分配的,在大多数裸机,单线程环境中,这是一个固定的链路时间分配。

Consequently all the information you need about memory allocation is probably already provided by your linker. 因此,您可能已经由链接器提供了有关内存分配的所有信息。 Often (depending on your tool-chain and linker parameters used) basic information is output when the linker runs. 通常(取决于您使用的工具链和链接器参数)在链接器运行时输出基本信息。 You can usually request that a full linker map file is generated and this will give you detailed information. 您通常可以请求生成完整的链接器映射文件,这将为您提供详细信息。 Some linkers can perform stack usage analysis that will give you worst case stack usage for any particular function. 一些链接器可以执行堆栈使用分析,这将为您提供任何特定功能的最坏情况堆栈使用。 In a single threaded environment, the stack usage from main() will give worst case overall usage (although interrupt handlers need consideration, the linker is not thread or interrupt aware, and some architectures have separate interrupt stacks, some are shared). 在单线程环境中,main()的堆栈使用情况将给出最坏情况下的总体使用情况(尽管需要考虑中断处理程序,链接器不是线程或中断感知,并且某些体系结构具有单独的中断堆栈,其中一些是共享的)。

Although the heap itself is typically a fixed allocation (often all the available memory after the linker has performed static allocation of stack and static data), if you are using dynamic memory allocation, it may be useful at run-time to know how much memory has been allocated from the heap, as well as information about the number of allocations, average size of allocation, and the number of free blocks and their sizes also. 虽然堆本身通常是固定分配(通常是链接器执行堆栈和静态数据的静态分配后的所有可用内存),但如果使用动态内存分配,则在运行时知道有多少内存可能很有用已从堆中分配,以及有关分配数量,平均分配大小以及空闲块数量及其大小的信息。 Because dynamic memory allocation is implemented by your system's standard library any such analysis facility will be specific to your library, and may not be provided at all. 由于动态内存分配是由系统的标准库实现的,因此任何此类分析工具都将特定于您的库,并且可能根本不提供。 If you have the library source you could implement such facilities yourself. 如果您有库源,您可以自己实现这些工具。

In a multi-threaded environment, thread stacks may be allocated statically or from the heap, but either way the same analysis methods described above apply. 在多线程环境中,线程堆栈可以静态分配或从堆中分配,但无论采用哪种方式,上述相同的分析方法都适用。 For stack usage analysis, the worst-case for each thread is measured from the entry point of each thread rather than from main(). 对于堆栈使用情况分析,每个线程的最坏情况是从每个线程的入口点而不是从main()开始测量的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM