Skip to content

Commit a73197e

Browse files
author
Zhou Xiao
committed
feat(ble): optimized printf functions code size and speed
(cherry picked from commit e4be25b) Co-authored-by: Zhou Xiao <zhouxiao@espressif.com>
1 parent 9e0c4d2 commit a73197e

File tree

1 file changed

+70
-96
lines changed

1 file changed

+70
-96
lines changed

components/bt/common/ble_log/ble_log_spi_out.c

Lines changed: 70 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#define SPI_OUT_FRAME_OVERHEAD (8)
3434
#define SPI_OUT_PACKET_LOSS_FRAME_SIZE (6)
3535
#define SPI_OUT_TRANS_ITVL_MIN_US (30)
36+
#define SPI_OUT_UL_LOG_STR_BUF_SIZE (100)
3637

3738
#if SPI_OUT_TS_SYNC_ENABLED
3839
#define SPI_OUT_TS_SYNC_TIMEOUT (1000 * 1000)
@@ -106,6 +107,7 @@ static uint32_t last_tx_done_ts = 0;
106107
static bool ul_log_inited = false;
107108
static SemaphoreHandle_t ul_log_mutex = NULL;
108109
static spi_out_log_cb_t *ul_log_cb = NULL;
110+
static uint8_t *ul_log_str_buf = NULL;
109111

110112
#if SPI_OUT_LL_ENABLED
111113
static bool ll_log_inited = false;
@@ -148,6 +150,8 @@ static void spi_out_log_flush(void);
148150

149151
static int spi_out_ul_log_init(void);
150152
static void spi_out_ul_log_deinit(void);
153+
static void spi_out_ul_log_write(uint8_t source, const uint8_t *addr, uint16_t len, bool with_ts);
154+
static bool spi_out_ul_log_printf(uint8_t source, const char *format, va_list args);
151155

152156
#if SPI_OUT_LL_ENABLED
153157
static int spi_out_ll_log_init(void);
@@ -452,6 +456,13 @@ static int spi_out_ul_log_init(void)
452456
goto mutex_init_failed;
453457
}
454458

459+
// Initialize string buffer
460+
ul_log_str_buf = (uint8_t *)malloc(SPI_OUT_UL_LOG_STR_BUF_SIZE);
461+
if (!ul_log_str_buf) {
462+
ESP_LOGE(BLE_LOG_TAG, "Failed to initialize string buffer for upper layer task log!");
463+
goto str_buf_init_failed;
464+
}
465+
455466
// Initialize log control block
456467
if (spi_out_log_cb_init(&ul_log_cb, SPI_OUT_UL_TASK_BUF_SIZE) != 0) {
457468
ESP_LOGE(BLE_LOG_TAG, "Failed to initialize log control blocks for upper layer task log!");
@@ -464,6 +475,11 @@ static int spi_out_ul_log_init(void)
464475
return 0;
465476

466477
log_cb_init_failed:
478+
if (ul_log_str_buf) {
479+
free(ul_log_str_buf);
480+
ul_log_str_buf = NULL;
481+
}
482+
str_buf_init_failed:
467483
vSemaphoreDelete(ul_log_mutex);
468484
mutex_init_failed:
469485
return -1;
@@ -474,19 +490,59 @@ static void spi_out_ul_log_deinit(void)
474490
if (!ul_log_inited) {
475491
return;
476492
}
493+
ul_log_inited = false;
477494

478495
xSemaphoreTake(ul_log_mutex, portMAX_DELAY);
496+
if (ul_log_str_buf) {
497+
free(ul_log_str_buf);
498+
ul_log_str_buf = NULL;
499+
}
479500
spi_out_log_cb_deinit(&ul_log_cb);
480501
xSemaphoreGive(ul_log_mutex);
481502

482503
vSemaphoreDelete(ul_log_mutex);
483504
ul_log_mutex = NULL;
484505

485506
ESP_LOGI(BLE_LOG_TAG, "Succeeded to deinitialize upper layer log!");
486-
ul_log_inited = false;
487507
return;
488508
}
489509

510+
static void spi_out_ul_log_write(uint8_t source, const uint8_t *addr, uint16_t len, bool with_ts)
511+
{
512+
uint16_t total_len = with_ts? (len + sizeof(uint32_t)): len;
513+
bool need_append;
514+
if (spi_out_log_cb_check_trans(ul_log_cb, total_len, &need_append)) {
515+
if (with_ts) {
516+
uint32_t esp_ts = esp_timer_get_time();
517+
need_append |= spi_out_log_cb_write(ul_log_cb, (const uint8_t *)&esp_ts,
518+
sizeof(uint32_t), addr, len, source);
519+
} else {
520+
need_append |= spi_out_log_cb_write(ul_log_cb, addr, len, NULL, 0, source);
521+
}
522+
}
523+
if (need_append) {
524+
spi_out_log_cb_append_trans(ul_log_cb);
525+
}
526+
spi_out_log_cb_write_packet_loss(ul_log_cb, LOG_CB_TYPE_UL);
527+
}
528+
529+
static bool spi_out_ul_log_printf(uint8_t source, const char *format, va_list args)
530+
{
531+
int len = vsnprintf((char *)ul_log_str_buf, SPI_OUT_UL_LOG_STR_BUF_SIZE, format, args);
532+
if (len < 0) {
533+
return false;
534+
}
535+
536+
// Truncate string if overflowed
537+
if (len >= SPI_OUT_UL_LOG_STR_BUF_SIZE) {
538+
len = SPI_OUT_UL_LOG_STR_BUF_SIZE - 1;
539+
ul_log_str_buf[len] = '\0';
540+
}
541+
542+
spi_out_ul_log_write(source, ul_log_str_buf, len, true);
543+
return true;
544+
}
545+
490546
#if SPI_OUT_LL_ENABLED
491547
static int spi_out_ll_log_init(void)
492548
{
@@ -913,15 +969,8 @@ int ble_log_spi_out_write(uint8_t source, const uint8_t *addr, uint16_t len)
913969
return -1;
914970
}
915971

916-
bool need_append;
917972
xSemaphoreTake(ul_log_mutex, portMAX_DELAY);
918-
if (spi_out_log_cb_check_trans(ul_log_cb, len, &need_append)) {
919-
need_append |= spi_out_log_cb_write(ul_log_cb, addr, len, NULL, 0, source);
920-
}
921-
if (need_append) {
922-
spi_out_log_cb_append_trans(ul_log_cb);
923-
}
924-
spi_out_log_cb_write_packet_loss(ul_log_cb, LOG_CB_TYPE_UL);
973+
spi_out_ul_log_write(source, addr, len, false);
925974
xSemaphoreGive(ul_log_mutex);
926975
return 0;
927976
}
@@ -936,45 +985,13 @@ int ble_log_spi_out_printf(uint8_t source, const char *format, ...)
936985
va_list args;
937986
va_start(args, format);
938987

939-
// Get len as ref to allocate heap memory
940-
va_list args_copy;
941-
va_copy(args_copy, args);
942-
int len = vsnprintf(NULL, 0, format, args_copy);
943-
va_end(args_copy);
944-
945-
// Length validation
946-
if ((len < 0) || (len > 0xFFFF)) {
947-
va_end(args);
948-
return -1;
949-
}
950-
951-
// Allocate memory
952-
uint8_t *buffer = malloc(len + 1);
953-
if (!buffer) {
954-
va_end(args);
955-
return -1;
956-
}
957-
958-
// Generate string
959-
vsnprintf((char *)buffer, len + 1, format, args);
960-
va_end(args);
961-
962-
uint32_t esp_ts = esp_timer_get_time();
963-
bool need_append;
964988
xSemaphoreTake(ul_log_mutex, portMAX_DELAY);
965-
if (spi_out_log_cb_check_trans(ul_log_cb, sizeof(uint32_t) + len, &need_append)) {
966-
need_append |= spi_out_log_cb_write(ul_log_cb, (const uint8_t *)&esp_ts, sizeof(uint32_t),
967-
(const uint8_t *)buffer, len, source);
968-
}
969-
if (need_append) {
970-
spi_out_log_cb_append_trans(ul_log_cb);
971-
}
972-
spi_out_log_cb_write_packet_loss(ul_log_cb, LOG_CB_TYPE_UL);
989+
bool ret = spi_out_ul_log_printf(source, format, args);
973990
xSemaphoreGive(ul_log_mutex);
974991

975-
// Release
976-
free(buffer);
977-
return 0;
992+
va_end(args);
993+
994+
return ret? 0: -1;
978995
}
979996

980997
int ble_log_spi_out_printf_enh(uint8_t source, uint8_t level, const char *tag, const char *format, ...)
@@ -983,54 +1000,19 @@ int ble_log_spi_out_printf_enh(uint8_t source, uint8_t level, const char *tag, c
9831000
return -1;
9841001
}
9851002

986-
// Create log prefix in the format: "[level][tag] "
987-
char prefix[32];
988-
int prefix_len = snprintf(prefix, sizeof(prefix), "[%d][%s] ", level, tag ? tag : "NULL");
989-
990-
// Compute the length of the formatted log message
9911003
va_list args;
9921004
va_start(args, format);
993-
va_list args_copy;
994-
va_copy(args_copy, args);
995-
int log_len = vsnprintf(NULL, 0, format, args_copy);
996-
va_end(args_copy);
997-
998-
// Validate length
999-
if (log_len < 0 || log_len > 0xFFFF) {
1000-
va_end(args);
1001-
return -1;
1002-
}
1003-
1004-
// Compute total log length (prefix + formatted message)
1005-
int total_len = prefix_len + log_len;
1006-
1007-
// Allocate memory for the complete log message
1008-
uint8_t *buffer = malloc(total_len + 1);
1009-
if (!buffer) {
1010-
va_end(args);
1011-
return -1;
1012-
}
10131005

1014-
// Construct the final log message
1015-
memcpy(buffer, prefix, prefix_len); // Copy the prefix
1016-
vsnprintf((char *)(buffer + prefix_len), log_len + 1, format, args);
1017-
va_end(args);
1018-
1019-
uint32_t esp_ts = esp_timer_get_time();
1020-
bool need_append;
1006+
// Create log prefix in the format: "[level][tag] "
10211007
xSemaphoreTake(ul_log_mutex, portMAX_DELAY);
1022-
if (spi_out_log_cb_check_trans(ul_log_cb, sizeof(uint32_t) + total_len, &need_append)) {
1023-
need_append |= spi_out_log_cb_write(ul_log_cb, (const uint8_t *)&esp_ts, sizeof(uint32_t),
1024-
(const uint8_t *)buffer, total_len, source);
1025-
}
1026-
if (need_append) {
1027-
spi_out_log_cb_append_trans(ul_log_cb);
1028-
}
1029-
spi_out_log_cb_write_packet_loss(ul_log_cb, LOG_CB_TYPE_UL);
1008+
snprintf((char *)ul_log_str_buf, SPI_OUT_UL_LOG_STR_BUF_SIZE,
1009+
"[%d][%s] %s", level, tag? tag: "NULL", format);
1010+
bool ret = spi_out_ul_log_printf(source, (const char *)ul_log_str_buf, args);
10301011
xSemaphoreGive(ul_log_mutex);
10311012

1032-
free(buffer);
1033-
return 0;
1013+
va_end(args);
1014+
1015+
return ret? 0: -1;
10341016
}
10351017

10361018
int ble_log_spi_out_write_with_ts(uint8_t source, const uint8_t *addr, uint16_t len)
@@ -1039,16 +1021,8 @@ int ble_log_spi_out_write_with_ts(uint8_t source, const uint8_t *addr, uint16_t
10391021
return -1;
10401022
}
10411023

1042-
uint32_t esp_ts = esp_timer_get_time();
1043-
bool need_append;
10441024
xSemaphoreTake(ul_log_mutex, portMAX_DELAY);
1045-
if (spi_out_log_cb_check_trans(ul_log_cb, sizeof(uint32_t) + len, &need_append)) {
1046-
need_append |= spi_out_log_cb_write(ul_log_cb, (const uint8_t *)&esp_ts, sizeof(uint32_t), addr, len, source);
1047-
}
1048-
if (need_append) {
1049-
spi_out_log_cb_append_trans(ul_log_cb);
1050-
}
1051-
spi_out_log_cb_write_packet_loss(ul_log_cb, LOG_CB_TYPE_UL);
1025+
spi_out_ul_log_write(source, addr, len, true);
10521026
xSemaphoreGive(ul_log_mutex);
10531027
return 0;
10541028
}

0 commit comments

Comments
 (0)