Efficient use of vTaskList and vTaskGetRunTimeStats

HitecSmartHome
Posts: 11
Joined: Mon Mar 18, 2024 9:22 am

Efficient use of vTaskList and vTaskGetRunTimeStats

Postby HitecSmartHome » Wed Apr 10, 2024 9:16 am

Hello guys!

I want to gather data about my tasks into a JsonDocument with ArduinoJson.
The problem I'm facing is that vTaskList and vTaskGetRunTimeStats does not provide any struct or usable variables to retrive information.

My thiking would be something like this

Code: Select all

//retrive vTaskList into a struct or something
task_list_struct taskInfos = getTaskList();
// Add info to a JsonDocument
JsonDocument taskInfo;
taskInfo["name"] = taskInfos.name;
Or it would be even better if we got an interable list of task infos or something.

This works:

Code: Select all

char taskListBuff[2048] = {0};
char taskRunTimeStatsBuff[2048] = {0};
vTaskList(taskListBuff);
vTaskGetRunTimeStats(taskRunTimeStatsBuff);
printf("vTaskList: %s\n", taskListBuff);
printf("vTaskGetRunTimeStats: %s\n", taskRunTimeStatsBuff);
But it is not efficient. I don't want to print it, I want to send it.

Does anyone know how to do that? How does vTaskList and vTaskGetRunTimeStats gets the data?


HitecSmartHome
Posts: 11
Joined: Mon Mar 18, 2024 9:22 am

Re: Efficient use of vTaskList and vTaskGetRunTimeStats

Postby HitecSmartHome » Thu Apr 11, 2024 6:26 am

I solved it eventually

Code: Select all

void TaskInfo::get() {
    UBaseType_t uxArraySize;
    TaskStatus_t *taskStatusArray;
    uint32_t ulTotalRunTime,ulStatsAsPercentage;
    uxArraySize = uxTaskGetNumberOfTasks();
    taskStatusArray = (TaskStatus_t *)pvPortMalloc(uxArraySize * sizeof(TaskStatus_t));
    if (taskStatusArray != NULL) {
        uxArraySize = uxTaskGetSystemState(taskStatusArray, uxArraySize, &ulTotalRunTime);
        ulTotalRunTime /= 100UL;

        for (UBaseType_t i = 0; i < uxArraySize; i++) {
            ulStatsAsPercentage = taskStatusArray[i].ulRunTimeCounter / ulTotalRunTime;
            printf("Name: %s\n",taskStatusArray[i].pcTaskName);
            printf("Run time percentage: %d\n",ulStatsAsPercentage);
        }

        vPortFree(taskStatusArray);
    }
}
This method prints out all the task names and run time in percentages compared to the global runtime.

Who is online

Users browsing this forum: No registered users and 69 guests