From 8936eb32db8351cbb8b6593df7308b55f2f93504 Mon Sep 17 00:00:00 2001 From: p2r3 Date: Wed, 20 Aug 2025 15:21:02 +0300 Subject: [PATCH] make esp task yielding more consistent and reduce overhead --- include/globals.h | 8 ++------ src/CMakeLists.txt | 2 +- src/globals.c | 18 ++++++++++++++++++ src/main.c | 14 +++++++------- src/packets.c | 12 ++++++------ src/tools.c | 5 ++--- 6 files changed, 36 insertions(+), 23 deletions(-) diff --git a/include/globals.h b/include/globals.h index 7d24b38..74fe1f0 100644 --- a/include/globals.h +++ b/include/globals.h @@ -7,13 +7,9 @@ #ifdef ESP_PLATFORM #define WIFI_SSID "your-ssid" #define WIFI_PASS "your-password" - #include "esp_task_wdt.h" - #define wdt_reset(); \ - esp_task_wdt_reset(); \ - vTaskDelay(1); \ - esp_task_wdt_reset(); + void task_yield (); #else - #define wdt_reset(); + #define task_yield(); #endif #define true 1 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4d8b581..bfd5890 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,5 +4,5 @@ FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*) idf_component_register(SRCS ${app_sources} - PRIV_REQUIRES esp_wifi nvs_flash + PRIV_REQUIRES esp_wifi nvs_flash esp_timer INCLUDE_DIRS ".") diff --git a/src/globals.c b/src/globals.c index 831946c..0046632 100644 --- a/src/globals.c +++ b/src/globals.c @@ -5,6 +5,24 @@ #include "globals.h" +#ifdef ESP_PLATFORM + #include "esp_task_wdt.h" + #include "esp_timer.h" + + // Time between vTaskDelay calls in microseconds + #define TASK_YIELD_INTERVAL 1000 * 1000 + // How many ticks to delay for on each yield + #define TASK_YIELD_TICKS 1 + + int64_t last_yield = 0; + void task_yield () { + int64_t time_now = esp_timer_get_time(); + if (time_now - last_yield < TASK_YIELD_INTERVAL) return; + vTaskDelay(TASK_YIELD_TICKS); + last_yield = time_now; + } +#endif + ssize_t recv_count; uint8_t recv_buffer[256] = {0}; diff --git a/src/main.c b/src/main.c index d4a06da..e3936be 100644 --- a/src/main.c +++ b/src/main.c @@ -16,6 +16,7 @@ #include "esp_wifi.h" #include "esp_event.h" #include "esp_task_wdt.h" + #include "esp_timer.h" #include "lwip/sockets.h" #include "lwip/netdb.h" #else @@ -97,7 +98,7 @@ void handlePacket (int client_fd, int length, int packet_id) { sc_synchronizePlayerPosition(client_fd, spawn_x, spawn_y, spawn_z, spawn_yaw, spawn_pitch); - wdt_reset(); + task_yield(); for (uint8_t i = 0; i < 41; i ++) { sc_setContainerSlot(client_fd, 0, serverSlotToClientSlot(0, i), player->inventory_count[i], player->inventory_items[i]); @@ -115,7 +116,7 @@ void handlePacket (int client_fd, int length, int packet_id) { sc_startWaitingForChunks(client_fd); sc_setCenterChunk(client_fd, _x, _z); - wdt_reset(); + task_yield(); // Send spawn chunk first sc_chunkDataAndUpdateLight(client_fd, _x, _z); @@ -128,7 +129,7 @@ void handlePacket (int client_fd, int length, int packet_id) { // Re-synchronize player position after all chunks have been sent sc_synchronizePlayerPosition(client_fd, spawn_x, spawn_y, spawn_z, spawn_yaw, spawn_pitch); - wdt_reset(); + task_yield(); // Register all existing players and spawn their entities, and broadcast // information about the joining player to all existing players. @@ -398,7 +399,8 @@ int main () { * client connection. */ while (true) { - if (client_count == 0) { wdt_reset(); } + // Check if it's time to yield to the idle task + task_yield(); // Attempt to accept a new connection for (int i = 0; i < MAX_PLAYERS; i ++) { @@ -473,8 +475,6 @@ int main () { disconnectClient(&clients[client_index], 4); continue; } - // Reset watchdog and yield between packets - wdt_reset(); } @@ -486,7 +486,6 @@ int main () { #ifdef ESP_PLATFORM void bareiron_main (void *pvParameters) { - esp_task_wdt_add(NULL); main(); vTaskDelete(NULL); } @@ -528,6 +527,7 @@ void wifi_init () { } void app_main () { + esp_timer_early_init(); wifi_init(); } diff --git a/src/packets.c b/src/packets.c index 4b13481..c45ca83 100644 --- a/src/packets.c +++ b/src/packets.c @@ -308,8 +308,8 @@ int sc_chunkDataAndUpdateLight (int client_fd, int _x, int _z) { writeByte(client_fd, 0); // biome bits writeByte(client_fd, 0); // biome palette } - // reset watchdog and yield - wdt_reset(); + // yield to idle task + task_yield(); // send chunk sections for (int i = 0; i < 20; i ++) { @@ -325,8 +325,8 @@ int sc_chunkDataAndUpdateLight (int client_fd, int _x, int _z) { // biome data writeByte(client_fd, 0); // bits per entry writeByte(client_fd, W_plains); // biome palette - // reset watchdog and yield - wdt_reset(); + // yield to idle task + task_yield(); } // send 8 chunk sections (up to Y=192) with no blocks @@ -337,8 +337,8 @@ int sc_chunkDataAndUpdateLight (int client_fd, int _x, int _z) { writeByte(client_fd, 0); // biome bits writeByte(client_fd, 0); // biome palette } - // reset watchdog and yield - wdt_reset(); + // yield to idle task + task_yield(); writeVarInt(client_fd, 0); // omit block entities diff --git a/src/tools.c b/src/tools.c index e06bb79..7effd75 100644 --- a/src/tools.c +++ b/src/tools.c @@ -47,8 +47,7 @@ ssize_t recv_all (int client_fd, void *buf, size_t n, uint8_t require_first) { ssize_t r = recv(client_fd, p + total, n - total, 0); if (r < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) { - // spin until data arrives - wdt_reset(); + task_yield(); continue; } else { return -1; // real error @@ -78,7 +77,7 @@ ssize_t send_all (int client_fd, const void *buf, size_t len) { return -1; } if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) { - wdt_reset(); + task_yield(); continue; } return -1;