How to get execution time of each task?

Hello everyone,

I'm working on a project where I have three tasks that need to be executed in a specific order. I want to optimize the performance of my code . One way to do this is to measure the execution time of each task.

My question is: how can I get the execution time of each task in my terminal? Is there a built-in function or library that can help me with this?

I'm working with freeRTOS and ESP32, but any general advice or specific solutions are welcome.

/* Include FreeRTOS APIs and defines */
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"

#define LED1_PIN 2
#define LED2_PIN 4
#define LED3_PIN 5

void task1(void *pvParameters) {
  while (1) {
    digitalWrite(LED1_PIN, HIGH);
    vTaskDelay(2000 / portTICK_PERIOD_MS);
    digitalWrite(LED1_PIN, LOW);
    vTaskDelay(2000 / portTICK_PERIOD_MS);
  }
}

void task2(void *pvParameters) {
  while (1) {
    digitalWrite(LED2_PIN, HIGH);
    vTaskDelay(500 / portTICK_PERIOD_MS);
    digitalWrite(LED2_PIN, LOW);
    vTaskDelay(500 / portTICK_PERIOD_MS);
  }
}

void task3(void *pvParameters) {
  while (1) {
    digitalWrite(LED3_PIN, HIGH);
    vTaskDelay(250 / portTICK_PERIOD_MS);
    digitalWrite(LED3_PIN, LOW);
    vTaskDelay(250 / portTICK_PERIOD_MS);
  }
}

void setup() {
  pinMode(LED1_PIN, OUTPUT);
  pinMode(LED2_PIN, OUTPUT);
  pinMode(LED3_PIN, OUTPUT);

  xTaskCreate(task1, "Task 1", 1000, NULL, 1, NULL);
  xTaskCreate(task2, "Task 2", 1000, NULL, 1, NULL);
  xTaskCreate(task3, "Task 3", 1000, NULL, 1, NULL);

 // vTaskStartScheduler();
}

void loop() {
  // Do nothing here.
}

Thank you in advance for your help!

It's a complicated question.
For the task you quoted, are you looking for:

  1. The task runs forever, because of the while(1) loop.
  2. The task runs for 2*500/portTICK_PERIOD_MS (+epsilon), because that's the whole task.
  3. The task runs for about the duration of the digitalWrite() call, because the vTaskDelay() blocks (allows another task to run.)

Usually, you'd mostly be interested in the maximum time that a task would run before blocking or being preempted (assuming you're running freertos in preemptive mode.)

Useful concepts are "idle time" - the amount of time that there are no runnable tasks (they are all blocking) and "load average" (so it was called, back in the mainframe days) - the number of tasks that are currently runnable, but can't because some other task is running. There is also the max latency - if a tasks (low priority) runs "all the time", and immediately blocks, how often does it actually get run? (sometimes the scheduler itself will track this, although it gets complicated if there are multiple task priorities.)

Then I'd assure that order by using some type of interlock / synchronization between the tasks ... eg. Notifications, Semaphores, etc.

For execution time save mills in a unsigned long integer When finished subtract the save time from the current mills. I have also used a logic analyzer and toggle a pin when entering and editing a routine.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.