Skip to content

Commit 39146a7

Browse files
committed
sys/log: As per review comments, improve log_fcb_init_bmarks()
1 parent 96f43aa commit 39146a7

File tree

3 files changed

+85
-43
lines changed

3 files changed

+85
-43
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ struct log_fcb_bset {
5252
/** The maximum number of bookmarks. */
5353
int lfs_cap;
5454

55+
/** The maximum number of sector bookmarks. */
56+
int lfs_sect_cap;
57+
5558
/** The number of currently used non-sector bookmarks. */
5659
int lfs_non_sect_size;
5760

@@ -61,6 +64,9 @@ struct log_fcb_bset {
6164
/** The index where the next non-sector bmark will get written */
6265
uint32_t lfs_next_non_sect;
6366

67+
/** The sector interval at which to insert sector bookmarks */
68+
int lfs_sect_bmark_itvl;
69+
6470
/** The index where the next sector bmark will get written */
6571
uint32_t lfs_next_sect;
6672
};

sys/log/full/src/log_fcb2.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,12 @@ log_fcb2_start_append(struct log *log, int len, struct fcb2_entry *loc)
187187
/* The FCB needs to be rotated, reinit previously allocated
188188
* bookmarks
189189
*/
190-
log_fcb_init_bmarks(fcb_log, fcb_log->fl_bset.lfs_bmarks,
191-
fcb_log->fl_bset.lfs_cap,
192-
fcb_log->fl_bset.lfs_en_sect_bmarks);
190+
rc = log_fcb_init_bmarks(fcb_log, fcb_log->fl_bset.lfs_bmarks,
191+
fcb_log->fl_bset.lfs_cap,
192+
fcb_log->fl_bset.lfs_en_sect_bmarks);
193+
if (rc) {
194+
goto err;
195+
}
193196
#endif
194197

195198
#if MYNEWT_VAL(LOG_STORAGE_WATERMARK)

sys/log/full/src/log_fcb_bmark.c

Lines changed: 73 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424
#if MYNEWT_VAL(LOG_FCB_BOOKMARKS)
2525
#include "log/log.h"
2626
#include "log/log_fcb.h"
27-
#include <console/console.h>
2827

2928
#if MYNEWT_VAL(LOG_FCB_SECTOR_BOOKMARKS)
3029
static int
3130
log_fcb_init_sector_bmarks(struct fcb_log *fcb_log)
3231
{
3332
int rc = 0;
3433
int i = 0;
34+
int j = 0;
35+
struct log_fcb_bset *bset = &fcb_log->fl_bset;
3536
struct log_entry_hdr ueh = {0};
3637
#if MYNEWT_VAL(LOG_FCB)
3738
struct fcb_entry loc = {0};
@@ -54,8 +55,8 @@ log_fcb_init_sector_bmarks(struct fcb_log *fcb_log)
5455
/* Start adding a bookmark from the end of array just before
5556
* non-sector bookmarks
5657
*/
57-
fcb_log->fl_bset.lfs_next_sect = fcb_log->fl_fcb.f_sector_cnt - 1;
58-
for (i = 0; i < fcb_log->fl_fcb.f_sector_cnt; i++) {
58+
bset->lfs_next_sect = bset->lfs_sect_cap - 1;
59+
for (i = 0; i < bset->lfs_sect_cap; i++) {
5960
rc = log_read_hdr(fcb_log->fl_log, &loc, &ueh);
6061
if (rc) {
6162
/* Read failed, don't add a bookmark, done adding bookmarks */
@@ -69,7 +70,17 @@ log_fcb_init_sector_bmarks(struct fcb_log *fcb_log)
6970
}
7071

7172
#if MYNEWT_VAL(LOG_FCB)
72-
fa = fcb_getnext_area(&fcb_log->fl_fcb, loc.fe_area);
73+
j = 0;
74+
75+
/* Keep skipping sectors until lfs_sect_bmark_itvl is reached */
76+
do {
77+
fa = fcb_getnext_area(&fcb_log->fl_fcb, loc.fe_area);
78+
if (!fa) {
79+
break;
80+
}
81+
j++;
82+
} while (j < bset->lfs_sect_bmark_itvl);
83+
7384
if (!fa) {
7485
break;
7586
}
@@ -80,8 +91,21 @@ log_fcb_init_sector_bmarks(struct fcb_log *fcb_log)
8091
break;
8192
}
8293
#else
83-
/* Get next range */
84-
range = fcb2_getnext_range(&fcb_log->fl_fcb, &loc);
94+
j = 0;
95+
96+
/* Keep skipping rangess until lfs_sect_bmark_itvl is reached */
97+
do {
98+
/* Get next range */
99+
range = fcb2_getnext_range(&fcb_log->fl_fcb, &loc);
100+
if (!range) {
101+
break;
102+
}
103+
j++;
104+
} while (j < bset->lfs_sect_bmark_itvl);
105+
106+
if (!range) {
107+
break;
108+
}
85109

86110
/* First entry in the next area */
87111
rc = fcb2_getnext_in_area(&fcb_log->fl_fcb, range, &loc);
@@ -97,38 +121,51 @@ log_fcb_init_sector_bmarks(struct fcb_log *fcb_log)
97121

98122
int
99123
log_fcb_init_bmarks(struct fcb_log *fcb_log,
100-
struct log_fcb_bmark *buf, int bmark_count,
124+
struct log_fcb_bmark *buf, int avl_bmark_cnt,
101125
bool en_sect_bmarks)
102126
{
103-
int bmark_cnt = MYNEWT_VAL(LOG_FCB_NUM_ABS_BOOKMARKS);
104-
105-
#if MYNEWT_VAL(LOG_FCB_SECTOR_BOOKMARKS)
106-
if (en_sect_bmarks) {
107-
bmark_cnt += fcb_log->fl_fcb.f_sector_cnt;
108-
}
109-
#endif
127+
struct log_fcb_bset *bset = &fcb_log->fl_bset;
128+
int reqd_bmark_cnt = MYNEWT_VAL(LOG_FCB_NUM_ABS_BOOKMARKS);
110129

111-
/* Make sure we have allocated the exact number of bookmarks
112-
*/
113-
if (bmark_count != bmark_cnt)
114-
{
115-
return -1;
130+
(void)reqd_bmark_cnt;
131+
if (!bset || !buf || !avl_bmark_cnt) {
132+
return SYS_EINVAL;
116133
}
117134

118-
memset(&fcb_log->fl_bset, 0, sizeof(fcb_log->fl_bset));
119-
memset(buf, 0, sizeof(struct log_fcb_bmark) * bmark_count);
135+
memset(bset, 0, sizeof(*bset));
136+
memset(buf, 0, sizeof(struct log_fcb_bmark) * avl_bmark_cnt);
120137

121-
fcb_log->fl_bset = (struct log_fcb_bset) {
138+
*bset = (struct log_fcb_bset) {
122139
.lfs_bmarks = buf,
123-
.lfs_cap = bmark_count,
140+
.lfs_cap = avl_bmark_cnt,
124141
.lfs_en_sect_bmarks = en_sect_bmarks
125142
};
126143

144+
#if MYNEWT_VAL(LOG_FCB_SECTOR_BOOKMARKS)
145+
if (en_sect_bmarks) {
146+
/* Default sector bookmark interval is 1 */
147+
bset->lfs_sect_bmark_itvl = 1;
148+
reqd_bmark_cnt += fcb_log->fl_fcb.f_sector_cnt;
149+
/* Make sure we have allocated the exact number of bookmarks */
150+
if (avl_bmark_cnt < reqd_bmark_cnt) {
151+
/* Not enough space allocated for sector bookmarks,
152+
* add bookmarks at sector intervals */
153+
bset->lfs_sect_bmark_itvl =
154+
fcb_log->fl_fcb.f_sector_cnt / avl_bmark_cnt;
155+
bset->lfs_sect_cap = avl_bmark_cnt -
156+
MYNEWT_VAL(LOG_FCB_NUM_ABS_BOOKMARKS);
157+
} else {
158+
bset->lfs_sect_cap = fcb_log->fl_fcb.f_sector_cnt;
159+
}
160+
}
161+
#endif
162+
127163
#if MYNEWT_VAL(LOG_FCB_SECTOR_BOOKMARKS)
128164
if (en_sect_bmarks) {
129165
return log_fcb_init_sector_bmarks(fcb_log);
130166
}
131167
#endif
168+
132169
return 0;
133170
}
134171

@@ -203,11 +240,11 @@ log_fcb_closest_bmark(struct fcb_log *fcb_log, uint32_t index,
203240
#if MYNEWT_VAL(LOG_FCB)
204241
if (!fcb_log->fl_bset.lfs_bmarks[i].lfb_entry.fe_area) {
205242
#if MYNEWT_VAL(LOG_FCB_SECTOR_BOOKMARKS)
206-
if (i < fcb_log->fl_fcb.f_sector_cnt) {
243+
if (i < fcb_log->fl_bset.lfs_sect_cap) {
207244
/* Jump to the non-sector bookmarks since sector
208245
* bookmarks are empty here on
209246
*/
210-
i = fcb_log->fl_fcb.f_sector_cnt - 1;
247+
i = fcb_log->fl_bset.lfs_sect_cap - 1;
211248
continue;
212249
}
213250
#else
@@ -219,11 +256,11 @@ log_fcb_closest_bmark(struct fcb_log *fcb_log, uint32_t index,
219256
#elif MYNEWT_VAL(LOG_FCB2)
220257
if (!fcb_log->fl_bset.lfs_bmarks[i].lfb_entry.fe_range) {
221258
#if MYNEWT_VAL(LOG_FCB_SECTOR_BOOKMARKS)
222-
if (i < fcb_log->fl_fcb.f_sector_cnt) {
259+
if (i < fcb_log->fl_bset.lfs_sect_cap) {
223260
/* Jump to the non-sector bookmarks since sector
224261
* bookmarks are empty here on
225262
*/
226-
i = fcb_log->fl_fcb.f_sector_cnt - 1;
263+
i = fcb_log->fl_bset.lfs_sect_cap - 1;
227264
continue;
228265
}
229266
#else
@@ -274,11 +311,10 @@ log_fcb_insert_sect_bmark(struct fcb_log *fcb_log, struct fcb2_entry *entry,
274311
.lfb_index = index,
275312
};
276313

277-
if (bset->lfs_size < fcb_log->fl_fcb.f_sector_cnt &&
278-
bset->lfs_size < bset->lfs_cap) {
314+
if (bset->lfs_size < fcb_log->fl_bset.lfs_sect_cap) {
279315
bset->lfs_size++;
280-
bset->lfs_next_sect = (bset->lfs_next_sect - 1)%
281-
fcb_log->fl_fcb.f_sector_cnt;
316+
bset->lfs_next_sect = (bset->lfs_next_sect - 1) %
317+
fcb_log->fl_bset.lfs_sect_cap;
282318
} else {
283319
return -1;
284320
}
@@ -363,16 +399,15 @@ log_fcb_add_bmark(struct fcb_log *fcb_log, struct fcb2_entry *entry,
363399
return rc;
364400
}
365401
MODLOG_DEBUG(LOG_MODULE_DEFAULT, "insert bmark index: %u, pos: %u\n",
366-
(unsigned int)index, bset->lfs_next_sect);
402+
index, bset->lfs_next_sect);
367403
} else {
368404
/* Replace oldest non-sector bmark */
369405
rc = log_fcb_replace_non_sect_bmark(fcb_log, entry, index,
370406
bset->lfs_next_non_sect +
371407
(bset->lfs_en_sect_bmarks ?
372-
fcb_log->fl_fcb.f_sector_cnt : 0));
408+
fcb_log->fl_fcb.f_sector_cnt : 0));
373409
MODLOG_DEBUG(LOG_MODULE_DEFAULT, "replace bmark index: %u, pos: %u\n",
374-
(unsigned int)index,
375-
(unsigned int)bset->lfs_next_non_sect +
410+
index, bset->lfs_next_non_sect +
376411
(bset->lfs_en_sect_bmarks ?
377412
fcb_log->fl_fcb.f_sector_cnt : 0));
378413

@@ -389,18 +424,16 @@ log_fcb_add_bmark(struct fcb_log *fcb_log, struct fcb2_entry *entry,
389424
if (bset->lfs_size >= MYNEWT_VAL(LOG_FCB_NUM_ABS_BOOKMARKS)) {
390425
/* Replace oldest non-sector bmark */
391426
rc = log_fcb_replace_non_sect_bmark(fcb_log, entry, index,
392-
bset->lfs_next_non_sect);
427+
bset->lfs_next_non_sect);
393428
MODLOG_DEBUG(LOG_MODULE_DEFAULT, "replace bmark index: %u, pos: %u\n",
394-
(unsigned int)index,
395-
(unsigned int)bset->lfs_next_non_sect);
429+
index, bset->lfs_next_non_sect);
396430
bset->lfs_next_non_sect = (bset->lfs_next_non_sect + 1) %
397431
MYNEWT_VAL(LOG_FCB_NUM_ABS_BOOKMARKS);
398432
} else {
399433
rc = log_fcb_replace_non_sect_bmark(fcb_log, entry, index,
400434
bset->lfs_size);
401435
MODLOG_DEBUG(LOG_MODULE_DEFAULT, "replace bmark index: %u, pos: %u\n",
402-
(unsigned int)index,
403-
bset->lfs_size);
436+
index, bset->lfs_size);
404437
if (!bset->lfs_size) {
405438
/* First non-sector bmark position */
406439
bset->lfs_next_non_sect = 0;

0 commit comments

Comments
 (0)