• ucos iii 任务栈使用率统计方法


    第一步、使能任务统计功能

    修改文件 os_cfg.h中的 OS_CFG_STAT_TASK_STK_CHK_EN宏。

                                                               /* -------------------------- TASK MANAGEMENT -------------------------- */
    #define OS_CFG_STAT_TASK_EN             DEF_ENABLED        /* Enable (DEF_ENABLED) the statistics task                              */
    #define OS_CFG_STAT_TASK_STK_CHK_EN     DEF_ENABLED        /*     Check task stacks (DEF_ENABLED) from the statistic task           */
    
    • 1
    • 2
    • 3

    第二步、创建任务时开启任务统计功能

    创建任务函数如下:

    void  OSTaskCreate (OS_TCB        *p_tcb,
                        CPU_CHAR const*p_name,
                        OS_TASK_PTR    p_task,
                        void          *p_arg,
                        OS_PRIO        prio,
                        CPU_STK       *p_stk_base,
                        CPU_STK_SIZE   stk_limit,
                        CPU_STK_SIZE   stk_size,
                        OS_MSG_QTY     q_size,
                        OS_TICK        time_quanta,
                        void          *p_ext,
                        OS_OPT         opt,
                        OS_ERR        *p_err)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    倒数第二个形参opt,参数可选择如下:

    *              opt            contains additional information (or options) about the behavior of the task.
    *                             See OS_OPT_TASK_xxx in OS.H.  Current choices are:
    *
    *                                 OS_OPT_TASK_NONE            No option selected
    *                                 OS_OPT_TASK_STK_CHK         Stack checking to be allowed for the task
    *                                 OS_OPT_TASK_STK_CLR         Clear the stack when the task is created
    *                                 OS_OPT_TASK_SAVE_FP         If the CPU has floating-point registers, save them
    *                                                             during a context switch.
    *                                 OS_OPT_TASK_NO_TLS          If the caller doesn't want or need TLS (Thread Local
    *                                                             Storage) support for the task.  If you do not include this
    *                                                             option, TLS will be supported by default.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    则在创建任务时加入 OS_OPT_TASK_STK_CHK标志,则任务在创建时会开启统计功能。

    第三步、获取任务统计信息

    函数如下:

    /*
    ************************************************************************************************************************
    *                                                    STACK CHECKING
    *
    * Description: This function is called to calculate the amount of free memory left on the specified task's stack.
    *
    * Arguments  : p_tcb       is a pointer to the TCB of the task to check.  If you specify a NULL pointer then
    *                          you are specifying that you want to check the stack of the current task.
    *
    *              p_free      is a pointer to a variable that will receive the number of free 'entries' on the task's stack.
    *
    *              p_used      is a pointer to a variable that will receive the number of used 'entries' on the task's stack.
    *
    *              p_err       is a pointer to a variable that will contain an error code.
    *
    *                              OS_ERR_NONE               Upon success
    *                              OS_ERR_PTR_INVALID        If either 'p_free' or 'p_used' are NULL pointers
    *                              OS_ERR_TASK_NOT_EXIST     If the stack pointer of the task is a NULL pointer
    *                              OS_ERR_TASK_OPT           If you did NOT specified OS_OPT_TASK_STK_CHK when the task
    *                                                        was created
    *                              OS_ERR_TASK_STK_CHK_ISR   You called this function from an ISR
    *
    * Returns    : none
    *
    * Note(s)    : none
    ************************************************************************************************************************
    */
    
    void  OSTaskStkChk (OS_TCB        *p_tcb,
                        CPU_STK_SIZE  *p_free,
                        CPU_STK_SIZE  *p_used,
                        OS_ERR        *p_err);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • p_tcb:所需信息的任务控制块
    • p_free:指针变量,保存当前任务控制块的剩余任务栈
    • p_used:指针变量,保存当前任务控制块的使用任务栈

    p_free和p_used的和就是创建任务时设置的任务栈总大小。

  • 相关阅读:
    【算法与数据结构】前言
    浅谈食品加工厂能耗情况分析平台解决方案
    网络流量预测工具
    进销存软件哪个好?
    【FPGA教程案例77】通信案例3——数据组帧,帧同步、拆帧
    [中秋特别定制版本]绝美登录页面搭配[登录数据存储到服务器](服务器宝塔数据库开通+短信服务开通+后端redis验证码缓存)
    发明专利和实用新型专利的根本区别
    2022-08-29 第五组 张明敏 学习笔记
    Centos7.9安装PHP8
    马赫数相关函数
  • 原文地址:https://blog.csdn.net/niu_88/article/details/126514696