Skip to content

Commit cd281ab

Browse files
kamnxtkartben
authored andcommitted
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 <[email protected]>
1 parent 8348d9a commit cd281ab

File tree

1 file changed

+21
-34
lines changed

1 file changed

+21
-34
lines changed

subsys/logging/backends/log_backend_fs.c

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -105,42 +105,34 @@ static int create_log_dir(const char *path)
105105

106106
}
107107

108+
static int get_log_path(char *buf, size_t buf_len, int num)
109+
{
110+
if (num > MAX_FILE_NUMERAL) {
111+
return -EINVAL;
112+
}
113+
return snprintf(buf, buf_len, "%s/%s%04d", CONFIG_LOG_BACKEND_FS_DIR,
114+
CONFIG_LOG_BACKEND_FS_FILE_PREFIX, num);
115+
}
116+
108117
static int check_log_file_exist(int num)
109118
{
110-
struct fs_dir_t dir;
111119
struct fs_dirent ent;
120+
char fname[MAX_PATH_LEN];
112121
int rc;
113122

114-
fs_dir_t_init(&dir);
123+
rc = get_log_path(fname, sizeof(fname), num);
115124

116-
rc = fs_opendir(&dir, CONFIG_LOG_BACKEND_FS_DIR);
117-
if (rc) {
118-
return -EIO;
125+
if (rc < 0) {
126+
return rc;
119127
}
120128

121-
while (true) {
122-
rc = fs_readdir(&dir, &ent);
123-
if (rc < 0) {
124-
rc = -EIO;
125-
goto close_dir;
126-
}
127-
if (ent.name[0] == 0) {
128-
break;
129-
}
129+
rc = fs_stat(fname, &ent);
130130

131-
rc = get_log_file_id(&ent);
132-
133-
if (rc == num) {
134-
rc = 1;
135-
goto close_dir;
136-
}
131+
if (rc == 0) {
132+
return 1;
133+
} else if (rc == -ENOENT) {
134+
return 0;
137135
}
138-
139-
rc = 0;
140-
141-
close_dir:
142-
(void) fs_closedir(&dir);
143-
144136
return rc;
145137
}
146138

@@ -333,8 +325,7 @@ static int allocate_new_file(struct fs_file_t *file)
333325
curr_file_num = newest;
334326

335327
/* Is there space left in the newest file? */
336-
snprintf(fname, sizeof(fname), "%s/%s%04d", CONFIG_LOG_BACKEND_FS_DIR,
337-
CONFIG_LOG_BACKEND_FS_FILE_PREFIX, curr_file_num);
328+
get_log_path(fname, sizeof(fname), curr_file_num);
338329
rc = fs_open(file, fname, FS_O_CREATE | FS_O_WRITE | FS_O_APPEND);
339330
if (rc < 0) {
340331
goto out;
@@ -395,9 +386,7 @@ static int allocate_new_file(struct fs_file_t *file)
395386
}
396387
}
397388

398-
snprintf(fname, sizeof(fname), "%s/%s%04d",
399-
CONFIG_LOG_BACKEND_FS_DIR,
400-
CONFIG_LOG_BACKEND_FS_FILE_PREFIX, curr_file_num);
389+
get_log_path(fname, sizeof(fname), curr_file_num);
401390

402391
rc = fs_open(file, fname, FS_O_CREATE | FS_O_WRITE);
403392
if (rc < 0) {
@@ -416,9 +405,7 @@ static int del_oldest_log(void)
416405
static char dellname[MAX_PATH_LEN];
417406

418407
while (true) {
419-
snprintf(dellname, sizeof(dellname), "%s/%s%04d",
420-
CONFIG_LOG_BACKEND_FS_DIR,
421-
CONFIG_LOG_BACKEND_FS_FILE_PREFIX, oldest);
408+
get_log_path(dellname, sizeof(dellname), oldest);
422409
rc = fs_unlink(dellname);
423410

424411
if ((rc == 0) || (rc == -ENOENT)) {

0 commit comments

Comments
 (0)