From cb63c581bee4e4100352a280af926ccbaacad961 Mon Sep 17 00:00:00 2001 From: wangfq Date: Mon, 17 Aug 2026 14:26:22 +0800 Subject: [PATCH] =?UTF-8?q?perf(vd960DBN):=20=E4=BA=8C=E8=BD=AERAM?= =?UTF-8?q?=E7=98=A6=E8=BA=AB=20=E2=80=94=20RECE=5FBUF=5FLEN=201024=20+=20?= =?UTF-8?q?ARP=2016=20+=20=E4=B8=AD=E6=96=AD=E8=B7=AF=E5=BE=84static=20(20?= =?UTF-8?q?26-08-17)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit .map 分析: BLE 栈固定占 RAM 低 16KB 不可裁; 用户 48KB 内 .bss 41.7KB。 - net_config.h: RECE_BUF_LEN MSS×2(1400)→1024 (MSS=700帧+头≈740B, 5数组省~2.6KB) - net_config.h: WCHNET_NUM_ARP_TABLE 50→16 (停车场景IP少, 省~0.8KB) - tcp_json_srv.c: tmp_buf[RECE_BUF_LEN]/frame[800] 局部→static (中断上下文栈数组防溢出) 栈 4.7KB→~6.3KB, 中断栈峰值 -1.8KB。devlog 记录 .map 分析结论。 --- .../APP/include/net_config.h | 7 ++++-- .../APP/tcp_json_srv.c | 4 ++-- vd960DBN/docs/devlog.md | 24 ++++++++++++++----- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/net_config.h b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/net_config.h index 9d094aa..75b756a 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/net_config.h +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/net_config.h @@ -90,7 +90,10 @@ extern "C" { /* If you want to achieve a higher transmission speed, * try to increase RECE_BUF_LEN to (WCHNET_TCP_MSS*4) * and increase WCHNET_NUM_TCP_SEG to (WCHNET_NUM_TCP*4)*/ -#define RECE_BUF_LEN (WCHNET_TCP_MSS*2) /* socket receive buffer size */ +/* 2026-08-17 RAM 瘦身: MSS×2(1400)→1024 — MSS=700 时完整 TCP 段+头≈740B, + 1024 足够; 5 个 RECE_BUF_LEN 数组 (SocketRecvBuf/MyBuf/tmp/_iot_wchnet_buf/ + g_json_wchnet_buf) 合计省 ~2.6KB .bss (栈空间 +2.6KB) */ +#define RECE_BUF_LEN 1024 /* socket receive buffer size */ #define WCHNET_NUM_PBUF WCHNET_NUM_POOL_BUF /* Number of PBUF structures */ @@ -98,7 +101,7 @@ extern "C" { #define WCHNET_MEM_HEAP_SIZE (((WCHNET_TCP_MSS+0x10+54+8)*WCHNET_NUM_TCP_SEG)+ETH_TX_BUF_SZE+64+2*0x18) /* memory heap size */ -#define WCHNET_NUM_ARP_TABLE 50 /* Number of ARP lists */ +#define WCHNET_NUM_ARP_TABLE 16 /* 2026-08-17: 50→16 (停车设备同时连接 IP 少, 省 ~0.8KB .bss) */ #define WCHNET_MEM_ALIGNMENT 4 /* 4 byte alignment */ diff --git a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/tcp_json_srv.c b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/tcp_json_srv.c index e1fbfb3..75ee361 100644 --- a/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/tcp_json_srv.c +++ b/vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/tcp_json_srv.c @@ -1003,13 +1003,13 @@ void tcp_json_handle_sock_int(uint8_t socketid, uint8_t intstat) { uint16_t space = TCP_JSON_RECV_BUF_LEN - g_json_recv_len; if (recv_len > space) recv_len = space; uint32_t rd_len = recv_len; - uint8_t tmp_buf[RECE_BUF_LEN]; + static uint8_t tmp_buf[RECE_BUF_LEN]; /* 2026-08-17: static — 中断上下文 1KB 栈数组 (防溢出) */ WCHNET_SocketRecv(socketid, tmp_buf, &rd_len); g_json_last_comm_ts = mstick(); // 有数据交互 memcpy(g_json_recv_buf + g_json_recv_len, tmp_buf, (uint16_t)rd_len); g_json_recv_len += (uint16_t)rd_len; - char frame[TCP_JSON_MAX_FRAME]; + static char frame[TCP_JSON_MAX_FRAME]; /* 2026-08-17: static — 中断上下文 800B 栈数组 (防溢出) */ while (json_extract_frame(g_json_recv_buf, &g_json_recv_len, frame, sizeof(frame))) { PRINT("JSON recv: %s\n", frame); json_process_frame(socketid, frame); diff --git a/vd960DBN/docs/devlog.md b/vd960DBN/docs/devlog.md index 0dc79ae..1648544 100644 --- a/vd960DBN/docs/devlog.md +++ b/vd960DBN/docs/devlog.md @@ -65,13 +65,25 @@ BOOT_CNT:1 @0x2000f84c END:0x2000f834 SP:0x2000ffd0 ⚠ ETH_MAX_PACKET_SIZE 768 是贴着 MSS=700 的最小值:TCP 帧 758B ✓; 若现场出现 >728B 数据的大 UDP 包会截断,保守可改 1024(省 2.48KB)。 +### .map 分析 + 二轮瘦身(2026-08-17 晚,commit 后述) +用户提供编译后 .map 解析结论: +- **BLE 栈(libwchble.a)固定占用 RAM 低 16KB(0x20000000~0x20004000,链接脚本外)**——WCH CH32V20x 工程约定,不可裁剪 +- 用户 48KB:.data 1.6K + .bss 41.7K + .noinit 28B → 栈 4.7KB(实测稳定) +- .bss 大块:RECE_BUF_LEN 系列 ~10KB、Mem_Heap 7.2KB、MACRx/TxBuf 3.8KB、eth DMA 5.9KB、SPI_FLASH_BUF 4KB、ARP 1.2KB、iot buf 2KB + +二轮瘦身(省 ~3.4KB,栈 4.7KB → ~6.3KB + 中断栈峰值降 1.8KB): + +| 文件 | 项目 | 改动 | 收益 | +|------|------|------|------| +| net_config.h | RECE_BUF_LEN | MSS×2(1400)→**1024**(MSS=700 帧+头≈740B 够) | 5 数组省 ~2.6KB | +| net_config.h | WCHNET_NUM_ARP_TABLE | 50→**16**(停车设备连接 IP 少) | ~0.8KB | +| tcp_json_srv.c | tmp_buf[RECE_BUF_LEN] | 局部→**static**(中断上下文 1KB 栈数组) | 栈峰值 -1KB | +| tcp_json_srv.c | frame[TCP_JSON_MAX_FRAME] | 局部→**static**(中断上下文 800B) | 栈峰值 -0.8KB | + ### 遗留(根治) -- [ ] **确认 BLE 栈(libwchble.a)实际占用**:需用户提供编译后 .map(obj/*.map 或 MRS Release 目录) - BLE 栈若是 20+KB 固定成本,应用层已省 4.8KB 足够; 若可裁剪更好 -- [ ] (map 确认后可选)RECE_BUF_LEN 1400→1024:SocketRecvBuf[3][1400]+MyBuf 省 1.5KB; - ARP 表 50→16 -- [ ] 局部大数组转 static(tcp_json data_json[1024]×2、net_srv resp[600]):省 .bss 有余量后做,栈峰值 -2KB+ -- [ ] output_cfg 恢复:减栈后重新启用调试打印 +- [x] BLE 栈占用确认:16KB 固定(RAM 低区),不可裁剪 +- [ ] (可选)g_report_cfg 1400B 结构体审查(ReportConfig 含大数组?) +- [ ] (可选)Mem_Heap_Memory 7.2KB 是 WCHNET 库内部堆,需查库配置是否可减 - [ ] 快照区 3s 延后初始化保留(已确认非根因,作为启动早期 SPI 减压无害) ### 验证