From 7e2f261a58baab8d7a3494769e8a725d25410fc3 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Thu, 29 Sep 2022 22:04:24 +0200 Subject: [PATCH] freertos, app_trace, heap: use esp_cpu_get_cycle_count to get ccount portGET_RUN_TIME_COUNTER_VALUE should only be used in the kernel. --- .../Config/SEGGER_SYSVIEW_Config_FreeRTOS.c | 3 ++- .../performance/test_freertos_scheduling_time.c | 5 +++-- .../test/performance/test_isr_latency.c | 13 +++++++------ .../test_apps/main/test_allocator_timings.c | 17 +++++++++-------- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/components/app_trace/sys_view/Sample/Config/SEGGER_SYSVIEW_Config_FreeRTOS.c b/components/app_trace/sys_view/Sample/Config/SEGGER_SYSVIEW_Config_FreeRTOS.c index 9e70542d99..9e7a4aed47 100644 --- a/components/app_trace/sys_view/Sample/Config/SEGGER_SYSVIEW_Config_FreeRTOS.c +++ b/components/app_trace/sys_view/Sample/Config/SEGGER_SYSVIEW_Config_FreeRTOS.c @@ -74,6 +74,7 @@ Revision: $Rev: 3734 $ #include "esp_app_trace.h" #include "esp_app_trace_util.h" #include "esp_intr_alloc.h" +#include "esp_cpu.h" #include "soc/soc.h" #include "soc/interrupts.h" #include "esp_private/esp_clk.h" @@ -260,7 +261,7 @@ U32 SEGGER_SYSVIEW_X_GetTimestamp(void) gptimer_get_raw_count(s_sv_gptimer, &ts); return (U32) ts; // return lower part of counter value #elif TS_USE_CCOUNT - return portGET_RUN_TIME_COUNTER_VALUE(); + return esp_cpu_get_cycle_count(); #elif TS_USE_ESP_TIMER return (U32) esp_timer_get_time(); // return lower part of counter value #endif diff --git a/components/freertos/test/performance/test_freertos_scheduling_time.c b/components/freertos/test/performance/test_freertos_scheduling_time.c index cd231aeb86..238a58d538 100644 --- a/components/freertos/test/performance/test_freertos_scheduling_time.c +++ b/components/freertos/test/performance/test_freertos_scheduling_time.c @@ -11,6 +11,7 @@ #include "freertos/semphr.h" #include "freertos/queue.h" #include "esp_intr_alloc.h" +#include "esp_cpu.h" #include "unity.h" #include "test_utils.h" @@ -27,7 +28,7 @@ static void test_task_1(void *arg) { test_context_t *context = (test_context_t *)arg; for( ;; ) { - context->before_sched = portGET_RUN_TIME_COUNTER_VALUE(); + context->before_sched = esp_cpu_get_cycle_count(); vPortYield(); } @@ -43,7 +44,7 @@ static void test_task_2(void *arg) { vPortYield(); for(int i = 0; i < NUMBER_OF_ITERATIONS; i++) { - accumulator += (portGET_RUN_TIME_COUNTER_VALUE() - context->before_sched); + accumulator += (esp_cpu_get_cycle_count() - context->before_sched); vPortYield(); } diff --git a/components/freertos/test/performance/test_isr_latency.c b/components/freertos/test/performance/test_isr_latency.c index 79e4168600..02d50bc096 100644 --- a/components/freertos/test/performance/test_isr_latency.c +++ b/components/freertos/test/performance/test_isr_latency.c @@ -12,6 +12,7 @@ #include "freertos/semphr.h" #include "freertos/queue.h" #include "esp_intr_alloc.h" +#include "esp_cpu.h" #include "unity.h" #include "test_utils.h" #if CONFIG_IDF_TARGET_ARCH_XTENSA @@ -39,19 +40,19 @@ static uint32_t delta_exit_cycles = 0; static void software_isr_using_parameter_vportyield(void *arg) { (void)arg; BaseType_t yield; - delta_enter_cycles += portGET_RUN_TIME_COUNTER_VALUE() - cycle_before_trigger; + delta_enter_cycles += esp_cpu_get_cycle_count() - cycle_before_trigger; TEST_CLR_INT_MASK(1 << SW_ISR_LEVEL_1); xSemaphoreGiveFromISR(sync, &yield); portYIELD_FROM_ISR(yield); - cycle_before_exit = portGET_RUN_TIME_COUNTER_VALUE(); + cycle_before_exit = esp_cpu_get_cycle_count(); } static void software_isr_using_no_argument_vportyield(void *arg) { (void)arg; BaseType_t yield; - delta_enter_cycles += portGET_RUN_TIME_COUNTER_VALUE() - cycle_before_trigger; + delta_enter_cycles += esp_cpu_get_cycle_count() - cycle_before_trigger; TEST_CLR_INT_MASK(1 << SW_ISR_LEVEL_1); @@ -59,17 +60,17 @@ static void software_isr_using_no_argument_vportyield(void *arg) { if(yield) { portYIELD_FROM_ISR(); } - cycle_before_exit = portGET_RUN_TIME_COUNTER_VALUE(); + cycle_before_exit = esp_cpu_get_cycle_count(); } static void test_task(void *arg) { (void) arg; for(int i = 0;i < 10000; i++) { - cycle_before_trigger = portGET_RUN_TIME_COUNTER_VALUE(); + cycle_before_trigger = esp_cpu_get_cycle_count(); TEST_SET_INT_MASK(1 << SW_ISR_LEVEL_1); xSemaphoreTake(sync, portMAX_DELAY); - delta_exit_cycles += portGET_RUN_TIME_COUNTER_VALUE() - cycle_before_exit; + delta_exit_cycles += esp_cpu_get_cycle_count() - cycle_before_exit; } delta_enter_cycles /= 10000; diff --git a/components/heap/test_apps/main/test_allocator_timings.c b/components/heap/test_apps/main/test_allocator_timings.c index 7fe740a7a8..2b291d80ad 100644 --- a/components/heap/test_apps/main/test_allocator_timings.c +++ b/components/heap/test_apps/main/test_allocator_timings.c @@ -10,6 +10,7 @@ #include "esp_attr.h" #include "esp_heap_caps.h" #include "esp_log.h" +#include "esp_cpu.h" #include #include #include @@ -41,9 +42,9 @@ TEST_CASE("Heap many random allocations timings", "[heap]") */ size_t new_size = (uint32_t)rand() % 1024; - cycles_before = portGET_RUN_TIME_COUNTER_VALUE(); + cycles_before = esp_cpu_get_cycle_count(); void *new_p = heap_caps_realloc(p[n], new_size, MALLOC_CAP_DEFAULT); - realloc_time_average = portGET_RUN_TIME_COUNTER_VALUE() - cycles_before; + realloc_time_average = esp_cpu_get_cycle_count() - cycles_before; ESP_LOGD(TAG, "realloc %p -> %p (%zu -> %zu) time spent cycles: %lld \n", p[n], new_p, s[n], new_size, realloc_time_average); heap_caps_check_integrity(MALLOC_CAP_DEFAULT, true); @@ -66,9 +67,9 @@ TEST_CASE("Heap many random allocations timings", "[heap]") } TEST_ASSERT(heap_caps_check_integrity(MALLOC_CAP_DEFAULT, true)); - cycles_before = portGET_RUN_TIME_COUNTER_VALUE(); + cycles_before = esp_cpu_get_cycle_count(); heap_caps_free(p[n]); - free_time_average = portGET_RUN_TIME_COUNTER_VALUE() - cycles_before; + free_time_average = esp_cpu_get_cycle_count() - cycles_before; ESP_LOGD(TAG, "freed %p (%zu) time spent cycles: %lld\n", p[n], s[n], free_time_average); @@ -81,9 +82,9 @@ TEST_CASE("Heap many random allocations timings", "[heap]") s[n] = rand() % 1024; heap_caps_check_integrity(MALLOC_CAP_DEFAULT, true); - cycles_before = portGET_RUN_TIME_COUNTER_VALUE(); + cycles_before = esp_cpu_get_cycle_count(); p[n] = heap_caps_malloc(s[n], MALLOC_CAP_DEFAULT); - alloc_time_average = portGET_RUN_TIME_COUNTER_VALUE() - cycles_before; + alloc_time_average = esp_cpu_get_cycle_count() - cycles_before; ESP_LOGD(TAG, "malloc %p (%zu) time spent cycles: %lld \n", p[n], s[n], alloc_time_average); @@ -99,9 +100,9 @@ TEST_CASE("Heap many random allocations timings", "[heap]") } for (int i = 0; i < NUM_POINTERS; i++) { - cycles_before = portGET_RUN_TIME_COUNTER_VALUE(); + cycles_before = esp_cpu_get_cycle_count(); heap_caps_free( p[i]); - free_time_average = portGET_RUN_TIME_COUNTER_VALUE() - cycles_before; + free_time_average = esp_cpu_get_cycle_count() - cycles_before; if (!heap_caps_check_integrity(MALLOC_CAP_DEFAULT, true)) { printf("FAILED during cleanup after freeing %p\n", p[i]);