Page 1 of 1

Efficient use of vTaskList and vTaskGetRunTimeStats

Posted: Wed Apr 10, 2024 9:16 am
by HitecSmartHome
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?

Re: Efficient use of vTaskList and vTaskGetRunTimeStats

Posted: Thu Apr 11, 2024 12:43 am
by boarchuz

Re: Efficient use of vTaskList and vTaskGetRunTimeStats

Posted: Thu Apr 11, 2024 6:26 am
by HitecSmartHome
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.