This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

[Bugreport] Unecessary high RAM usage if NRF_LOG_BACKEND_SERIAL_USES_RTT is set

Platform: nRF52-DK using SDK12.1.0 and S132

Summary: Using nrf_log module from SDK along with RTT by setting NRF_LOG_BACKEND_SERIAL_USES_RTT flag causes nrf_log_serial.c to allocate excessive amount of memory for log buffer despite the fact that this memory is not used.

Way to reproduce: Use any example from the SDK, set it to log over RTT using large log buffer (NRF_LOG_BACKEND_SERIAL_USES_RTT=1 and for example NRF_LOG_BACKEND_MAX_STRING_LENGTH=4096) and try to NRF_LOG_DEBUG over 1024 bytes in blocking mode.

Expected behavior: All information is logged properly. RAM usage of nrf_log related modules corresponds to user settings.

Actual behavior: Only 1024 bytes are logged before truncation happens, memory usage is NRF_LOG_BACKEND_MAX_STRING_LENGTH+1024

Cause: nrf_log_serial.callocates m_rtt_buffer which is NRF_LOG_BACKEND_MAX_STRING_LENGTH bytes wide but this buffer isn't used by Segger_RTT which is underlying logger backend. Despite of programmer's intent to call SEGGER_RTT_ConfigUpBuffer and set log buffer pointer to this buffer Segger init code ignores it. It's due to buffer id number being equal to 0.

Here's the relevant code snippet from SEGGER_RTT.c

if (BufferIndex < (unsigned)_SEGGER_RTT.MaxNumUpBuffers) {
    SEGGER_RTT_LOCK(SavedState);
    if (BufferIndex > 0u) {
      _SEGGER_RTT.aUp[BufferIndex].sName        = sName;
      _SEGGER_RTT.aUp[BufferIndex].pBuffer      = pBuffer;
      _SEGGER_RTT.aUp[BufferIndex].SizeOfBuffer = BufferSize;
      _SEGGER_RTT.aUp[BufferIndex].RdOff        = 0u;
      _SEGGER_RTT.aUp[BufferIndex].WrOff        = 0u;
    }
    _SEGGER_RTT.aUp[BufferIndex].Flags          = Flags;
    SEGGER_RTT_UNLOCK(SavedState);
    r =  0;
  }

Note the if (BufferIndex > 0u) condition which is not met and Segger RTT still logs into it's internal buffer which by default has size of 1024 bytes (defined in SEGGER_RTT_Conf.h). This causes premature log truncation despite the user's intent of increasing log buffer size by setting NRF_LOG_BACKEND_MAX_STRING_LENGTH to a larger value.

Parents Reply Children
No Data
Related