| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- /*
- * Copyright (c) 2006-2021, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2026-02-04 Administrator the first version
- * 2026-10-08 Administrator 取消后台 sys_monitor 线程(避免每 10s 自动刷屏):
- * 统计改为"用到时现场采集";新增控制台命令
- * Msh_DisplaySysMonitor 手动输出全部统计
- * 2026-10-08 Administrator 命令升级为类似 linux top 的界面:循环刷新
- * CPU/RAM/外部Flash/分区/上报进度/工作状态/外设/
- * 线程等;刷新间隔可调(命令参数),按 q 退出
- */
- #include "sys_monitor.h"
- #include "cpu_usage.h"
- #include "control.h"
- #include "sht30_collect.h"
- #include <rtdevice.h>
- #include <fal.h>
- #include <spi_flash_sfud.h>
- #include <stdlib.h>
- #include <time.h>
- #define DBG_ENABLE
- #define DBG_COLOR
- #define DBG_TAG "sysmonitor"
- #define DBG_LEVEL DBG_LOG
- #include <rtdbg.h> //must after of DBG_LVL, DBG_TAG or other options
- extern long list_thread(void); /* rt-thread/components/finsh/cmd.c:内核线程列表 */
- Device_Status_typedef device_status;
- System_stats_t system_status;
- /* 刷新实时统计字段(原 sys_monitor 线程的职责,2026-10-08 改为"用到时现场采集")。
- * 调用点:①控制台命令 top / Msh_DisplaySysMonitor;②upload_sys_monitor_info() 上报前。
- * CPU 温度 = 读 MCU 内部温度传感器、CPU 利用率 = 读 idle 统计、运行时长 = 系统 tick,
- * 均为非阻塞快速操作,可在任意线程上下文安全调用。 */
- void sys_monitor_refresh(void)
- {
- system_status.cpu_tempture = (int)Get_Temprate()/100;
- system_status.cpu_usage = (int)cpu_load_average();
- if(system_status.cpu_usage > system_status.cpu_usage_max)
- {
- system_status.cpu_usage_max = system_status.cpu_usage;
- }
- system_status.system_uptime = (rt_uint32_t)(rt_tick_get() / RT_TICK_PER_SECOND);
- }
- /* ==================== top 式系统监控命令 ==================== */
- #define SYS_MON_DEF_INTERVAL_SEC 1 /* 默认刷新间隔(秒) */
- #define SYS_MON_MAX_INTERVAL_SEC 60 /* 刷新间隔上限(秒) */
- #define SYS_MON_KEY_SLICE_MS 250 /* 等待刷新点时按键检查粒度 */
- /* 绘制一屏监控信息:
- * CPU(利用率/温度/运行时长)、RAM(堆)、外部Flash参数、FAL分区表、数据上报进度、
- * 工作状态与进度、外设设备状态、通信/报警状态、线程状态(list_thread)。 */
- static void sys_mon_show_screen(void)
- {
- rt_uint32_t total = 0, used = 0, max_used = 0;
- rt_uint32_t total_sec;
- time_t now;
- static struct tm tm_buf;
- int i;
- size_t part_num = 0;
- const struct fal_partition *part_table;
- const struct fal_flash_dev *onchip;
- sfud_flash_t nor;
- /* 清屏并回到左上角(VT100 序列,Xshell/MobaXterm/secureCRT 等终端均支持) */
- rt_kprintf("\033[2J\033[H");
- /* ---- 头部:时间 ---- */
- now = time(RT_NULL);
- tm_buf = *localtime(&now);
- rt_kprintf("================ 系统监控 [q:退出] ================\n");
- rt_kprintf("时间: %04d-%02d-%02d %02d:%02d:%02d\n",
- tm_buf.tm_year + 1900, tm_buf.tm_mon + 1, tm_buf.tm_mday,
- tm_buf.tm_hour, tm_buf.tm_min, tm_buf.tm_sec);
- /* ---- CPU ---- */
- total_sec = (rt_uint32_t)(rt_tick_get() / RT_TICK_PER_SECOND);
- system_status.system_uptime = total_sec;
- system_status.cpu_usage = (int)cpu_load_average();
- if(system_status.cpu_usage > system_status.cpu_usage_max)
- {
- system_status.cpu_usage_max = system_status.cpu_usage;
- }
- system_status.cpu_tempture = (int)Get_Temprate()/100;
- rt_kprintf("[CPU] 利用率:%d%% (峰值:%d%%) 温度:%dC 运行:%d时%d分%d秒\n",
- system_status.cpu_usage, system_status.cpu_usage_max,
- system_status.cpu_tempture,
- total_sec / 3600, (total_sec % 3600) / 60, total_sec % 60);
- /* ---- RAM(堆) ---- */
- rt_memory_info(&total, &used, &max_used);
- if (total > 0)
- {
- rt_kprintf("[RAM] 已用:%u.%uKB / %uKB (%u%%) 峰值:%u.%uKB\n",
- (unsigned)(used / 1024), (unsigned)((used % 1024) * 10 / 1024),
- (unsigned)(total / 1024), (unsigned)(used * 100 / total),
- (unsigned)(max_used / 1024), (unsigned)((max_used % 1024) * 10 / 1024));
- }
- else
- {
- rt_kprintf("[RAM] 堆信息不可用\n");
- }
- /* ---- 外部 Flash 参数(SFUD) ---- */
- nor = rt_sfud_flash_find_by_dev_name(FAL_USING_NOR_FLASH_DEV_NAME);
- if (nor != RT_NULL)
- {
- rt_kprintf("[Flash] 外部:%s %s 容量:%uKB 擦除:%uB JEDEC:%02X-%02X-%02X\n",
- nor->name, nor->chip.name,
- (unsigned)(nor->chip.capacity / 1024),
- (unsigned)(nor->chip.erase_gran),
- nor->chip.mf_id, nor->chip.type_id, nor->chip.capacity_id);
- }
- else
- {
- rt_kprintf("[Flash] 外部:%s 未找到(未初始化?)\n", FAL_USING_NOR_FLASH_DEV_NAME);
- }
- onchip = fal_flash_device_find("onchip_flash");
- if (onchip != RT_NULL)
- {
- rt_kprintf(" 片内:%s 容量:%uKB 块:%uKB\n",
- onchip->name,
- (unsigned)(onchip->len / 1024),
- (unsigned)(onchip->blk_size / 1024));
- }
- /* ---- FAL 分区表(外部 Flash 使用分布) ---- */
- part_table = fal_get_partition_table(&part_num);
- if (part_table != RT_NULL)
- {
- for (i = 0; i < (int)part_num; i++)
- {
- rt_kprintf("%s %-9s @%-13s 起址:%6uK 大小:%5uK\n",
- (i == 0) ? "[分区]" : " ",
- part_table[i].name, part_table[i].flash_name,
- (unsigned)(part_table[i].offset / 1024),
- (unsigned)(part_table[i].len / 1024));
- }
- }
- /* ---- 数据上报进度 ---- */
- rt_kprintf("[上报] 距下次上传:%us (间隔:%umin) 累计上传:%u 次 RTU: rx:%u tx:%u B\n",
- (unsigned)device_status.num_updata, (unsigned)device_status.dattim,
- (unsigned)system_status.json_upload_count,
- (unsigned)system_status.uart2_rx_bytes,
- (unsigned)system_status.uart2_tx_bytes);
- /* ---- 工作状态与进度 ---- */
- rt_kprintf("[工作] 状态:%s [开关机]:%s workdone:%d | 收集[%s]:%umin 加热[%s]:%dmin | 光:%d 雨:%d 温:%d 欠压:%d\n",
- (device_status.work_status == 1) ? "工作中" : "待机",
- (device_status.ds == 1) ? "开机" : "关机",
- device_status.work_done,
- (device_status.collt_status == 1) ? "开" : "关",
- device_status.clt_time,
- (device_status.heat_status == 1) ? "开" : "关",
- device_status.heattime,
- device_status.sun_status, device_status.rain_status,
- device_status.temp_status, device_status.bat_status);
- /* ---- 外设(关键设备的打开状态) ---- */
- {
- static const char *dev_names[] = {"uart1", "uart2", "uart4", "uart5",
- "adc1", "wdt", "rtc", "spi10"};
- rt_kprintf("[外设] ");
- for (i = 0; i < (int)(sizeof(dev_names) / sizeof(dev_names[0])); i++)
- {
- rt_device_t dev = rt_device_find(dev_names[i]);
- if (dev == RT_NULL)
- {
- rt_kprintf("%s:NA ", dev_names[i]);
- }
- else
- {
- rt_kprintf("%s:%s ", dev_names[i], (dev->ref_count > 0) ? "开" : "关");
- }
- }
- rt_kprintf("\n");
- }
- /* ---- 通信与报警状态 ---- */
- rt_kprintf("[状态] net:%s csq:%s sim:%s | 485:%d RTU启动:%d |[报警] 相机:%d 加热:%d 补光:%d\n",
- device_status.net, device_status.csq, device_status.sim,
- device_status.sensor_485_sta, device_status.rtu_isboot,
- device_status.camsta, device_status.heat_sta, device_status.led_sta);
- /* ---- 动作统计 ---- */
- rt_kprintf("[统计] 拍照:%u 清扫:%u 震动:%u | 仓门 上开:%u |上关:%u |下开:%u | 下关:%u\n",
- (unsigned)system_status.photo_taken_count,
- (unsigned)system_status.cleaning_count,
- (unsigned)system_status.vibration_count,
- (unsigned)system_status.upper_door_open_count,
- (unsigned)system_status.upper_door_close_count,
- (unsigned)system_status.lower_door_open_count,
- (unsigned)system_status.lower_door_close_count);
- /* ---- 线程状态(复用内核 list_thread 输出) ---- */
- rt_kprintf("[线程]\n");
- list_thread();
- rt_kprintf("========== top [间隔秒] [次数] 刷新, 按 q 或 Ctrl+C 退出 ==========\n");
- }
- /* 检查控制台是否收到退出按键(q/Q/Ctrl+C)。
- * 串口 INT_RX 模式下 rt_device_read 为非阻塞:无数据时返回 0,可直接轮询。
- * 返回 1 表示请求退出;顺带丢弃其它按键,避免残留字符干扰后续 shell 输入。 */
- static int sys_mon_exit_key(void)
- {
- rt_device_t console = rt_console_get_device();
- char ch;
- if (console == RT_NULL)
- {
- return 0;
- }
- while (rt_device_read(console, 0, &ch, 1) == 1)
- {
- if (ch == 'q' || ch == 'Q' || ch == 0x03) /* 0x03 = Ctrl+C */
- {
- return 1;
- }
- }
- return 0;
- }
- /* 控制台命令:top 式系统监控,持续刷新显示。
- * 用法:Msh_DisplaySysMonitor [刷新间隔秒 1-60,默认1] [刷新次数,省略/0=持续刷新]
- * 持续刷新模式下按 q(或 Ctrl+C)退出。别名:top
- * 示例:top —— 每 1 秒刷新,q 退出
- * top 5 —— 每 5 秒刷新,q 退出
- * top 2 30 —— 每 2 秒刷新,共 30 次后自动退出 */
- void Msh_DisplaySysMonitor(int argc, char **argv)
- {
- int interval = SYS_MON_DEF_INTERVAL_SEC;
- int times = 0; /* 0=持续刷新 */
- int count = 0;
- int exited = 0;
- if (argc >= 2)
- {
- interval = atoi(argv[1]);
- if (interval < 1) interval = 1;
- if (interval > SYS_MON_MAX_INTERVAL_SEC) interval = SYS_MON_MAX_INTERVAL_SEC;
- }
- if (argc >= 3)
- {
- times = atoi(argv[2]);
- if (times < 0) times = 0;
- }
- while (1)
- {
- sys_mon_show_screen();
- count++;
- if (times > 0 && count >= times)
- {
- break;
- }
- /* 等待下一个刷新点:分片等待,期间检查退出按键 */
- {
- int waited = 0;
- int total_ms = interval * 1000;
- while (waited < total_ms)
- {
- int step = SYS_MON_KEY_SLICE_MS;
- if ((total_ms - waited) < step)
- {
- step = total_ms - waited;
- }
- rt_thread_mdelay(step);
- waited += step;
- if (sys_mon_exit_key())
- {
- exited = 1;
- break;
- }
- }
- }
- if (exited)
- {
- break;
- }
- }
- rt_kprintf("系统监控已退出.\n");
- }
- MSH_CMD_EXPORT(Msh_DisplaySysMonitor, system monitor (top style): [interval_s] [count]);
- MSH_CMD_EXPORT_ALIAS(Msh_DisplaySysMonitor, top, system monitor (top style));
|