xiaoqizhan opened a new pull request, #18767:
URL: https://github.com/apache/nuttx/pull/18767

   When CONFIG_FS_BACKTRACE is enabled, collecting a stack trace for every new 
file descriptor adds avoidable overhead to tasks that use only a small number 
of FDs.
   
   Add a dynamic threshold option so backtrace capture is enabled only after 
the per-task FD count reaches the configured limit.
   
   *Note: Please adhere to [Contributing 
Guidelines](https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md).*
   
   ## Summary
   
    This patch introduces a dynamic backtrace threshold control mechanism for 
file descriptors.
   
       **Why the change is necessary:**
       Currently, when `CONFIG_FS_BACKTRACE` is enabled, the system collects a 
stack backtrace for every newly allocated file descriptor. However, this 
imposes unnecessary and avoidable
     performance overhead on tasks that only allocate a small number of file 
descriptors and never exceed typical limits or experience FD leaks.
   
       **What it does and how:**
       This change introduces `CONFIG_FS_BACKTRACE_DYNAMIC`. When enabled, it 
avoids capturing stack traces for normal, small-scale file descriptor 
allocations. The system tracks the number of
     open file descriptors per task (`fl_open_count`) and only begins filling 
the backtrace array (`f_backtrace`) once the per-task file descriptor count 
reaches a configurable threshold
     (`CONFIG_FS_BACKTRACE_DEFAULT_THRESHOLD`).
   
       This feature significantly improves runtime performance for tasks with 
normal FD usage while still preserving the powerful diagnostic capabilities of 
`CONFIG_FS_BACKTRACE` for detecting
     resource leaks when they actually occur.
   
   
   ## Impact
   
       * **Users:** Improves runtime performance and reduces system overhead by 
omitting backtrace collection for tasks that use few file descriptors.
       * **Build process:** Adds new Kconfig options 
(`CONFIG_FS_BACKTRACE_DYNAMIC` and `CONFIG_FS_BACKTRACE_DEFAULT_THRESHOLD`).
       * **Hardware:** None.
       * **Documentation:** No significant impact, but the Kconfig help text 
has been updated to reflect the new feature.
       * **Security:** None.
       * **Compatibility:** Backward compatible. The dynamic feature defaults 
to `n`, preserving the original behavior of `CONFIG_FS_BACKTRACE` unless 
explicitly enabled.
   
   ## Testing
   
    **Host Machine:** Ubuntu Linux (x86_64)
       **Target:** Verified on generic simulators and local boards (e.g. 
`sim:nsh`)
   
       **Verification Steps:**
       1. Built the system with `CONFIG_FS_BACKTRACE=y` and 
`CONFIG_FS_BACKTRACE_DYNAMIC=n` to ensure the original backtrace logic works 
without regression.
       2. Built the system with `CONFIG_FS_BACKTRACE_DYNAMIC=y` and set a 
default threshold (e.g., 60).
       3. Created an application that opens multiple files iteratively:
          * Verified that backtraces are **not** captured for the first 59 file 
descriptors.
          * Verified that once the threshold (60th FD) is reached, the 
`fl_bt_enabled` flag is set, and backtraces are successfully captured and 
stored for subsequent file descriptors.
       4. Verified that file descriptors closed (`fl_open_count` decremented) 
do not cause the threshold trigger to oscillate incorrectly (once enabled, it 
stays enabled for that task's
     fdlist).
       5. Tested `fdlist_copy` (task creation) to ensure backtrace thresholds 
and states are correctly propagated/initialized for child tasks.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to