I want to simulate CPU load, so I can test some modules in an isolated setting.
My current code looks like this:
Code: Select all
void TaskLoadSymulate(void *pvParameters) {
float load = *(float *)pvParameters;
float loop_time = 200;
int64_t busy_time = load * loop_time;
TickType_t free_time = (1 - load) * loop_time;
ESP_LOGI(TAG, "busy=%lld free=%u", busy_time, free_time);
while(1)
{
int64_t time = esp_timer_get_time();
int64_t wakeup = time + busy_time;
while(time < wakeup) {
time = esp_timer_get_time();
heap_caps_check_integrity_all(true);
}
vTaskDelay(free_time / portTICK_PERIOD_MS);
}
vTaskDelete(NULL);
}
void app_main(void)
{
float load = 0.5;
xTaskCreatePinnedToCore(TaskLoadSymulate, "load0", 1024 * 5, &load, configMAX_PRIORITIES, NULL, 0);
load = 0.1;
xTaskCreatePinnedToCore(TaskLoadSymulate, "load1", 1024 * 5, &load, configMAX_PRIORITIES, NULL, 1);
size_t buffer_stats_size = 4000;
char *buffer_stats = (char *)malloc(buffer_stats_size);
while(1)
{
vTaskGetRunTimeStats( buffer_stats);
ESP_LOGW(TAG, "Run time stats\tTaskNum[%02d]\t RAM:%d\n%s", uxTaskGetNumberOfTasks(), xPortGetFreeHeapSize(), buffer_stats);
vTaskDelay(pdMS_TO_TICKS(30000));
}
free(buffer_stats);
vTaskDelete(NULL);
}
Code: Select all
I (315) slave: busy=100 free=100
I (315) slave: busy=20 free=180
W (325) slave: Run time stats TaskNum[10] RAM:281108
load1 0 <1%
main 4903 8%
IDLE1 4160 7%
IDLE0 0 <1%
load0 4065 7%
Tmr Svc 27 <1%
dport 44 <1%
esp_timer 31 <1%
ipc1 22596 39%
ipc0 17655 30%
W (30355) slave: Run time stats TaskNum[09] RAM:282248
main 36594 <1%
IDLE1 30014586 49%
IDLE0 29955074 49%
load0 48177 <1%
load1 15356 <1%
Tmr Svc 56 <1%
ipc1 22596 <1%
ipc0 17655 <1%
esp_timer 31 <1%