Skip to content

Commit efdadf6

Browse files
committed
sys/log: Add bookmarks per sector to improve reading
- Add sector bookmarks for reading optimization - Retain older absolute bookmarks behavior - CLI: Add '-t' option in log_shell for measuring read time (cherry-picking Jerzy's commit) - CLI: Add '-i' option in log_shell for specifying index where log read should start - CLI: Add '-b' option in log_shell for reading bookmarks
1 parent 54f9d69 commit efdadf6

File tree

10 files changed

+446
-56
lines changed

10 files changed

+446
-56
lines changed

fs/fcb/include/fcb/fcb.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,28 @@ typedef int (*fcb_walk_cb)(struct fcb_entry *loc, void *arg);
168168
int fcb_walk(struct fcb *, struct flash_area *, fcb_walk_cb cb, void *cb_arg);
169169
int fcb_getnext(struct fcb *, struct fcb_entry *loc);
170170

171+
/**
172+
* Get first entry in the provided flash area
173+
*
174+
* @param fcb Pointer to FCB
175+
* @param fa Optional pointer to flash area
176+
* @param loc Pointer to first FCB entry in the provided flash area
177+
*
178+
* @return 0 on success, non-zero on failure
179+
*/
180+
int fcb_getnext_in_area(struct fcb *fcb, struct flash_area *fa,
181+
struct fcb_entry *loc);
182+
183+
/**
184+
* Get next area pointer from the FCB pointer to by fcb pointer
185+
*
186+
* @param fcb Pointer to the FCB
187+
* @param fap Pointer to the flash_area
188+
*
189+
* @return Pointer to the flash_area that comes next
190+
*/
191+
struct flash_area *fcb_getnext_area(struct fcb *fcb, struct flash_area *fap);
192+
171193
#if MYNEWT_VAL_FCB_BIDIRECTIONAL
172194
/**
173195
* Call 'cb' for every element in flash circular buffer moving

fs/fcb/src/fcb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fcb_init(struct fcb *fcb)
9090
assert((fcb->f_align & (fcb->f_align - 1)) == 0);
9191

9292
while (1) {
93-
rc = fcb_getnext_in_area(fcb, &fcb->f_active);
93+
rc = fcb_getnext_in_area(fcb, NULL, &fcb->f_active);
9494
if (rc == FCB_ERR_NOVAR) {
9595
rc = FCB_OK;
9696
break;

fs/fcb/src/fcb_getnext.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,19 @@ fcb_start_offset(struct fcb *fcb)
6363
}
6464

6565
int
66-
fcb_getnext_in_area(struct fcb *fcb, struct fcb_entry *loc)
66+
fcb_getnext_in_area(struct fcb *fcb, struct flash_area *fa,
67+
struct fcb_entry *loc)
6768
{
6869
int rc;
6970

71+
/* If a flash area is specified, find first entry in that area */
72+
if (fa) {
73+
loc->fe_area = fa;
74+
loc->fe_elem_off = fcb_len_in_flash(fcb, sizeof(struct fcb_disk_area));
75+
loc->fe_elem_ix = 0;
76+
loc->fe_data_len = 0;
77+
}
78+
7079
rc = fcb_elem_info(fcb, loc);
7180
if (rc == 0 || rc == FCB_ERR_CRC) {
7281
do {

fs/fcb/src/fcb_priv.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ fcb_len_in_flash(struct fcb *fcb, uint16_t len)
4747
return (len + (fcb->f_align - 1)) & ~(fcb->f_align - 1);
4848
}
4949

50-
int fcb_getnext_in_area(struct fcb *fcb, struct fcb_entry *loc);
5150
struct flash_area *fcb_getnext_area(struct fcb *fcb, struct flash_area *fap);
5251
int fcb_getnext_nolock(struct fcb *fcb, struct fcb_entry *loc);
5352

sys/log/full/include/log/log_fcb.h

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ struct log_fcb_bmark {
3939
#endif
4040
/* The index of the log entry that the FCB entry contains. */
4141
uint32_t lfb_index;
42+
/* Is this a sector boundary bookmark */
43+
bool lfb_sect_bmark;
4244
};
4345

4446
/** A set of fcb log bookmarks. */
@@ -49,11 +51,17 @@ struct log_fcb_bset {
4951
/** The maximum number of bookmarks. */
5052
int lfs_cap;
5153

54+
/** The number of currently used non-sector bookmarks. */
55+
int lfs_non_s_size;
56+
5257
/** The number of currently usable bookmarks. */
5358
int lfs_size;
5459

5560
/** The index where the next bookmark will get written. */
56-
int lfs_next;
61+
uint32_t lfs_next;
62+
63+
/** The index where the next non-sector bmark will get written */
64+
uint32_t lfs_next_non_s;
5765
};
5866

5967
/**
@@ -72,6 +80,7 @@ struct fcb_log {
7280
#if MYNEWT_VAL(LOG_FCB_BOOKMARKS)
7381
struct log_fcb_bset fl_bset;
7482
#endif
83+
struct log *fl_log;
7584
};
7685

7786
#elif MYNEWT_VAL(LOG_FCB2)
@@ -113,9 +122,18 @@ struct fcb_log {
113122
* @param fcb_log The log to configure.
114123
* @param buf The buffer to use for bookmarks.
115124
* @param bmark_count The bookmark capacity of the supplied buffer.
125+
*
126+
* @return 0 on success, non-zero on failure
116127
*/
117-
void log_fcb_init_bmarks(struct fcb_log *fcb_log,
118-
struct log_fcb_bmark *buf, int bmark_count);
128+
int log_fcb_init_bmarks(struct fcb_log *fcb_log,
129+
struct log_fcb_bmark *buf, int bmark_count);
130+
131+
/** @brief Remove bookmarks which point to oldest FCB/FCB2 area. This is
132+
* meant to get called just before the area is rotated out.
133+
*
134+
* @param fcb_log The fcb_log to operate on.
135+
*/
136+
void log_fcb_rotate_bmarks(struct fcb_log *fcb_log);
119137

120138
/**
121139
* @brief Erases all bookmarks from the supplied fcb_log.
@@ -125,39 +143,44 @@ void log_fcb_init_bmarks(struct fcb_log *fcb_log,
125143
void log_fcb_clear_bmarks(struct fcb_log *fcb_log);
126144

127145
/**
128-
* @brief Remove bookmarks which point to oldest FCB/FCB2 area. This is
129-
* meant to get called just before the area is rotated out.
146+
* @brief Get bookmarks for a particular log
130147
*
131-
* @param fcb_log The fcb_log to operate on.
148+
* @param log Pointer to the log we want to read bookmarks from
149+
* @param bmarks_size Pointer to the variable we want to read bookmarks into
150+
*
151+
* @return Pointer to the bookmarks array for the provided log
132152
*/
133-
void log_fcb_rotate_bmarks(struct fcb_log *fcb_log);
153+
struct log_fcb_bmark *log_fcb_get_bmarks(struct log *log, uint32_t *bmarks_size);
134154

135155
/**
136156
* @brief Searches an fcb_log for the closest bookmark that comes before or at
137157
* the specified index.
138158
*
139159
* @param fcb_log The log to search.
140160
* @param index The index to look for.
161+
* @param min_diff If bookmark was found, fill it up with the difference
162+
* else it will return -1.
141163
*
142164
* @return The closest bookmark on success;
143165
* NULL if the log has no applicable bookmarks.
144166
*/
145-
const struct log_fcb_bmark *
146-
log_fcb_closest_bmark(const struct fcb_log *fcb_log, uint32_t index);
167+
struct log_fcb_bmark *
168+
log_fcb_closest_bmark(struct fcb_log *fcb_log, uint32_t index, int *min_diff);
147169

148170
/**
149171
* Inserts a bookmark into the provided log.
150172
*
151173
* @param fcb_log The log to insert a bookmark into.
152174
* @param entry The entry the bookmark should point to.
153175
* @param index The log entry index of the bookmark.
176+
* @paran sect_bmark Bool indicating it is a sector bookmark.
154177
*/
155178
#if MYNEWT_VAL(LOG_FCB)
156-
void log_fcb_add_bmark(struct fcb_log *fcb_log, const struct fcb_entry *entry,
157-
uint32_t index);
179+
void log_fcb_add_bmark(struct fcb_log *fcb_log, struct fcb_entry *entry,
180+
uint32_t index, bool sect_bmark);
158181
#elif MYNEWT_VAL(LOG_FCB2)
159-
void log_fcb_add_bmark(struct fcb_log *fcb_log, const struct fcb2_entry *entry,
160-
uint32_t index);
182+
void log_fcb_add_bmark(struct fcb_log *fcb_log, struct fcb2_entry *entry,
183+
uint32_t index, bool sect_bmark);
161184
#endif
162185
#endif
163186

sys/log/full/src/log_fcb.c

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ fcb_get_fa_hdr(struct fcb *fcb, struct log *log, struct fcb_entry *fcb_entry, st
5353
* given offset is found, start walking from there.
5454
*/
5555
static int
56-
fcb_walk_back_find_start(struct fcb *fcb, struct log *log, struct log_offset *log_offset, struct fcb_entry *fcb_entry)
56+
fcb_walk_back_find_start(struct fcb *fcb, struct log *log,
57+
struct log_offset *log_offset,
58+
struct fcb_entry *fcb_entry)
5759
{
5860
struct flash_area *fap;
5961
struct log_entry_hdr hdr;
@@ -108,6 +110,7 @@ fcb_walk_back_find_start(struct fcb *fcb, struct log *log, struct log_offset *lo
108110
* ignored; the "index" field is used instead.
109111
*
110112
* XXX: We should rename "timestamp" or make it an actual timestamp.
113+
* If bmark is found, fill up min_diff.
111114
*
112115
* The "index" field corresponds to a log entry index.
113116
*
@@ -119,10 +122,10 @@ fcb_walk_back_find_start(struct fcb *fcb, struct log *log, struct log_offset *lo
119122
*/
120123
static int
121124
log_fcb_find_gte(struct log *log, struct log_offset *log_offset,
122-
struct fcb_entry *out_entry)
125+
struct fcb_entry *out_entry, int *min_diff)
123126
{
124127
#if MYNEWT_VAL(LOG_FCB_BOOKMARKS)
125-
const struct log_fcb_bmark *bmark;
128+
struct log_fcb_bmark *bmark;
126129
#endif
127130
struct log_entry_hdr hdr;
128131
struct fcb_log *fcb_log;
@@ -166,7 +169,7 @@ log_fcb_find_gte(struct log *log, struct log_offset *log_offset,
166169
}
167170

168171
#if MYNEWT_VAL(LOG_FCB_BOOKMARKS)
169-
bmark = log_fcb_closest_bmark(fcb_log, log_offset->lo_index);
172+
bmark = log_fcb_closest_bmark(fcb_log, log_offset->lo_index, min_diff);
170173
if (bmark != NULL) {
171174
*out_entry = bmark->lfb_entry;
172175
bmark_found = true;
@@ -209,13 +212,21 @@ log_fcb_start_append(struct log *log, int len, struct fcb_entry *loc)
209212
struct fcb_log *fcb_log;
210213
struct flash_area *old_fa;
211214
int rc = 0;
215+
#if MYNEWT_VAL(LOG_FCB_SECTOR_BOOKMARKS)
216+
int active_sector_cnt = 0;
217+
uint32_t idx = 0;
218+
#endif
212219
#if MYNEWT_VAL(LOG_STATS)
213220
int cnt;
214221
#endif
215222

216223
fcb_log = (struct fcb_log *)log->l_arg;
217224
fcb = &fcb_log->fl_fcb;
218225

226+
/* Cache sector count before appending */
227+
#if MYNEWT_VAL(LOG_FCB_SECTOR_BOOKMARKS)
228+
active_sector_cnt = fcb->f_active_sector_entry_count;
229+
#endif
219230
while (1) {
220231
rc = fcb_append(fcb, len, loc);
221232
if (rc == 0) {
@@ -250,8 +261,10 @@ log_fcb_start_append(struct log *log, int len, struct fcb_entry *loc)
250261
log->l_rotate_notify_cb(log);
251262
}
252263

253-
#if MYNEWT_VAL(LOG_FCB_BOOKMARKS)
254-
/* The FCB needs to be rotated. */
264+
#if MYNEWT_VAL(LOG_FCB_BOOKMARKS) && !MYNEWT_VAL(LOG_FCB_SECTOR_BOOKMARKS)
265+
/* The FCB needs to be rotated. For sector bookmarks
266+
* we just re-initialize the bookmarks
267+
*/
255268
log_fcb_rotate_bmarks(fcb_log);
256269
#endif
257270

@@ -260,6 +273,14 @@ log_fcb_start_append(struct log *log, int len, struct fcb_entry *loc)
260273
goto err;
261274
}
262275

276+
#if MYNEWT_VAL(LOG_FCB_SECTOR_BOOKMARKS)
277+
/* The FCB needs to be rotated, reinit previously allocated
278+
* bookmarks
279+
*/
280+
log_fcb_init_bmarks(fcb_log, fcb_log->fl_bset.lfs_bmarks,
281+
fcb_log->fl_bset.lfs_cap);
282+
#endif
283+
263284
#if MYNEWT_VAL(LOG_STORAGE_WATERMARK)
264285
/*
265286
* FCB was rotated successfully so let's check if watermark was within
@@ -273,6 +294,20 @@ log_fcb_start_append(struct log *log, int len, struct fcb_entry *loc)
273294
#endif
274295
}
275296

297+
#if MYNEWT_VAL(LOG_FCB_SECTOR_BOOKMARKS)
298+
/* Add bookmark if entry is added to a new sector */
299+
if (!rc && log->l_log->log_type != LOG_TYPE_STREAM) {
300+
if (active_sector_cnt > fcb->f_active_sector_entry_count) {
301+
#if MYNEWT_VAL(LOG_GLOBAL_IDX)
302+
idx = g_log_info.li_next_index;
303+
#else
304+
idx = log->l_idx;
305+
#endif
306+
log_fcb_add_bmark(fcb_log, loc, idx, true);
307+
}
308+
}
309+
#endif
310+
276311
err:
277312
return (rc);
278313
}
@@ -582,12 +617,13 @@ log_fcb_walk_impl(struct log *log, log_walk_func_t walk_func,
582617
struct flash_area *fap;
583618
int rc;
584619
struct fcb_entry_cache cache;
620+
int min_diff = -1;
585621

586622
fcb_log = log->l_arg;
587623
fcb = &fcb_log->fl_fcb;
588624

589625
/* Locate the starting point of the walk. */
590-
rc = log_fcb_find_gte(log, log_offset, &loc);
626+
rc = log_fcb_find_gte(log, log_offset, &loc, &min_diff);
591627
switch (rc) {
592628
case 0:
593629
/* Found a starting point. */
@@ -611,9 +647,12 @@ log_fcb_walk_impl(struct log *log, log_walk_func_t walk_func,
611647
#if MYNEWT_VAL(LOG_FCB_BOOKMARKS)
612648
/* If a minimum index was specified (i.e., we are not just retrieving the
613649
* last entry), add a bookmark pointing to this walk's start location.
650+
* Only add a bmark if the index is non-zero and an exactly matching bmark
651+
* was not found. If an exactly matching bmark was found, min_diff is 0,
652+
* else it stays -1 or is great than 0.
614653
*/
615-
if (log_offset->lo_ts >= 0) {
616-
log_fcb_add_bmark(fcb_log, &loc, log_offset->lo_index);
654+
if ((log_offset->lo_ts >= 0 && log_offset->lo_index > 0) && min_diff != 0) {
655+
log_fcb_add_bmark(fcb_log, &loc, log_offset->lo_index, false);
617656
}
618657
#endif
619658

@@ -674,15 +713,15 @@ log_fcb_flush(struct log *log)
674713
static int
675714
log_fcb_registered(struct log *log)
676715
{
716+
struct fcb_log *fl = (struct fcb_log *)log->l_arg;
717+
718+
fl->fl_log = log;
677719
#if MYNEWT_VAL(LOG_STORAGE_WATERMARK)
678-
struct fcb_log *fl;
679720
#if MYNEWT_VAL(LOG_PERSIST_WATERMARK)
680721
struct fcb *fcb;
681722
struct fcb_entry loc;
682723
#endif
683724

684-
fl = (struct fcb_log *)log->l_arg;
685-
686725
#if MYNEWT_VAL(LOG_PERSIST_WATERMARK)
687726
fcb = &fl->fl_fcb;
688727

sys/log/full/src/log_fcb2.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ log_fcb2_find_gte(struct log *log, struct log_offset *log_offset,
6363
struct fcb_log *fcb_log;
6464
struct fcb2 *fcb;
6565
int rc;
66+
int min_diff = -1;
6667

6768
fcb_log = log->l_arg;
6869
fcb = &fcb_log->fl_fcb;
@@ -103,7 +104,7 @@ log_fcb2_find_gte(struct log *log, struct log_offset *log_offset,
103104
return SYS_EUNKNOWN;
104105
}
105106
#if MYNEWT_VAL(LOG_FCB_BOOKMARKS)
106-
bmark = log_fcb_closest_bmark(fcb_log, log_offset->lo_index);
107+
bmark = log_fcb_closest_bmark(fcb_log, log_offset->lo_index, &min_diff);
107108
if (bmark != NULL) {
108109
*out_entry = bmark->lfb_entry;
109110
}
@@ -169,7 +170,7 @@ log_fcb2_start_append(struct log *log, int len, struct fcb2_entry *loc)
169170
}
170171
#endif
171172

172-
#if MYNEWT_VAL(LOG_FCB_BOOKMARKS)
173+
#if MYNEWT_VAL(LOG_FCB_BOOKMARKS) && !MYNEWT_VAL(LOG_FCB_SECTOR_BOOKMARKS)
173174
/* The FCB needs to be rotated. */
174175
log_fcb_rotate_bmarks(fcb_log);
175176
#endif
@@ -179,6 +180,14 @@ log_fcb2_start_append(struct log *log, int len, struct fcb2_entry *loc)
179180
goto err;
180181
}
181182

183+
#if MYNEWT_VAL(LOG_FCB_SECTOR_BOOKMARKS)
184+
/* The FCB needs to be rotated, reinit previously allocated
185+
* bookmarks
186+
*/
187+
log_fcb_init_bmarks(fcb_log, fcb_log->fl_bset.lfs_bmarks,
188+
fcb_log->fl_bset.lfs_cap);
189+
#endif
190+
182191
#if MYNEWT_VAL(LOG_STORAGE_WATERMARK)
183192
/*
184193
* FCB was rotated successfully so let's check if watermark was within
@@ -497,8 +506,8 @@ log_fcb2_walk(struct log *log, log_walk_func_t walk_func,
497506
/* If a minimum index was specified (i.e., we are not just retrieving the
498507
* last entry), add a bookmark pointing to this walk's start location.
499508
*/
500-
if (log_off->lo_ts >= 0) {
501-
log_fcb_add_bmark(fcb_log, &loc, log_off->lo_index);
509+
if ((log_off->lo_ts >= 0 && log_off->lo_index > 0) && min_diff != 0) {
510+
log_fcb_add_bmark(fcb_log, &loc, log_off->lo_index, false);
502511
}
503512
#endif
504513

0 commit comments

Comments
 (0)