diff --git a/src/shell/full/src/cmds/shell_cpup.c b/src/shell/full/src/cmds/shell_cpup.c index 0f3d795e5d7763e7fc69f6d5588aa12aff8d8a91..ace2ab55b3bc03773863717079ddf41beb24f245 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;