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

nrf_log fixes in SDK14.1.0

I've noticed following line in SDK14.1 release notes:

`

  • Fixed a bug where nrf_log could crash when heavily loaded. `

I'm using SDK13.1 in my current project and I'm experiencing nrf_log related crashes under load as described here.

Please elaborate on what exactly was fixed and if it's related to problems I'm experiencing. Also if possible please provide some pointers on how to backport this fix into SDK13.1. Unfortunately I can't upgrade to SDK14.1 at this time.

Parents
  • Hi,

    you can try to find function cont_buf_prealloc in nrf_log_frontend.c and update following code:

    uint32_t available_words = (m_log_data.mask + 1) -
                               (m_log_data.wr_idx & m_log_data.mask);
    

    should be changed to:

    uint32_t available_words = (m_log_data.mask + 1) -
                                (m_log_data.wr_idx - m_log_data.rd_idx);
    

    That should help.

    UPDATE Actually, the fix provided was not good and there was still a bug in SDK 14.2.0 in that function. Here is the updated version the function:

    static inline uint32_t * cont_buf_prealloc(uint32_t len32,
                                           uint32_t * p_offset,
                                           uint32_t * p_wr_idx){
    uint32_t * p_buf = NULL;
    
    len32 += PUSHED_HEADER_SIZE; // Increment because 32bit header is needed to be stored.
    
    CRITICAL_REGION_ENTER();
    *p_wr_idx = m_log_data.wr_idx;
    uint32_t available_words = (m_log_data.mask + 1) -
                                (m_log_data.wr_idx - m_log_data.rd_idx);
    uint32_t cont_words =  (m_log_data.mask + 1) - (m_log_data.wr_idx & m_log_data.mask);
    
    //available space is continuous
    uint32_t curr_pos_available = (available_words <= cont_words) ? available_words : cont_words;
    uint32_t start_pos_available = (available_words <= cont_words) ? 0 : (available_words - cont_words);
    if (len32 <= curr_pos_available)
    {
        // buffer will fit as is
        p_buf              = &m_log_data.buffer[(m_log_data.wr_idx + 1) & m_log_data.mask];
        m_log_data.wr_idx += len32;
        *p_offset          = 0;
    }
    else if (len32 <= start_pos_available)
    {
        // wraping to the begining of the buffer
        m_log_data.wr_idx += (len32 + cont_words);
        *p_offset          = cont_words;
        p_buf              = m_log_data.buffer;
    }
    
    CRITICAL_REGION_EXIT();
    
    return p_buf;}
    
Reply
  • Hi,

    you can try to find function cont_buf_prealloc in nrf_log_frontend.c and update following code:

    uint32_t available_words = (m_log_data.mask + 1) -
                               (m_log_data.wr_idx & m_log_data.mask);
    

    should be changed to:

    uint32_t available_words = (m_log_data.mask + 1) -
                                (m_log_data.wr_idx - m_log_data.rd_idx);
    

    That should help.

    UPDATE Actually, the fix provided was not good and there was still a bug in SDK 14.2.0 in that function. Here is the updated version the function:

    static inline uint32_t * cont_buf_prealloc(uint32_t len32,
                                           uint32_t * p_offset,
                                           uint32_t * p_wr_idx){
    uint32_t * p_buf = NULL;
    
    len32 += PUSHED_HEADER_SIZE; // Increment because 32bit header is needed to be stored.
    
    CRITICAL_REGION_ENTER();
    *p_wr_idx = m_log_data.wr_idx;
    uint32_t available_words = (m_log_data.mask + 1) -
                                (m_log_data.wr_idx - m_log_data.rd_idx);
    uint32_t cont_words =  (m_log_data.mask + 1) - (m_log_data.wr_idx & m_log_data.mask);
    
    //available space is continuous
    uint32_t curr_pos_available = (available_words <= cont_words) ? available_words : cont_words;
    uint32_t start_pos_available = (available_words <= cont_words) ? 0 : (available_words - cont_words);
    if (len32 <= curr_pos_available)
    {
        // buffer will fit as is
        p_buf              = &m_log_data.buffer[(m_log_data.wr_idx + 1) & m_log_data.mask];
        m_log_data.wr_idx += len32;
        *p_offset          = 0;
    }
    else if (len32 <= start_pos_available)
    {
        // wraping to the begining of the buffer
        m_log_data.wr_idx += (len32 + cont_words);
        *p_offset          = cont_words;
        p_buf              = m_log_data.buffer;
    }
    
    CRITICAL_REGION_EXIT();
    
    return p_buf;}
    
Children
Related