sys_monitor.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2026-02-04 Administrator the first version
  9. * 2026-10-08 Administrator 取消后台 sys_monitor 线程(避免每 10s 自动刷屏):
  10. * 统计改为"用到时现场采集";新增控制台命令
  11. * Msh_DisplaySysMonitor 手动输出全部统计
  12. * 2026-10-15 同步移植至 apm32f1(PCB_V2):字段按本工程裁剪(collt_status 无此字段)
  13. * 2026-10-08 Administrator 命令升级为类似 linux top 的界面:循环刷新
  14. * CPU/RAM/外部Flash/分区/上报进度/工作状态/外设/
  15. * 线程等;刷新间隔可调(命令参数),按 q 退出
  16. */
  17. #include "sys_monitor.h"
  18. #include "cpu_usage.h"
  19. #include "control.h"
  20. #include "sht30_collect.h"
  21. #include <rtdevice.h>
  22. #include <fal.h>
  23. #include <spi_flash_sfud.h>
  24. #include <stdlib.h>
  25. #include <time.h>
  26. #define DBG_ENABLE
  27. #define DBG_COLOR
  28. #define DBG_TAG "sysmonitor"
  29. #define DBG_LEVEL DBG_LOG
  30. #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
  31. extern long list_thread(void); /* rt-thread/components/finsh/cmd.c:内核线程列表 */
  32. /* device_status 的唯一定义在 main.c(control.h 提供 extern),此处不重复定义 */
  33. System_stats_t system_status;
  34. /* 刷新实时统计字段(原 sys_monitor 线程的职责,2026-10-08 改为"用到时现场采集")。
  35. * 调用点:①控制台命令 top / Msh_DisplaySysMonitor;②upload_sys_monitor_info() 上报前。
  36. * CPU 温度 = 读 MCU 内部温度传感器、CPU 利用率 = 读 idle 统计、运行时长 = 系统 tick,
  37. * 均为非阻塞快速操作,可在任意线程上下文安全调用。 */
  38. void sys_monitor_refresh(void)
  39. {
  40. system_status.cpu_tempture = (int)Get_Temprate()/100;
  41. system_status.cpu_usage = (int)cpu_load_average();
  42. if(system_status.cpu_usage > system_status.cpu_usage_max)
  43. {
  44. system_status.cpu_usage_max = system_status.cpu_usage;
  45. }
  46. system_status.system_uptime = (rt_uint32_t)(rt_tick_get() / RT_TICK_PER_SECOND);
  47. }
  48. /* ==================== top 式系统监控命令 ==================== */
  49. #define SYS_MON_DEF_INTERVAL_SEC 1 /* 默认刷新间隔(秒) */
  50. #define SYS_MON_MAX_INTERVAL_SEC 60 /* 刷新间隔上限(秒) */
  51. #define SYS_MON_KEY_SLICE_MS 250 /* 等待刷新点时按键检查粒度 */
  52. /* 绘制一屏监控信息:
  53. * CPU(利用率/温度/运行时长)、RAM(堆)、外部Flash参数、FAL分区表、数据上报进度、
  54. * 工作状态与进度、外设设备状态、通信/报警状态、线程状态(list_thread)。 */
  55. static void sys_mon_show_screen(void)
  56. {
  57. rt_uint32_t total = 0, used = 0, max_used = 0;
  58. rt_uint32_t total_sec;
  59. time_t now;
  60. static struct tm tm_buf;
  61. int i;
  62. size_t part_num = 0;
  63. const struct fal_partition *part_table;
  64. const struct fal_flash_dev *onchip;
  65. sfud_flash_t nor;
  66. /* 清屏并回到左上角(VT100 序列,Xshell/MobaXterm/secureCRT 等终端均支持) */
  67. rt_kprintf("\033[2J\033[H");
  68. /* ---- 头部:时间 ---- */
  69. now = time(RT_NULL);
  70. tm_buf = *localtime(&now);
  71. rt_kprintf("================ 系统监控 [q:退出] ================\n");
  72. rt_kprintf("时间: %04d-%02d-%02d %02d:%02d:%02d\n",
  73. tm_buf.tm_year + 1900, tm_buf.tm_mon + 1, tm_buf.tm_mday,
  74. tm_buf.tm_hour, tm_buf.tm_min, tm_buf.tm_sec);
  75. /* ---- CPU ---- */
  76. total_sec = (rt_uint32_t)(rt_tick_get() / RT_TICK_PER_SECOND);
  77. system_status.system_uptime = total_sec;
  78. system_status.cpu_usage = (int)cpu_load_average();
  79. if(system_status.cpu_usage > system_status.cpu_usage_max)
  80. {
  81. system_status.cpu_usage_max = system_status.cpu_usage;
  82. }
  83. system_status.cpu_tempture = (int)Get_Temprate()/100;
  84. rt_kprintf("[CPU] 利用率:%d%% (峰值:%d%%) 温度:%dC 运行:%d时%d分%d秒\n",
  85. system_status.cpu_usage, system_status.cpu_usage_max,
  86. system_status.cpu_tempture,
  87. total_sec / 3600, (total_sec % 3600) / 60, total_sec % 60);
  88. /* ---- RAM(堆) ---- */
  89. rt_memory_info(&total, &used, &max_used);
  90. if (total > 0)
  91. {
  92. rt_kprintf("[RAM] 已用:%u.%uKB / %uKB (%u%%) 峰值:%u.%uKB\n",
  93. (unsigned)(used / 1024), (unsigned)((used % 1024) * 10 / 1024),
  94. (unsigned)(total / 1024), (unsigned)(used * 100 / total),
  95. (unsigned)(max_used / 1024), (unsigned)((max_used % 1024) * 10 / 1024));
  96. }
  97. else
  98. {
  99. rt_kprintf("[RAM] 堆信息不可用\n");
  100. }
  101. /* ---- 外部 Flash 参数(SFUD) ----
  102. ★ 2026-10-30 修复:原来传 NOR_FLASH_DEV_NAME("norflash0"),那是 FAL 设备名;
  103. SFUD 的注册名是 rtconfig 的 FAL_USING_NOR_FLASH_DEV_NAME(本机 "W25Q16"),
  104. 不同名 → 恒返回 NULL,top 里外部 Flash 一直显示"未找到"。 */
  105. nor = rt_sfud_flash_find_by_dev_name(FAL_USING_NOR_FLASH_DEV_NAME);
  106. if (nor != RT_NULL)
  107. {
  108. rt_kprintf("[Flash] 外部:%s %s 容量:%uKB 擦除:%uB JEDEC:%02X-%02X-%02X\n",
  109. nor->name, nor->chip.name,
  110. (unsigned)(nor->chip.capacity / 1024),
  111. (unsigned)(nor->chip.erase_gran),
  112. nor->chip.mf_id, nor->chip.type_id, nor->chip.capacity_id);
  113. }
  114. else
  115. {
  116. /* 打印用 FAL 设备名 + SFUD 注册名两个都给出:现场排查时经常分不清这两者 */
  117. rt_kprintf("[Flash] 外部:%s(SFUD名:%s) 未找到(未初始化?)\n",
  118. NOR_FLASH_DEV_NAME, FAL_USING_NOR_FLASH_DEV_NAME);
  119. }
  120. onchip = fal_flash_device_find("onchip_flash");
  121. if (onchip != RT_NULL)
  122. {
  123. rt_kprintf(" 片内:%s 容量:%uKB 块:%uKB\n",
  124. onchip->name,
  125. (unsigned)(onchip->len / 1024),
  126. (unsigned)(onchip->blk_size / 1024));
  127. }
  128. /* ---- FAL 分区表(外部 Flash 使用分布) ---- */
  129. part_table = fal_get_partition_table(&part_num);
  130. if (part_table != RT_NULL)
  131. {
  132. for (i = 0; i < (int)part_num; i++)
  133. {
  134. rt_kprintf("%s %-9s @%-13s 起址:%6uK 大小:%5uK\n",
  135. (i == 0) ? "[分区]" : " ",
  136. part_table[i].name, part_table[i].flash_name,
  137. (unsigned)(part_table[i].offset / 1024),
  138. (unsigned)(part_table[i].len / 1024));
  139. }
  140. }
  141. /* ---- 数据上报进度 ---- */
  142. rt_kprintf("[上报] 距下次上传:%us (间隔:%umin) 累计上传:%u 次 RTU: rx:%u tx:%u B\n",
  143. (unsigned)device_status.num_updata, (unsigned)device_status.dattim,
  144. (unsigned)system_status.json_upload_count,
  145. (unsigned)system_status.uart2_rx_bytes,
  146. (unsigned)system_status.uart2_tx_bytes);
  147. /* ---- 工作状态与进度 ---- */
  148. rt_kprintf("[工作] 状态:%s [开关机]:%s workdone:%d | 收集:%umin 加热[%s]:%dmin | 光:%d 雨:%d 温:%d 欠压:%d\n",
  149. (device_status.work_status == 1) ? "工作中" : "待机",
  150. (device_status.ds == 1) ? "开机" : "关机",
  151. device_status.work_done,
  152. device_status.clt_time,
  153. (device_status.heat_status == 1) ? "开" : "关",
  154. device_status.heattime,
  155. device_status.sun_status, device_status.rain_status,
  156. device_status.temp_status, device_status.bat_status);
  157. /* ---- 外设(关键设备的打开状态) ---- */
  158. {
  159. static const char *dev_names[] = {"uart1", "uart2", "uart4", "uart5",
  160. "adc1", "wdt", "rtc", "spi10"};
  161. rt_kprintf("[外设] ");
  162. for (i = 0; i < (int)(sizeof(dev_names) / sizeof(dev_names[0])); i++)
  163. {
  164. rt_device_t dev = rt_device_find(dev_names[i]);
  165. if (dev == RT_NULL)
  166. {
  167. rt_kprintf("%s:NA ", dev_names[i]);
  168. }
  169. else
  170. {
  171. rt_kprintf("%s:%s ", dev_names[i], (dev->ref_count > 0) ? "开" : "关");
  172. }
  173. }
  174. rt_kprintf("\n");
  175. }
  176. /* ---- 通信与报警状态 ---- */
  177. rt_kprintf("[状态] net:%s csq:%s sim:%s | 485:%d RTU启动:%d |[报警] 相机:%d 加热:%d 补光:%d\n",
  178. device_status.net, device_status.csq, device_status.sim,
  179. device_status.sensor_485_sta, device_status.rtu_isboot,
  180. device_status.camsta, device_status.heat_sta, device_status.led_sta);
  181. /* ---- 动作统计 ---- */
  182. rt_kprintf("[统计] 拍照:%u 清扫:%u 震动:%u | 仓门 上开:%u |上关:%u |下开:%u | 下关:%u\n",
  183. (unsigned)system_status.photo_taken_count,
  184. (unsigned)system_status.cleaning_count,
  185. (unsigned)system_status.vibration_count,
  186. (unsigned)system_status.upper_door_open_count,
  187. (unsigned)system_status.upper_door_close_count,
  188. (unsigned)system_status.lower_door_open_count,
  189. (unsigned)system_status.lower_door_close_count);
  190. /* ---- 线程状态(复用内核 list_thread 输出) ---- */
  191. rt_kprintf("[线程]\n");
  192. list_thread();
  193. rt_kprintf("========== top [间隔秒] [次数] 刷新, 按 q 或 Ctrl+C 退出 ==========\n");
  194. }
  195. /* 检查控制台是否收到退出按键(q/Q/Ctrl+C)。
  196. * 串口 INT_RX 模式下 rt_device_read 为非阻塞:无数据时返回 0,可直接轮询。
  197. * 返回 1 表示请求退出;顺带丢弃其它按键,避免残留字符干扰后续 shell 输入。 */
  198. static int sys_mon_exit_key(void)
  199. {
  200. rt_device_t console = rt_console_get_device();
  201. char ch;
  202. if (console == RT_NULL)
  203. {
  204. return 0;
  205. }
  206. while (rt_device_read(console, 0, &ch, 1) == 1)
  207. {
  208. if (ch == 'q' || ch == 'Q' || ch == 0x03) /* 0x03 = Ctrl+C */
  209. {
  210. return 1;
  211. }
  212. }
  213. return 0;
  214. }
  215. /* 控制台命令:top 式系统监控,持续刷新显示。
  216. * 用法:Msh_DisplaySysMonitor [刷新间隔秒 1-60,默认1] [刷新次数,省略/0=持续刷新]
  217. * 持续刷新模式下按 q(或 Ctrl+C)退出。别名:top
  218. * 示例:top —— 每 1 秒刷新,q 退出
  219. * top 5 —— 每 5 秒刷新,q 退出
  220. * top 2 30 —— 每 2 秒刷新,共 30 次后自动退出 */
  221. void Msh_DisplaySysMonitor(int argc, char **argv)
  222. {
  223. int interval = SYS_MON_DEF_INTERVAL_SEC;
  224. int times = 0; /* 0=持续刷新 */
  225. int count = 0;
  226. int exited = 0;
  227. if (argc >= 2)
  228. {
  229. interval = atoi(argv[1]);
  230. if (interval < 1) interval = 1;
  231. if (interval > SYS_MON_MAX_INTERVAL_SEC) interval = SYS_MON_MAX_INTERVAL_SEC;
  232. }
  233. if (argc >= 3)
  234. {
  235. times = atoi(argv[2]);
  236. if (times < 0) times = 0;
  237. }
  238. while (1)
  239. {
  240. sys_mon_show_screen();
  241. count++;
  242. if (times > 0 && count >= times)
  243. {
  244. break;
  245. }
  246. /* 等待下一个刷新点:分片等待,期间检查退出按键 */
  247. {
  248. int waited = 0;
  249. int total_ms = interval * 1000;
  250. while (waited < total_ms)
  251. {
  252. int step = SYS_MON_KEY_SLICE_MS;
  253. if ((total_ms - waited) < step)
  254. {
  255. step = total_ms - waited;
  256. }
  257. rt_thread_mdelay(step);
  258. waited += step;
  259. if (sys_mon_exit_key())
  260. {
  261. exited = 1;
  262. break;
  263. }
  264. }
  265. }
  266. if (exited)
  267. {
  268. break;
  269. }
  270. }
  271. rt_kprintf("系统监控已退出.\n");
  272. }
  273. MSH_CMD_EXPORT(Msh_DisplaySysMonitor, system monitor (top style): [interval_s] [count]);
  274. MSH_CMD_EXPORT_ALIAS(Msh_DisplaySysMonitor, top, system monitor (top style));