memory-analysis

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

固件内存分析

Firmware Memory Analysis

适用场景

Applicable Scenarios

  • 构建完成后需要了解固件的 Flash/RAM 使用情况。
  • 需要找出占用空间最大的函数或变量。
  • 需要对比两次构建的内存变化,追踪代码膨胀。
  • 需要确认固件是否即将超出芯片内存限制。
  • 需要解析链接脚本获取芯片 Flash/RAM 总容量。
  • Need to understand the Flash/RAM usage of the firmware after build completion.
  • Need to find the functions or variables that occupy the most space.
  • Need to compare memory changes between two builds and track code bloat.
  • Need to confirm whether the firmware is about to exceed the chip memory limit.
  • Need to parse linker scripts to obtain the total Flash/RAM capacity of the chip.

必要输入

Required Inputs

  • .map
    文件路径(GCC/ARM 链接器生成),或 ELF 文件路径。
  • 可选的链接脚本路径(用于计算使用率百分比)。
  • 可选的告警阈值和 Top-N 符号数量。
  • Path to .map file (generated by GCC/ARM linker), or path to ELF file.
  • Optional linker script path (used to calculate usage percentage).
  • Optional alarm threshold and Top-N symbol count.

自动探测

Automatic Detection

  • --scan
    模式自动搜索构建目录中的
    .map
    和 ELF 文件。
  • 解析
    .map
    文件中的
    Memory Configuration
    块获取 FLASH/RAM 总容量。
  • 若提供链接脚本,从
    MEMORY {}
    块解析容量信息。
  • 自动检测
    arm-none-eabi-size
    arm-none-eabi-readelf
    可用性。
  • --scan
    mode automatically searches for .map and ELF files in the build directory.
  • Parse the
    Memory Configuration
    block in the .map file to obtain the total FLASH/RAM capacity.
  • If a linker script is provided, parse capacity information from the
    MEMORY {}
    block.
  • Automatically detect the availability of
    arm-none-eabi-size
    and
    arm-none-eabi-readelf
    .

执行步骤

Execution Steps

  1. 先阅读 references/usage.md,确认本次是环境探测、构建目录扫描,还是执行分析。
  2. 若不确定工具是否可用,先运行 scripts/memory_analyzer.py
    --detect
    模式确认。
  3. 使用
    --scan <build_dir>
    自动查找可分析的文件。
  4. 使用
    --map-file
    --elf
    执行内存分析。
  5. 可选使用
    --linker-script
    获取总容量并计算使用率。
  6. 使用
    --top <N>
    查看占用最大的符号。
  7. 使用
    --compare
    对比两个
    .map
    文件的差异。
  1. First read references/usage.md to confirm whether this is environment detection, build directory scanning, or execution of analysis.
  2. If unsure whether the tool is available, first run the
    --detect
    mode of scripts/memory_analyzer.py to confirm.
  3. Use
    --scan <build_dir>
    to automatically find analyzable files.
  4. Use
    --map-file
    or
    --elf
    to perform memory analysis.
  5. Optionally use
    --linker-script
    to obtain total capacity and calculate usage rate.
  6. Use
    --top <N>
    to view the symbols that occupy the most space.
  7. Use
    --compare
    to compare differences between two .map files.

失败分流

Failure Diversion

  • arm-none-eabi-size
    arm-none-eabi-readelf
    不可用且需要 ELF 分析时,返回
    environment-missing
  • 当指定的
    .map
    或 ELF 文件不存在时,返回
    artifact-missing
  • .map
    文件格式无法识别或解析失败时,返回
    project-config-error
  • 当内存使用率超过告警阈值时,在成功结果中附带告警信息。
  • Return
    environment-missing
    when
    arm-none-eabi-size
    or
    arm-none-eabi-readelf
    is unavailable and ELF analysis is required.
  • Return
    artifact-missing
    when the specified .map or ELF file does not exist.
  • Return
    project-config-error
    when the .map file format cannot be recognized or parsing fails.
  • When memory usage exceeds the alarm threshold, attach alarm information to the successful result.

平台说明

Platform Notes

  • .map
    文件解析使用纯正则,无需外部工具,全平台可用。
  • ELF 分析需要
    arm-none-eabi-size
    ,通常随交叉编译工具链安装。
  • 链接脚本解析使用纯正则,支持 GCC LD 格式。
  • .map file parsing uses pure regular expressions, no external tools required, available on all platforms.
  • ELF analysis requires
    arm-none-eabi-size
    , which is usually installed with the cross-compilation toolchain.
  • Linker script parsing uses pure regular expressions, supporting GCC LD format.

输出约定

Output Conventions

  • 输出各 section(.text, .rodata, .data, .bss)的大小和地址。
  • 输出 Flash 使用量(.text + .rodata + .data)和 RAM 使用量(.data + .bss)。
  • 若有总容量信息,输出使用率百分比和告警状态。
  • 输出按大小排序的 Top-N 符号列表。
  • 对比模式输出各 section 和符号的增减变化。
  • Output the size and address of each section (.text, .rodata, .data, .bss).
  • Output Flash usage (.text + .rodata + .data) and RAM usage (.data + .bss).
  • If total capacity information is available, output usage percentage and alarm status.
  • Output a sorted list of Top-N symbols by size.
  • Comparison mode outputs the increase/decrease changes of each section and symbol.

交接关系

Handover Relationships

  • 当内存即将溢出需要优化时,建议用户审查 Top-N 大符号。
  • 当需要定位具体函数在代码中的位置时,可配合 IDE 或
    grep
    使用。
  • When memory is about to overflow and optimization is needed, it is recommended that users review the Top-N large symbols.
  • When needing to locate the specific position of a function in the code, it can be used with IDE or
    grep
    .