resource-optimization-lowend
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseResource Optimization (Low-End Systems)
资源优化(低端系统)
Purpose
目标
Guide agents through flash and RAM optimization on constrained devices: compiler size flags, linker map analysis, stack usage measurement, dead code elimination, and size-vs-speed tradeoffs — for bare-metal and small RTOS images.
指导 Agent 在资源受限设备上进行 flash 和 RAM 优化:编译器体积优化标志、linker map 分析、栈使用量测量、无用代码消除,以及体积与速度之间的权衡——适用于裸机和小型 RTOS 镜像。
When to Use
使用场景
- Firmware exceeds flash budget
- Stack overflow in production only
- Choosing vs
-Oson MCU-O2 - Finding unexpected or
.rodatagrowth.bss
- 固件超出 flash 预算
- 栈溢出仅在生产环境中发生
- 在 MCU 上选择 与
-Os-O2 - 发现异常的 或
.rodata增长.bss
Workflow
工作流
1. Size measurement toolchain
1. 体积测量工具链
bash
arm-none-eabi-size -A firmware.elf
arm-none-eabi-objdump -h firmware.elf
nm --size-sort -S firmware.elf | tail -20bash
arm-none-eabi-size -A firmware.elf
arm-none-eabi-objdump -h firmware.elf
nm --size-sort -S firmware.elf | tail -202. Linker map file
2. Linker map 文件
ld
/* linker.ld */
OUTPUT_FORMAT("elf32-littlearm")
ENTRY(Reset_Handler)
SECTIONS
{
/* ... */
}
/* Generate map */
/* gcc ... -Wl,-Map=firmware.map */bash
grep -E '\.text|\.rodata|\.data|\.bss' firmware.map | headIdentify largest symbols and unexpected library pull-in.
ld
/* linker.ld */
OUTPUT_FORMAT("elf32-littlearm")
ENTRY(Reset_Handler)
SECTIONS
{
/* ... */
}
/* Generate map */
/* gcc ... -Wl,-Map=firmware.map */bash
grep -E '\.text|\.rodata|\.data|\.bss' firmware.map | head找出最大的符号和意外引入的库。
3. Compiler flags
3. 编译器标志
| Flag | Effect |
|---|---|
| Size-first optimization |
| Per-symbol sections |
| Drop unused sections |
| Cross-TU dead code elimination |
| Smaller newlib (GCC ARM) |
makefile
CFLAGS += -Os -ffunction-sections -fdata-sections
LDFLAGS += -Wl,--gc-sections -Wl,--print-memory-usage| 标志 | 效果 |
|---|---|
| 优先优化体积 |
| 按符号划分段 |
| 丢弃未使用的段 |
| 跨 TU 消除无用代码 |
| 更小的 newlib(GCC ARM) |
makefile
CFLAGS += -Os -ffunction-sections -fdata-sections
LDFLAGS += -Wl,--gc-sections -Wl,--print-memory-usage4. Stack analysis
4. 栈分析
bash
undefinedbash
undefinedStatic (if built with -fstack-usage)
Static (if built with -fstack-usage)
find . -name '*.su' -exec cat {} ;
find . -name '*.su' -exec cat {} ;
Linker stack symbol
Linker stack symbol
grep _estack firmware.map
Runtime: fill stack with pattern (`0xDEADBEEF`), run tests, scan high-water mark. FreeRTOS: `uxTaskGetStackHighWaterMark`.grep _estack firmware.map
运行时:使用模式(`0xDEADBEEF`)填充栈,运行测试并扫描 high-water mark。FreeRTOS:`uxTaskGetStackHighWaterMark`。5. RAM categories
5. RAM 类别
| Section | Tactic |
|---|---|
| Shrink buffers; pool allocators |
| Move const to flash ( |
| Heap | Avoid malloc; fixed pools |
| Stack | Reduce nesting; smaller ISR stacks |
| 段 | 策略 |
|---|---|
| 缩小缓冲区;使用池分配器 |
| 将 const 移至 flash( |
| Heap | 避免 malloc;使用固定内存池 |
| Stack | 减少嵌套;缩小 ISR 栈 |
6. Size vs speed decision
6. 体积与速度的决策
Hot path in ISR or 1 kHz loop?
├── Yes → -O2 for that file (#pragma GCC optimize)
└── No → -Os globallyHot path in ISR or 1 kHz loop?
├── Yes → -O2 for that file (#pragma GCC optimize)
└── No → -Os globally7. Agent usage
7. Agent 使用方式
/resource-optimization-lowend Find top 10 flash consumers in firmware.map and suggest -gc-sections fixes/resource-optimization-lowend Find top 10 flash consumers in firmware.map and suggest -gc-sections fixesCommon Problems
常见问题
| Symptom | Cause | Fix |
|---|---|---|
| printf pulls 20+ KB | Full newlib printf | |
| Section collected | |
| Stack overflow late | Deep call + IRQ nest | Measure HW stack; increase |
| RAM zero but big ELF | | Check VMA/LMA |
| LTO link fail | Mixed compiler versions | Same GCC for all objects |
| 症状 | 原因 | 修复方案 |
|---|---|---|
| printf 引入 20+ KB | 完整的 newlib printf | 重定向 |
| 段被回收 | 在 linker script 中使用 |
| 栈在后期才溢出 | 调用层级过深 + IRQ 嵌套 | 测量硬件栈;增大 |
| RAM 为零但 ELF 很大 | | 检查 VMA/LMA |
| LTO 链接失败 | 编译器版本混用 | 所有目标文件使用同一 GCC |
Related Skills
相关 Skill
- — MEMORY/Sections layout
skills/embedded/linker-scripts - — stack symbol
skills/baremetal/baremetal-startup - — RAM retention
skills/baremetal/low-power-embedded - — optimization flags
skills/compilers/gcc - — embedded Rust size patterns
skills/rust/rust-no-std
- — MEMORY/Sections 布局
skills/embedded/linker-scripts - — 栈符号
skills/baremetal/baremetal-startup - — RAM 保留
skills/baremetal/low-power-embedded - — 优化标志
skills/compilers/gcc - — 嵌入式 Rust 体积优化模式
skills/rust/rust-no-std