eric2013 发表于 2023-5-28 16:00:10

关于H7-TOOL高效检测RTOS任务栈使用率检测问题




现在部分RTOS需要使能宏定义才会检测,也有需要调用函数才可以检测,像uCOS-II这种是需要调用函数OSTaskStkChk来检测的。

如果用户开的任务比较多,任务栈也比较大,执行一轮的时间略多,C的代码如下:

/*
*********************************************************************************************************
*                                           STACK CHECKING
*
* Description: This function is called to check the amount of free memory left on the specified task's
*            stack.
*
* Arguments: prio          is the task priority
*
*            p_stk_data    is a pointer to a data structure of type OS_STK_DATA.
*
* Returns    : OS_ERR_NONE            upon success
*            OS_ERR_PRIO_INVALID    if the priority you specify is higher that the maximum allowed
*                                     (i.e. > OS_LOWEST_PRIO) or, you have not specified OS_PRIO_SELF.
*            OS_ERR_TASK_NOT_EXISTif the desired task has not been created or is assigned to a Mutex PIP
*            OS_ERR_TASK_OPT      if you did NOT specified OS_TASK_OPT_STK_CHK when the task was created
*            OS_ERR_PDATA_NULL      if 'p_stk_data' is a NULL pointer
*********************************************************************************************************
*/
#if (OS_TASK_STAT_STK_CHK_EN > 0u) && (OS_TASK_CREATE_EXT_EN > 0u)
INT8UOSTaskStkChk (INT8U         prio,
                     OS_STK_DATA*p_stk_data)
{
    OS_TCB    *ptcb;
    OS_STK    *pchk;
    INT32U   nfree;
    INT32U   size;
#if OS_CRITICAL_METHOD == 3u                           /* Allocate storage for CPU status register   */
    OS_CPU_SRcpu_sr = 0u;
#endif



#if OS_ARG_CHK_EN > 0u
    if (prio > OS_LOWEST_PRIO) {                     /* Make sure task priority is valid             */
      if (prio != OS_PRIO_SELF) {
            return (OS_ERR_PRIO_INVALID);
      }
    }
    if (p_stk_data == (OS_STK_DATA *)0) {            /* Validate 'p_stk_data'                        */
      return (OS_ERR_PDATA_NULL);
    }
#endif
    p_stk_data->OSFree = 0u;                           /* Assume failure, set to 0 size                */
    p_stk_data->OSUsed = 0u;
    OS_ENTER_CRITICAL();
    if (prio == OS_PRIO_SELF) {                        /* See if check for SELF                        */
      prio = OSTCBCur->OSTCBPrio;
    }
    ptcb = OSTCBPrioTbl;
    if (ptcb == (OS_TCB *)0) {                         /* Make sure task exist                         */
      OS_EXIT_CRITICAL();
      return (OS_ERR_TASK_NOT_EXIST);
    }
    if (ptcb == OS_TCB_RESERVED) {
      OS_EXIT_CRITICAL();
      return (OS_ERR_TASK_NOT_EXIST);
    }
    if ((ptcb->OSTCBOpt & OS_TASK_OPT_STK_CHK) == 0u) { /* Make sure stack checking option is set      */
      OS_EXIT_CRITICAL();
      return (OS_ERR_TASK_OPT);
    }
    nfree = 0u;
    size= ptcb->OSTCBStkSize;
    pchk= ptcb->OSTCBStkBottom;
    OS_EXIT_CRITICAL();
#if OS_STK_GROWTH == 1u
    while (*pchk++ == (OS_STK)0) {                  /* Compute the number of zero entries on the stk */
      nfree++;
    }
#else
    while (*pchk-- == (OS_STK)0) {
      nfree++;
    }
#endif
    p_stk_data->OSFree = nfree;                     /* Store   number of free entries on the stk   */
    p_stk_data->OSUsed = size - nfree;                /* Compute number of entries used on the stk   */
    return (OS_ERR_NONE);
}
#endif

而H7-TOOL的RTOS Trace功能去检索的话,初步是打算采样此贴的方案,但是效率不够高,后面得找个更高效的方法


H7-TOOL检索程序系统stack和heap使用情况,方便大家使用了malloc和中断嵌套情况的监测(2023-05-03)
https://www.armbbs.cn/forum.php?mod=viewthread&tid=119024


https://img.anfulai.cn/dz/attachment/forum/202305/03/094956fz28268tcaxanr28.png



页: [1]
查看完整版本: 关于H7-TOOL高效检测RTOS任务栈使用率检测问题