simulate CPU load

nicolasmm
Posts: 7
Joined: Thu Sep 05, 2019 5:20 pm

simulate CPU load

Postby nicolasmm » Fri Jan 31, 2020 7:49 pm

Hello,
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);
}
But it isn't working properly, according to the log below, the load task didn't take too much to compute. ¿Do you have any suggestions?

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%

username
Posts: 477
Joined: Thu May 03, 2018 1:18 pm

Re: simulate CPU load

Postby username » Sat Feb 01, 2020 4:04 am

Code: Select all

#define SPIN_ITER           500000  //Actual CPU cycles used will depend on compiler optimization

static void spin_task(void *arg)
{
   
    while (1) {
        //Consume CPU cycles
        for (int i = 0; i < SPIN_ITER; i++) {
            __asm__ __volatile__("NOP");
        }
        vTaskDelay(pdMS_TO_TICKS(100));
    }
}

which comes from this example
https://github.com/espressif/esp-idf/bl ... ple_main.c

Who is online

Users browsing this forum: No registered users and 111 guests