From d6423b55273d62cb4a502223673934accee287da Mon Sep 17 00:00:00 2001 From: Jiuzhu Dong Date: Tue, 4 Jan 2022 18:33:11 +0800 Subject: [PATCH] romfs: expand file cache by CONFIG_FS_ROMFS_FCACHE_NSECTORS The default size of file cache is size of a sector, it may not be good size for optimizing read/write speed in physical device. So we can set the config according to speed test profile to optimize access IO speed. Signed-off-by: dongjiuzhu1 --- fs/romfs/Kconfig | 7 +++++++ fs/romfs/fs_romfs.c | 6 ++++-- fs/romfs/fs_romfs.h | 4 +++- fs/romfs/fs_romfsutil.c | 33 +++++++++++++++++++++++++-------- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/fs/romfs/Kconfig b/fs/romfs/Kconfig index f0389ad6b5..427cf3bf85 100644 --- a/fs/romfs/Kconfig +++ b/fs/romfs/Kconfig @@ -20,4 +20,11 @@ config FS_ROMFS_CACHE_NODE is mounted so that we can quick access entry of ROMFS filesystem on emmc/sdcard. +config FS_ROMFS_CACHE_FILE_NSECTORS + int "The number of file cache sector" + range 1 256 + default 1 + ---help--- + The number of file cache sector + endif diff --git a/fs/romfs/fs_romfs.c b/fs/romfs/fs_romfs.c index 031f0078ae..38dafdb0e8 100644 --- a/fs/romfs/fs_romfs.c +++ b/fs/romfs/fs_romfs.c @@ -424,7 +424,7 @@ static ssize_t romfs_read(FAR struct file *filep, FAR char *buffer, */ nsectors = SEC_NSECTORS(rm, buflen); - if (nsectors > 0 && sectorndx == 0) + if (nsectors >= rf->rf_ncachesector && sectorndx == 0) { /* Read maximum contiguous sectors directly to the user's * buffer without using our tiny read buffer. @@ -460,7 +460,9 @@ static ssize_t romfs_read(FAR struct file *filep, FAR char *buffer, /* Copy the partial sector into the user buffer */ - bytesread = rm->rm_hwsectorsize - sectorndx; + bytesread = (rf->rf_cachesector + rf->rf_ncachesector - sector) * + rm->rm_hwsectorsize - sectorndx; + sectorndx = rf->rf_ncachesector * rm->rm_hwsectorsize - bytesread; if (bytesread > buflen) { /* We will not read to the end of the buffer */ diff --git a/fs/romfs/fs_romfs.h b/fs/romfs/fs_romfs.h index 4e28b8e319..2db26b5884 100644 --- a/fs/romfs/fs_romfs.h +++ b/fs/romfs/fs_romfs.h @@ -149,8 +149,10 @@ struct romfs_mountpt_s struct romfs_file_s { uint32_t rf_startoffset; /* Offset to the start of the file data */ + uint32_t rf_endsector; /* Last sector of the file data */ uint32_t rf_size; /* Size of the file in bytes */ - uint32_t rf_cachesector; /* Current sector in the rf_buffer */ + uint32_t rf_cachesector; /* First sector in the rf_buffer */ + uint32_t rf_ncachesector; /* Number of sectors in the rf_buffer */ FAR uint8_t *rf_buffer; /* File sector buffer, allocated if rm_xipbase==0 */ uint8_t rf_type; /* File type (for fstat()) */ char rf_path[1]; /* Path of open file */ diff --git a/fs/romfs/fs_romfsutil.c b/fs/romfs/fs_romfsutil.c index 1f9a057be3..326cf98cb3 100644 --- a/fs/romfs/fs_romfsutil.c +++ b/fs/romfs/fs_romfsutil.c @@ -574,17 +574,18 @@ int romfs_filecacheread(FAR struct romfs_mountpt_s *rm, { int ret; - finfo("sector: %" PRId32 " cached: %" PRId32 + finfo("sector: %" PRId32 " cached: %" PRId32 " ncached: %" PRId32 "" " sectorsize: %d XIP base: %p buffer: %p\n", - sector, rf->rf_cachesector, rm->rm_hwsectorsize, - rm->rm_xipbase, rf->rf_buffer); + sector, rf->rf_cachesector, rf->rf_ncachesector, + rm->rm_hwsectorsize, rm->rm_xipbase, rf->rf_buffer); /* rf->rf_cachesector holds the current sector that is buffer in or * referenced by rf->rf_buffer. If the requested sector is the same as this * sector then we do nothing. */ - if (rf->rf_cachesector != sector) + if (rf->rf_cachesector > sector || + rf->rf_cachesector + rf->rf_ncachesector <= sector) { /* Check the access mode */ @@ -599,10 +600,15 @@ int romfs_filecacheread(FAR struct romfs_mountpt_s *rm, } else { + if (sector + rf->rf_ncachesector - 1 > rf->rf_endsector) + { + sector = rf->rf_endsector + 1 - rf->rf_ncachesector; + } + /* In non-XIP mode, we will have to read the new sector. */ finfo("Calling romfs_hwread\n"); - ret = romfs_hwread(rm, rf->rf_buffer, sector, 1); + ret = romfs_hwread(rm, rf->rf_buffer, sector, rf->rf_ncachesector); if (ret < 0) { ferr("ERROR: romfs_hwread failed: %d\n", ret); @@ -802,18 +808,29 @@ int romfs_fileconfigure(FAR struct romfs_mountpt_s *rm, { /* We'll put a valid address in rf_buffer just in case. */ - rf->rf_cachesector = 0; - rf->rf_buffer = rm->rm_xipbase; + rf->rf_cachesector = 0; + rf->rf_buffer = rm->rm_xipbase; + rf->rf_ncachesector = 1; } else { + uint32_t nsectors; + + rf->rf_endsector = SEC_NSECTORS(rm, rf->rf_startoffset + rf->rf_size); + nsectors = rf->rf_endsector - SEC_NSECTORS(rm, rf->rf_startoffset) + 1; + if (nsectors > CONFIG_FS_ROMFS_CACHE_FILE_NSECTORS) + { + nsectors = CONFIG_FS_ROMFS_CACHE_FILE_NSECTORS; + } + /* Nothing in the cache buffer */ rf->rf_cachesector = (uint32_t)-1; + rf->rf_ncachesector = nsectors; /* Create a file buffer to support partial sector accesses */ - rf->rf_buffer = (FAR uint8_t *)kmm_malloc(rm->rm_hwsectorsize); + rf->rf_buffer = kmm_malloc(rm->rm_hwsectorsize * rf->rf_ncachesector); if (!rf->rf_buffer) { return -ENOMEM;