FreeRTOS任务运行时间查看

有时需要监测系统运行时各个任务抢占cpu执行时间比例,RTOS提供了vTaskGetRunTimeStats接口获取

1 <FreeRTOSConfig.h>配置运行信息需要定义的宏

#define configUSE_TRACE_FACILITY                        1
#define configUSE_STATS_FORMATTING_FUNCTIONS            1   //uxTaskGetSystemState
#define configGENERATE_RUN_TIME_STATS                   1
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS()        os_run_timer_init()
#define portGET_RUN_TIME_COUNTER_VALUE()                os_run_timer_cnt_get()

2. 定义任务运行定时器,定时时间精度起码小于OS调度的tick十分之一,这样采集的时间才会比较准确--本例中采用的时GDF32450的Timer2

uint32_t volatile run_timer_cnt = 0;

void os_run_timer_init(void)
{
    timer_parameter_struct timer_initpara;
    rcu_periph_clock_enable(RCU_TIMER2);
    rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
    timer_deinit(TIMER2);

    timer_initpara.prescaler         = 120 - 1;     //120MHz -- 1us
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 10 - 1;      //load value: 10us
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER2, &timer_initpara);

    timer_interrupt_flag_clear(TIMER2, TIMER_INT_UP);
    timer_interrupt_enable(TIMER2, TIMER_INT_UP);
    nvic_irq_enable(TIMER2_IRQn, 3, 0);
    timer_enable(TIMER2);
}

void TIMER2_IRQHandler(void)
{
    if (timer_interrupt_flag_get(TIMER2, TIMER_INT_UP) != RESET) {
        timer_interrupt_flag_clear(TIMER2, TIMER_INT_UP);
        run_timer_cnt++;
    }
}

uint32_t os_run_timer_cnt_get(void)
{
    return run_timer_cnt;
}

3.使用两个任务作为运行任务,一个任务作为运行时间监测任务

static void func(void *para)
{
    while(1) {
        printf("%s work............\n", (char *)para);
        vTaskDelay(100 * portTICK_RATE_MS);
    }
}

static void monitor_func(void *para)
{
    static char monitor_log[1024];
    static char monitor_log1[1024];

    while(1) {
        vTaskDelay(1000 * portTICK_RATE_MS);  
        vTaskGetRunTimeStats((char *)monitor_log);
        printf("task_name\t\trun_times/100us\tpercentage\r\n");
        printf("%s\r\n", monitor_log);
    }
}


void os_runtime_calc(void)
{
    xTaskCreate((TaskFunction_t)func, "task1", 256, "task1", 2, &task1);
    xTaskCreate((TaskFunction_t)func, "task2", 256, "task2", 2, &task2);
    xTaskCreate((TaskFunction_t)monitor_func, "task_monitor", 256, "monitor", 3, &task_monitor);
    vTaskStartScheduler();
}

4.运行结果

 

5.任务运行时间检测机制分析

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值