1
0
mirror of https://github.com/p2r3/bareiron.git synced 2025-10-02 07:35:08 +02:00

make esp task yielding more consistent and reduce overhead

This commit is contained in:
p2r3
2025-08-20 15:21:02 +03:00
parent 5f3dcc56f9
commit 8936eb32db
6 changed files with 36 additions and 23 deletions

View File

@@ -7,13 +7,9 @@
#ifdef ESP_PLATFORM #ifdef ESP_PLATFORM
#define WIFI_SSID "your-ssid" #define WIFI_SSID "your-ssid"
#define WIFI_PASS "your-password" #define WIFI_PASS "your-password"
#include "esp_task_wdt.h" void task_yield ();
#define wdt_reset(); \
esp_task_wdt_reset(); \
vTaskDelay(1); \
esp_task_wdt_reset();
#else #else
#define wdt_reset(); #define task_yield();
#endif #endif
#define true 1 #define true 1

View File

@@ -4,5 +4,5 @@
FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*) FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*)
idf_component_register(SRCS ${app_sources} idf_component_register(SRCS ${app_sources}
PRIV_REQUIRES esp_wifi nvs_flash PRIV_REQUIRES esp_wifi nvs_flash esp_timer
INCLUDE_DIRS ".") INCLUDE_DIRS ".")

View File

@@ -5,6 +5,24 @@
#include "globals.h" #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; ssize_t recv_count;
uint8_t recv_buffer[256] = {0}; uint8_t recv_buffer[256] = {0};

View File

@@ -16,6 +16,7 @@
#include "esp_wifi.h" #include "esp_wifi.h"
#include "esp_event.h" #include "esp_event.h"
#include "esp_task_wdt.h" #include "esp_task_wdt.h"
#include "esp_timer.h"
#include "lwip/sockets.h" #include "lwip/sockets.h"
#include "lwip/netdb.h" #include "lwip/netdb.h"
#else #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); 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 ++) { for (uint8_t i = 0; i < 41; i ++) {
sc_setContainerSlot(client_fd, 0, serverSlotToClientSlot(0, i), player->inventory_count[i], player->inventory_items[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_startWaitingForChunks(client_fd);
sc_setCenterChunk(client_fd, _x, _z); sc_setCenterChunk(client_fd, _x, _z);
wdt_reset(); task_yield();
// Send spawn chunk first // Send spawn chunk first
sc_chunkDataAndUpdateLight(client_fd, _x, _z); 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 // Re-synchronize player position after all chunks have been sent
sc_synchronizePlayerPosition(client_fd, spawn_x, spawn_y, spawn_z, spawn_yaw, spawn_pitch); 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 // Register all existing players and spawn their entities, and broadcast
// information about the joining player to all existing players. // information about the joining player to all existing players.
@@ -398,7 +399,8 @@ int main () {
* client connection. * client connection.
*/ */
while (true) { 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 // Attempt to accept a new connection
for (int i = 0; i < MAX_PLAYERS; i ++) { for (int i = 0; i < MAX_PLAYERS; i ++) {
@@ -473,8 +475,6 @@ int main () {
disconnectClient(&clients[client_index], 4); disconnectClient(&clients[client_index], 4);
continue; continue;
} }
// Reset watchdog and yield between packets
wdt_reset();
} }
@@ -486,7 +486,6 @@ int main () {
#ifdef ESP_PLATFORM #ifdef ESP_PLATFORM
void bareiron_main (void *pvParameters) { void bareiron_main (void *pvParameters) {
esp_task_wdt_add(NULL);
main(); main();
vTaskDelete(NULL); vTaskDelete(NULL);
} }
@@ -528,6 +527,7 @@ void wifi_init () {
} }
void app_main () { void app_main () {
esp_timer_early_init();
wifi_init(); wifi_init();
} }

View File

@@ -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 bits
writeByte(client_fd, 0); // biome palette writeByte(client_fd, 0); // biome palette
} }
// reset watchdog and yield // yield to idle task
wdt_reset(); task_yield();
// send chunk sections // send chunk sections
for (int i = 0; i < 20; i ++) { for (int i = 0; i < 20; i ++) {
@@ -325,8 +325,8 @@ int sc_chunkDataAndUpdateLight (int client_fd, int _x, int _z) {
// biome data // biome data
writeByte(client_fd, 0); // bits per entry writeByte(client_fd, 0); // bits per entry
writeByte(client_fd, W_plains); // biome palette writeByte(client_fd, W_plains); // biome palette
// reset watchdog and yield // yield to idle task
wdt_reset(); task_yield();
} }
// send 8 chunk sections (up to Y=192) with no blocks // 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 bits
writeByte(client_fd, 0); // biome palette writeByte(client_fd, 0); // biome palette
} }
// reset watchdog and yield // yield to idle task
wdt_reset(); task_yield();
writeVarInt(client_fd, 0); // omit block entities writeVarInt(client_fd, 0); // omit block entities

View File

@@ -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); ssize_t r = recv(client_fd, p + total, n - total, 0);
if (r < 0) { if (r < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
// spin until data arrives task_yield();
wdt_reset();
continue; continue;
} else { } else {
return -1; // real error return -1; // real error
@@ -78,7 +77,7 @@ ssize_t send_all (int client_fd, const void *buf, size_t len) {
return -1; return -1;
} }
if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
wdt_reset(); task_yield();
continue; continue;
} }
return -1; return -1;