From cd281ab40b684d2df484d7d2ea6f122218b338be Mon Sep 17 00:00:00 2001 From: Kamil Krzyzanowski Date: Tue, 4 Feb 2025 14:25:41 +0100 Subject: [PATCH] logging: fs backend: simplify checking if file exists In order to check if the desired log file exists, the backend would open the directory, then go through all the files seeing if one of them matches the correct filename. Simplify to just `fs_stat` the file instead. This has the added side effect of lowering the time spent checking if the file exists after every log. Some quick testing revealed the time spent checking went down from ~150-300ms to ~10ms (on my specific board, with a nRF9160 writing to a LittleFS on external flash). Signed-off-by: Kamil Krzyzanowski --- subsys/logging/backends/log_backend_fs.c | 55 +++++++++--------------- 1 file changed, 21 insertions(+), 34 deletions(-) diff --git a/subsys/logging/backends/log_backend_fs.c b/subsys/logging/backends/log_backend_fs.c index 94db8caaa3b..8be917466b6 100644 --- a/subsys/logging/backends/log_backend_fs.c +++ b/subsys/logging/backends/log_backend_fs.c @@ -105,42 +105,34 @@ static int create_log_dir(const char *path) } +static int get_log_path(char *buf, size_t buf_len, int num) +{ + if (num > MAX_FILE_NUMERAL) { + return -EINVAL; + } + return snprintf(buf, buf_len, "%s/%s%04d", CONFIG_LOG_BACKEND_FS_DIR, + CONFIG_LOG_BACKEND_FS_FILE_PREFIX, num); +} + static int check_log_file_exist(int num) { - struct fs_dir_t dir; struct fs_dirent ent; + char fname[MAX_PATH_LEN]; int rc; - fs_dir_t_init(&dir); + rc = get_log_path(fname, sizeof(fname), num); - rc = fs_opendir(&dir, CONFIG_LOG_BACKEND_FS_DIR); - if (rc) { - return -EIO; + if (rc < 0) { + return rc; } - while (true) { - rc = fs_readdir(&dir, &ent); - if (rc < 0) { - rc = -EIO; - goto close_dir; - } - if (ent.name[0] == 0) { - break; - } + rc = fs_stat(fname, &ent); - rc = get_log_file_id(&ent); - - if (rc == num) { - rc = 1; - goto close_dir; - } + if (rc == 0) { + return 1; + } else if (rc == -ENOENT) { + return 0; } - - rc = 0; - -close_dir: - (void) fs_closedir(&dir); - return rc; } @@ -333,8 +325,7 @@ static int allocate_new_file(struct fs_file_t *file) curr_file_num = newest; /* Is there space left in the newest file? */ - snprintf(fname, sizeof(fname), "%s/%s%04d", CONFIG_LOG_BACKEND_FS_DIR, - CONFIG_LOG_BACKEND_FS_FILE_PREFIX, curr_file_num); + get_log_path(fname, sizeof(fname), curr_file_num); rc = fs_open(file, fname, FS_O_CREATE | FS_O_WRITE | FS_O_APPEND); if (rc < 0) { goto out; @@ -395,9 +386,7 @@ static int allocate_new_file(struct fs_file_t *file) } } - snprintf(fname, sizeof(fname), "%s/%s%04d", - CONFIG_LOG_BACKEND_FS_DIR, - CONFIG_LOG_BACKEND_FS_FILE_PREFIX, curr_file_num); + get_log_path(fname, sizeof(fname), curr_file_num); rc = fs_open(file, fname, FS_O_CREATE | FS_O_WRITE); if (rc < 0) { @@ -416,9 +405,7 @@ static int del_oldest_log(void) static char dellname[MAX_PATH_LEN]; while (true) { - snprintf(dellname, sizeof(dellname), "%s/%s%04d", - CONFIG_LOG_BACKEND_FS_DIR, - CONFIG_LOG_BACKEND_FS_FILE_PREFIX, oldest); + get_log_path(dellname, sizeof(dellname), oldest); rc = fs_unlink(dellname); if ((rc == 0) || (rc == -ENOENT)) {