FreeRTOS任务运行时间查看

  • Post author:
  • Post category:其他


有时需要监测系统运行时各个任务抢占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.任务运行时间检测机制分析



版权声明:本文为Tommy666666原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。