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 <chenrun1@xiaomi.com>
This commit is contained in:
chenrun1 2024-07-11 17:38:22 +08:00 committed by Xiang Xiao
parent c528244f19
commit dba77ff043
2 changed files with 15 additions and 0 deletions

View File

@ -30,6 +30,7 @@
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <nuttx/fs/fs.h>
#include <nuttx/lib/lib.h> #include <nuttx/lib/lib.h>
#include <nuttx/kmalloc.h> #include <nuttx/kmalloc.h>
#include <nuttx/mutex.h> #include <nuttx/mutex.h>
@ -715,6 +716,10 @@ retry:
goto out; goto out;
} }
/* Update filep lock state */
filep->locked = true;
/* When there is a lock change, we need to wake up the blocking lock */ /* When there is a lock change, we need to wake up the blocking lock */
if (bucket->nwaiter > 0) if (bucket->nwaiter > 0)
@ -751,6 +756,11 @@ void file_closelk(FAR struct file *filep)
bool deleted = false; bool deleted = false;
int ret; int ret;
if (filep->locked == false)
{
return;
}
path = lib_get_pathbuffer(); path = lib_get_pathbuffer();
if (path == NULL) if (path == NULL)
{ {
@ -784,6 +794,7 @@ void file_closelk(FAR struct file *filep)
{ {
deleted = true; deleted = true;
file_lock_delete(file_lock); file_lock_delete(file_lock);
filep->locked = false;
} }
} }

View File

@ -480,6 +480,10 @@ struct file
#if CONFIG_FS_BACKTRACE > 0 #if CONFIG_FS_BACKTRACE > 0
FAR void *f_backtrace[CONFIG_FS_BACKTRACE]; /* Backtrace to while file opens */ FAR void *f_backtrace[CONFIG_FS_BACKTRACE]; /* Backtrace to while file opens */
#endif #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. /* This defines a two layer array of files indexed by the file descriptor.