This is an automated email from the ASF dual-hosted git repository.

vipulrahane pushed a commit to branch vipul/bmarks
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit 8afbb2ea5ddeebde955f8a50d038d45374aa8540
Author: Vipul Rahane <[email protected]>
AuthorDate: Wed Oct 2 14:55:13 2024 -0700

    sys/log: Add bookmarks per sector to improve reading
---
 sys/log/full/src/log_fcb.c       | 16 ++++++++++++++
 sys/log/full/src/log_fcb_bmark.c | 46 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+)

diff --git a/sys/log/full/src/log_fcb.c b/sys/log/full/src/log_fcb.c
index 5ed640820..08c9d1f83 100644
--- a/sys/log/full/src/log_fcb.c
+++ b/sys/log/full/src/log_fcb.c
@@ -207,7 +207,9 @@ log_fcb_start_append(struct log *log, int len, struct 
fcb_entry *loc)
     struct fcb *fcb;
     struct fcb_log *fcb_log;
     struct flash_area *old_fa;
+    int active_sector_cnt = 0;
     int rc = 0;
+    uint32_t idx = 0;
 #if MYNEWT_VAL(LOG_STATS)
     int cnt;
 #endif
@@ -215,6 +217,8 @@ log_fcb_start_append(struct log *log, int len, struct 
fcb_entry *loc)
     fcb_log = (struct fcb_log *)log->l_arg;
     fcb = &fcb_log->fl_fcb;
 
+    /* Cache sector count before appending */
+    active_sector_cnt = fcb->f_active_id;
     while (1) {
         rc = fcb_append(fcb, len, loc);
         if (rc == 0) {
@@ -272,6 +276,18 @@ log_fcb_start_append(struct log *log, int len, struct 
fcb_entry *loc)
 #endif
     }
 
+    /* Add bookmark if entry is added to a new sector */
+    if (!rc && log->l_log->log_type != LOG_TYPE_STREAM) {
+        if (active_sector_cnt > fcb->f_active_id) {
+#if MYNEWT_VAL(LOG_GLOBAL_IDX)
+            idx = g_log_info.li_next_index;
+#else
+            idx = log->l_idx;
+#endif
+            log_fcb_add_bmark(fcb_log, loc, idx);
+        }
+    }
+
 err:
     return (rc);
 }
diff --git a/sys/log/full/src/log_fcb_bmark.c b/sys/log/full/src/log_fcb_bmark.c
index e9282771a..614b4fd3c 100644
--- a/sys/log/full/src/log_fcb_bmark.c
+++ b/sys/log/full/src/log_fcb_bmark.c
@@ -25,6 +25,50 @@
 
 #include "log/log_fcb.h"
 
+static int
+log_fcb_init_sector_bmarks(struct fcb_log *fcb_log)
+{
+    int rc = 0;
+#if MYNEWT_VAL(LOG_STORAGE_INFO)
+    struct log_storage_info info = {0};
+    struct fcb_entry loc = {0};
+    struct flash_area *fe_area = NULL;
+    struct log_entry_hdr ueh;
+
+    rc = log_storage_info(fcb_log, &info);
+    if (rc) {
+        return SYS_ENOENT;
+    }
+
+    rc = fcb_getnext(fcb_log->fl_fcb, &loc);
+    if (rc) {
+        return -1;
+    }
+
+    for (i = 0; i < info.used/fcb_log->fl_fcb->f_sector_cnt; i++) {
+        rc = log_read_hdr(log, loc, &ueh);
+        if (rc != sizeof(ueh)) {
+            /* Read failed, don't add a bookmark, done adding bookmarks */
+            rc = SYS_EOK;
+            break;
+        }
+
+        log_fcb_add_bmark(fcb_log, &loc, ueh.ue_index);
+
+        rc = fcb_getnext_area(fcb_log->fl_fcb, loc.fe_area);
+        if (rc) {
+            break;
+        }
+
+        rc = fcb_getnext_in_area(fcb_log->fl_fcb, &loc);
+        if (rc) {
+            break;
+        }
+    }
+#endif
+    return rc;
+}
+
 void
 log_fcb_init_bmarks(struct fcb_log *fcb_log,
                     struct log_fcb_bmark *buf, int bmark_count)
@@ -33,6 +77,8 @@ log_fcb_init_bmarks(struct fcb_log *fcb_log,
         .lfs_bmarks = buf,
         .lfs_cap = bmark_count,
     };
+
+    log_fcb_init_sector_bmarks(fcb_log);
 }
 
 void

Reply via email to