-
Notifications
You must be signed in to change notification settings - Fork 371
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
Changes from all commits
091f2a4
fa41971
a43ecf6
1082b27
96f43aa
39146a7
208c970
940cc46
83b3365
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
vrahane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** 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; | ||
}; | ||
|
||
/** | ||
|
@@ -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) | ||
|
@@ -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 | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, let me give it a shot. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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 | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(<fbu_fcb_log, ltfbu_bmarks, cfg->bmark_count); | ||
log_fcb_init_bmarks(<fbu_fcb_log, ltfbu_bmarks, cfg->bmark_count, false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the only place in mynewt repository where There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, §or_count, NULL);
test_log_sectors = calloc(sector_count, sizeof(*test_log_sectors));
flash_area_to_sectors(FLASH_AREA_TEST_LOG, §or_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_register("log", <fbu_log, &log_fcb_handler, <fbu_fcb_log, | ||
|
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 |
Uh oh!
There was an error while loading. Please reload this page.