From cdcbef61b4f00d994beea787929ce7bf18059ced Mon Sep 17 00:00:00 2001 From: chengzhiming Date: Tue, 14 Oct 2025 11:24:43 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B0=86cpup=E5=91=BD=E4=BB=A4=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E7=9A=84cpu=E4=BD=BF=E7=94=A8=E7=8E=87=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E7=99=BE=E5=88=86=E5=8F=B7=E5=BD=A2=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chengzhiming --- src/shell/full/src/cmds/shell_cpup.c | 65 +++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 10 deletions(-) diff --git a/src/shell/full/src/cmds/shell_cpup.c b/src/shell/full/src/cmds/shell_cpup.c index 0f3d795e..ace2ab55 100644 --- a/src/shell/full/src/cmds/shell_cpup.c +++ b/src/shell/full/src/cmds/shell_cpup.c @@ -16,6 +16,8 @@ #include "prt_cpup.h" #include "prt_cpup_internal.h" #include "prt_sys_external.h" +#include "securec.h" +#include "prt_task.h" static void ShowCpupConfigInfo() { @@ -41,36 +43,79 @@ static void ShowCpupConfigInfo() return; } + +static void GetUsageParts(U32 usage, char*buffer, U16 bufferLength, U16 maxLength) +{ + U16 integer = usage / 100; + U16 fractional = usage % 100; + U16 ret = snprintf_s(buffer, bufferLength, maxLength, "%d.%02d", integer, fractional); + if (ret == -1) { + PRINTK("Error: Function failed - possible truncation or constraint violation\n"); + buffer[0] = '\0'; + return; + } +} + static void ShowCpuUsage() { + char *name = NULL; + char percentBuf[8]; // cpu使用率缓冲区 + const char *taskNameBuf = NULL; // 任务名字符缓冲区 + U16 usageLen = 0; + char *percent = "%%"; // 百分号无法打印,暂时写成字符串 + U16 bufferLength = 0; if (g_cpupNow == NULL) { PRINTK("CPUP is not enable!\n"); return; } - U32 outNum = 0; struct CpupThread *cpup = malloc(sizeof(struct CpupThread) * OS_MAX_TCB_NUM); if (cpup == NULL) { return; } - PRT_CpupThread(OS_MAX_TCB_NUM, cpup, &outNum); - - PRINTK("taskId CpuUsage\n"); - PRINTK("----------------------\n"); + PRINTK("taskId \ttaskName\t\tCpuUsage\n"); + PRINTK("--------------- ----------------------- ----------\n"); for (int i = 0; i < outNum; i++) { - PRINTK("0x%-8x %u\n", cpup[i].id, cpup[i].usage); + GetUsageParts((U32)(cpup[i].usage), percentBuf, sizeof (percentBuf), sizeof (percentBuf)-1); + PRT_TaskGetName(cpup[i].id, &taskNameBuf); // 获取任务名 + name = (cpup[i].id == OS_CPUP_INT_ID) ? "Interrupt" : taskNameBuf; + bufferLength = strlen (percentBuf); + if (bufferLength == strlen ("0.00")) { + PRINTK("0x%-8x\t%-20s\t %-4s%s\n", cpup[i].id, name, percentBuf, percent); + } else if (bufferLength == strlen ("00.00")) { + PRINTK("0x%-8x\t%-20s\t %-5s%s\n", cpup[i].id, name, percentBuf, percent); + } else { + PRINTK("0x%-8x\t%-20s\t%-6s%s\n", cpup[i].id, name, percentBuf, percent); + } } - - PRINTK("----------------------\n"); - PRINTK("Total CpuUsage: %u\n", PRT_CpupNow()); + PRINTK("--------------- ----------------------- ----------\n"); + GetUsageParts(PRT_CpupNow(), percentBuf, sizeof (percentBuf), sizeof (percentBuf)-1); + PRINTK("Total CpuUsage: %s%s\n", percentBuf, percent); free(cpup); - return; } UINT32 OsShellCmdCpup(UINT32 argc, const CHAR **argv) { + U32 ret; + + /* 用户首次执行cpup命令,开始初始化 */ + if (g_cpupStat == CPUP_STAT_NEEDINIT) { + ret = OsCpupInit(); + if (ret != OS_OK) { + PRINTK("CPUP init failed!\n"); + return ret; + } + if (g_cpupWarnStat == CPUP_STAT_NEEDINIT) { + OsCpupWarnInit(); + g_cpupWarnStat = CPUP_STAT_RUNING; + } + g_cpupStat = CPUP_STAT_RUNING; + PRINTK("CPUP is now active. Re-enter 'cpup' to view CPU usage.\n"); + return OS_OK; + } + if (argc == 0) { ShowCpuUsage(); return OS_OK; -- Gitee