How to stop FreeRTOS from getting in my way?

spikeyamk
Posts: 2
Joined: Mon Nov 27, 2023 3:36 pm

How to stop FreeRTOS from getting in my way?

Postby spikeyamk » Mon Nov 27, 2023 3:58 pm

Hello,

I'm using ESP32-C6-DevkitC-1 and ESP-IDF and C++. Every time I write some piece of code and it takes some time to compute the FreeRTOS just complains in the Serial console output. The only solution to this problem I found if I put somewhere a 10 ms delay it will stop complaining.

So I went ahead into the idf.py menuconfig and disable all the log error messages and even then it was complaining about that it was trying to do a Task Switch and it couldn't because one of the tasks is blocking the Task Switch because it was still computing something.

But I didn't create any tasks at all. The code was run from the main function so I'm just confused as how to use FreeRTOS effectively and to full speed, because I cannot afford to put 10 ms delays everywhere.

Are these FreeRTOS error messages even something to worry about?

So I wrote this to test things out:
  1. #include <iostream>
  2. #include <cstdint>
  3. #include <chrono>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. std::chrono::milliseconds prime_test(const uint32_t stopper) {
  8.     const auto start = std::chrono::high_resolution_clock::now();
  9.     auto end = start;
  10.     for(uint32_t number = 2; number < stopper; ) {
  11.         const auto chunk_start = std::chrono::high_resolution_clock::now();
  12.         std::vector<uint32_t> primes;
  13.         primes.reserve(512);
  14.         size_t index = 0;
  15.         for(; number < stopper; number++) {
  16.             uint32_t divisor = (number - 1);
  17.             for(; divisor > 1; divisor--) {
  18.                 if((number % divisor) == 0) {
  19.                     break;
  20.                 }
  21.             }
  22.             if(divisor == 1) {
  23.                 try {
  24.                     primes.push_back(number);
  25.                 } catch(...) {
  26.                     std::cerr << "ERROR: prime_test(" << stopper << "): failed to primes.push_back(number)\n";
  27.                     break;
  28.                 }
  29.             }
  30.         }
  31.         const auto chunk_end = std::chrono::high_resolution_clock::now();
  32.         end += (chunk_end - chunk_start);
  33.  
  34.         std::find_if(primes.begin(), primes.end(), [print_index = 0, index](const uint32_t element) mutable {
  35.             if(index == 0 || element != 0) {
  36.                 std::printf("primes[%zu]: %lu\n", print_index++, element);
  37.                 return false;
  38.             } else {
  39.                 return true;
  40.             }
  41.         });
  42.  
  43.         std::cout << "prime_test(): calcation chunk duration: " << chunk_end - chunk_start << std::endl;
  44.     }
  45.     return std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
  46. }
  47.  
  48. extern "C" void app_main() {
  49.     std::cout << "prime_test(40'000): " << prime_test(40'000) << std::endl;;
  50. }
which outputs: prime_test(40'000): 46921ms

Can somebody please try and run this and confirm to me if I'm doing something wrong with FreeRTOS. Is this normal performance for an EPS32C6 at default 160 MHz clock? Can I improve the performance somehow without changing the code by configuring FreeRTOS somehow? By changing the tickrate of FreeRTOS? Anyone? Please help.

Thanks in advance!

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: How to stop FreeRTOS from getting in my way?

Postby ESP_Sprite » Tue Nov 28, 2023 2:16 am

You probably want to disable the task watchdog in ESP-IDF menuconfig. Normally it's not good to hog the CPU for a long time, but if you're doing a synthetic benchmark, it should be OK. Also note t81749hat FreeRTOS housekeeping probably adds a bit to the CPU load, but it should be less than a percent. It's probably a better idea to run the test twice; you'll get better results the 2nd time as the caches will be all warmed up.

MicroController
Posts: 1707
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: How to stop FreeRTOS from getting in my way?

Postby MicroController » Tue Nov 28, 2023 11:42 pm

Can I improve the performance somehow without changing the code by configuring FreeRTOS somehow?
Make sure you select to compile for speed/performance ("-O2") and not "default"/debug ("-Og").

MicroController
Posts: 1707
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: How to stop FreeRTOS from getting in my way?

Postby MicroController » Tue Nov 28, 2023 11:58 pm

And, please, do not write code like this

Code: Select all

    for(uint32_t number = 2; number < stopper; ) {
        ...
        for(; number < stopper; number++) {

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 141 guests