在esp32运行了很多任务的时候,往往需要查看各个任务所占用的资源消耗,具体方法如下:
1,工程代码在esp-idf环境能够正常编译的情况下:在终端进入idf.py menuconfig ->Component config
2,选择FreeRTOS
3,勾选相关配置
下面4个选项都要勾选上,不然会报错 undefined reference to `vTaskList 和
undefined reference to vTaskGetRunTimeStats
4,在代码中调用相关接口打印任务的信息
while (1)
{
char InfoBuffer[512] = {0};
memset(InfoBuffer, 0, 512); // 信息缓冲区清零
vTaskList((char *)&InfoBuffer);
printf("任务名 任务状态 优先级 剩余栈 任务序号 cpu核\r\n");
printf("\r\n%s\r\n", InfoBuffer);
#if 1
char CPU_RunInfo[400] = {0}; // 保存任务运行时间信息
memset(CPU_RunInfo, 0, 400); // 信息缓冲区清零
vTaskGetRunTimeStats((char *)&CPU_RunInfo);
printf("任务名 运行计数 使用率\r\n");
printf("%s", CPU_RunInfo);
printf("---------------------------------------------\r\n\n");
#endif
//打印剩余ram和堆容量
printf("IDLE: ****free internal ram %d all heap size: %d Bytes****\n", heap_caps_get_free_size(MALLOC_CAP_INTERNAL), heap_caps_get_free_size(MALLOC_CAP_8BIT));
printf("IDLE: ****free SPIRAM size: %d Bytes****\n", heap_caps_get_free_size(MALLOC_CAP_SPIRAM));
vTaskDelay((10000) / portTICK_PERIOD_MS);
}
5,打印结果:
由于esp32是双核cpu,所以cpu核0代表内核0,cpu核1代表内核1,cpu核-1代表内核不固定。
任务状态中X代表运行态,R代表绪态,B代表阻塞态,S代表挂起态