diff --git a/文豪/cppsupport/los_cppsupport.c b/文豪/cppsupport/los_cppsupport.c new file mode 100644 index 0000000..e6239ab --- /dev/null +++ b/文豪/cppsupport/los_cppsupport.c @@ -0,0 +1,74 @@ +/* ---------------------------------------------------------------------------- + * Copyright (c) Huawei Technologies Co., Ltd. 2013-2019. All rights reserved. + * Description: LiteOS Cpp Support Implementation + * Author: Huawei LiteOS Team + * Create: 2013-01-01 + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * --------------------------------------------------------------------------- */ + +#include "los_cppsupport_pri.h" +#include "los_printf.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +typedef VOID (*InitFunc)(VOID); + +/*------------------------------------------- +*`LITE_OS_SEC_TEXT_MINOR`¡¢`UINTPTR`µÈ¡£ +*ÔÚ`LOS_CppSystemInit`º¯ÊýÖУ¬Ëü±éÀúÁËÒ»¸ö³õʼ»¯º¯ÊýµÄÊý×飬 +*²¢ÒÀ´Îµ÷ÓÃÕâЩ³õʼ»¯º¯Êý¡£¸ù¾Ý´«ÈëµÄ`flag`²ÎÊý£¬ +*Ëü»áÔÚÌØ¶¨µÄλÖÃÖ´Ðгõʼ»¯º¯Êý¡£ +*--------------------------------------------*/ +LITE_OS_SEC_TEXT_MINOR INT32 LOS_CppSystemInit(UINTPTR initArrayStart, UINTPTR initArrayEnd, INT32 flag) +{ + UINTPTR fastEnd = (UINTPTR)&__fast_end; + UINTPTR *start = (UINTPTR *)initArrayStart; + InitFunc initFunc = NULL; + +#ifdef LOSCFG_AARCH64 + __register_frame(__EH_FRAME_BEGIN__); +#endif + + for (; start != (UINTPTR *)initArrayEnd; ++start) { + if ((flag == BEFORE_SCATTER) && ((UINTPTR)*start > fastEnd)) { + continue; + } else if ((flag == AFTER_SCATTER) && ((UINTPTR)*start <= fastEnd)) { + continue; + } + + initFunc = (InitFunc)(*start); + initFunc(); + } + + return 0; +} + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ diff --git a/文豪/cpup/cpup_shellcmd.c b/文豪/cpup/cpup_shellcmd.c new file mode 100644 index 0000000..8e4160b --- /dev/null +++ b/文豪/cpup/cpup_shellcmd.c @@ -0,0 +1,134 @@ +/* ---------------------------------------------------------------------------- + * Copyright (c) Huawei Technologies Co., Ltd. 2013-2020. All rights reserved. + * Description: ShellCmd Cpup + * Author: Huawei LiteOS Team + * Create: 2013-01-01 + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * --------------------------------------------------------------------------- */ + +#include "los_config.h" +#ifdef LOSCFG_SHELL +#include "stdio.h" +#include "stdlib.h" +#include "los_cpup_pri.h" +#include "los_task_pri.h" +#include "shcmd.h" +#include "shell.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +VOID OsCmdCpupOperateOneParam(UINT32 mode) //¸ù¾Ý²ÎÊýmode»ñȡϵͳCPUʹÓÃÂʵÄÀúÊ·Êý¾Ý// +{ + UINT32 ret; + + if (mode == CPUP_LAST_TEN_SECONDS) { //¹ýÈ¥10sÄÚ// + PRINTK("\nSysCpuUsage in 10s: "); + } else if (mode == CPUP_LAST_ONE_SECONDS) { //¹ýÈ¥1sÄÚ// + PRINTK("\nSysCpuUsage in 1s: "); + } else { //Àúʷƽ¾ùCPUʹÓÃÂÊ// + PRINTK("\nSysCpuUsage in all time: "); + } + ret = LOS_HistorySysCpuUsage(mode); //»ñÈ¡CPUʹÓÃÂʵÄÊýÖµ// + PRINTK("%u.%u", ret / LOS_CPUP_PRECISION_MULT, ret % LOS_CPUP_PRECISION_MULT); +} + +VOID OsCmdCpupOperateTwoParam(UINT32 mode, UINT32 taskId) //¸ù¾Ý²ÎÊýmode»ñÈ¡²ÎÊýtaskIdËùÖ¸¶¨// +{ //µÄÈÎÎñµÄCPUʹÓÃÂʵÄÀúÊ·Êý¾Ý// + UINT32 ret; + + if (mode == CPUP_LAST_TEN_SECONDS) { //¹ýÈ¥10sÄÚ// + PRINTK("\nTaskId %u CpuUsage in 10s: ", taskId); + } else if (mode == CPUP_LAST_ONE_SECONDS) { //¹ýÈ¥1sÄÚ// + PRINTK("\nTaskId %u CpuUsage in 1s: ", taskId); + } else { //Àúʷƽ¾ùCPUʹÓÃÂÊ// + PRINTK("\nTaskId %u CpuUsage in all time: ", taskId); + } + ret = LOS_HistoryTaskCpuUsage(taskId, mode); //»ñÈ¡CPUʹÓÃÂʵÄÊýÖµ// + PRINTK("%u.%u", ret / LOS_CPUP_PRECISION_MULT, ret % LOS_CPUP_PRECISION_MULT); +} + +LITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdCpup(INT32 argc, const CHAR **argv) //¸ù¾ÝÊäÈëµÄ²ÎÊýÀ´»ñȡϵͳ»òÖ¸¶¨ÈÎÎñµÄCPUʹÓÃÂÊ// +{ + size_t mode, taskId; + CHAR *bufMode = NULL; + CHAR *bufId = NULL; + LosTaskCB *taskCB = NULL; + UINT32 ret; + + if (argc <= 0) { //¼ì²é²ÎÊý¸öÊý£¬Èç¹ûûÓвÎÊý£¬Ôò»ñȡϵͳ¹ýÈ¥10ÃëµÄCPUʹÓÃÂʲ¢´òÓ¡Êä³ö// + ret = LOS_HistorySysCpuUsage(CPUP_LAST_TEN_SECONDS); + PRINTK("\nSysCpuUsage in 10s: %u.%u", ret / LOS_CPUP_PRECISION_MULT, ret % LOS_CPUP_PRECISION_MULT); + return 0; + } + + mode = strtoul(argv[0], &bufMode, 0); //½«µÚÒ»¸ö²ÎÊýת»»ÎªÎÞ·ûºÅÕûÐÍÊýmode£¬±íʾCPUʹÓÃÂʵĻñȡģʽ// +/*------------------------------------------------------------------*/ + //ÅжÏÊäÈëµÄmodeÊÇ·ñÓÐЧ// + if ((bufMode == NULL) || (*bufMode != 0)) { + PRINTK("\nThe input mode is invalid. Please try again.\n"); + return 0; + } + + if (mode > CPUP_ALL_TIME) { + mode = CPUP_ALL_TIME; + } +/*------------------------------------------------------------------*/ +//Èç¹ûÖ»ÓÐÒ»¸ö²ÎÊý£¬Ôòµ÷ÓÃOsCmdCpupOperateOneParamº¯ÊýÀ´»ñȡϵͳCPUʹÓÃÂÊ// + if (argc == 1) { + OsCmdCpupOperateOneParam((UINT32)mode); + return 0; + } +/*------------------------------------------------------------------*/ +//ÅжÏÊäÈëµÄtaskIdÊÇ·ñÓÐЧ// + taskId = strtoul(argv[1], &bufId, 0); + if ((taskId >= g_taskMaxNum) || (*bufId != 0)) { + PRINTK("\nThe input taskId is invalid. Please try again.\n"); + return 0; + } + taskCB = OS_TCB_FROM_TID(taskId); + if (taskCB->taskStatus & OS_TASK_STATUS_UNUSED) { + PRINTK("\nThe task is unused. Please try again.\n"); + return 0; + } +/*-----------------------------------------------------------------*/ +//Èç¹ûÓÐÁ½¸ö²ÎÊý£¬ÔòµÚ¶þ¸ö²ÎÊýΪÈÎÎñID£¬È»ºóµ÷ÓÃOsCmdCpupOperateTwoParamº¯ÊýÀ´»ñȡָ¶¨ÈÎÎñµÄCPUʹÓÃÂÊ// + if (argc == 2) { + OsCmdCpupOperateTwoParam((UINT32)mode, (UINT32)taskId); + return 0; + } + + PRINTK("cpup [MODE] \ncpup [MODE] [TASKID] \n"); + return 0; +} +//ͨ¹ýÔÚShellÖÕ¶ËÊäÈëcpupÃüÁ¸ù¾ÝÊäÈëÏàÓ¦µÄ²ÎÊýÀ´»ñÈ¡CPUʹÓÃÂʵÄÀúÊ·Êý¾Ý// +SHELLCMD_ENTRY(cpup_shellcmd, CMD_TYPE_EX, "cpup", XARGS, (CmdCallBackFunc)OsShellCmdCpup); +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ +#endif /* LOSCFG_SHELL */ diff --git a/文豪/cpup/los_cpup.c b/文豪/cpup/los_cpup.c new file mode 100644 index 0000000..688ff34 --- /dev/null +++ b/文豪/cpup/los_cpup.c @@ -0,0 +1,593 @@ +/* ---------------------------------------------------------------------------- + * Copyright (c) Huawei Technologies Co., Ltd. 2013-2020. All rights reserved. + * Description : LiteOS Cpu Usage Calculation Module Implementation + * Author: Huawei LiteOS Team + * Create: 2013-01-01 + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * --------------------------------------------------------------------------- */ + +#include "los_cpup_pri.h" +#include "los_task_pri.h" +#include "los_base.h" +#include "los_swtmr.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#ifdef LOSCFG_KERNEL_CPUP + +LITE_OS_SEC_BSS STATIC UINT16 g_cpupSwtmrId; //ÓÃÓڼǼCPUʹÓÃÂÊͳ¼Æ¶¨Ê±Æ÷µÄID// +LITE_OS_SEC_BSS STATIC UINT16 g_cpupInitFlg = 0; //ÓÃÓÚ±ê¼ÇCPUʹÓÃÂÊÄ£¿éÊÇ·ñÒѾ­³õʼ»¯// +LITE_OS_SEC_BSS OsCpupCB *g_cpup = NULL; //ÓÃÓÚ±£´æCPUʹÓÃÂÊÏà¹ØµÄÐÅÏ¢// +LITE_OS_SEC_BSS STATIC UINT16 g_cpupMaxNum; //±íʾCPUʹÓÃÂÊͳ¼ÆÐÅÏ¢µÄ×î´óÊýÁ¿// +LITE_OS_SEC_BSS STATIC UINT16 g_cpupTaskMaxNum; //±íʾCPUʹÓÃÂÊͳ¼ÆÐÅÏ¢µÄ×î´óÈÎÎñÊýÁ¿// +LITE_OS_SEC_BSS STATIC UINT16 g_hisPos = 0; /* current Sampling point of historyTime */ +LITE_OS_SEC_DATA_INIT STATIC UINT32 runningTasks[LOSCFG_KERNEL_CORE_NUM] = { + [0 ... (LOSCFG_KERNEL_CORE_NUM - 1)] = (UINT32)-1 +}; //ÓÃÓڼǼÿ¸öºËÐÄÉÏÕýÔÚÔËÐеÄÈÎÎñµÄID// +LITE_OS_SEC_BSS STATIC UINT64 cpuHistoryTime[OS_CPUP_HISTORY_RECORD_NUM + 1]; + //ÓÃÓÚ±£´æCPUÀúʷʱ¼ä¼Ç¼// +LITE_OS_SEC_BSS STATIC UINT64 g_startCycles = 0; //ÓÃÓڼǼÈÎÎñÇл»Ç°µÄʱÖÓÖÜÆÚÊý// +#ifdef LOSCFG_CPUP_INCLUDE_IRQ //ÓÃÓÚÅжÏÊÇ·ñ°üº¬ÖжÏÏà¹ØµÄ¹¦ÄÜ// +//ÈôÂú×ãÌõ¼þ±àÒë// +LITE_OS_SEC_BSS UINT64 g_timeInIrqPerTskSwitch[LOSCFG_KERNEL_CORE_NUM]; //ÓÃÓڼǼÿ¸öºËÐÄÉÏÈÎÎñÇл»ÆÚ¼ä·¢ÉúµÄÖжϵÄʱ¼ä// +LITE_OS_SEC_BSS STATIC UINT64 g_intTimeStart[LOSCFG_KERNEL_CORE_NUM]; //ÓÃÓڼǼÿ¸öºËÐÄÉÏÖжϿªÊ¼µÄʱ¼ä// +#endif + +#define HIGH_BITS 32 + +#define CPUP_PRE_POS(pos) (((pos) == 0) ? (OS_CPUP_HISTORY_RECORD_NUM - 1) : ((pos) - 1)) +#define CPUP_POST_POS(pos) (((pos) == (OS_CPUP_HISTORY_RECORD_NUM - 1)) ? 0 : ((pos) + 1)) + +LITE_OS_SEC_TEXT_INIT OsCpupCB *OsCpupCBGet(UINT32 index) +{ + return &g_cpup[index]; +} + +/*¶¨ÆÚ¸üÐÂÀúʷʱ¼ä²ÉÑùµãºÍÈÎÎñµÄÀúÊ·ÔËÐÐʱ¼ä£¬ÒÔ±ãºóÐø¼ÆËãCPUʹÓÃÂÊ*/ +LITE_OS_SEC_TEXT_INIT VOID OsCpupGuard(VOID) +{ + UINT16 prevPos = g_hisPos; + UINT16 loop; + UINT16 runTaskId; + UINT64 curCycle; + UINT32 intSave; + + if (g_cpupInitFlg == 0) { //±íʾCPUʹÓÃÂÊÄ£¿éÉÐδ³õʼ»¯£¬Ö±½Ó·µ»Ø// + return; + } + //ÈôÒѳõʼ»¯£¬Ôò½ûÖ¹Öжϣ¬²¢»ñÈ¡µ±Ç°Ê±ÖÓÖÜÆÚÊý// + intSave = LOS_IntLock(); + curCycle = OsCpupGetCycle(); + + g_hisPos = CPUP_POST_POS(g_hisPos); //¸üÐÂÀúʷʱ¼ä²ÉÑùµãµÄλÖÃ// + cpuHistoryTime[prevPos] = curCycle; //¼Ç¼±¾´Î²ÉÑùµÄʱÖÓÖÜÆÚÊý// + +/*¸üÐÂÿ¸öÈÎÎñºÍCPUºËÐĵÄÀúÊ·ÔËÐÐʱ¼ä*/ + for (loop = 0; loop < g_cpupMaxNum; loop++) { + g_cpup[loop].historyTime[prevPos] = g_cpup[loop].allTime; + } + + for (loop = 0; loop < LOSCFG_KERNEL_CORE_NUM; loop++) { + runTaskId = runningTasks[loop]; //»ñÈ¡¸ÃÈÎÎñÔÚµ±Ç°ºËÐÄÉϵÄID// + /* reacquire the cycle to prevent flip */ + curCycle = OsCpupGetCycle(); + /*½«¸ÃÈÎÎñ´Ó¿ªÊ¼µ½µ±Ç°Ê±¿ÌËùÓõÄʱÖÓÖÜÆÚÊý¼ÓÈë¸ÃÈÎÎñÔÚÀúʷʱ¼ä²ÉÑùµãÉϵÄÀúÊ·ÔËÐÐʱ¼äÖÐ*/ + g_cpup[runTaskId].historyTime[prevPos] += curCycle - g_cpup[runTaskId].startTime; +#ifdef LOSCFG_CPUP_INCLUDE_IRQ //ÅжÏÖжϹ¦ÄÜÊÇ·ñ±»°üº¬ÔÚÅäÖÃÖÐ// + /*´Ó¸ÃÈÎÎñµÄÀúÊ·ÔËÐÐʱ¼äÖмõÈ¥¸ÃÈÎÎñÔÚÈÎÎñÇл»ÆÚ¼ä·¢ÉúµÄÖжÏËùÓõÄʱ¼ä*/ + g_cpup[runTaskId].historyTime[prevPos] -= g_timeInIrqPerTskSwitch[loop]; +#endif + } + + LOS_IntRestore(intSave); //»Ö¸´ÖжÏ״̬// +} + +/*´´½¨Ò»¸öÈí¼þ¶¨Ê±Æ÷£¬ÒԱ㶨ÆÚ¸üÐÂÀúʷʱ¼ä²ÉÑùµãºÍÈÎÎñµÄÀúÊ·ÔËÐÐʱ¼ä£¬´Ó¶øÊµÏÖCPUʹÓÃÂʵÄͳ¼Æ*/ +LITE_OS_SEC_TEXT_INIT VOID OsCpupGuardCreator(VOID) +{ + /*ÏÂÃæº¯ÊýµÄ²ÎÊý·Ö±ðΪ*/ + //É趨¶¨Ê±Æ÷µÄÖÜÆÚÊÇÿÃëÖӵĻù±¾ºËÐÄʱÖӵδðÊý// + //¹¤×÷ģʽΪÖÜÆÚģʽ// + //»Øµ÷º¯Êý// + //¶¨Ê±Æ÷ID±£´æÔÚg_cpupSwtmrId±äÁ¿ÖÐ// + //×îºóÒ»¸ö²ÎÊýΪ0±íʾ²»Ð¯´ø¶îÍâ²ÎÊý// + (VOID)LOS_SwtmrCreate(LOSCFG_BASE_CORE_TICK_PER_SECOND, LOS_SWTMR_MODE_PERIOD, + (SWTMR_PROC_FUNC)OsCpupGuard, &g_cpupSwtmrId, 0); + + (VOID)LOS_SwtmrStart(g_cpupSwtmrId); //Æô¶¯¸ÃÈí¼þ¶¨Ê±Æ÷// +} + +/*³õʼ»¯CPUʹÓÃÂÊͳ¼ÆÄ£¿é£¬ÆäÖаüÀ¨´´½¨Ò»¸ö×î¸ßÓÅÏȼ¶µÄÈÎÎñ£¬ +¸ÃÈÎÎñµÄÈë¿Úº¯ÊýΪOsCpupGuardCreator£¬ +ÓÃÓÚ´´½¨ºÍÆô¶¯ÖÜÆÚÐÔµ÷ÓÃOsCpupGuard()º¯ÊýµÄÈí¼þ¶¨Ê±Æ÷*/ +LITE_OS_SEC_TEXT_INIT VOID OsCpupGuardInit(VOID) +{ + TSK_INIT_PARAM_S taskInitParam; + UINT32 tempId; + //³õʼ»¯taskInitParam£¬²¢½«ÆäÄÚÈÝÇåÁã// + (VOID)memset_s((void *)(&taskInitParam), sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S)); + taskInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)OsCpupGuardCreator; //Ö¸¶¨ÈÎÎñÈë¿Úº¯ÊýΪOsCpupGuardCreator£¬¼´´´½¨ºÍÆô¶¯Èí¼þ¶¨Ê±Æ÷µÄº¯Êý// + taskInitParam.uwStackSize = LOS_TASK_MIN_STACK_SIZE; //Ö¸¶¨ÈÎÎñÕ»µÄ´óСΪ×îСÈÎÎñÕ»´óС// + taskInitParam.pcName = "CpupGuardCreator"; //Ö¸¶¨ÈÎÎñµÄÃû³ÆÎª"CpupGuardCreator"// + taskInitParam.usTaskPrio = OS_TASK_PRIORITY_HIGHEST; //Ö¸¶¨ÈÎÎñµÄÓÅÏȼ¶Îª×î¸ßÓÅÏȼ¶// + taskInitParam.uwResved = LOS_TASK_STATUS_DETACHED; //Ö¸¶¨ÈÎÎñ״̬ΪLOS_TASK_STATUS_DETACHED// +#ifdef LOSCFG_KERNEL_SMP //ÅжϲÙ×÷ϵͳÊÇ·ñÖ§³Ö¶àºË// + taskInitParam.usCpuAffiMask = CPUID_TO_AFFI_MASK(ArchCurrCpuid()); //ÉèÖÃÈÎÎñµÄCPUÇ׺ÍÐÔÑÚÂë// +#endif + /*´«ÈëÉÏÊö³õʼ»¯ºÃµÄ²ÎÊýtaskInitParam£¬²¢½«ÈÎÎñID±£´æÔÚtempIdÖÐ*/ + (VOID)LOS_TaskCreate(&tempId, &taskInitParam); +} + +/* + * Description: initialization of CPUP + * Return : LOS_OK or Error Information + */ +LITE_OS_SEC_TEXT_INIT UINT32 OsCpupInit(VOID) +{ + UINT32 size; + + g_cpupTaskMaxNum = g_taskMaxNum; + g_cpupMaxNum = g_cpupTaskMaxNum; +/*Èç¹ûÅäÖÃÁ˰üº¬ÖжϵÄCPUʹÓÃÂÊͳ¼Æ£¨LOSCFG_CPUP_INCLUDE_IRQ£©£¬ +Ôò½«g_cpupMaxNumÔö¼ÓLOSCFG_PLATFORM_HWI_LIMIT*/ +#ifdef LOSCFG_CPUP_INCLUDE_IRQ + g_cpupMaxNum += LOSCFG_PLATFORM_HWI_LIMIT; +#endif + + /* every task has only one record, and it won't operated at the same time */ + size = g_cpupMaxNum * sizeof(OsCpupCB); //¼ÆËãÐèÒª·ÖÅäµÄÄÚ´æ¿Õ¼ä´óС// + g_cpup = (OsCpupCB *)LOS_MemAlloc(m_aucSysMem0, size); //·ÖÅäÄڴ棬ÓÃÓÚ´æ´¢OsCpupCB½á¹¹ÌåÊý×é// + if (g_cpup == NULL) { //ÄÚ´æ·ÖÅäʧ°Ü// + return LOS_ERRNO_CPUP_NO_MEMORY; + } + + OsCpupGuardInit(); //³õʼ»¯CPUʹÓÃÂÊͳ¼ÆÄ£¿é// + + (VOID)memset_s(g_cpup, size, 0, size); //½«·ÖÅäµÄÄÚ´æ¿Õ¼äÇåÁã// + g_cpupInitFlg = 1; //±íʾCPUʹÓÃÂÊͳ¼ÆÄ£¿éÒѾ­³õʼ»¯Íê³É// + + return LOS_OK; //³õʼ»¯³É¹¦// +} + +LITE_OS_SEC_TEXT_INIT VOID LOS_CpupReset(VOID) //ÓÃÓÚÖØÖÃCPUʹÓÃÂÊͳ¼ÆÄ£¿é// +{ + UINT32 cpupIndex; + UINT32 maxNum = g_cpupMaxNum; + UINT64 curCycle; + UINT16 loop; + UINT32 intSave; + + /*¼ì²éÈ«¾Ö±äÁ¿g_cpupÊÇ·ñΪNULL£¬Èç¹ûÊÇ£¬ÔòÖ±½Ó·µ»Ø£¬²»Ö´ÐÐÈκβÙ×÷*/ + if (g_cpup == NULL) { + return; + } + + g_cpupInitFlg = 0; //±íʾCPUʹÓÃÂÊͳ¼ÆÄ£¿éδ³õʼ»¯// + intSave = LOS_IntLock(); //Ëø¶¨Öжϣ¬·ÀÖ¹ÔÚÖØÖùý³ÌÖз¢ÉúÖжÏ// + (VOID)LOS_SwtmrStop(g_cpupSwtmrId); //Í£Ö¹CPUʹÓÃÂÊͳ¼Æ¶¨Ê±Æ÷// + curCycle = OsCpupGetCycle(); //»ñÈ¡µ±Ç°µÄCPUÖÜÆÚÊý// + + /*½«cpuHistoryTimeÊý×éÖеÄËùÓÐÔªËØ¶¼ÉèÖÃΪcurCycle£¬Çå¿ÕÀúʷʱ¼ä¼Ç¼*/ + for (loop = 0; loop < (OS_CPUP_HISTORY_RECORD_NUM + 1); loop++) { + cpuHistoryTime[loop] = curCycle; + } + + /*±éÀúÿ¸öCPUʹÓÃÂÊͳ¼Æ½á¹¹Ì壬 + ²¢½«ÆäÆðʼʱ¼äºÍ×Üʱ¼ä¶¼ÉèÖÃΪcurCycle£¬ + ͬʱÇå¿Õÿ¸ö½á¹¹ÌåÖеÄÀúʷʱ¼ä¼Ç¼*/ + for (cpupIndex = 0; cpupIndex < maxNum; cpupIndex++) { + g_cpup[cpupIndex].startTime = curCycle; + g_cpup[cpupIndex].allTime = curCycle; + for (loop = 0; loop < (OS_CPUP_HISTORY_RECORD_NUM + 1); loop++) { + g_cpup[cpupIndex].historyTime[loop] = curCycle; + } + } + +/*Èç¹ûÅäÖÃÁ˰üº¬ÖжϵÄCPUʹÓÃÂÊͳ¼Æ£¬ +Ôò½«g_timeInIrqPerTskSwitchÊý×éÖеÄËùÓÐÔªËØ¶¼ÉèÖÃΪ0£¬ +ÓÃÓڼǼÈÎÎñÇл»ÆÚ¼äµÄÖжÏʱ¼ä*/ +#ifdef LOSCFG_CPUP_INCLUDE_IRQ + for (loop = 0; loop < LOSCFG_KERNEL_CORE_NUM; loop++) { + g_timeInIrqPerTskSwitch[loop] = 0; + } +#endif + + (VOID)LOS_SwtmrStart(g_cpupSwtmrId); //ÖØÐÂÆô¶¯CPUʹÓÃÂÊͳ¼Æ¶¨Ê±Æ÷// + LOS_IntRestore(intSave); //»Ö¸´ÖжÏ״̬// + g_cpupInitFlg = 1; //±íʾCPUʹÓÃÂÊͳ¼ÆÄ£¿éÒѾ­ÖØÐ³õʼ»¯Íê³É// + + return; +} + +/*ÓÃÓÚÉèÖÃCPUÖÜÆÚÊýÆðʼֵ£¬ÒÔ±ãºóÃæ¼Ç¼ʱ¼äʱ +Äܹ»ÕýÈ·¼ÆËãCPUÖÜÆÚÊýµÄ²îÖµ£¬´Ó¶øµÃµ½Ê±¼ä¼ä¸ô*/ +LITE_OS_SEC_TEXT_MINOR VOID OsCpupSetCycle(UINT64 startCycles) +{ + g_startCycles = startCycles; + return; +} + +/* + * Description: get current cycles count + * Return : current cycles count + */ +LITE_OS_SEC_TEXT_MINOR UINT64 OsCpupGetCycle(VOID) +{ + UINT32 high; + UINT32 low; + UINT64 cycles; + + LOS_GetCpuCycle(&high, &low); + cycles = ((UINT64)high << HIGH_BITS) + low; //½«¸ßλºÍµÍλµÄ¼ÆÊýÖµºÏ²¢³ÉÒ»¸ö64λ¼ÆÊýÖµ// + if (g_startCycles == 0) { + g_startCycles = cycles; + } + + /* + * The cycles should keep growing, if the checking failed, + * it mean LOS_GetCpuCycle has the problem which should be fixed. + */ + LOS_ASSERT(cycles >= g_startCycles); + + return (cycles - g_startCycles); +} + +/* + * Description: start task to get cycles count in current task beginning + */ +LITE_OS_SEC_TEXT_MINOR VOID OsTaskCycleStart(VOID) //ÓÃÓÚÔÚÈÎÎñ¿ªÊ¼Ê±»ñÈ¡CPUÖÜÆÚ¼ÆÊý// +{ + UINT32 taskId; + LosTaskCB *runTask = NULL; + + if (g_cpupInitFlg == 0) { //Èç¹ûΪ0Ôò±íʾ»¹Î´½øÐÐCPUÖÜÆÚ¼ÆÊýµÄ³õʼ»¯£¬Ö±½Ó·µ»Ø// + return; + } + + runTask = OsCurrTaskGet(); //»ñÈ¡µ±Ç°ÕýÔÚÔËÐеÄÈÎÎñ// + taskId = runTask->taskId; //»ñÈ¡µ±Ç°ÈÎÎñµÄID// + + g_cpup[taskId].id = taskId; + g_cpup[taskId].startTime = OsCpupGetCycle(); + + return; +} + +/* + * Description: quit task and get cycle count + */ +LITE_OS_SEC_TEXT_MINOR VOID OsTaskCycleEnd(VOID) +{ + UINT32 taskId; + UINT64 cpuCycle; + LosTaskCB *runTask = NULL; + + if (g_cpupInitFlg == 0) { + return; + } + + runTask = OsCurrTaskGet(); + taskId = runTask->taskId; + + if (g_cpup[taskId].startTime == 0) { //ÅжÏÊÇ·ñÒѾ­¼Ç¼ÁËÈÎÎñµÄ¿ªÊ¼Ê±¼ä£¬// + return; //ÈôΪ0Ôò±íʾδ¼Ç¼¿ªÊ¼Ê±¼ä£¬Ö±½Ó·µ»Ø// + } + + cpuCycle = OsCpupGetCycle(); + g_cpup[taskId].allTime += cpuCycle - g_cpup[taskId].startTime; //»ñÈ¡ÈÎÎñ´Ó¿ªÊ¼µ½½áÊøµÄCPUÖÜÆÚÊý// + +#ifdef LOSCFG_CPUP_INCLUDE_IRQ //Èç¹û¿ªÆôÁ˰üº¬Öжϴ¦Àíʱ¼äµÄͳ¼Æ£¬ÐèÒª¶ÔÈÎÎñµÄ×ÜÔËÐÐʱ¼ä½øÐÐÐÞÕý// + UINT32 cpuId = ArchCurrCpuid(); + g_cpup[taskId].allTime -= g_timeInIrqPerTskSwitch[cpuId]; //¼õÈ¥µ±Ç°CPUºËÐÄÉÏÖжϴ¦ÀíËùÕ¼ÓõÄʱ¼ä + g_timeInIrqPerTskSwitch[cpuId] = 0; +#endif + g_cpup[taskId].startTime = 0; //±íʾÈÎÎñµÄÔËÐÐʱ¼äͳ¼Æ½áÊø// + + return; +} + +/* + * Description: start task to get cycles count in current task ending + */ +LITE_OS_SEC_TEXT_MINOR VOID OsTaskCycleEndStart(const LosTaskCB *newTask) +{ + UINT64 cpuCycle; + LosTaskCB *runTask = NULL; + OsCpupCB *cpup = NULL; + UINT32 cpuId = ArchCurrCpuid(); + + if ((g_cpupInitFlg == 0) || (newTask == NULL)) { + return; + } + + runTask = OsCurrTaskGet(); + cpuCycle = OsCpupGetCycle(); + + cpup = &g_cpup[runTask->taskId]; + if (cpup->startTime != 0) { + cpup->allTime += cpuCycle - cpup->startTime; //±íʾ¸ÃÈÎÎñ֮ǰÒѾ­¿ªÊ¼ÁËCPUÖÜÆÚ¼ÆÊý£¬ÐèÒª¶ÔÆä×ÜÔËÐÐʱ¼ä½øÐиüÐÂ// +#ifdef LOSCFG_CPUP_INCLUDE_IRQ //¸ù¾ÝÊÇ·ñÂú×ãÌõ¼þÅäÖ㬶Ô×ÜÔËÐÐʱ¼ä½øÐÐÐÞÕý// + cpup->allTime -= g_timeInIrqPerTskSwitch[cpuId]; + g_timeInIrqPerTskSwitch[cpuId] = 0; +#endif + } + + cpup = &g_cpup[newTask->taskId]; + /*½«ÐÂÈÎÎñµÄIDºÍµ±Ç°CPUÖÜÆÚ¼ÆÊýÖµ±£´æ£¬±íʾÐÂÈÎÎñµÄCPUÖÜÆÚ¼ÆÊý¿ªÊ¼*/ + cpup->id = newTask->taskId; + cpup->startTime = cpuCycle; + runningTasks[cpuId] = newTask->taskId; //¸üе±Ç°CPUºËÐÄÉÏÕýÔÚÔËÐеÄÈÎÎñID// + + return; +} + +/*ÓÃÓÚ»ñÈ¡CPUÖÜÆÚ¼ÆÊýÊý¾ÝÔÚÊý×éÖеÄλÖÃ*/ +LITE_OS_SEC_TEXT_MINOR STATIC VOID OsCpupGetPos(UINT32 mode, UINT16 *curPosPointer, UINT16 *prePosPointer) +{ + UINT16 curPos; + UINT16 tmpPos; + UINT16 prePos; + + tmpPos = g_hisPos; //»ñÈ¡µ±Ç°Î»ÖÃ// + curPos = CPUP_PRE_POS(tmpPos); //»ñȡǰһ¸öλÖÃ// + + /* + * The current position has nothing to do with the CPUP modes, + * however, the previous position differs. + */ + switch (mode) { + /*±íʾÐèÒª»ñÈ¡×î½üÒ»ÃëÄÚµÄCPUÖÜÆÚ¼ÆÊýÊý¾Ý£¬ + ´Ëʱ½«Ç°Ò»¸öλÖÃÉèÖÃΪµ±Ç°Î»ÖõÄǰһ¸öλÖÃ*/ + case CPUP_LAST_ONE_SECONDS: + prePos = CPUP_PRE_POS(curPos); + break; + /*±íʾÐèÒª»ñÈ¡×î½üÊ®ÃëÄÚµÄCPUÖÜÆÚ¼ÆÊýÊý¾Ý£¬ + ´Ëʱ½«Ç°Ò»¸öλÖÃÉèÖÃΪµ±Ç°Î»ÖÃ*/ + case CPUP_LAST_TEN_SECONDS: + prePos = tmpPos; + break; + /*±íʾÐèÒª»ñÈ¡ËùÓÐʱ¼äÄÚµÄCPUÖÜÆÚ¼ÆÊýÊý¾Ý£¬ + ´Ëʱ½«Ç°Ò»¸öλÖÃÉèÖÃΪÊý×éµÄ×îºóÒ»¸öλÖÃ*/ + case CPUP_ALL_TIME: + /* fall-through */ + default: + prePos = OS_CPUP_HISTORY_RECORD_NUM; + break; + } + + *curPosPointer = curPos; //±£´æµ±Ç°Î»ÖÃ// + *prePosPointer = prePos; //±£´æÇ°Ò»¸öλÖÃ// + + return; +} + +/*ÓÃÓÚ¼ì²éCPUʹÓÃÂÊͳ¼Æ²ÎÊýµÄºÏ·¨ÐÔ*/ +LITE_OS_SEC_TEXT_MINOR STATIC INLINE UINT32 OsCpuUsageParaCheck(UINT32 taskId) +{ + if (g_cpupInitFlg == 0) { + return LOS_ERRNO_CPUP_NO_INIT; + } + + if (OS_TSK_GET_INDEX(taskId) >= g_taskMaxNum) { //ÈÎÎñIDË÷ÒýÖµ³¬¹ýÁË×î´óÈÎÎñÊý£¬// + return LOS_ERRNO_CPUP_TSK_ID_INVALID; //±íʾÈÎÎñIDÎÞЧ£¬·µ»Ø´íÎóÂë// + } + + /* weather the task is created */ + if (g_cpup[taskId].id != taskId) { + return LOS_ERRNO_CPUP_THREAD_NO_CREATED; //±íʾ¸ÃÈÎÎñÉÐδ´´½¨£¬·µ»Ø´íÎóÂë// + } + + if ((g_cpup[taskId].status & OS_TASK_STATUS_UNUSED) || (g_cpup[taskId].status == 0)) { + return LOS_ERRNO_CPUP_THREAD_NO_CREATED; //±íʾ¸ÃÈÎÎñÉÐδ´´½¨£¬·µ»Ø´íÎóÂë// + } + + return LOS_OK; //±íʾͨ¹ý¼ì²é// +} + +LITE_OS_SEC_TEXT_MINOR UINT32 LOS_HistorySysCpuUsage(UINT32 mode) //ÓÃÓÚ»ñÈ¡ÀúʷϵͳCPUʹÓÃÂÊ// +{ + UINT64 cpuCycleAll; + UINT64 idleCycleAll = 0; + UINT32 cpup = 0; + UINT16 pos; + UINT16 prePos; + UINT32 intSave; + UINT32 idleTaskId; +#ifdef LOSCFG_KERNEL_SMP + UINT32 cpuId = 0; +#endif + + if (g_cpupInitFlg == 0) { + return LOS_ERRNO_CPUP_NO_INIT; + } + + /* get end time of current task */ + intSave = LOS_IntLock(); //Ëø¶¨ÖжÏ״̬// + OsTaskCycleEnd(); //»ñÈ¡µ±Ç°ÈÎÎñµÄ½áÊøÊ±¼ä// + + OsCpupGetPos(mode, &pos, &prePos); //»ñÈ¡ÀúÊ·CPUʹÓÃÂÊÊý¾ÝµÄλÖÃÐÅÏ¢// + cpuCycleAll = cpuHistoryTime[pos] - cpuHistoryTime[prePos]; //¼ÆËãCPUµÄ×ÜÖÜÆÚÊý// + +#ifdef LOSCFG_KERNEL_SMP //ÅжÏÊÇ·ñΪSMPϵͳ// + /* For SMP system, each idle task needs to be accounted */ + while (cpuId < LOSCFG_KERNEL_CORE_NUM) { + idleTaskId = g_percpu[cpuId].idleTaskId; + //ÀÛ¼Ó¸÷ºËÐĵĿÕÏÐÈÎÎñµÄÖÜÆÚÊýµ½idleCycleAllÖÐ// + idleCycleAll += g_cpup[idleTaskId].historyTime[pos] - g_cpup[idleTaskId].historyTime[prePos]; + cpuId++; + } + cpuCycleAll *= LOSCFG_KERNEL_CORE_NUM; +#else + idleTaskId = OsGetIdleTaskId(); //Ö±½Ó»ñÈ¡¿ÕÏÐÈÎÎñµÄÖÜÆÚÊý¡£// + idleCycleAll = g_cpup[idleTaskId].historyTime[pos] - g_cpup[idleTaskId].historyTime[prePos]; +#endif + + if (cpuCycleAll) { + cpup = (LOS_CPUP_PRECISION - (UINT32)((LOS_CPUP_PRECISION * idleCycleAll) / cpuCycleAll)); + } //µÃ³öCPUʹÓÃÂÊ// + + OsTaskCycleStart(); //ÖØÐ¿ªÊ¼ÈÎÎñµÄʱ¼äͳ¼Æ// + LOS_IntRestore(intSave); //»Ö¸´ÖжÏ״̬// + + return cpup; +} + +LITE_OS_SEC_TEXT_MINOR UINT32 LOS_HistoryTaskCpuUsage(UINT32 taskId, UINT32 mode) +{ + UINT64 cpuCycleAll; + UINT64 cpuCycleCurTask; + UINT16 pos; + UINT16 prePos; + UINT32 intSave; + UINT32 cpup = 0; + UINT32 ret; + + if (g_cpupInitFlg == 0) { + return LOS_ERRNO_CPUP_NO_INIT; + } + + //¶Ô´«ÈëµÄÈÎÎñID½øÐвÎÊý¼ì²é£¬¼ì²é½á¹û²»µÈÓÚLOS_OK£¬Ôò·µ»ØÏàÓ¦µÄ´íÎóÂë// + ret = OsCpuUsageParaCheck(taskId); + if (ret != LOS_OK) { + return ret; + } + OsCpupCB *taskCpup = &g_cpup[taskId]; + + intSave = LOS_IntLock(); + OsTaskCycleEnd(); + + OsCpupGetPos(mode, &pos, &prePos); //»ñÈ¡ÀúÊ·CPUʹÓÃÂÊÊý¾ÝµÄλÖÃÐÅÏ¢// + cpuCycleAll = cpuHistoryTime[pos] - cpuHistoryTime[prePos]; //¼ÆËãCPUµÄ×ÜÖÜÆÚÊý// + cpuCycleCurTask = taskCpup->historyTime[pos] - taskCpup->historyTime[prePos]; //¼ÆË㵱ǰÈÎÎñµÄCPUÖÜÆÚÊý// + if (cpuCycleAll) { + cpup = (UINT32)((LOS_CPUP_PRECISION * cpuCycleCurTask) / cpuCycleAll); + } //¼ÆËãµÃ³öCPUʹÓÃÂÊ// + + OsTaskCycleStart(); + LOS_IntRestore(intSave); + + return cpup; +} + +//ÓÃÓÚ»ñÈ¡ËùÓÐÈÎÎñÀúÊ·CPUʹÓÃÂÊ// +LITE_OS_SEC_TEXT_MINOR UINT32 LOS_AllCpuUsage(UINT16 maxNum, CPUP_INFO_S *cpupInfo, UINT32 mode, UINT16 flag) +{ + UINT16 loop; + UINT16 pos; + UINT16 prePos; + UINT32 intSave; + UINT64 cpuCycleAll; + UINT64 cpuCycleCurTask; + UINT16 numTmpMax = maxNum; + UINT16 numTmpMin = 0; + UINT16 numMax = g_cpupTaskMaxNum; + + if (g_cpupInitFlg == 0) { + return LOS_ERRNO_CPUP_NO_INIT; + } + + if (cpupInfo == NULL) { //¼ì²é´«ÈëµÄÖ¸ÕëcpupInfoÊÇ·ñΪ¿Õ£¬Èç¹ûΪ¿Õ£¬·µ»Ø´íÎóÂë// + return LOS_ERRNO_CPUP_TASK_PTR_NULL; + } + + if (maxNum == 0) { //¼ì²é´«ÈëµÄmaxNumÊÇ·ñµÈÓÚ0£¬Èç¹ûµÈÓÚ0£¬·µ»Ø´íÎóÂë// + return LOS_ERRNO_CPUP_MAXNUM_INVALID; + } + +#ifdef LOSCFG_CPUP_INCLUDE_IRQ //Èç¹ûºê±»¶¨Òå// + if (flag == 0) { + numTmpMax += g_cpupTaskMaxNum; + numTmpMin += g_cpupTaskMaxNum; + numMax = g_cpupMaxNum; + } +#endif + + if (numTmpMax > numMax) { + numTmpMax = numMax; + } + + intSave = LOS_IntLock(); + OsTaskCycleEnd(); + + OsCpupGetPos(mode, &pos, &prePos); //»ñÈ¡ÀúÊ·CPUʹÓÃÂÊÊý¾ÝµÄλÖÃÐÅÏ¢// + cpuCycleAll = cpuHistoryTime[pos] - cpuHistoryTime[prePos]; //¼ÆËãCPUµÄ×ÜÖÜÆÚÊý// + + /*ͨ¹ýÑ­»·±éÀúËùÓÐÈÎÎñµÄCPUʹÓÃÂÊÊý¾Ý£¬¼ÆË㵱ǰÈÎÎñµÄCPUÖÜÆÚÊý²¢±£´æ*/ + for (loop = numTmpMin; loop < numTmpMax; loop++) { + if ((g_cpup[loop].status & OS_TASK_STATUS_UNUSED) || (g_cpup[loop].status == 0)) { + continue; + } + + cpuCycleCurTask = g_cpup[loop].historyTime[pos] - g_cpup[loop].historyTime[prePos]; + cpupInfo[loop - numTmpMin].usStatus = g_cpup[loop].status; + + if (cpuCycleAll) { + cpupInfo[loop - numTmpMin].uwUsage = (UINT32)((LOS_CPUP_PRECISION * cpuCycleCurTask) / cpuCycleAll); + } + } + + OsTaskCycleStart(); + LOS_IntRestore(intSave); + + return LOS_OK; +} + +#ifdef LOSCFG_CPUP_INCLUDE_IRQ +/*ÓÃÓÚÔÚÖжϿªÊ¼Ê±¼Ç¼µ±Ç°µÄʱÖÓÖÜÆÚÊý£¬ÒÔ±ãÓÚºóÐø×öÖжϴ¦Àíʱ¼äµÄͳ¼ÆºÍ·ÖÎö*/ +LITE_OS_SEC_TEXT_MINOR VOID OsCpupIrqStart(VOID) +{ + g_intTimeStart[ArchCurrCpuid()] = OsCpupGetCycle(); + return; +} + +/*ÔÚÖжϽáÊøÊ±½øÐÐͳ¼Æ²Ù×÷£¬ +°üÀ¨¼Ç¼ÖжϺš¢¸üÐÂÖÐ¶ÏÆÚ¼äÈÎÎñÇл»µÄʱ¼äÒÔ¼°ÀÛ¼ÓÖÐ¶ÏÆÚ¼äµÄ×Üʱ¼ä*/ +LITE_OS_SEC_TEXT_MINOR VOID OsCpupIrqEnd(UINT32 intNum) +{ + UINT64 intTimeEnd = OsCpupGetCycle(); //»ñÈ¡µ±Ç°µÄʱÖÓÖÜÆÚÊý// + UINT32 cpuId = ArchCurrCpuid(); //»ñÈ¡µ±Ç°CPUµÄID// + + if (g_cpupInitFlg == 0) { + return; + } + + g_cpup[g_taskMaxNum + intNum].id = intNum; + g_cpup[g_taskMaxNum + intNum].status = OS_TASK_STATUS_RUNNING; //½«ÖжÏ״̬ÉèΪÔËÐÐÖÐ// + g_timeInIrqPerTskSwitch[cpuId] += (intTimeEnd - g_intTimeStart[cpuId]); //Ôö¼ÓÖÐ¶ÏÆÚ¼äÈÎÎñÇл»ËùÏûºÄµÄʱ¼ä// + g_cpup[g_taskMaxNum + intNum].allTime += (intTimeEnd - g_intTimeStart[cpuId]); + + return; +} +#endif + +#endif /* LOSCFG_KERNEL_CPUP */ +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ + diff --git a/文豪/los_cppsupport_pri.h b/文豪/los_cppsupport_pri.h new file mode 100644 index 0000000..5e3fcb9 --- /dev/null +++ b/文豪/los_cppsupport_pri.h @@ -0,0 +1,53 @@ +/* ---------------------------------------------------------------------------- + * Copyright (c) Huawei Technologies Co., Ltd. 2013-2019. All rights reserved. + * Description: Cpp Support HeadFile + * Author: Huawei LiteOS Team + * Create: 2013-01-01 + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * --------------------------------------------------------------------------- */ + +#ifndef _LOS_CPPSUPPORT_PRI_H +#define _LOS_CPPSUPPORT_PRI_H //·ÀÖ¹¶à´Î°üº¬Í¬Ò»¸öÍ·Îļþ// + +#include "los_cppsupport.h" + +#ifdef __cplusplus //¸ù¾Ý±àÒëÆ÷ÊÇ·ñΪC++// +#if __cplusplus //À´Ê¹Óá°extern 'C'¡±// +extern "C" { //¶ÔÒÔÏ´úÂë¿éÄÚÈݽøÐÐ// +#endif /* __cplusplus */ //ÕýÈ·´¦Àíº¯ÊýÃû// +#endif /* __cplusplus */ + +extern CHAR __fast_end; //ÉùÃ÷Ò»¸ö_fast_endµÄCHARÀàÐͱäÁ¿// + +#ifdef LOSCFG_AARCH64 //Ìõ¼þ±àÒë// +extern UINT8 __EH_FRAME_BEGIN__[]; //ÉùÃ÷Ò»¸öUINT8ÀàÐ͵ÄÊý×é// +VOID __register_frame(VOID *begin); +#endif + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ //¹Ø±Õextern'C'×÷ÓÃÓò// +#endif /* __cplusplus */ + +#endif /* _LOS_CPPSUPPORT_PRI_H */ diff --git a/文豪/los_cpup_pri.h b/文豪/los_cpup_pri.h new file mode 100644 index 0000000..f8da73e --- /dev/null +++ b/文豪/los_cpup_pri.h @@ -0,0 +1,74 @@ +/* ---------------------------------------------------------------------------- + * Copyright (c) Huawei Technologies Co., Ltd. 2013-2020. All rights reserved. + * Description: Cpup HeadFile + * Author: Huawei LiteOS Team + * Create: 2013-01-01 + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * --------------------------------------------------------------------------- */ + +#ifndef _LOS_CPUP_PRI_H +#define _LOS_CPUP_PRI_H //·ÀÖ¹¶à´Î°üº¬Í¬Ò»¸öÍ·Îļþ// + +#include "los_cpup.h" //¿ÉÄܰüº¬ÁËһЩÓëCPUʹÓÃÂÊ// +#include "los_task_pri.h" //ͳ¼ÆÏà¹ØµÄ¹«¹²ÉùÃ÷ºÍ¶¨Òå// + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#define OS_CPUP_HISTORY_RECORD_NUM 11 //¶¨ÒåÀúÊ·¼Ç¼µÄÊýÁ¿Îª11// +#define LOS_CPUP_PRECISION 1000 //¶¨ÒåCPUʹÓÃÂʵľ«¶ÈΪ1000// +#define LOS_CPUP_PRECISION_MULT (LOS_CPUP_PRECISION / 100) //¿ÉÄÜÓÃÓÚ¼ÆËãCPUʹÓÃÂÊ// + +typedef struct { + UINT32 id; /* Task ID */ + UINT16 status; /* Task status */ + UINT64 allTime; /* Total running time */ + UINT64 startTime; /* Time before a task is invoked */ + UINT64 historyTime[OS_CPUP_HISTORY_RECORD_NUM + 1]; /* Historical running time, the last one saves zero */ +} OsCpupCB; + +extern OsCpupCB *OsCpupCBGet(UINT32 index); //¸ù¾ÝË÷Òý»ñÈ¡ `OsCpupCB` ½á¹¹ÌåÖ¸Õë// +extern UINT32 OsCpupInit(VOID); //CPUʹÓÃÂÊͳ¼ÆÄ£¿éµÄ³õʼ»¯º¯Êý// +extern VOID OsCpupSetCycle(UINT64 startCycles); //ÉèÖÃCPUÖÜÆÚ¼ÆÊý// +extern UINT64 OsCpupGetCycle(VOID); //»ñÈ¡CPUÖÜÆÚ¼ÆÊý// +extern VOID OsTaskCycleStart(VOID); //ÈÎÎñÔËÐÐÖÜÆÚµÄÆðʼ// +extern VOID OsTaskCycleEnd(VOID); //ÈÎÎñÔËÐÐÖÜÆÚµÄ½áÊø// +extern VOID OsTaskCycleEndStart(const LosTaskCB *newTask); //¸üÐÂÈÎÎñÔËÐÐÖÜÆÚ£¬²¢¿ªÊ¼ÐµÄÈÎÎñÖÜÆÚ// +#ifdef LOSCFG_CPUP_INCLUDE_IRQ +VOID OsCpupIrqStart(VOID); //ÔÚǰһÌõÌõ¼þ¶¨ÒåÏ£¬ÓÃÓÚ´¦Àí// +VOID OsCpupIrqEnd(UINT32); //ÖжϵÄCPUʹÓÃÂÊͳ¼Æº¯Êý// +#endif + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#endif /* _LOS_CPUP_PRI_H */ + +/*Õâ¶Î´úÂ붨ÒåÁËһЩÓÃÓÚCPUʹÓÃÂÊͳ¼ÆµÄÊý¾Ý½á¹¹ºÍº¯ÊýÉùÃ÷£¬ +ÓÃÓÚÔÚ²Ù×÷ϵͳÖÐʵÏÖ¶ÔÈÎÎñºÍÖжϵÄCPUÀûÓÃÂʽøÐÐ¼à¿ØºÍͳ¼Æ¡£*/ \ No newline at end of file diff --git a/文豪/los_trace_pri.h b/文豪/los_trace_pri.h new file mode 100644 index 0000000..762fd9f --- /dev/null +++ b/文豪/los_trace_pri.h @@ -0,0 +1,150 @@ +/* ---------------------------------------------------------------------------- + * Copyright (c) Huawei Technologies Co., Ltd. 2019-2020. All rights reserved. + * Description: LiteOS Trace Module Private HeadFile + * Author: Huawei LiteOS Team + * Create: 2019-08-30 + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * --------------------------------------------------------------------------- */ + +#ifndef _LOS_TRACE_PRI_H +#define _LOS_TRACE_PRI_H + +#include "los_trace.h" +#include "los_task_pri.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#ifdef LOSCFG_TRACE_CONTROL_AGENT +#define TRACE_CMD_END_CHAR 0xD +#endif + +#define TRACE_ERROR PRINT_ERR +#define TRACE_MODE_OFFLINE 0 +#define TRACE_MODE_ONLINE 1 + +/* just task and hwi were traced */ +#define TRACE_DEFAULT_MASK (TRACE_HWI_FLAG | TRACE_TASK_FLAG) +#define TRACE_CTL_MAGIC_NUM 0xDEADBEEF +#define TRACE_BIGLITTLE_WORD 0x12345678 +#define TRACE_VERSION(MODE) (0xFFFFFFFF & (MODE)) +#define TRACE_MASK_COMBINE(c1, c2, c3, c4) (((c1) << 24) | ((c2) << 16) | ((c3) << 8) | (c4)) + +#define TRACE_GET_MODE_FLAG(type) ((type) & 0xFFFFFFF0) + +extern SPIN_LOCK_S g_traceSpin; +#define TRACE_LOCK(state) LOS_SpinLockSave(&g_traceSpin, &(state)) +#define TRACE_UNLOCK(state) LOS_SpinUnlockRestore(&g_traceSpin, (state)) + +typedef VOID (*TRACE_DUMP_HOOK)(BOOL toClient); +extern TRACE_DUMP_HOOK g_traceDumpHook; + +enum TraceCmd { + TRACE_CMD_START = 1, + TRACE_CMD_STOP, + TRACE_CMD_SET_EVENT_MASK, + TRACE_CMD_RECODE_DUMP, + TRACE_CMD_MAX_CODE, +}; + +/** + * @ingroup los_trace + * struct to store the trace cmd from traceClient. + */ +typedef struct { + UINT8 cmd; + UINT8 param1; + UINT8 param2; + UINT8 param3; + UINT8 param4; + UINT8 param5; + UINT8 end; +} TraceClientCmd; + +/** + * @ingroup los_trace + * struct to store the event infomation + */ +typedef struct { + UINT32 cmd; /* trace start or stop cmd */ + UINT32 param; /* magic numb stand for notify msg */ +} TraceNotifyFrame; + +/** + * @ingroup los_trace + * struct to store the trace config information. + */ +typedef struct { + struct WriteCtrl { + UINT16 curIndex; /* The current record index */ + UINT16 maxRecordCount; /* The max num of track items */ + UINT16 curObjIndex; /* The current obj index */ + UINT16 maxObjCount; /* The max num of obj index */ + ObjData *objBuf; /* Pointer to obj info data */ + TraceEventFrame *frameBuf; /* Pointer to the track items */ + } ctrl; + OfflineHead *head; +} TraceOfflineHeaderInfo; + +extern UINT32 OsTraceGetMaskTid(UINT32 taskId); +extern VOID OsTraceSetObj(ObjData *obj, const LosTaskCB *tcb); +extern VOID OsTraceWriteOrSendEvent(const TraceEventFrame *frame); +extern UINT32 OsTraceBufInit(VOID *buf, UINT32 size); +extern VOID OsTraceObjAdd(UINT32 eventType, UINT32 taskId); +extern BOOL OsTraceIsEnable(VOID); +extern OfflineHead *OsTraceRecordGet(VOID); + +#ifdef LOSCFG_RECORDER_MODE_ONLINE +extern VOID OsTraceSendHead(VOID); +extern VOID OsTraceSendObjTable(VOID); +extern VOID OsTraceSendNotify(UINT32 type, UINT32 value); + +#define OsTraceNotifyStart() do { \ + OsTraceSendNotify(SYS_START, TRACE_CTL_MAGIC_NUM); \ + OsTraceSendHead(); \ + OsTraceSendObjTable(); \ + } while (0) + +#define OsTraceNotifyStop() do { \ + OsTraceSendNotify(SYS_STOP, TRACE_CTL_MAGIC_NUM); \ + } while (0) + +#define OsTraceReset() +#define OsTraceRecordDump(toClient) +#else +extern VOID OsTraceReset(VOID); +extern VOID OsTraceRecordDump(BOOL toClient); +#define OsTraceNotifyStart() +#define OsTraceNotifyStop() +#endif + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#endif /* _LOS_TRACE_PRI_H */ diff --git a/文豪/trace/los_trace.c b/文豪/trace/los_trace.c new file mode 100644 index 0000000..1f19ec2 --- /dev/null +++ b/文豪/trace/los_trace.c @@ -0,0 +1,424 @@ +/* ---------------------------------------------------------------------------- + * Copyright (c) Huawei Technologies Co., Ltd. 2019-2020. All rights reserved. + * Description: LiteOS Trace Implementation + * Author: Huawei LiteOS Team + * Create: 2019-08-31 + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * --------------------------------------------------------------------------- */ +#include "uart.h" +#include "los_trace_pri.h" +#include "trace_pipeline.h" + +#ifdef LOSCFG_KERNEL_SMP +#include "los_mp_pri.h" +#endif + +#ifdef LOSCFG_SHELL +#include "shcmd.h" +#include "shell.h" +#endif + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#ifdef LOSCFG_KERNEL_TRACE +LITE_OS_SEC_BSS STATIC UINT32 g_traceEventCount; //×·×Ùʼþ¼ÆÊý// +LITE_OS_SEC_BSS STATIC volatile enum TraceState g_traceState = TRACE_UNINIT; //×·×Ù״̬// +LITE_OS_SEC_DATA_INIT STATIC volatile BOOL g_enableTrace = FALSE; //ÊÇ·ñÆôÓÃ×·×Ù¹¦ÄÜ// +LITE_OS_SEC_BSS STATIC UINT32 g_traceMask = TRACE_DEFAULT_MASK; //×·×ÙÑÚÂ룬ÓÃÓÚ¹ýÂË×·×Ùʼþ// + +#ifdef LOSCFG_TRACE_CONTROL_AGENT +LITE_OS_SEC_BSS STATIC UINT32 g_traceTaskId; //×·×ÙÈÎÎñµÄID// +#endif + +#define EVENT_MASK 0xFFFFFFF0 +#define MIN(x, y) ((x) < (y) ? (x) : (y)) + +LITE_OS_SEC_BSS STATIC TRACE_HWI_FILTER_HOOK g_traceHwiFliterHook = NULL; + +LITE_OS_SEC_BSS SPIN_LOCK_INIT(g_traceSpin); + +STATIC_INLINE BOOL OsTraceHwiFilter(UINT32 hwiNum) //ÓÃÓÚÅжÏÊÇ·ñÐèÒª¹ýÂËij¸öÓ²¼þÖжÏ// +{ + BOOL ret = ((hwiNum == NUM_HAL_INTERRUPT_UART) || (hwiNum == OS_TICK_INT_NUM)); +#ifdef LOSCFG_KERNEL_SMP + ret |= (hwiNum == LOS_MP_IPI_SCHEDULE); +#endif + if (g_traceHwiFliterHook != NULL) { + ret |= g_traceHwiFliterHook(hwiNum); + } + return ret; +} + +//ÓÃÓÚÉèÖÃ×·×ÙʼþµÄÖ¡ÐÅÏ¢// +STATIC VOID OsTraceSetFrame(TraceEventFrame *frame, UINT32 eventType, UINTPTR identity, const UINTPTR *params, + UINT16 paramCount) +{ + INT32 i; + UINT32 intSave; + + (VOID)memset_s(frame, sizeof(TraceEventFrame), 0, sizeof(TraceEventFrame)); + + if (paramCount > LOSCFG_TRACE_FRAME_MAX_PARAMS) { + paramCount = LOSCFG_TRACE_FRAME_MAX_PARAMS; + } + + TRACE_LOCK(intSave); + frame->curTask = OsTraceGetMaskTid(OsCurrTaskGet()->taskId); + frame->identity = identity; + frame->curTime = HalClockGetCycles(); + frame->eventType = eventType; + +#ifdef LOSCFG_TRACE_FRAME_CORE_MSG + frame->core.cpuId = ArchCurrCpuid(); //CPUµÄID// + frame->core.hwiActive = OS_INT_ACTIVE ? TRUE : FALSE; //Ó²¼þÖжϻ״̬// + frame->core.taskLockCnt = MIN(OsPercpuGet()->taskLockCnt, 0xF); /* taskLockCnt is 4 bits, max vaule = 0xF */ //ÈÎÎñËø¼ÆÊý// + frame->core.paramCount = paramCount; //²ÎÊýÊýÄ¿// +#endif + +#ifdef LOSCFG_TRACE_FRAME_EVENT_COUNT + frame->eventCount = g_traceEventCount; //×·×ÙʼþµÄ¼ÆÊý// + g_traceEventCount++; +#endif + TRACE_UNLOCK(intSave); + + for (i = 0; i < paramCount; i++) { + frame->params[i] = params[i]; + } +} + +VOID OsTraceSetObj(ObjData *obj, const LosTaskCB *tcb) //ÉèÖöÔÏóµÄ×·×ÙÐÅÏ¢// +{ + errno_t ret; + (VOID)memset_s(obj, sizeof(ObjData), 0, sizeof(ObjData)); + + obj->id = OsTraceGetMaskTid(tcb->taskId); //»ñÈ¡ÈÎÎñID// + obj->prio = tcb->priority; //»ñÈ¡ÈÎÎñÓÅÏȼ¶// + + ret = strncpy_s(obj->name, LOSCFG_TRACE_OBJ_MAX_NAME_SIZE, tcb->taskName, LOSCFG_TRACE_OBJ_MAX_NAME_SIZE - 1); + if (ret != EOK) { + TRACE_ERROR("Task name copy failed!\n"); + } +} + +//´¦Àí×·×Ùʼþ¹³×Ó// +VOID OsTraceHook(UINT32 eventType, UINTPTR identity, const UINTPTR *params, UINT16 paramCount) +{ + if ((eventType == TASK_CREATE) || (eventType == TASK_PRIOSET)) { + OsTraceObjAdd(eventType, identity); /* handle important obj info, these can not be filtered */ + } + + if ((g_enableTrace == TRUE) && (eventType & g_traceMask)) { + UINTPTR id = identity; + if (TRACE_GET_MODE_FLAG(eventType) == TRACE_HWI_FLAG) { + if (OsTraceHwiFilter(identity)) { + return; + } + } else if (TRACE_GET_MODE_FLAG(eventType) == TRACE_TASK_FLAG) { + id = OsTraceGetMaskTid(identity); + } else if (eventType == MEM_INFO_REQ) { + LOS_MEM_POOL_STATUS status; + LOS_MemInfoGet((VOID *)identity, &status); + LOS_TRACE(MEM_INFO, identity, status.uwTotalUsedSize, status.uwTotalFreeSize); + return; + } + + TraceEventFrame frame; + OsTraceSetFrame(&frame, eventType, id, params, paramCount); + + OsTraceWriteOrSendEvent(&frame); //½«×·×ÙʼþÐÅϢдÈë»ò·¢ËͳöÈ¥// + } +} + +BOOL OsTraceIsEnable(VOID) //ÅжÏ×·×ÙÊÇ·ñ´¦ÓÚÆôÓÃ״̬// +{ + return g_enableTrace == TRUE; +} + +STATIC VOID OsTraceHookInstall(VOID) //°²×°×·×Ù¹³×Ó// +{ + g_traceEventHook = OsTraceHook; +#ifdef LOSCFG_RECORDER_MODE_OFFLINE + g_traceDumpHook = OsTraceRecordDump; //ÔÚÀëÏ߼ǼģʽÏÂÉèÖÃ×·×Ùת´¢¹³×Ó// +#endif +} + +#ifdef LOSCFG_TRACE_CONTROL_AGENT +STATIC BOOL OsTraceCmdIsValid(const TraceClientCmd *msg) //Åжϴ«ÈëµÄTraceClientCmd½á¹¹ÌåÊÇ·ñÓÐЧ// +{ + return ((msg->end == TRACE_CMD_END_CHAR) && (msg->cmd < TRACE_CMD_MAX_CODE)); +} + +STATIC VOID OsTraceCmdHandle(const TraceClientCmd *msg) //´¦Àí´«ÈëµÄTraceClientCmd½á¹¹Ìå// +{ + if (!OsTraceCmdIsValid(msg)) { + return; + } + + switch (msg->cmd) { + case TRACE_CMD_START: //Æô¶¯×·×Ù// + LOS_TraceStart(); + break; + case TRACE_CMD_STOP: //Í£Ö¹×·×Ù// + LOS_TraceStop(); + break; + case TRACE_CMD_SET_EVENT_MASK: //ÉèÖÃʼþÑÚÂë// + /* 4 params(UINT8) composition the mask(UINT32) */ + LOS_TraceEventMaskSet(TRACE_MASK_COMBINE(msg->param1, msg->param2, msg->param3, msg->param4)); + break; + case TRACE_CMD_RECODE_DUMP: //×·×ټǼת´¢// + LOS_TraceRecordDump(TRUE); + break; + default: + break; + } +} + +VOID TraceAgent(VOID) //²»¶ÏµÈ´ý×·×ÙÊý¾ÝµÄµ½´ï£¬²¢´¦Àí½ÓÊÕµ½µÄ×·×ÙÃüÁî// +{ + UINT32 ret; + TraceClientCmd msg; + + while (1) { + (VOID)memset_s(&msg, sizeof(TraceClientCmd), 0, sizeof(TraceClientCmd)); + ret = OsTraceDataWait(); + if (ret == LOS_OK) { + OsTraceDataRecv((UINT8 *)&msg, sizeof(TraceClientCmd), 0); + OsTraceCmdHandle(&msg); + } + } +} + +STATIC UINT32 OsCreateTraceAgentTask(VOID) //´´½¨Ò»¸ö×·×Ù´úÀíÈÎÎñ£¬ÓÃÓÚÖ´ÐÐ×·×ÙÂß¼­// +{ + UINT32 ret; + TSK_INIT_PARAM_S taskInitParam; + + (VOID)memset_s((VOID *)(&taskInitParam), sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S)); + taskInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)TraceAgent; + taskInitParam.usTaskPrio = LOSCFG_TRACE_TASK_PRIORITY; + taskInitParam.pcName = "TraceAgent"; + taskInitParam.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE; +#ifdef LOSCFG_KERNEL_SMP + taskInitParam.usCpuAffiMask = CPUID_TO_AFFI_MASK(ArchCurrCpuid()); +#endif + ret = LOS_TaskCreate(&g_traceTaskId, &taskInitParam); + return ret; +} +#endif + +UINT32 LOS_TraceInit(VOID *buf, UINT32 size) //³õʼ»¯×·×Ù¹¦ÄÜ// +{ + UINT32 intSave; + UINT32 ret; + + TRACE_LOCK(intSave); + if (g_traceState != TRACE_UNINIT) { + TRACE_ERROR("trace has been initialized already, the current state is :%d\n", g_traceState); + ret = LOS_ERRNO_TRACE_ERROR_STATUS; + goto LOS_ERREND; + } + +#ifdef LOSCFG_TRACE_CLIENT_INTERACT //ÅжÏÊÇ·ñÐèÒª³õʼ»¯×·×ٹܵÀ// + ret = OsTracePipelineInit(); + if (ret != LOS_OK) { + goto LOS_ERREND; + } +#endif + +#ifdef LOSCFG_TRACE_CONTROL_AGENT //ÅжÏÊÇ·ñÐèÒª´´½¨×·×Ù´úÀíÈÎÎñ// + ret = OsCreateTraceAgentTask(); + if (ret != LOS_OK) { + TRACE_ERROR("trace init create agentTask error :0x%x\n", ret); + goto LOS_ERREND; + } +#endif + + ret = OsTraceBufInit(buf, size); //³õʼ»¯×·×Ù»º³åÇø// + if (ret != LOS_OK) { + goto LOS_RELEASE; + } + + OsTraceHookInstall(); //°²×°×·×Ù¹³×Ó// + + g_traceEventCount = 0; + + /*ÅжÏÊÇ·ñÐèÒªµÈ´ý×·×Ù¿Í»§¶ËÆô¶¯×·×Ù*/ +#ifdef LOSCFG_RECORDER_MODE_ONLINE /* Wait trace client to start trace */ + g_enableTrace = FALSE; + g_traceState = TRACE_INITED; +#else + g_enableTrace = TRUE; + g_traceState = TRACE_STARTED; +#endif + TRACE_UNLOCK(intSave); + return LOS_OK; +LOS_RELEASE: +#ifdef LOSCFG_TRACE_CONTROL_AGENT //ÅжÏÊÇ·ñÐèҪɾ³ý×·×Ù´úÀíÈÎÎñ// + LOS_TaskDelete(g_traceTaskId); +#endif +LOS_ERREND: + TRACE_UNLOCK(intSave); + return ret; +} + +UINT32 LOS_TraceStart(VOID) //ÓÃÓÚÆô¶¯×·×Ù¹¦ÄÜ// +{ + UINT32 intSave; + UINT32 ret = LOS_OK; + + TRACE_LOCK(intSave); + if (g_traceState == TRACE_STARTED) { + goto START_END; + } + + if (g_traceState == TRACE_UNINIT) { + TRACE_ERROR("trace not inited, be sure LOS_TraceInit excute success\n"); + ret = LOS_ERRNO_TRACE_ERROR_STATUS; + goto START_END; + } + + OsTraceNotifyStart(); //֪ͨ׷×Ù¹¦ÄÜ¿ªÊ¼¹¤×÷// + + g_enableTrace = TRUE; + g_traceState = TRACE_STARTED; + + TRACE_UNLOCK(intSave); + LOS_TRACE(MEM_INFO_REQ, m_aucSysMem0); //·¢ËÍ×·×ÙÐÅÏ¢// + return ret; +START_END: + TRACE_UNLOCK(intSave); + return ret; +} + +VOID LOS_TraceStop(VOID) //ÓÃÓÚÍ£Ö¹×·×Ù¹¦ÄÜ// +{ + UINT32 intSave; + + TRACE_LOCK(intSave); + if (g_traceState != TRACE_STARTED) { + goto STOP_END; + } + + g_enableTrace = FALSE; + g_traceState = TRACE_STOPED; + OsTraceNotifyStop(); //֪ͨ׷×Ù¹¦ÄÜÍ£Ö¹¹¤×÷// +STOP_END: + TRACE_UNLOCK(intSave); +} + +VOID LOS_TraceEventMaskSet(UINT32 mask) //ÉèÖÃ×·×ÙʼþÑÚÂë// +{ + g_traceMask = mask & EVENT_MASK; +} + +VOID LOS_TraceRecordDump(BOOL toClient) //ת´¢×·×ټǼ// +{ + if (g_traceState != TRACE_STOPED) { + TRACE_ERROR("trace dump must after trace stopped , the current state is : %d\n", g_traceState); + return; + } + OsTraceRecordDump(toClient); +} + +OfflineHead *LOS_TraceRecordGet(VOID) //»ñȡ׷×ټǼ// +{ + return OsTraceRecordGet(); +} + +VOID LOS_TraceReset(VOID) //ÖØÖÃ×·×Ù¹¦ÄÜ// +{ + if (g_traceState == TRACE_UNINIT) { + TRACE_ERROR("trace not inited, be sure LOS_TraceInit excute success\n"); + return; + } + + OsTraceReset(); +} + +VOID LOS_TraceHwiFilterHookReg(TRACE_HWI_FILTER_HOOK hook) //×¢²áÖжϹýÂ˹³×Ó// +{ + UINT32 intSave; + + TRACE_LOCK(intSave); + g_traceHwiFliterHook = hook; + TRACE_UNLOCK(intSave); +} + +#ifdef LOSCFG_SHELL +LITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdTraceSetMask(INT32 argc, const CHAR **argv) //ÉèÖÃ×·×ÙʼþÑÚÂë// +{ + size_t mask; + CHAR *endPtr = NULL; + + if (argc >= 2) { /* 2:Just as number of parameters */ + PRINTK("\nUsage: trace_mask or trace_mask ID\n"); + return OS_ERROR; + } + + if (argc == 0) { + mask = TRACE_DEFAULT_MASK; + } else { + mask = strtoul(argv[0], &endPtr, 0); + } + LOS_TraceEventMaskSet((UINT32)mask); + return LOS_OK; +} + +LITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdTraceDump(INT32 argc, const CHAR **argv) //½øÐÐ×·×ټǼµÄת´¢// +{ + BOOL toClient; + CHAR *endPtr = NULL; + + if (argc >= 2) { /* 2:Just as number of parameters */ + PRINTK("\nUsage: trace_dump or trace_dump [1/0]\n"); + return OS_ERROR; + } + + if (argc == 0) { + toClient = FALSE; + } else { + toClient = strtoul(argv[0], &endPtr, 0) != 0 ? TRUE : FALSE; + } + LOS_TraceRecordDump(toClient); + return LOS_OK; +} + +/*ÃüÁîÐк¯ÊýµÄ×¢²á*/ +SHELLCMD_ENTRY(tracestart_shellcmd, CMD_TYPE_EX, "trace_start", 0, (CmdCallBackFunc)LOS_TraceStart); //×·×Ù¹¦Äܵįô¶¯// +SHELLCMD_ENTRY(tracestop_shellcmd, CMD_TYPE_EX, "trace_stop", 0, (CmdCallBackFunc)LOS_TraceStop); //×·×Ù¹¦ÄܵÄÍ£Ö¹// +SHELLCMD_ENTRY(tracesetmask_shellcmd, CMD_TYPE_EX, "trace_mask", 1, (CmdCallBackFunc)OsShellCmdTraceSetMask); //ÉèÖÃ×·×ÙʼþÑÚÂë// +SHELLCMD_ENTRY(tracereset_shellcmd, CMD_TYPE_EX, "trace_reset", 0, (CmdCallBackFunc)LOS_TraceReset); //ÖØÖÃ×·×ټǼ// +SHELLCMD_ENTRY(tracedump_shellcmd, CMD_TYPE_EX, "trace_dump", 1, (CmdCallBackFunc)OsShellCmdTraceDump); //ת´¢×·×ټǼ// +#endif + +#endif /* LOSCFG_KERNEL_TRACE */ + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ diff --git a/文豪/trace/pipeline/serial/trace_pipeline_serial.c b/文豪/trace/pipeline/serial/trace_pipeline_serial.c new file mode 100644 index 0000000..e61300b --- /dev/null +++ b/文豪/trace/pipeline/serial/trace_pipeline_serial.c @@ -0,0 +1,98 @@ +/* ---------------------------------------------------------------------------- + * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. + * Description: LiteOS Trace Pipeline of Serial Implementation + * Author: Huawei LiteOS Team + * Create: 2020-03-31 + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * --------------------------------------------------------------------------- */ + +#include "trace_pipeline_serial.h" +#include "trace_pipeline.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#ifdef LOSCFG_TRACE_CONTROL_AGENT +UINT32 SerialPipelineInit(VOID) //ÓÃÓÚ³õʼ»¯´®ÐйܵÀ// +{ + return uart_hwiCreate(); +} + +UINT32 SerialDataReceive(UINT8 *data, UINT32 size, UINT32 timeout) //ÓÃÓÚ´Ó´®ÐйܵÀ½ÓÊÕÊý¾Ý// +{ + return uart_read(data, size, timeout); +} + +UINT32 SerialWait(VOID) //ÓÃÓڵȴý´®ÐйܵÀ×¼±¸¾ÍÐ÷// +{ + return uart_wait_adapt(); +} + +#else//4ºÅ³µ£¬ÃÜÂë2930 + +UINT32 SerialPipelineInit(VOID) //±íʾ³õʼ»¯³É¹¦// +{ + return LOS_OK; +} + +UINT32 SerialDataReceive(UINT8 *data, UINT32 size, UINT32 timeout) //±íʾ½ÓÊÕµ½ÁËÊý¾Ý// +{ + return LOS_OK; +} + +UINT32 SerialWait(VOID) //±íʾÒѾ­×¼±¸¾ÍÐ÷// +{ + return LOS_OK; +} +#endif + +VOID SerialDataSend(UINT16 len, UINT8 *data) //ÓÃÓÚÏò´®ÐйܵÀ·¢ËÍÊý¾Ý// +{ + UINT32 i; + + for (i = 0; i < len; i++) { + UART_PUTC(data[i]); + } +} + +STATIC const TracePipelineOps g_serialOps = { + .init = SerialPipelineInit, + .dataSend = SerialDataSend, + .dataRecv = SerialDataReceive, + .wait = SerialWait, +}; + +UINT32 OsTracePipelineInit(VOID) //ÓÃÓÚ³õʼ»¯×·×ٹܵÀ²¢×¢²áÓëÖ®Ïà¹ØµÄ²Ù×÷½Ó¿Ú// +{ + OsTracePipelineReg(&g_serialOps); + return g_serialOps.init(); +} + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ diff --git a/文豪/trace/pipeline/serial/trace_pipeline_serial.h b/文豪/trace/pipeline/serial/trace_pipeline_serial.h new file mode 100644 index 0000000..a3cf977 --- /dev/null +++ b/文豪/trace/pipeline/serial/trace_pipeline_serial.h @@ -0,0 +1,50 @@ +/* ---------------------------------------------------------------------------- + * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. + * Description: LiteOS Trace Pipeline of Serial Implementation HeadFile + * Author: Huawei LiteOS Team + * Create: 2020-03-16 + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * --------------------------------------------------------------------------- */ + +#ifndef _TRACE_PIPELINE_SERIAL_H +#define _TRACE_PIPELINE_SERIAL_H + +#include "los_typedef.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +extern INT32 uart_putc(CHAR c); //ÓÃÓÚÏò´®¿Ú·¢ËÍÒ»¸ö×Ö·û// + +#define UART_PUTC(c) uart_putc((c)) //µ÷ÓÃuart_putcº¯Êý// + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#endif /* _TRACE_PIPELINE_SERIAL_H */ diff --git a/文豪/trace/pipeline/trace_pipeline.c b/文豪/trace/pipeline/trace_pipeline.c new file mode 100644 index 0000000..33e5f63 --- /dev/null +++ b/文豪/trace/pipeline/trace_pipeline.c @@ -0,0 +1,156 @@ +/* ---------------------------------------------------------------------------- + * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. + * Description: LiteOS Trace Pipeline Implementation + * Author: Huawei LiteOS Team + * Create: 2020-03-31 + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * --------------------------------------------------------------------------- */ + +#include "trace_pipeline.h" +#include "trace_tlv.h" +#include "los_trace_pri.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +//·ÀÖ¹»º³åÇøÒç³ö// +LITE_OS_SEC_BSS SPIN_LOCK_INIT(g_pipeSpin); //³õʼ»¯Ò»¸ö×ÔÐýËø// +#define PIPE_LOCK(state) LOS_SpinLockSave(&g_pipeSpin, &(state)) //¶Ô×ÔÐýËø¼ÓËø²Ù×÷// +#define PIPE_UNLOCK(state) LOS_SpinUnlockRestore(&g_pipeSpin, (state)) //¶Ô×ÔÐýËø½âËø²Ù×÷// + +STATIC TlvTable g_traceTlvTblNotify[] = { //¶¨Òå½á¹¹ÌåµÄ³ÉÔ±±äÁ¿ÀàÐͺʹóС// + { CMD, LOS_OFF_SET_OF(TraceNotifyFrame, cmd), sizeof(UINT32) }, + { PARAMS, LOS_OFF_SET_OF(TraceNotifyFrame, param), sizeof(UINT32) }, + { TRACE_TLV_TYPE_NULL, 0, 0 }, +}; + +STATIC TlvTable g_traceTlvTblHead[] = { //¶¨Òå½á¹¹ÌåµÄ³ÉÔ±±äÁ¿ÀàÐͺʹóС// + { ENDIAN, LOS_OFF_SET_OF(TraceBaseHeaderInfo, bigLittleEndian), sizeof(UINT32) }, + { VERSION, LOS_OFF_SET_OF(TraceBaseHeaderInfo, version), sizeof(UINT32) }, + { CLOCK_FREQ, LOS_OFF_SET_OF(TraceBaseHeaderInfo, clockFreq), sizeof(UINT32) }, + { TRACE_TLV_TYPE_NULL, 0, 0 }, +}; + +STATIC TlvTable g_traceTlvTblObj[] = { //¶¨Òå½á¹¹ÌåµÄ³ÉÔ±±äÁ¿ÀàÐÍ¡¢Æ«ÒÆÎ»ÖúʹóС// + { ADDR, LOS_OFF_SET_OF(ObjData, id), sizeof(UINT32) }, + { PRIO, LOS_OFF_SET_OF(ObjData, prio), sizeof(UINT32) }, + { NAME, LOS_OFF_SET_OF(ObjData, name), sizeof(CHAR) * LOSCFG_TRACE_OBJ_MAX_NAME_SIZE }, + { TRACE_TLV_TYPE_NULL, 0, 0 }, +}; + +STATIC TlvTable g_traceTlvTblEvent[] = { //¶¨Òå½á¹¹ÌåµÄ³ÉÔ±±äÁ¿ÀàÐͺʹóС// +#ifdef LOSCFG_TRACE_FRAME_CORE_MSG + { CORE, LOS_OFF_SET_OF(TraceEventFrame, core), sizeof(UINT32) }, +#endif + { EVENT_CODE, LOS_OFF_SET_OF(TraceEventFrame, eventType), sizeof(UINT32) }, + { CUR_TIME, LOS_OFF_SET_OF(TraceEventFrame, curTime), sizeof(UINT64) }, + +#ifdef LOSCFG_TRACE_FRAME_EVENT_COUNT + { EVENT_COUNT, LOS_OFF_SET_OF(TraceEventFrame, eventCount), sizeof(UINT32) }, +#endif + { CUR_TASK, LOS_OFF_SET_OF(TraceEventFrame, curTask), sizeof(UINT32) }, + { IDENTITY, LOS_OFF_SET_OF(TraceEventFrame, identity), sizeof(UINTPTR) }, + { EVENT_PARAMS, LOS_OFF_SET_OF(TraceEventFrame, params), sizeof(UINTPTR) * LOSCFG_TRACE_FRAME_MAX_PARAMS }, + { TRACE_TLV_TYPE_NULL, 0, 0 }, +}; + +STATIC TlvTable *g_traceTlvTbl[] = { + g_traceTlvTblNotify, + g_traceTlvTblHead, + g_traceTlvTblObj, + g_traceTlvTblEvent +}; + +STATIC UINT32 DefaultPipelineInit(VOID) +{ + return LOS_OK; +} + +STATIC VOID DefaultDataSend(UINT16 len, UINT8 *data) +{ //½«lenºÍdata²ÎÊý±ê¼ÇΪ"δʹÓÃ"// + (VOID)len; //±ÜÃâ±àÒëÆ÷²úÉú¾¯¸æ// + (VOID)data; +} + +STATIC UINT32 DefaultDataReceive(UINT8 *data, UINT32 size, UINT32 timeout) +{ + (VOID)data; //½«data¡¢sizeºÍtimeout²ÎÊý±ê¼ÇΪ"δʹÓÃ"// + (VOID)size; + (VOID)timeout; + return LOS_OK; +} + +STATIC UINT32 DefaultWait(VOID) +{ + return LOS_OK; +} + +STATIC TracePipelineOps g_defaultOps = { + .init = DefaultPipelineInit, + .dataSend = DefaultDataSend, + .dataRecv = DefaultDataReceive, + .wait = DefaultWait, +}; + +STATIC const TracePipelineOps *g_tracePipelineOps = &g_defaultOps; + +VOID OsTracePipelineReg(const TracePipelineOps *ops) +{ + g_tracePipelineOps = ops; //ÓÃÓÚ×¢²áÒ»¸öTracePipelineOps½á¹¹ÌåÖ¸Õë// +} + +VOID OsTraceDataSend(UINT8 type, UINT16 len, UINT8 *data) //ÓÃÓÚ·¢ËÍ×·×ÙÊý¾Ý// +{ + UINT32 intSave; + UINT8 outBuf[LOSCFG_TRACE_TLV_BUF_SIZE] = {0}; + + if ((type > TRACE_MSG_MAX) || (len > LOSCFG_TRACE_TLV_BUF_SIZE)) { //ÊäÈë²ÎÊýµÄºÏ·¨ÐÔ¼ì²é// + return; + } + + //¶ÔÊý¾Ý½øÐбàÂë// + len = OsTraceDataEncode(type, g_traceTlvTbl[type], data, &outBuf[0], sizeof(outBuf)); + + PIPE_LOCK(intSave); //»ñÈ¡¹ÜµÀËø£¬·ÀÖ¹¶àÏ̲߳¢·¢·ÃÎÊ// + g_tracePipelineOps->dataSend(len, &outBuf[0]); //·¢ËͱàÂëºóµÄÊý¾Ý// + PIPE_UNLOCK(intSave); //ÊͷŹܵÀËø// +} + +UINT32 OsTraceDataRecv(UINT8 *data, UINT32 size, UINT32 timeout) //ÓÃÓÚ½ÓÊÕ×·×ÙÊý¾Ý// +{ + return g_tracePipelineOps->dataRecv(data, size, timeout); +} + +UINT32 OsTraceDataWait(VOID) //ÓÃÓڵȴý×·×ÙÊý¾Ý// +{ + return g_tracePipelineOps->wait(); +} + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ diff --git a/文豪/trace/pipeline/trace_pipeline.h b/文豪/trace/pipeline/trace_pipeline.h new file mode 100644 index 0000000..49e292b --- /dev/null +++ b/文豪/trace/pipeline/trace_pipeline.h @@ -0,0 +1,104 @@ +/* ---------------------------------------------------------------------------- + * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. + * Description: LiteOS Trace Pipeline Implementation HeadFile + * Author: Huawei LiteOS Team + * Create: 2020-03-16 + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * --------------------------------------------------------------------------- */ + +#ifndef _TRACE_PIPELINE_H +#define _TRACE_PIPELINE_H + +#include "los_typedef.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +/*ÓÃÓÚʵÏÖ×·×ÙÁ÷Ë®ÏߵŦÄÜ*/ +typedef struct { + UINT32 (*init)(VOID); //ÓÃÓÚ³õʼ»¯// + VOID (*dataSend)(UINT16 len, UINT8 *data); //ÓÃÓÚ·¢ËÍÊý¾Ý// + UINT32 (*dataRecv)(UINT8 *data, UINT32 size, UINT32 timeout); //ÓÃÓÚ½ÓÊÕÊý¾Ý// + UINT32 (*wait)(VOID); //ÓÃÓڵȴý²Ù×÷// +} TracePipelineOps; + +/* used as tlv's tag */ +enum TraceMsgType { //ÓÃÓÚ±íʾ׷×ÙÏûÏ¢µÄÀàÐÍ// + //ÀàÐÍÈçÏÂ// + NOTIFY, //֪ͨ// + HEAD, //Í·²¿// + OBJ, //¶ÔÏó// + EVENT, //ʼþ// + + TRACE_MSG_MAX, //×î´óÖµ// +}; + +enum TraceNotifySubType { //ÓÃÓÚ±íʾ֪ͨÏûÏ¢µÄ×ÓÀàÐÍ// + CMD = 0x1, + PARAMS, +}; + +enum TraceHeadSubType { //ÓÃÓÚ±íʾ׷×ÙÍ·²¿ÐÅÏ¢µÄ×ÓÀàÐÍ// + ENDIAN = 0x1, //×Ö½ÚÐò// + VERSION, //°æ±¾// + OBJ_SIZE, //¶ÔÏó´óС// + OBJ_COUNT, //¶ÔÏó¼ÆÊý// + CUR_INDEX, //µ±Ç°Ë÷Òý// + MAX_RECODE, + CUR_OBJ_INDEX, + CLOCK_FREQ, +}; + +enum TraceObjSubType { //ÓÃÓÚ±íʾ׷×Ù¶ÔÏóµÄ×ÓÀàÐÍ// + ADDR = 0x1, //µØÖ·// + PRIO, //ÓÅÏȼ¶/ + NAME, //Ãû³Æ// +}; + +enum TraceEvtSubType { //ÓÃÓÚ±íʾ׷×ÙʼþµÄ×ÓÀàÐÍ// + CORE = 0x1, //ºËÐıàºÅ// + EVENT_CODE, //ʼþÂë// + CUR_TIME, //µ±Ç°Ê±¼ä// + EVENT_COUNT, //ʼþ¼ÆÊý// + CUR_TASK, //µ±Ç°ÈÎÎñ// + IDENTITY, //Éí·ÝÐÅÏ¢// + EVENT_PARAMS, //ʼþ²ÎÊý// +}; + +extern VOID OsTracePipelineReg(const TracePipelineOps *ops); //ÓÃÓÚ×¢²á×·×ٹܵÀ²Ù×÷// +extern UINT32 OsTracePipelineInit(VOID); //ÓÃÓÚ³õʼ»¯×·×ٹܵÀ// + +extern VOID OsTraceDataSend(UINT8 type, UINT16 len, UINT8 *data); //ÓÃÓÚ·¢ËÍ×·×ÙÊý¾Ý// +extern UINT32 OsTraceDataRecv(UINT8 *data, UINT32 size, UINT32 timeout); //ÓÃÓÚ½ÓÊÕ×·×ÙÊý¾Ý// +extern UINT32 OsTraceDataWait(VOID); //ÓÃÓڵȴý×·×ÙÊý¾Ý// + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#endif /* _TRACE_PIPELINE_H */ diff --git a/文豪/trace/pipeline/trace_tlv.c b/文豪/trace/pipeline/trace_tlv.c new file mode 100644 index 0000000..4545766 --- /dev/null +++ b/文豪/trace/pipeline/trace_tlv.c @@ -0,0 +1,121 @@ +/* ---------------------------------------------------------------------------- + * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. + * Description: LiteOS Trace Tlv Implementation + * Author: Huawei LiteOS Team + * Create: 2020-03-31 + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * --------------------------------------------------------------------------- */ + +#include "trace_tlv.h" +#include "securec.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#define CRC_WIDTH 8 //CRCµÄλ¿í// +#define CRC_POLY 0x1021 //CRC¶àÏîʽ// +#define CRC_TOPBIT 0x8000 //CRC¼ÆËãÖеÄ×î¸ßλ// + +STATIC UINT16 CalcCrc16(const UINT8 *buf, UINT32 len) //ÓÃÓÚ¼ÆËã16λCRCУÑéÖµ// +{ + UINT32 i; + UINT16 crc = 0; + + for (; len > 0; len--) { + crc = crc ^ (*buf++ << CRC_WIDTH); + for (i = 0; i < CRC_WIDTH; i++) { + if (crc & CRC_TOPBIT) { + crc = (crc << 1) ^ CRC_POLY; + } else { + crc <<= 1; + } + } + } + return crc; +} + +STATIC UINT32 OsWriteTlv(UINT8 *tlvBuf, UINT8 type, UINT8 len, UINT8 *value) //½«TLVÊý¾ÝдÈëTLV»º³åÇø// +{ + TraceMsgTlvBody *body = (TraceMsgTlvBody *)tlvBuf; + + if (len == 0) { + return 0; + } + + body->type = type; + body->len = len; + /* Do not check return value for performance, if copy failed, only this package will be discarded */ + (VOID)memcpy_s(body->value, len, value, len); + return len + sizeof(body->type) + sizeof(body->len); //·µ»ØÊµ¼ÊдÈëµÄ×Ö½ÚÊý// +} + +/*¸ù¾ÝTLV±íÖе͍Ò壬½«Ô´Êý¾Ý±àÂë³ÉTLV¸ñʽ²¢Ð´ÈëTLV»º³åÇø*/ +STATIC UINT32 OsTlvEncode(const TlvTable *table, UINT8 *srcBuf, UINT8 *tlvBuf, INT32 tlvBufLen) +{ + UINT32 len = 0; + const TlvTable *tlvTableItem = table; + + while (tlvTableItem->tag != TRACE_TLV_TYPE_NULL) { + if ((len + tlvTableItem->elemSize + sizeof(UINT8) + sizeof(UINT8)) > tlvBufLen) { + break; + } + len += OsWriteTlv(tlvBuf + len, tlvTableItem->tag, tlvTableItem->elemSize, srcBuf + tlvTableItem->elemOffset); + tlvTableItem++; + } + return len; +} + +/*½«Ô´Êý¾Ý°´ÕÕTLV±íµÄ¶¨Òå±àÂ룬²¢Ð´ÈëÄ¿±ê»º³åÇø*/ +UINT32 OsTraceDataEncode(UINT8 type, const TlvTable *table, UINT8 *src, UINT8 *dest, INT32 destLen) +{ + UINT16 crc; + INT32 len; + INT32 tlvBufLen; + UINT8 *tlvBuf = NULL; + + TraceMsgTlvHead *head = (TraceMsgTlvHead *)dest; + tlvBufLen = destLen - sizeof(TraceMsgTlvHead); + + if ((tlvBufLen <= 0) || (table == NULL)) { + return 0; + } + + tlvBuf = dest + sizeof(TraceMsgTlvHead); + len = OsTlvEncode(table, src, tlvBuf, tlvBufLen); + crc = CalcCrc16(tlvBuf, len); + + head->magicNum = TRACE_TLV_MSG_HEAD; + head->msgType = type; + head->len = len; + head->crc = crc; + return len + sizeof(TraceMsgTlvHead); +} + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ diff --git a/文豪/trace/pipeline/trace_tlv.h b/文豪/trace/pipeline/trace_tlv.h new file mode 100644 index 0000000..1871012 --- /dev/null +++ b/文豪/trace/pipeline/trace_tlv.h @@ -0,0 +1,95 @@ +/* ---------------------------------------------------------------------------- + * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. + * Description: LiteOS Trace Tlv Implementation HeadFile + * Author: Huawei LiteOS Team + * Create: 2020-03-16 + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * --------------------------------------------------------------------------- */ + +#ifndef _TRACE_TLV_H +#define _TRACE_TLV_H + +#include "los_typedef.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#define TRACE_TLV_MSG_HEAD 0xFF +#define TRACE_TLV_TYPE_NULL 0xFF + +typedef struct { + UINT8 magicNum; //ħÊõÊý×Ö// + UINT8 msgType; //ÏûÏ¢ÀàÐÍ// + UINT16 len; //ÏûÏ¢³¤¶È// + UINT16 crc; //CRCУÑéÂë// +} TraceMsgTlvHead; //TLVÏûÏ¢µÄÍ·²¿// + +typedef struct { + UINT8 type; //ÏûÏ¢ÀàÐÍ/// + UINT8 len; //ÏûÏ¢³¤¶È// + UINT8 value[]; //Êý¾ÝÄÚÈÝ// +} TraceMsgTlvBody; //TLVÏûÏ¢µÄÏûÏ¢Ìå// + +typedef struct { + UINT8 tag; //±êÇ©// + UINT8 elemOffset; //ÔªËØÆ«ÒÆ// + UINT8 elemSize; //ÔªËØ´óС// +} TlvTable; //TLV±íÏî// + +/** + * @ingroup los_trace + * @brief Encode trace raw data. + * + * @par Description: + * This API is used to encode trace raw data to tlv data. + * @attention + * + * + * @param type [IN] Type #UINT8. The type stands for different struct of src data. + * @param src [IN] Type #UINT8 *. The raw trace data. + * @param table [IN] Type #const TlvTable *. The tlv table descript elemOffset and elemSize. + * @param dest [OUT] Type #UINT8 *. The tlv data. + * @param destLen [IN] Type #UINT8 *. The tlv buf max len. + + * @retval #0 convert failed. + * @retval #UINT32 convert success bytes. + * + * @par Dependency: + * + * @see LOS_TraceDataEncode + * @since Huawei LiteOS V200R005C00 + */ +extern UINT32 OsTraceDataEncode(UINT8 type, const TlvTable *table, UINT8 *src, UINT8 *dest, INT32 destLen); +/*¸ù¾ÝTLV±íµÄÃèÊöÐÅÏ¢£¬½«Ô­Ê¼×·×ÙÊý¾Ý½øÐбàÂ룬Éú³ÉÏàÓ¦µÄTLVÊý¾Ý*/ +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#endif /* _TRACE_TLV_H */ \ No newline at end of file diff --git a/文豪/trace/trace_offline.c b/文豪/trace/trace_offline.c new file mode 100644 index 0000000..f23a43f --- /dev/null +++ b/文豪/trace/trace_offline.c @@ -0,0 +1,264 @@ +/* ---------------------------------------------------------------------------- + * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. + * Description: LiteOS Trace Offline Mode Implementation + * Author: Huawei LiteOS Team + * Create: 2020-03-31 + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * --------------------------------------------------------------------------- */ + +#include "los_trace_pri.h" +#include "trace_pipeline.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#ifdef LOSCFG_RECORDER_MODE_OFFLINE +#define BITS_NUM_FOR_TASK_ID 16 //Ö¸¶¨ÁËÈÎÎñIDµÄλÊý// + +LITE_OS_SEC_BSS STATIC TraceOfflineHeaderInfo g_traceRecoder; //ÓÃÀ´¼Ç¼ÀëÏ߸ú×ټǼµÄÏà¹ØÐÅÏ¢// +LITE_OS_SEC_BSS STATIC UINT32 g_tidMask[LOSCFG_BASE_CORE_TSK_LIMIT] = {0}; //ÓÃÓÚ´æ´¢ÈÎÎñIDµÄÑÚÂë// + +UINT32 OsTraceGetMaskTid(UINT32 tid) //ÓÃÓÚ»ñÈ¡ÑÚÂëºóµÄÈÎÎñID// +{ + return tid | ((tid < LOSCFG_BASE_CORE_TSK_LIMIT) ? g_tidMask[tid] << BITS_NUM_FOR_TASK_ID : 0); /* tid < 65535 */ +} + +UINT32 OsTraceBufInit(VOID *buf, UINT32 size) //ÓÃÓÚ³õʼ»¯ÀëÏ߸ú×Ù»º³åÇø// +{ + UINT32 headSize; + + headSize = sizeof(OfflineHead) + sizeof(ObjData) * LOSCFG_TRACE_OBJ_MAX_NUM; //ÓÃÓÚ´æ´¢ÀëÏ߸ú×ټǼͷ²¿ÐÅÏ¢// + if (size <= headSize) { + TRACE_ERROR("trace buf size not enough than 0x%x\n", headSize); + return LOS_ERRNO_TRACE_BUF_TOO_SMALL; + } + + if (buf == NULL) { + buf = LOS_MemAlloc(m_aucSysMem1, size); + if (buf == NULL) { + return LOS_ERRNO_TRACE_NO_MEMORY; + } + } + + (VOID)memset_s(buf, size, 0, size); + g_traceRecoder.head = (OfflineHead *)buf; + g_traceRecoder.head->baseInfo.bigLittleEndian = TRACE_BIGLITTLE_WORD; + g_traceRecoder.head->baseInfo.version = TRACE_VERSION(TRACE_MODE_OFFLINE); + g_traceRecoder.head->baseInfo.clockFreq = GET_SYS_CLOCK(); + g_traceRecoder.head->objSize = sizeof(ObjData); + g_traceRecoder.head->frameSize = sizeof(TraceEventFrame); + g_traceRecoder.head->objOffset = sizeof(OfflineHead); + g_traceRecoder.head->frameOffset = headSize; + g_traceRecoder.head->totalLen = size; + + g_traceRecoder.ctrl.curIndex = 0; + g_traceRecoder.ctrl.curObjIndex = 0; + g_traceRecoder.ctrl.maxObjCount = LOSCFG_TRACE_OBJ_MAX_NUM; + g_traceRecoder.ctrl.maxRecordCount = (size - headSize) / sizeof(TraceEventFrame); + g_traceRecoder.ctrl.objBuf = (ObjData *)((UINTPTR)buf + g_traceRecoder.head->objOffset); + g_traceRecoder.ctrl.frameBuf = (TraceEventFrame *)((UINTPTR)buf + g_traceRecoder.head->frameOffset); + + return LOS_OK; +} + +VOID OsTraceObjAdd(UINT32 eventType, UINT32 taskId) //ÓÃÓÚÏòÀëÏ߸ú×Ù»º³åÇøÖÐÌí¼Ó¶ÔÏóÊý¾Ý// +{ + UINT32 intSave; + UINT32 index; + ObjData *obj = NULL; + + TRACE_LOCK(intSave); + /* add obj begin */ + index = g_traceRecoder.ctrl.curObjIndex; + if (index >= LOSCFG_TRACE_OBJ_MAX_NUM) { /* do nothing when config LOSCFG_TRACE_OBJ_MAX_NUM = 0 */ + TRACE_UNLOCK(intSave); + return; + } + obj = &g_traceRecoder.ctrl.objBuf[index]; + + if (taskId < LOSCFG_BASE_CORE_TSK_LIMIT) { + g_tidMask[taskId]++; + } + + OsTraceSetObj(obj, OS_TCB_FROM_TID(taskId)); + + g_traceRecoder.ctrl.curObjIndex++; + if (g_traceRecoder.ctrl.curObjIndex >= g_traceRecoder.ctrl.maxObjCount) { + g_traceRecoder.ctrl.curObjIndex = 0; /* turn around */ + } + /* add obj end */ + TRACE_UNLOCK(intSave); +} + +VOID OsTraceWriteOrSendEvent(const TraceEventFrame *frame) //ÓÃÓÚÏòÀëÏ߸ú×Ù»º³åÇøÖÐдÈë»ò·¢ËÍʼþÖ¡Êý¾Ý// +{ + UINT16 index; + UINT32 intSave; + + TRACE_LOCK(intSave); + index = g_traceRecoder.ctrl.curIndex; + (VOID)memcpy_s(&g_traceRecoder.ctrl.frameBuf[index], sizeof(TraceEventFrame), frame, sizeof(TraceEventFrame)); + + g_traceRecoder.ctrl.curIndex++; + if (g_traceRecoder.ctrl.curIndex >= g_traceRecoder.ctrl.maxRecordCount) { + g_traceRecoder.ctrl.curIndex = 0; + } + TRACE_UNLOCK(intSave); +} + +VOID OsTraceReset(VOID) //ÓÃÓÚÖØÖÃÀëÏ߸ú×Ù»º³åÇø// +{ + UINT32 intSave; + UINT32 bufLen; + + TRACE_LOCK(intSave); + bufLen = sizeof(TraceEventFrame) * g_traceRecoder.ctrl.maxRecordCount; + (VOID)memset_s(g_traceRecoder.ctrl.frameBuf, bufLen, 0, bufLen); + g_traceRecoder.ctrl.curIndex = 0; + TRACE_UNLOCK(intSave); +} + +STATIC VOID OsTraceInfoObj(VOID) //ÓÃÓÚ´òÓ¡ÀëÏ߸ú×Ù¶ÔÏóÐÅÏ¢// +{ + UINT32 i; + ObjData *obj = &g_traceRecoder.ctrl.objBuf[0]; + + if (g_traceRecoder.ctrl.maxObjCount > 0) { + PRINTK("CurObjIndex = %u\n", g_traceRecoder.ctrl.curObjIndex); + PRINTK("Index TaskID TaskPrio TaskName \n"); + for (i = 0; i < g_traceRecoder.ctrl.maxObjCount; i++, obj++) { + PRINTK("%-7u 0x%-6x %-10u %s\n", i, obj->id, obj->prio, obj->name); + } + PRINTK("\n"); + } +} + +STATIC VOID OsTraceInfoEventTitle(VOID) //ÓÃÓÚ´òÓ¡ÀëÏ߸ú×Ùʼþ±êÌâ// +{ + PRINTK("CurEvtIndex = %u\n", g_traceRecoder.ctrl.curIndex); + + PRINTK("Index Time(cycles) EventType CurTask Identity "); +#ifdef LOSCFG_TRACE_FRAME_CORE_MSG + PRINTK("cpuId hwiActive taskLockCnt "); +#endif +#ifdef LOSCFG_TRACE_FRAME_EVENT_COUNT + PRINTK("eventCount "); +#endif + if (LOSCFG_TRACE_FRAME_MAX_PARAMS > 0) { + PRINTK("params "); + } + PRINTK("\n"); +} + +STATIC VOID OsTraceInfoEventData(VOID) //ÓÃÓÚ´òÓ¡ÀëÏ߸ú×ÙʼþÊý¾Ý// +{ + UINT32 i, j; + TraceEventFrame *frame = &g_traceRecoder.ctrl.frameBuf[0]; + + for (i = 0; i < g_traceRecoder.ctrl.maxRecordCount; i++, frame++) { + PRINTK("%-7u 0x%-15llx 0x%-12x 0x%-7x 0x%-11x ", i, frame->curTime, frame->eventType, + frame->curTask, frame->identity); +#ifdef LOSCFG_TRACE_FRAME_CORE_MSG + UINT32 taskLockCnt = frame->core.taskLockCnt; +#ifdef LOSCFG_KERNEL_SMP + /* + * For smp systems, TRACE_LOCK will requst taskLock, and this counter + * will increase by 1 in that case. + */ + taskLockCnt -= 1; +#endif + PRINTK("%-11u %-11u %-11u", frame->core.cpuId, frame->core.hwiActive, taskLockCnt); +#endif +#ifdef LOSCFG_TRACE_FRAME_EVENT_COUNT + PRINTK("%-11u", frame->eventCount); +#endif + for (j = 0; j < LOSCFG_TRACE_FRAME_MAX_PARAMS; j++) { + PRINTK("0x%-11x", frame->params[j]); + } + PRINTK("\n"); + } +} + +STATIC VOID OsTraceInfoDisplay(VOID) //ÓÃÓÚÏÔʾÀëÏ߸ú×ÙÐÅÏ¢// +{ + OfflineHead *head = g_traceRecoder.head; + + PRINTK("*******TraceInfo begin*******\n"); + PRINTK("clockFreq = %u\n", head->baseInfo.clockFreq); + + OsTraceInfoObj(); + + OsTraceInfoEventTitle(); + OsTraceInfoEventData(); + + PRINTK("*******TraceInfo end*******\n"); +} + +#ifdef LOSCFG_TRACE_CLIENT_INTERACT +STATIC VOID OsTraceSendInfo(VOID) //ÓÃÓÚ·¢ËÍÀëÏ߸ú×ÙÐÅÏ¢// +{ + UINT32 i; + ObjData *obj = NULL; + TraceEventFrame *frame = NULL; + + OsTraceDataSend(HEAD, sizeof(OfflineHead), (UINT8 *)g_traceRecoder.head); + + obj = &g_traceRecoder.ctrl.objBuf[0]; + for (i = 0; i < g_traceRecoder.ctrl.maxObjCount; i++) { + OsTraceDataSend(OBJ, sizeof(ObjData), (UINT8 *)(obj + i)); + } + + frame = &g_traceRecoder.ctrl.frameBuf[0]; + for (i = 0; i < g_traceRecoder.ctrl.maxRecordCount; i++) { + OsTraceDataSend(EVENT, sizeof(TraceEventFrame), (UINT8 *)(frame + i)); + } +} +#endif + +VOID OsTraceRecordDump(BOOL toClient) //ÓÃÓÚÊä³ö»ò·¢ËÍÀëÏ߸ú×ÙÐÅÏ¢// +{ + if (!toClient) { //ÓÃÓÚָʾÊÇÊä³ö»¹ÊÇ·¢ËÍÀëÏ߸ú×ÙÐÅÏ¢// + OsTraceInfoDisplay(); + return; + } + +#ifdef LOSCFG_TRACE_CLIENT_INTERACT + OsTraceSendInfo(); +#endif +} + +OfflineHead *OsTraceRecordGet(VOID) //ÓÃÓÚ»ñÈ¡ÀëÏ߸ú×ټǼµÄÍ·ÐÅÏ¢// +{ + return g_traceRecoder.head; +} + +#endif /* LOSCFG_RECORDER_MODE_OFFLINE */ + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ diff --git a/文豪/trace/trace_online.c b/文豪/trace/trace_online.c new file mode 100644 index 0000000..6faba44 --- /dev/null +++ b/文豪/trace/trace_online.c @@ -0,0 +1,117 @@ +/* ---------------------------------------------------------------------------- + * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. + * Description: LiteOS Trace Online Mode Implementation + * Author: Huawei LiteOS Team + * Create: 2020-03-31 + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * --------------------------------------------------------------------------- */ + +#include "los_trace_pri.h" +#include "trace_pipeline.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#ifdef LOSCFG_RECORDER_MODE_ONLINE +UINT32 OsTraceGetMaskTid(UINT32 taskId) //ÓÃÓÚ»ñÈ¡ÈÎÎñIDµÄÑÚÂë// +{ + return taskId; +} + +UINT32 OsTraceBufInit(VOID *buf, UINT32 size) //³õʼ»¯¸ú×Ù»º³åÇø// +{ + (VOID)buf; + (VOID)size; + return LOS_OK; +} + +VOID OsTraceSendHead(VOID) //·¢Ë͸ú×ÙÊý¾ÝµÄÍ·ÐÅÏ¢// +{ + TraceBaseHeaderInfo head = { + .bigLittleEndian = TRACE_BIGLITTLE_WORD, + .version = TRACE_VERSION(TRACE_MODE_ONLINE), + .clockFreq = GET_SYS_CLOCK(), + }; + + OsTraceDataSend(HEAD, sizeof(TraceBaseHeaderInfo), (UINT8 *)&head); +} + +VOID OsTraceSendNotify(UINT32 type, UINT32 value) //·¢ËÍ֪ͨÀàÐ͵ĸú×ÙÊý¾Ý// +{ + TraceNotifyFrame frame = { + .cmd = type, + .param = value, + }; + + OsTraceDataSend(NOTIFY, sizeof(TraceNotifyFrame), (UINT8 *)&frame); +} + +STATIC VOID OsTraceSendObj(const LosTaskCB *tcb) //·¢ËͶÔÏóÀàÐÍΪobjµÄ¸ú×ÙÊý¾Ý// +{ + ObjData obj; + + OsTraceSetObj(&obj, tcb); + OsTraceDataSend(OBJ, sizeof(ObjData), (UINT8 *)&obj); +} + +VOID OsTraceSendObjTable(VOID) //·¢ËͶÔÏó±íÀàÐ͵ĸú×ÙÊý¾Ý// +{ + UINT32 loop; + LosTaskCB *tcb = NULL; + + for (loop = 0; loop < g_taskMaxNum; ++loop) { + tcb = g_taskCBArray + loop; + if (tcb->taskStatus & OS_TASK_STATUS_UNUSED) { + continue; + } + OsTraceSendObj(tcb); + } +} + +VOID OsTraceObjAdd(UINT32 eventType, UINT32 taskId) //Ïò¸ú×ټǼÖÐÌí¼Ó¶ÔÏó// +{ + if (OsTraceIsEnable()) { + OsTraceSendObj(OS_TCB_FROM_TID(taskId)); + } +} + +VOID OsTraceWriteOrSendEvent(const TraceEventFrame *frame) //·¢ËÍʼþÊý¾Ý// +{ + OsTraceDataSend(EVENT, sizeof(TraceEventFrame), (UINT8 *)frame); +} + +OfflineHead *OsTraceRecordGet(VOID) //»ñÈ¡ÀëÏ߸ú×ټǼͷ// +{ + return NULL; //±íʾûÓÐÀëÏ߸ú×ټǼ¿ÉÓÃ// +} + +#endif /* LOSCFG_RECORDER_MODE_ONLINE */ + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */