fs: fat: dynamically allocate memory for temporary buffer
PATCH 1/2 fs: fat: dynamically allocate memory for temporary buffer
From: Stefan Agner <[email protected]>
Drop the statically allocated get_contents_vfatname_block and dynamically allocate a buffer only if required. This saves 64KiB of memory.
--- fs/fat/fat.c | 19 +++++++++++++------ 1 files changed, 13 insertions(+), 6 deletions(-)
fs/fat/fat.c +13 -6
@@ -307,9 +307,6 @@ get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)* into 'buffer'. * Update the number of bytes read in *gotsize or return -1 on fatal errors. */ -__u8 get_contents_vfatname_block[MAX_CLUSTSIZE ] -__aligned(ARCH_DMA_MINALIGN) ; -static int get_contents (fsdata *mydata,dir_entry *dentptr,loff_t pos,__u8 *buffer,loff_t maxsize,loff_t *gotsize) {
@@ -320,7 +317,7 @@ static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,loff_t actsize; *gotsize =0 ; -debug ("Filesize: %llu bytes\n" , filesize); +debug ("Filesize: %llu bytes, starting at pos %llu\n" , filesize, pos);if (pos >= filesize) {debug ("Read position past EOF: %llu\n" , pos);
@@ -352,15 +349,25 @@ static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,/* align to beginning of next cluster if any */ if (pos) { +__u8 *tmp_buffer; + + tmp_buffer =malloc_cache_aligned (MAX_CLUSTSIZE ); +if (!tmp_buffer) { +debug ("Error: allocating buffer\n" ); +return -ENOMEM ; + } + actsize =min (filesize, (loff_t )bytesperclust); -if (get_cluster (mydata, curclust, get_contents_vfatname_block, +if (get_cluster (mydata, curclust, tmp_buffer, (int )actsize) !=0 ) {printf ("Error reading cluster\n" ); +free (tmp_buffer);return -1 ; } filesize -= actsize; actsize -= pos; -memcpy (buffer, get_contents_vfatname_block + pos, actsize); +memcpy (buffer, tmp_buffer + pos, actsize); +free (tmp_buffer); *gotsize += actsize;if (!filesize)return 0 ;
Signature · 1 line
1.7.7.4
PATCH 2/2 fs: fat: Reduce default max clustersize 64KiB from malloc pool
From: Tien Fong Chee <[email protected]>
Release cluster block immediately when no longer use would help to reduce 64KiB memory allocated to the memory pool.
--- fs/fat/fat.c | 11 +++++++++-- 1 files changed, 9 insertions(+), 2 deletions(-)
fs/fat/fat.c +9 -2
@@ -1153,12 +1153,19 @@ int file_fat_read_at(const char *filename, loff_t pos, void *buffer, goto out_free_both;debug ("reading %s at pos %llu\n" , filename, pos); - ret =get_contents (&fsdata, itr->dent, pos, buffer, maxsize, actread); + +/* For saving default max clustersize memory allocated to malloc pool */ +dir_entry *dentptr = itr->dent; + +free (itr); + + ret =get_contents (&fsdata, dentptr, pos, buffer, maxsize, actread); out_free_both:free (fsdata.fatbuf); out_free_itr: -free (itr); +if (!itr) +free (itr);return ret; }
Signature · 1 line
1.7.7.4
Chee
PATCH 2/2 fs: fat: Reduce default max clustersize 64KiB from malloc pool
Quoted 25 lines · tien.fong.chee at intel.com
From: Tien Fong Chee <[email protected]>
Release cluster block immediately when no longer use would help to reduce
64KiB memory allocated to the memory pool.Signed-off-by Tien Fong Chee <[email protected]>---
fs/fat/fat.c | 11 +++++++++--1 files changed, 9 insertions(+), 2 deletions(-)fs/fat/fat.c +0
-0@@ -1153,12 +1153,19 @@ int file_fat_read_at(const char *filename, loff_t pos, void *buffer, goto out_free_both;
debug("reading %s at pos %llu\n", filename, pos);fs/fat/fat.c +0
-1- ret = get_contents(&fsdata, itr->dent, pos, buffer, maxsize,actread);
fs/fat/fat.c +2
-0+ +/* For saving default max clustersize memory allocated to malloc pool */
fs/fat/fat.c int file_fat_read_at(const char *filename, loff_t pos, void *buffer, · line 1160
Quoted 3 lines
fs/fat/fat.c +3
-0+dir_entry *dentptr = itr->dent; + +free (itr);
I noticed msising of assignning NULL to itr after freeing it. Other than this, any comment about this implementation to maximise the reusable on reduce memory pool.
Quoted 13 lines
fs/fat/fat.c +2
-0+ + ret = get_contents(&fsdata, dentptr, pos, buffer, maxsize,actread);
out_free_both:
free(fsdata.fatbuf);
out_free_itr:fs/fat/fat.c +2
-1-free (itr); +if (!itr) +free (itr);return ret;
}
PATCH 1/2 fs: fat: dynamically allocate memory for temporary buffer
In reply to tien.fong.chee at intel.comQuoted 27 lines · tien.fong.chee at intel.com
From: Stefan Agner <[email protected]>
Drop the statically allocated
get_contents_vfatname_blockand dynamically allocate a buffer only if required. This saves 64KiB of memory.Signed-off-by Stefan Agner <[email protected]>Signed-off-by Tien Fong Chee <[email protected]>--- fs/fat/fat.c | 19 +++++++++++++------ 1 files changed, 13 insertions(+), 6 deletions(-)fs/fat/fat.c +0
-3@@ -307,9 +307,6 @@ get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)* into 'buffer'. * Update the number of bytes read in *gotsize or return -1 on fatal errors. */ -__u8 get_contents_vfatname_block[MAX_CLUSTSIZE ] -__aligned(ARCH_DMA_MINALIGN) ; -static int get_contents (fsdata *mydata,dir_entry *dentptr,loff_t pos,__u8 *buffer,loff_t maxsize,loff_t *gotsize) {
fs/fat/fat.c static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, · line 320
Quoted 5 lines
fs/fat/fat.c +1
-1@@ -320,7 +317,7 @@ static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,loff_t actsize; *gotsize =0 ; -debug ("Filesize: %llu bytes\n" , filesize); +debug ("Filesize: %llu bytes, starting at pos %llu\n" , filesize, pos);
This looks like a separate change.
fs/fat/fat.c static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, · line 354
Quoted 9 lines
fs/fat/fat.c +3
-0if (pos >= filesize) {debug ("Read position past EOF: %llu\n" , pos);@@ -352,15 +349,25 @@ static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,/* align to beginning of next cluster if any */ if (pos) { +__u8 *tmp_buffer; + + tmp_buffer =malloc_cache_aligned (MAX_CLUSTSIZE );
Don't you know the cluster size by now ?
Quoted 8 lines
fs/fat/fat.c +6
-1+if (!tmp_buffer) { +debug ("Error: allocating buffer\n" ); +return -ENOMEM ; + } + actsize =min (filesize, (loff_t )bytesperclust); - if (get_cluster(mydata, curclust, get_contents_vfatname_block, + if (get_cluster(mydata, curclust, tmp_buffer,
fs/fat/fat.c static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, · line 370
Quoted 10 lines
fs/fat/fat.c +3
-1(int)actsize) != 0) {printf ("Error reading cluster\n" ); +free (tmp_buffer);return -1 ; } filesize -= actsize; actsize -= pos; -memcpy (buffer, get_contents_vfatname_block + pos, actsize); +memcpy (buffer, tmp_buffer + pos, actsize); +free (tmp_buffer);
How many times is this malloc()/free() called ? I wonder how much this slows things down and how much it fragments the heap. Maybe the amount of calls to this can be reduced somehow.
Quoted 4 lines
fs/fat/fat.c +0
-0*gotsize += actsize;if (!filesize)return 0 ;
Signature · 2 lines
Best regards, Marek Vasut
Chee
PATCH 1/2 fs: fat: dynamically allocate memory for temporary buffer
Quoted 33 lines · Marek Vasut
Quoted 32 lines · tien.fong.chee at intel.com
From: Stefan Agner <[email protected]>
Drop the statically allocated
get_contents_vfatname_blockand dynamically allocate a buffer only if required. This saves 64KiB of memory.Signed-off-by Stefan Agner <[email protected]>Signed-off-by Tien Fong Chee <[email protected]>---
fs/fat/fat.c | 19 +++++++++++++------1 files changed, 13 insertions(+), 6 deletions(-)fs/fat/fat.c +0
-0@@ -307,9 +307,6 @@ get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size) * into 'buffer'. * Update the number of bytes read in *gotsize or return -1 onfatal errors.
*/fs/fat/fat.c +0
-3-__u8 get_contents_vfatname_block[MAX_CLUSTSIZE ] -__aligned(ARCH_DMA_MINALIGN) ; -static int get_contents(fsdata *mydata,
dir_entry*dentptr,loff_tpos,
__u8*buffer,loff_tmaxsize,loff_t*gotsize)
{fs/fat/fat.c +0
-0@@ -320,7 +317,7 @@ static int get_contents(fsdata *mydata,
Quoted 8 lines
Quoted 7 lines
fs/fat/fat.c +0
-0dir_entry *dentptr, loff_t pos, loff_t actsize;
*gotsize = 0;fs/fat/fat.c +1
-1-debug ("Filesize: %llu bytes\n" , filesize); + debug("Filesize: %llu bytes, starting at pos %llu\n",filesize, pos);
This looks like a separate change.
Okay.
Quoted 13 lines
fs/fat/fat.c static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, · line 354
Quoted 12 lines
if (pos >= filesize) {
debug("Read position past EOF: %llu\n", pos);fs/fat/fat.c +0
-0@@ -352,15 +349,25 @@ static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
/* align to beginning of next cluster if any */ if (pos) {fs/fat/fat.c +3
-0+__u8 *tmp_buffer; + + tmp_buffer =malloc_cache_aligned (MAX_CLUSTSIZE );Don't you know the cluster size by now ?
Yeah, i think so, we can run allocate memory based on actsize. Please correct me if i'm wrong.
Quoted 19 lines
Quoted 19 lines
fs/fat/fat.c +5
-0+if (!tmp_buffer) { +debug ("Error: allocating buffer\n" ); +return -ENOMEM ; + } +actsize =
min(filesize, (loff_t)bytesperclust);fs/fat/fat.c +0
-1- if (get_cluster(mydata, curclust,
get_contents_vfatname_block,fs/fat/fat.c +1
-0+ if (get_cluster(mydata, curclust, tmp_buffer,(int)actsize) != 0) {
printf("Error reading cluster\n");fs/fat/fat.c +1
-0+free (tmp_buffer);return -1;
}
filesize -= actsize;
actsize -= pos;fs/fat/fat.c +0
-1- memcpy(buffer, get_contents_vfatname_block + pos,
Quoted 8 lines
fs/fat/fat.c static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, · line 370
Quoted 3 lines
actsize);
fs/fat/fat.c +2
-0+memcpy (buffer, tmp_buffer + pos, actsize); +free (tmp_buffer);How many times is this malloc()/free() called ? I wonder how much this
slows things down and how much it fragments the heap. Maybe the amount
of calls to this can be reduced somehow.
There is performance penalty for when reading file based on offset chunk by chunk at small memory such as OCRAM. However, i believe this doesn't impact in most use cases because most of them running in large DDR size. Most of the time, the reading of the file is starting from offset zero(no memory allocation is required).
I can "feel" that is not much difference actually in performance based on print out responding for my chunk by chunk file reading use case in these changes. But these changes with [patch 2/2] would help to maximize reusable from memory pool, which lead to only 1 block of max cluster is required allocated in memory pool instead of two blocks.
What do you think?
Quoted 7 lines
Quoted 6 lines
*gotsize += actsize;
if (!filesize)
return 0;