sys_monitor.c 11 KB

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