Skip to content

sys/log: Add optional support for sector bookmarks to optimize reading logs #3365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions fs/fcb/include/fcb/fcb.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,28 @@ typedef int (*fcb_walk_cb)(struct fcb_entry *loc, void *arg);
int fcb_walk(struct fcb *, struct flash_area *, fcb_walk_cb cb, void *cb_arg);
int fcb_getnext(struct fcb *, struct fcb_entry *loc);

/**
* Get first entry in the provided flash area
*
* @param fcb Pointer to FCB
* @param fap Optional pointer to flash area
* @param loc Pointer to first FCB entry in the provided flash area
*
* @return 0 on success, non-zero on failure
*/
int fcb_getnext_in_area(struct fcb *fcb, struct flash_area *fap,
struct fcb_entry *loc);

/**
* Get next area pointer from the FCB pointer
*
* @param fcb Pointer to the FCB
* @param fap Pointer to the flash_area
*
* @return Pointer to the flash_area that comes next
*/
struct flash_area *fcb_getnext_area(struct fcb *fcb, struct flash_area *fap);

#if MYNEWT_VAL_FCB_BIDIRECTIONAL
/**
* Call 'cb' for every element in flash circular buffer moving
Expand Down
2 changes: 1 addition & 1 deletion fs/fcb/src/fcb.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fcb_init(struct fcb *fcb)
assert((fcb->f_align & (fcb->f_align - 1)) == 0);

while (1) {
rc = fcb_getnext_in_area(fcb, &fcb->f_active);
rc = fcb_getnext_in_area(fcb, NULL, &fcb->f_active);
if (rc == FCB_ERR_NOVAR) {
rc = FCB_OK;
break;
Expand Down
11 changes: 10 additions & 1 deletion fs/fcb/src/fcb_getnext.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,19 @@ fcb_start_offset(struct fcb *fcb)
}

int
fcb_getnext_in_area(struct fcb *fcb, struct fcb_entry *loc)
fcb_getnext_in_area(struct fcb *fcb, struct flash_area *fap,
struct fcb_entry *loc)
{
int rc;

/* If a flash area is specified, find first entry in that area */
if (fap) {
loc->fe_area = fap;
loc->fe_elem_off = fcb_len_in_flash(fcb, sizeof(struct fcb_disk_area));
loc->fe_elem_ix = 0;
loc->fe_data_len = 0;
}

rc = fcb_elem_info(fcb, loc);
if (rc == 0 || rc == FCB_ERR_CRC) {
do {
Expand Down
1 change: 0 additions & 1 deletion fs/fcb/src/fcb_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ fcb_len_in_flash(struct fcb *fcb, uint16_t len)
return (len + (fcb->f_align - 1)) & ~(fcb->f_align - 1);
}

int fcb_getnext_in_area(struct fcb *fcb, struct fcb_entry *loc);
struct flash_area *fcb_getnext_area(struct fcb *fcb, struct flash_area *fap);
int fcb_getnext_nolock(struct fcb *fcb, struct fcb_entry *loc);

Expand Down
70 changes: 55 additions & 15 deletions sys/log/full/include/log/log_fcb.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,29 @@ struct log_fcb_bset {
/** Array of bookmarks. */
struct log_fcb_bmark *lfs_bmarks;

/** Enable sector bookmarks */
bool lfs_en_sect_bmarks;

/** The maximum number of bookmarks. */
int lfs_cap;

/** The maximum number of sector bookmarks. */
int lfs_sect_cap;

/** The number of currently used non-sector bookmarks. */
int lfs_non_sect_size;

/** The number of currently usable bookmarks. */
int lfs_size;

/** The index where the next bookmark will get written. */
int lfs_next;
/** The index where the next non-sector bmark will get written */
uint32_t lfs_next_non_sect;

/** The sector interval at which to insert sector bookmarks */
int lfs_sect_bmark_itvl;

/** The index where the next sector bmark will get written */
uint32_t lfs_next_sect;
};

/**
Expand All @@ -72,6 +87,7 @@ struct fcb_log {
#if MYNEWT_VAL(LOG_FCB_BOOKMARKS)
struct log_fcb_bset fl_bset;
#endif
struct log *fl_log;
};

#elif MYNEWT_VAL(LOG_FCB2)
Expand All @@ -87,6 +103,7 @@ struct fcb_log {
#if MYNEWT_VAL(LOG_FCB_BOOKMARKS)
struct log_fcb_bset fl_bset;
#endif
struct log *fl_log;
};
#endif

Expand All @@ -103,19 +120,35 @@ struct fcb_log {
* the log is walked, the starting point of the walk is added to the set of
* bookmarks.
*
* FCB rotation invalidates all bookmarks. It is up to the client code to
* FCB rotation invalidates all bookmarks. It is up to the client code to
* clear a log's bookmarks whenever rotation occurs.
*/

/**
* @brief Configures an fcb_log to use the specified buffer for bookmarks.
* If sector bookmarks are enabled, buffer should be big enough
* to accommodate bookmarks for the entire flash area that is allocated
* for the FCB log, i,e; sizeof(struct log_fcb_bmark) *
* my_log.fl_fcb.f_sector_cnt + MYNEWT_VAL(LOG_FCB_NUM_ABS_BOOKMARKS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can't stay like this. The function should return an error if buffer is too small and not just crash later on. But actually it would be better if it can work with smaller buffers as well and e.g. place bookmark every n-th sector or just skip bookmarks that don't fit in the buffer.

Copy link
Contributor Author

@vrahane vrahane Feb 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is possible but it complicates the code further. Just failing is a better approach. The user should know what they are doing. What if they have a bigger buffer ? Then we will have to handle that too. I would like to keep it a bit restricted in its usage.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not complicated. It's just a simple if statement added to log_fcb_init_sector_bmarks so log_fcb_add_bmark is not called for every sector.

You can simply calculate max number of bookmarks from the buffer size and then either add at most that number of bookmarks or, even better, divide number of sectors by that number and insert bookmark every n-th sector only so it doesn't overflow buffer. I don't ask you to handle any special cases, e.g. if the buffer is too big then it will simply have some free space left.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let me give it a shot.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrzej-kaczmarek Ok, I have added support for this. Please take a look. I am going to exercise it little bit more to make sure there are no edge cases I have forgotten.

*
* @param fcb_log The log to configure.
* @param buf The buffer to use for bookmarks.
* @param bmark_count The bookmark capacity of the supplied buffer.
* @param avl_bmark_cnt The bookmark capacity of the supplied buffer,
* available bookmark count
* @param en_sect_bmarks Enable sector bookmarks
*
* @return 0 on success, non-zero on failure
*/
void log_fcb_init_bmarks(struct fcb_log *fcb_log,
struct log_fcb_bmark *buf, int bmark_count);
int log_fcb_init_bmarks(struct fcb_log *fcb_log,
struct log_fcb_bmark *buf, int avl_bmark_cnt,
bool en_sect_bmarks);

/** @brief Remove bookmarks which point to oldest FCB/FCB2 area. This is
* meant to get called just before the area is rotated out.
*
* @param fcb_log The fcb_log to operate on.
*/
void log_fcb_rotate_bmarks(struct fcb_log *fcb_log);

/**
* @brief Erases all bookmarks from the supplied fcb_log.
Expand All @@ -125,39 +158,46 @@ void log_fcb_init_bmarks(struct fcb_log *fcb_log,
void log_fcb_clear_bmarks(struct fcb_log *fcb_log);

/**
* @brief Remove bookmarks which point to oldest FCB/FCB2 area. This is
* meant to get called just before the area is rotated out.
* @brief Get bookmarks for a particular log
*
* @param fcb_log The fcb_log to operate on.
* @param log Pointer to the log we want to read bookmarks from
* @param bmarks_size Pointer to the variable we want to read bookmarks into
*
* @return Pointer to the bookmarks array for the provided log
*/
void log_fcb_rotate_bmarks(struct fcb_log *fcb_log);
struct log_fcb_bmark *log_fcb_get_bmarks(struct log *log, uint32_t *bmarks_size);

/**
* @brief Searches an fcb_log for the closest bookmark that comes before or at
* the specified index.
*
* @param fcb_log The log to search.
* @param index The index to look for.
* @param min_diff If bookmark was found, fill it up with the difference
* else it will return -1.
*
* @return The closest bookmark on success;
* NULL if the log has no applicable bookmarks.
*/
const struct log_fcb_bmark *
log_fcb_closest_bmark(const struct fcb_log *fcb_log, uint32_t index);
log_fcb_closest_bmark(const struct fcb_log *fcb_log, uint32_t index, int *min_diff);

/**
* Inserts a bookmark into the provided log.
*
* @param fcb_log The log to insert a bookmark into.
* @param entry The entry the bookmark should point to.
* @param index The log entry index of the bookmark.
* @paran sect_bmark Bool indicating it is a sector bookmark.
*
* @return non-zero on failure, 0 on success
*/
#if MYNEWT_VAL(LOG_FCB)
void log_fcb_add_bmark(struct fcb_log *fcb_log, const struct fcb_entry *entry,
uint32_t index);
int log_fcb_add_bmark(struct fcb_log *fcb_log, struct fcb_entry *entry,
uint32_t index, bool sect_bmark);
#elif MYNEWT_VAL(LOG_FCB2)
void log_fcb_add_bmark(struct fcb_log *fcb_log, const struct fcb2_entry *entry,
uint32_t index);
int log_fcb_add_bmark(struct fcb_log *fcb_log, struct fcb2_entry *entry,
uint32_t index, bool sect_bmark);
#endif
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ ltfbu_init(const struct ltfbu_cfg *cfg)
TEST_ASSERT_FATAL(rc == 0);

if (cfg->bmark_count > 0) {
log_fcb_init_bmarks(&ltfbu_fcb_log, ltfbu_bmarks, cfg->bmark_count);
log_fcb_init_bmarks(&ltfbu_fcb_log, ltfbu_bmarks, cfg->bmark_count, false);
}

log_register("log", &ltfbu_log, &log_fcb_handler, &ltfbu_fcb_log,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ ltfbu_init(const struct ltfbu_cfg *cfg)
TEST_ASSERT_FATAL(rc == 0);

if (cfg->bmark_count > 0) {
log_fcb_init_bmarks(&ltfbu_fcb_log, ltfbu_bmarks, cfg->bmark_count);
log_fcb_init_bmarks(&ltfbu_fcb_log, ltfbu_bmarks, cfg->bmark_count, false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only place in mynewt repository where log_fcb_init_bmarks() is called. When I try to put similar code (except last argument is true) in actual code system crashes.
Is there a restriction on when this function should be called?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, there is no restrictions as such, I called it on the fcb from the slinky app and that seemed to work. Where did you call it from ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is test function called from main after

int
test_log_init(void)
{
    const struct flash_area *fa;
    struct fcb *fcbp;
    int sector_count;
    int rc;

    if (flash_area_open(FLASH_AREA_TEST_LOG, &fa)) {
        return SYS_EUNKNOWN;
    }

    flash_area_to_sectors(FLASH_AREA_TEST_LOG, &sector_count, NULL);
    test_log_sectors = calloc(sector_count, sizeof(*test_log_sectors));
    flash_area_to_sectors(FLASH_AREA_TEST_LOG, &sector_count, test_log_sectors);
    test_log_fcb.fl_entries = 0;
    fcbp = &test_log_fcb.fl_fcb;

    fcbp->f_magic = 0x7EADBADF;
    fcbp->f_version = g_log_info.li_version;
    fcbp->f_sector_cnt = sector_count - 1;
    fcbp->f_scratch_cnt = 1;
    fcbp->f_sectors = test_log_sectors;

    rc = fcb_init(fcbp);
    if (rc) {
        flash_area_erase(fa, 0, fa->fa_size);
        rc = fcb_init(fcbp);
        if (rc) {
            return rc;
        }
    }

#if MYNEWT_VAL_LOG_FCB_SECTOR_BOOKMARKS
    int bookmark_count = fcbp->f_sector_cnt + MYNEWT_VAL_LOG_FCB_NUM_ABS_BOOKMARKS;
    test_log_bookmarks = malloc(bookmark_count * sizeof(*test_log_bookmarks));

    log_fcb_init_bmarks(&test_log_fcb, test_log_bookmarks, bookmark_count, true);
#endif

    rc = log_register("test_log", &test_log, &log_fcb_handler,
                      &test_log_fcb, LOG_SYSLEVEL);

    return rc;
}

Order of calls based on the only example found in unit tests except unit tests call to log_fcb_init_bmarks() has false as last argument:

  • fcb_init()
  • log_fcb_init_bmarks() <- crashes inside
  • log_register()

}

log_register("log", &ltfbu_log, &log_fcb_handler, &ltfbu_fcb_log,
Expand Down
29 changes: 29 additions & 0 deletions sys/log/full/selftest/fcb_bookmarks_mixed/pkg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
pkg.name: sys/log/full/selftest/fcb_bookmarks_mixed
pkg.type: unittest
pkg.description: "Log unit tests; flash-alignment=8."
pkg.author: "Apache Mynewt <dev@mynewt.apache.org>"
pkg.homepage: "http://mynewt.apache.org/"
pkg.keywords:

pkg.deps:
- "@apache-mynewt-core/sys/console/stub"
- "@apache-mynewt-core/sys/log/full"
- "@apache-mynewt-core/sys/log/full/selftest/util"
- "@apache-mynewt-core/test/testutil"
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include "os/mynewt.h"
#include "log_test_util/log_test_util.h"
#include "log_test_fcb_bookmarks.h"

TEST_SUITE(log_test_suite_fcb_bookmarks)
{
log_test_case_fcb_bookmarks_s0_l1_b0_p100();
log_test_case_fcb_bookmarks_s0_l1_b5_p100();
log_test_case_fcb_bookmarks_s0_l10_b10_p100();
log_test_case_fcb_bookmarks_s10_l10_b10_p100();
}

int
main(int argc, char **argv)
{
log_test_suite_fcb_bookmarks();

return tu_any_failed;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#ifndef H_LOG_TEST_FCB_BOOKMARKS_
#define H_LOG_TEST_FCB_BOOKMARKS_

#include "os/mynewt.h"
#include "testutil/testutil.h"

struct ltfbu_cfg {
int skip_mod;
int body_len;
int bmark_count;
int pop_count;
};

void ltfbu_populate_log(int count);
void ltfbu_verify_log(uint32_t start_idx);
void ltfbu_init(const struct ltfbu_cfg *cfg);
void ltfbu_test_once(const struct ltfbu_cfg *cfg);

TEST_CASE_DECL(log_test_case_fcb_bookmarks_s0_l1_b0_p100);
TEST_CASE_DECL(log_test_case_fcb_bookmarks_s0_l1_b5_p100);
TEST_CASE_DECL(log_test_case_fcb_bookmarks_s0_l10_b10_p100);
TEST_CASE_DECL(log_test_case_fcb_bookmarks_s10_l10_b10_p100);

#endif
Loading
Loading