From dba77ff043508cf97b877e1379c971c11d8859d5 Mon Sep 17 00:00:00 2001 From: chenrun1 Date: Thu, 11 Jul 2024 17:38:22 +0800 Subject: [PATCH] fslock:Optimize the performance overhead caused by frequent close Summary: Indicate whether the file is currently locked by adding a new field locked to filep. 0 - Unlocked 1 - Locked The status of the filep at close is used to determine whether to continue with the following procedure. Optimizing performance: Before Time taken to close the file: 33984 nsec After Time taken to close the file: 23744 nsec Improvement of about 10 msec Signed-off-by: chenrun1 --- fs/vfs/fs_lock.c | 11 +++++++++++ include/nuttx/fs/fs.h | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/fs/vfs/fs_lock.c b/fs/vfs/fs_lock.c index bfb430577b..1c4bb006f9 100644 --- a/fs/vfs/fs_lock.c +++ b/fs/vfs/fs_lock.c @@ -30,6 +30,7 @@ #include #include +#include #include #include #include @@ -715,6 +716,10 @@ retry: goto out; } + /* Update filep lock state */ + + filep->locked = true; + /* When there is a lock change, we need to wake up the blocking lock */ if (bucket->nwaiter > 0) @@ -751,6 +756,11 @@ void file_closelk(FAR struct file *filep) bool deleted = false; int ret; + if (filep->locked == false) + { + return; + } + path = lib_get_pathbuffer(); if (path == NULL) { @@ -784,6 +794,7 @@ void file_closelk(FAR struct file *filep) { deleted = true; file_lock_delete(file_lock); + filep->locked = false; } } diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h index 939a8dd546..04f8c07558 100644 --- a/include/nuttx/fs/fs.h +++ b/include/nuttx/fs/fs.h @@ -480,6 +480,10 @@ struct file #if CONFIG_FS_BACKTRACE > 0 FAR void *f_backtrace[CONFIG_FS_BACKTRACE]; /* Backtrace to while file opens */ #endif + +#if CONFIG_FS_LOCK_BUCKET_SIZE > 0 + bool locked; /* Filelock state: false - unlocked, true - locked */ +#endif }; /* This defines a two layer array of files indexed by the file descriptor.