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

   ## Summary
   
   Add an optional pool for dynamically allocated `struct file` objects. A 
static
   reserve avoids individual heap allocations, and an optional expansion batch
   retains objects for reuse. The final file reference uses the same deallocator
   as failed-open paths. Expansion uses the selected FS heap through private
   callbacks; this does not require a new mempool API.
   
   ## Impact
   
   * Configuration: YES — `CONFIG_FS_PREALLOC_FILES` and 
`CONFIG_FS_ALLOC_FILES`,
     both defaulting to zero. With both zero, allocation is unchanged.
   * Behavior: a reserve without expansion has a fixed capacity and fails
     immediately when empty. Budget startup files and concurrent opens.
     Expanded storage is retained at the high-water mark. Descriptor tables
     and filesystem/socket private data still have separate allocations.
   * API/ABI and build process: NO changes. Hardware/drivers: NO changes.
   * Documentation: YES — allocation policy and costs in `file_descriptors.rst`.
   * Performance: allocation calls decrease; this is **not a simulator 
speedup**.
     In the instrumented 20,000-cycle allocation/zero/free loop, the baseline
     took 7,118,540 ns, the fixed pool 11,594,042 ns and reserve+expansion
     15,579,906 ns. These are individual host samples, not hardware latency 
bounds.
   
   ## Testing
   
   I confirm that the flat-mode configurations below were verified locally.
   Protected and kernel modes were compiled only.
   
   * Build host: Linux x86_64, gcc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0.
   * Base: `28db4789c34`; tested head: 
`35caf79973a3b41a431dba8cd51883463e6753ee`.
   * `sim:nsh`: default heap, fixed 16-object pool, reserve 16/batch 4,
     dynamic-only batch 4, and shared-heap reserve 16/batch 4 — 133 checks pass.
   * Tests compile the complete production `fs/inode/fs_files.c` a second time
     under private names. Its heap callbacks count allocations and inject 
failure;
     mempool allocation/release and VFS operations are real. Tests cover 
zeroing,
     exhaustion/reuse, expansion failure/recovery, references, failed open,
     descriptor-install failure, `dup`, four workers doing 500 open/dup/close
     cycles each, and 20 tasks exiting with four files left open.
   * Baseline replay: all lifecycle checks pass, while the new zero-heap-call
     property fails as expected. Both baseline and default perform 20,000 heap
     allocation/free pairs. The warmed pools perform zero such pairs. For 64
     simultaneous internal test objects, reserve 16/batch 4 needs 12 expansion
     allocations; dynamic-only batch 4 needs 16.
   * `sim:nsh` Make/CMake and ostest pass. `sim:nsh` is in the CI CMake test 
list.
   * `qemu-armv7r:pnsh` (protected) and `qemu-armv8a:knsh` (kernel) build with
     reserve 16/batch 4 and an independent FS heap. No runtime claim for them.
   * Small `sim:nsh` image, local test code and coverage disabled, equal config
     except the two new options: default-off has no text/data/bss change;
     reserve 16/batch 4 adds text +1728, data +224, bss +512 bytes.
   * `refresh.sh --silent sim:nsh` leaves the defconfig unchanged.
   
   **Limitations:** no real hardware runs. SMP direct-run stress is 
inconclusive:
   the branch and unmodified four-CPU baseline both stall intermittently on this
   host. The two-CPU baseline completed; the two-CPU branch also stalled in the
   concurrent test. Both branch variants complete all checks under GDB, with
   simulator interrupt signals passed through. This does not establish a 
reliable
   SMP stress pass, and the stall's cause is not established. No simulator or
   scheduler changes are included. Attaching only after native runs stalled
   captures both the two-CPU baseline and branch waiting for `g_cpu_irqlock`
   from the inode unlock path. The logs below preserve these results.
   
   The local harness is attached as reproducible test material instead of adding
   a config-specific application to this OS PR. Place its files under
   `apps/examples/filepoolcheck`, enable `EXAMPLES_FILEPOOLCHECK` in a flat 
build,
   and run `filepoolcheck` (`filepoolcheck require-pool` for an enabled pool).
   Use the listed configurations and the sibling `nuttx/` and `apps/` layout.
   
   Testing logs before change:
   
   <details>
   <summary>Baseline run: lifecycle passes, pool property fails as 
expected</summary>
   
   ```text
   Error mount tmpfs '' on '/tmp' failed: 20
   nsh> filepoolcheck require-pool
   INTERNAL allocate: PASS
   INTERNAL zero-initialized: PASS
   INTERNAL reuse-zeroed: PASS
   INTERNAL first-reference-keeps-object: PASS
   INTERNAL last-reference-releases-object: PASS
   BENCH cycles=20000 heap_allocations=20000 heap_releases=20000 ns=7118540
   INTERNAL repeated-zeroing: PASS
   INTERNAL no-heap-in-steady-state: FAIL
   CAPACITY count=64 heap_allocations=20066 heap_releases=20002
   INTERNAL bulk-zeroed: PASS
   INTERNAL bulk-capacity: PASS
   INTERNAL bulk-reuse: PASS
   INTERNAL allocator-failure-or-retained-pool: PASS
   INTERNAL exhausted-pool-failure: PASS
   INTERNAL recovery-after-failure: PASS
   INTERNAL RESULT checks=13 failures=1
   VFS open: PASS
   VFS dup: PASS
   VFS dup-shares-open-file: PASS
   VFS close-original: PASS
   VFS read-duplicate-after-close: PASS
   VFS close-duplicate: PASS
   VFS failed-open-releases-object: PASS
   VFS fd-install-failure: PASS
   VFS concurrent-open-dup-close: PASS
   VFS task-exit-closes-files: PASS
   VFS pool-remains-usable: PASS
   VFS available-capacity-restored: PASS
   FILEPOOL RESULT checks=12 failures=1
   nsh> poweroff
   ```
   </details>
   
   Testing logs after change:
   
   <details>
   <summary>sim:nsh — default</summary>
   
   ```text
   Error mount tmpfs '' on '/tmp' failed: 20
   nsh> filepoolcheck
   INTERNAL allocate: PASS
   INTERNAL zero-initialized: PASS
   INTERNAL reuse-zeroed: PASS
   INTERNAL first-reference-keeps-object: PASS
   INTERNAL last-reference-releases-object: PASS
   BENCH cycles=20000 heap_allocations=20000 heap_releases=20000 ns=7057846
   INTERNAL repeated-zeroing: PASS
   CAPACITY count=64 heap_allocations=20066 heap_releases=20002
   INTERNAL bulk-zeroed: PASS
   INTERNAL bulk-capacity: PASS
   INTERNAL bulk-reuse: PASS
   INTERNAL allocator-failure-or-retained-pool: PASS
   INTERNAL exhausted-pool-failure: PASS
   INTERNAL recovery-after-failure: PASS
   INTERNAL RESULT checks=12 failures=0
   VFS open: PASS
   VFS dup: PASS
   VFS dup-shares-open-file: PASS
   VFS close-original: PASS
   VFS read-duplicate-after-close: PASS
   VFS close-duplicate: PASS
   VFS failed-open-releases-object: PASS
   VFS fd-install-failure: PASS
   VFS concurrent-open-dup-close: PASS
   VFS task-exit-closes-files: PASS
   VFS pool-remains-usable: PASS
   VFS available-capacity-restored: PASS
   FILEPOOL RESULT checks=12 failures=0
   nsh> poweroff
   ```
   </details>
   
   <details>
   <summary>sim:nsh — fixed</summary>
   
   ```text
   Error mount tmpfs '' on '/tmp' failed: 20
   nsh> filepoolcheck require-pool
   INTERNAL allocate: PASS
   INTERNAL zero-initialized: PASS
   INTERNAL reuse-zeroed: PASS
   INTERNAL first-reference-keeps-object: PASS
   INTERNAL last-reference-releases-object: PASS
   BENCH cycles=20000 heap_allocations=0 heap_releases=0 ns=11594042
   INTERNAL repeated-zeroing: PASS
   INTERNAL no-heap-in-steady-state: PASS
   CAPACITY count=16 heap_allocations=0 heap_releases=0
   INTERNAL bulk-zeroed: PASS
   INTERNAL fixed-capacity: PASS
   INTERNAL full-pool-fails: PASS
   INTERNAL static-pool-never-uses-heap: PASS
   INTERNAL bulk-reuse: PASS
   INTERNAL allocator-failure-or-retained-pool: PASS
   INTERNAL exhausted-pool-failure: PASS
   INTERNAL recovery-after-failure: PASS
   POOL allocated=0 free=16 size=512
   INTERNAL pool-no-live-objects: PASS
   INTERNAL RESULT checks=16 failures=0
   VFS open: PASS
   VFS dup: PASS
   VFS dup-shares-open-file: PASS
   VFS close-original: PASS
   VFS read-duplicate-after-close: PASS
   VFS close-duplicate: PASS
   VFS failed-open-releases-object: PASS
   VFS fd-install-failure: PASS
   VFS concurrent-open-dup-close: PASS
   VFS task-exit-closes-files: PASS
   VFS pool-remains-usable: PASS
   VFS available-capacity-restored: PASS
   FILEPOOL RESULT checks=12 failures=0
   nsh> poweroff
   ```
   </details>
   
   <details>
   <summary>sim:nsh — expanding</summary>
   
   ```text
   Error mount tmpfs '' on '/tmp' failed: 20
   nsh> filepoolcheck require-pool
   INTERNAL allocate: PASS
   INTERNAL zero-initialized: PASS
   INTERNAL reuse-zeroed: PASS
   INTERNAL first-reference-keeps-object: PASS
   INTERNAL last-reference-releases-object: PASS
   BENCH cycles=20000 heap_allocations=0 heap_releases=0 ns=15579906
   INTERNAL repeated-zeroing: PASS
   INTERNAL no-heap-in-steady-state: PASS
   CAPACITY count=64 heap_allocations=12 heap_releases=0
   INTERNAL bulk-zeroed: PASS
   INTERNAL bulk-capacity: PASS
   INTERNAL bulk-reuse: PASS
   INTERNAL allocator-failure-or-retained-pool: PASS
   INTERNAL exhausted-pool-failure: PASS
   INTERNAL expansion-failure-reached-heap: PASS
   INTERNAL recovery-after-failure: PASS
   POOL allocated=0 free=64 size=2144
   INTERNAL pool-no-live-objects: PASS
   INTERNAL RESULT checks=15 failures=0
   VFS open: PASS
   VFS dup: PASS
   VFS dup-shares-open-file: PASS
   VFS close-original: PASS
   VFS read-duplicate-after-close: PASS
   VFS close-duplicate: PASS
   VFS failed-open-releases-object: PASS
   VFS fd-install-failure: PASS
   VFS concurrent-open-dup-close: PASS
   VFS task-exit-closes-files: PASS
   VFS pool-remains-usable: PASS
   VFS available-capacity-restored: PASS
   FILEPOOL RESULT checks=12 failures=0
   nsh> poweroff
   ```
   </details>
   
   <details>
   <summary>sim:nsh — dynamic</summary>
   
   ```text
   Error mount tmpfs '' on '/tmp' failed: 20
   nsh> filepoolcheck require-pool
   INTERNAL allocate: PASS
   INTERNAL zero-initialized: PASS
   INTERNAL reuse-zeroed: PASS
   INTERNAL first-reference-keeps-object: PASS
   INTERNAL last-reference-releases-object: PASS
   BENCH cycles=20000 heap_allocations=0 heap_releases=0 ns=11697390
   INTERNAL repeated-zeroing: PASS
   INTERNAL no-heap-in-steady-state: PASS
   CAPACITY count=64 heap_allocations=16 heap_releases=0
   INTERNAL bulk-zeroed: PASS
   INTERNAL bulk-capacity: PASS
   INTERNAL bulk-reuse: PASS
   INTERNAL allocator-failure-or-retained-pool: PASS
   INTERNAL exhausted-pool-failure: PASS
   INTERNAL expansion-failure-reached-heap: PASS
   INTERNAL recovery-after-failure: PASS
   POOL allocated=0 free=64 size=2176
   INTERNAL pool-no-live-objects: PASS
   INTERNAL RESULT checks=15 failures=0
   VFS open: PASS
   VFS dup: PASS
   VFS dup-shares-open-file: PASS
   VFS close-original: PASS
   VFS read-duplicate-after-close: PASS
   VFS close-duplicate: PASS
   VFS failed-open-releases-object: PASS
   VFS fd-install-failure: PASS
   VFS concurrent-open-dup-close: PASS
   VFS task-exit-closes-files: PASS
   VFS pool-remains-usable: PASS
   VFS available-capacity-restored: PASS
   FILEPOOL RESULT checks=12 failures=0
   nsh> poweroff
   ```
   </details>
   
   <details>
   <summary>sim:nsh — shared-heap</summary>
   
   ```text
   Error mount tmpfs '' on '/tmp' failed: 20
   nsh> filepoolcheck require-pool
   INTERNAL allocate: PASS
   INTERNAL zero-initialized: PASS
   INTERNAL reuse-zeroed: PASS
   INTERNAL first-reference-keeps-object: PASS
   INTERNAL last-reference-releases-object: PASS
   BENCH cycles=20000 heap_allocations=0 heap_releases=0 ns=11476550
   INTERNAL repeated-zeroing: PASS
   INTERNAL no-heap-in-steady-state: PASS
   CAPACITY count=64 heap_allocations=12 heap_releases=0
   INTERNAL bulk-zeroed: PASS
   INTERNAL bulk-capacity: PASS
   INTERNAL bulk-reuse: PASS
   INTERNAL allocator-failure-or-retained-pool: PASS
   INTERNAL exhausted-pool-failure: PASS
   INTERNAL expansion-failure-reached-heap: PASS
   INTERNAL recovery-after-failure: PASS
   POOL allocated=0 free=64 size=2144
   INTERNAL pool-no-live-objects: PASS
   INTERNAL RESULT checks=15 failures=0
   VFS open: PASS
   VFS dup: PASS
   VFS dup-shares-open-file: PASS
   VFS close-original: PASS
   VFS read-duplicate-after-close: PASS
   VFS close-duplicate: PASS
   VFS failed-open-releases-object: PASS
   VFS fd-install-failure: PASS
   VFS concurrent-open-dup-close: PASS
   VFS task-exit-closes-files: PASS
   VFS pool-remains-usable: PASS
   VFS available-capacity-restored: PASS
   FILEPOOL RESULT checks=12 failures=0
   nsh> poweroff
   ```
   </details>
   
   <details>
   <summary>Final local Make/CMake/ostest gate</summary>
   
   ```text
   target   : nuttx
   branch   : fs/file-mempool
   head     : 35caf79973a3b41a431dba8cd51883463e6753ee
   subject  : fs: add mempool for file objects
   commits  : 1
   base     : 28db4789c3 (merge-base with upstream/master)
   behind   : 0 commits of upstream/master
   nuttx    : 35caf79973
   apps     : f0dc662c01
   options  : CONFIG_FS_TMPFS CONFIG_FS_HEAPSIZE=1048576 
CONFIG_FS_PREALLOC_FILES=16 CONFIG_FS_ALLOC_FILES=4 CONFIG_DEBUG_FULLOPT 
CONFIG_COVERAGE_NONE CONFIG_DEBUG_ASSERTIONS
   host     : Linux x86_64, gcc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0
   checkpatch: rc=0
   signer   : jingfei <[email protected]>
   commits missing your Signed-off-by: 0
   make build: rc=0
   warnings in changed files: 0
   ostest: Exiting with status 0
   cmake reconfigure: rc=0
   cmake build: rc=0
   worktree tracked changes after: 0
   RESULT: PASS
   ```
   </details>
   
   <details>
   <summary>Protected and kernel build logs (last 16 lines)</summary>
   
   ```text
   qemu-armv7r:pnsh: file pool + independent FS heap build PASS
   
   AS:  armv7-r/arm_vectortab.S 
   CC:  qemu_boardinit.c 
   
   CC:  qemu_bringup.c 
   
   
   CPP:  
/home/mi/test/nuttx_apache/fs-upstream/nuttx/boards/arm/qemu/qemu-armv7r/scripts/nuttx.ld->
 
/home/mi/test/nuttx_apache/fs-upstream/nuttx/boards/arm/qemu/qemu-armv7r/scripts/nuttx.ld.tmp
 
   LD: nuttx
   arm-none-eabi-ld: warning: 
/home/mi/test/nuttx_apache/fs-upstream/nuttx/nuttx has a LOAD segment with RWX 
permissions
   Memory region         Used Size  Region Size  %age Used
             kflash:      168976 B       512 KB     32.23%
             uflash:          0 GB       512 KB      0.00%
              ksram:         12 KB         8 MB      0.15%
              usram:          0 GB         8 MB      0.00%
   CP: nuttx.bin
   
   qemu-armv8a:knsh: file pool + independent FS heap build PASS
   
   IN: libs/libc/libc.a -> staging/libc.a 
   
   CPP:  
/home/mi/test/nuttx_apache/fs-upstream/nuttx/boards/arm64/qemu/qemu-armv8a/scripts/ld-kernel.script->
 
/home/mi/test/nuttx_apache/fs-upstream/nuttx/boards/arm64/qemu/qemu-armv8a/scripts/ld-kernel.script.tmp
 
   CC:  qemu_boardinit.c 
   
   CC:  qemu_bringup.c 
   
   CC:  qemu_power.c 
   
   LD: nuttx
   Memory region         Used Size  Region Size  %age Used
               dram:        312 KB         2 MB     15.23%
              pgram:           0 B         4 MB      0.00%
   CP: nuttx.hex
   CP: nuttx.bin
   ```
   </details>
   
   <details>
   <summary>Small-image size output</summary>
   
   ```text
   size-baseline
   text    data     bss     dec     hex filename
    566157         2800   12896  581853   8e0dd nuttx
   
   size-default
   text    data     bss     dec     hex filename
    566157         2800   12896  581853   8e0dd nuttx
   
   size-enabled
   text    data     bss     dec     hex filename
    567885         3024   13408  584317   8ea7d nuttx
   ```
   </details>
   
   <details>
   <summary>SMP limitations and failed direct-run logs</summary>
   
   ```text
   SMP runtime is not a reliable stress pass on this host.
   
   The four-CPU branch binary built successfully but func-test timed out after
   120 seconds, with NSH input partly echoed. Five direct reruns using the
   same saved binary gave one complete zero-failure run and four timeouts.
   The unmodified four-CPU baseline also timed out, including three reruns.
   The two-CPU baseline completed once. The two-CPU branch (MM_BACKTRACE=0)
   timed out after reaching the concurrent VFS test; a PTY rerun did so too.
   
   With SIGUSR1/SIGUSR2/SIGALRM passed through GDB, both branch binaries
   completed all 27 checks and exited normally. These are diagnostic passes,
   not proof that the intermittent direct-run stall is fixed or harmless.
   The exact stall cause has not been established. No simulator, scheduler
   or mempool core changes are included in the patch.
   
   Native runs were also started with explicit ptrace permission, then attached
   only after stalling (no debugger present during the test). The two-CPU
   baseline stalls in a worker's inode_runlock path waiting for g_cpu_irqlock.
   The two-CPU branch stalls with one CPU in inode_runlock waiting for
   g_cpu_irqlock and the other in nxsched_process_delivered waiting for that
   lock. Logs: baseline-smp-two/native-backtrace.txt and
   smp-two/native-backtrace.txt. One branch native trace attempt completed
   normally before a second attempt reproduced the stall. This confirms a
   baseline stall in the same concurrency test, without proving an exact
   scheduler cause or a general SMP correctness result.
   
   Flat mode completed the same four-worker open/dup/close test in every
   tested allocator configuration. Protected/kernel runtime and hardware
   were not exercised. PR Testing must explicitly preserve this limitation.
   
   Baseline four-CPU:
   [CPU0] Error mount tmpfs '' on '/tmp' failed: 20
   nsh> f
   
   Branch two-CPU:
   [CPU0] Error mount tmpfs '' on '/tmp' failed: 20
   nsh> fiilepoolcheck require-pool
   INTERNAL allocate: PASS
   INTERNAL zero-initialized: PASS
   INTERNAL reuse-zeroed: PASS
   INTERNAL first-reference-keeps-object: PASS
   INTERNAL last-reference-releases-object: PASS
   BENCH cycles=20000 heap_allocations=0 heap_releases=0 ns=17255094
   INTERNAL repeated-zeroing: PASS
   INTERNAL no-heap-in-steady-state: PASS
   CAPACITY count=64 heap_allocations=12 heap_releases=0
   INTERNAL bulk-zeroed: PASS
   INTERNAL bulk-capacity: PASS
   INTERNAL bulk-reuse: PASS
   INTERNAL allocator-failure-or-retained-pool: PASS
   INTERNAL exhausted-pool-failure: PASS
   INTERNAL expansion-failure-reached-heap: PASS
   INTERNAL recovery-after-failure: PASS
   POOL allocated=0 free=64 size=3168
   INTERNAL pool-no-live-objects: PASS
   INTERNAL RESULT checks=15 failures=0
   VFS open: PASS
   VFS dup: PASS
   VFS dup-shares-open-file: PASS
   VFS close-original: PASS
   VFS read-duplicate-after-close: PASS
   VFS close-duplicate: PASS
   VFS failed-open-releases-object: PASS
   VFS fd-install-failure: PASS
   ```
   </details>
   
   <details>
   <summary>SMP diagnostic runs under GDB (not a stress pass)</summary>
   
   ```text
   Reading symbols from 
/home/mi/test/nuttx_apache/fs-upstream/pr-file-mempool/smp/nuttx...
   (gdb) set pagination off
   set pagination off
   (gdb) set confirm off
   set confirm off
   (gdb) handle SIGUSR1 SIGUSR2 SIGALRM nostop noprint pass
   handle SIGUSR1 SIGUSR2 SIGALRM nostop noprint pass
   Signal        Stop   Print   Pass to program Description
   SIGALRM       No     No      Yes             Alarm clock
   SIGUSR1       No     No      Yes             User defined signal 1
   SIGUSR2       No     No      Yes             User defined signal 2
   (gdb) run < 
/home/mi/test/nuttx_apache/fs-upstream/pr-file-mempool/require-pool.nsh
   
   <apache/fs-upstream/pr-file-mempool/require-pool.nsh
   Starting program: 
/home/mi/test/nuttx_apache/fs-upstream/pr-file-mempool/smp/nuttx < 
/home/mi/test/nuttx_apache/fs-upstream/pr-file-mempool/require-pool.nsh
   [Thread debugging using libthread_db enabled]
   Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
   [New Thread 0x7ffff3c0fdc0 (LWP 2925260)]
   [New Thread 0x7ffff3c201c0 (LWP 2925261)]
   [New Thread 0x7ffff3c305c0 (LWP 2925262)]
   [CPU0] Error mount tmpfs '' on '/tmp' failed: 20
   
   nsh> filepoolcheck require-pool
   
   INTERNAL allocate: PASS
   
   INTERNAL zero-initialized: PASS
   
   INTERNAL reuse-zeroed: PASS
   
   INTERNAL first-reference-keeps-object: PASS
   
   INTERNAL last-reference-releases-object: PASS
   
   BENCH cycles=20000 heap_allocations=0 heap_releases=0 ns=11768891
   
   INTERNAL repeated-zeroing: PASS
   
   INTERNAL no-heap-in-steady-state: PASS
   
   CAPACITY count=64 heap_allocations=12 heap_releases=0
   
   INTERNAL bulk-zeroed: PASS
   
   INTERNAL bulk-capacity: PASS
   
   INTERNAL bulk-reuse: PASS
   
   INTERNAL allocator-failure-or-retained-pool: PASS
   
   INTERNAL exhausted-pool-failure: PASS
   
   INTERNAL expansion-failure-reached-heap: PASS
   
   INTERNAL recovery-after-failure: PASS
   
   POOL allocated=0 free=64 size=2144
   
   INTERNAL pool-no-live-objects: PASS
   
   INTERNAL RESULT checks=15 failures=0
   
   VFS open: PASS
   
   VFS dup: PASS
   
   VFS dup-shares-open-file: PASS
   
   VFS close-original: PASS
   
   VFS read-duplicate-after-close: PASS
   
   VFS close-duplicate: PASS
   
   VFS failed-open-releases-object: PASS
   
   VFS fd-install-failure: PASS
   
   VFS concurrent-open-dup-close: PASS
   
   VFS task-exit-closes-files: PASS
   
   VFS pool-remains-usable: PASS
   
   VFS available-capacity-restored: PASS
   
   FILEPOOL RESULT checks=12 failures=0
   
   nsh> poweroff
   
   [Thread 0x7ffff3c305c0 (LWP 2925262) exited]
   [Thread 0x7ffff3c201c0 (LWP 2925261) exited]
   [Thread 0x7ffff7fa0740 (LWP 2925257) exited]
   [Thread 0x7ffff3c0fdc0 (LWP 2925260) exited]
   [New process 2925257]
   [Inferior 1 (process 2925257) exited normally]
   (gdb) thread apply all bt 18
   thread apply all bt 18
   (gdb) p g_file_mempool
   p g_file_mempool
   $1 = {initialbase = 0x4007c1e0 <g_file_pool_buffer>, initialsize = 520, 
blocksize = 32, interruptsize = 0, expandsize = 136, maxalloc = 0, wait = 
false, priv = 0x0, name = 0x40059cd2 "file", alloc = 0x40014700 
<file_pool_alloc>, free = 0x40014740 <file_pool_free>, check = 0x40014d90 
<file_pool_check>, ibase = 0x0, queue = {head = 0x0, tail = 0x0}, iqueue = 
{head = 0x0, tail = 0x0}, equeue = {head = 0x0, tail = 0x0}, nalloc = 0, lock = 
{owner = 0, next = 0}, waitsem = {val = {semcount = 0, mholder = 0}, flags = 0 
'\000', waitlist = {head = 0x0, tail = 0x0}}, init = false, procfs = {name = 
0x0, next = 0x0}}
   (gdb) quit
   quit
   
   Reading symbols from 
/home/mi/test/nuttx_apache/fs-upstream/pr-file-mempool/smp-two/nuttx...
   (gdb) set pagination off
   set pagination off
   (gdb) set confirm off
   set confirm off
   (gdb) handle SIGUSR1 SIGUSR2 SIGALRM nostop noprint pass
   handle SIGUSR1 SIGUSR2 SIGALRM nostop noprint pass
   Signal        Stop   Print   Pass to program Description
   SIGALRM       No     No      Yes             Alarm clock
   SIGUSR1       No     No      Yes             User defined signal 1
   SIGUSR2       No     No      Yes             User defined signal 2
   (gdb) run < 
/home/mi/test/nuttx_apache/fs-upstream/pr-file-mempool/require-pool.nsh
   
   <apache/fs-upstream/pr-file-mempool/require-pool.nsh
   Starting program: 
/home/mi/test/nuttx_apache/fs-upstream/pr-file-mempool/smp-two/nuttx < 
/home/mi/test/nuttx_apache/fs-upstream/pr-file-mempool/require-pool.nsh
   [Thread debugging using libthread_db enabled]
   Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
   [New Thread 0x7ffff3c0fec0 (LWP 2981322)]
   [CPU0] Error mount tmpfs '' on '/tmp' failed: 20
   
   nsh> filepoolcheck require-pool
   
   INTERNAL allocate: PASS
   
   INTERNAL zero-initialized: PASS
   
   INTERNAL reuse-zeroed: PASS
   
   INTERNAL first-reference-keeps-object: PASS
   
   INTERNAL last-reference-releases-object: PASS
   
   BENCH cycles=20000 heap_allocations=0 heap_releases=0 ns=17299166
   
   INTERNAL repeated-zeroing: PASS
   
   INTERNAL no-heap-in-steady-state: PASS
   
   CAPACITY count=64 heap_allocations=12 heap_releases=0
   
   INTERNAL bulk-zeroed: PASS
   
   INTERNAL bulk-capacity: PASS
   
   INTERNAL bulk-reuse: PASS
   
   INTERNAL allocator-failure-or-retained-pool: PASS
   
   INTERNAL exhausted-pool-failure: PASS
   
   INTERNAL expansion-failure-reached-heap: PASS
   
   INTERNAL recovery-after-failure: PASS
   
   POOL allocated=0 free=64 size=3168
   
   INTERNAL pool-no-live-objects: PASS
   
   INTERNAL RESULT checks=15 failures=0
   
   VFS open: PASS
   
   VFS dup: PASS
   
   VFS dup-shares-open-file: PASS
   
   VFS close-original: PASS
   
   VFS read-duplicate-after-close: PASS
   
   VFS close-duplicate: PASS
   
   VFS failed-open-releases-object: PASS
   
   VFS fd-install-failure: PASS
   
   VFS concurrent-open-dup-close: PASS
   
   VFS task-exit-closes-files: PASS
   
   VFS pool-remains-usable: PASS
   
   VFS available-capacity-restored: PASS
   
   FILEPOOL RESULT checks=12 failures=0
   
   nsh> poweroff
   
   [Thread 0x7ffff3c0fec0 (LWP 2981322) exited]
   [Inferior 1 (process 2981319) exited normally]
   (gdb) thread apply all bt 18
   thread apply all bt 18
   (gdb) p g_file_mempool
   p g_file_mempool
   $1 = {initialbase = 0x4007bf60 <g_file_pool_buffer>, initialsize = 776, 
blocksize = 32, interruptsize = 0, expandsize = 200, maxalloc = 0, wait = 
false, priv = 0x0, name = 0x40059e72 "file", alloc = 0x40015250 
<file_pool_alloc>, free = 0x40015290 <file_pool_free>, check = 0x400158e0 
<file_pool_check>, ibase = 0x0, queue = {head = 0x0, tail = 0x0}, iqueue = 
{head = 0x0, tail = 0x0}, equeue = {head = 0x0, tail = 0x0}, nalloc = 0, lock = 
{owner = 0, next = 0}, waitsem = {val = {semcount = 0, mholder = 0}, flags = 0 
'\000', waitlist = {head = 0x0, tail = 0x0}}, init = false, procfs = {name = 
0x0, next = 0x0, backtrace = false}}
   (gdb) quit
   quit
   ```
   </details>
   
   <details>
   <summary>Native SMP stall backtraces: baseline and branch</summary>
   
   ```text
   [New LWP 3114388]
   [Thread debugging using libthread_db enabled]
   Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
   0x00007759530e5738 in __GI___clock_nanosleep (clock_id=clock_id@entry=0, 
flags=flags@entry=0, req=req@entry=0x7ffc1ea02520, rem=rem@entry=0x0) at 
../sysdeps/unix/sysv/linux/clock_nanosleep.c:78
   
   Thread 2 (Thread 0x77594f00fec0 (LWP 3114388) "nuttx"):
   #0  0x0000000040005346 in spin_lock_notrace (lock=0x4007b810 
<g_cpu_irqlock>) at 
/home/mi/test/nuttx_apache/fs-upstream/nuttx/include/nuttx/spinlock.h:178
   #1  enter_critical_section_notrace () at irq/irq_csection.c:234
   #2  0x0000000040024a9e in nxsem_wait_slow (sem=sem@entry=0x40079280 
<g_inode_lock>) at semaphore/sem_wait.c:93
   #3  0x000000004000c01c in nxsem_wait (sem=sem@entry=0x40079280 
<g_inode_lock>) at semaphore/sem_wait.c:201
   #4  0x0000000040025270 in nxmutex_lock (mutex=0x40079280 <g_inode_lock>) at 
/home/mi/test/nuttx_apache/fs-upstream/nuttx/include/nuttx/mutex.h:503
   #5  up_read (rwsem=rwsem@entry=0x40079280 <g_inode_lock>) at 
semaphore/sem_rw.c:160
   #6  0x000000004001632e in inode_runlock () at inode/fs_inode.c:213
   #7  0x0000000040021e0d in inode_find (desc=desc@entry=0x77594f1ad5a0, 
inode=inode@entry=0x77594f1ad598) at inode/fs_inodefind.c:78
   #8  0x0000000040017744 in file_vopen (filep=filep@entry=0x77594f0114b0, 
path=path@entry=0x40056789 "/dev/null", oflags=oflags@entry=2, umask=0, 
ap=ap@entry=0x77594f1ad690) at vfs/fs_open.c:150
   #9  0x0000000040017a69 in nx_vopen (list=0x77594f1336d0, 
path=path@entry=0x40056789 "/dev/null", oflags=oflags@entry=2, 
ap=ap@entry=0x77594f1ad690) at vfs/fs_open.c:402
   #10 0x0000000040017d42 in open (path=0x40056789 "/dev/null", oflags=2) at 
vfs/fs_open.c:561
   #11 0x000000004002c040 in filepool_worker (arg=0x77594f17bf28, 
arg@entry=<error reading variable: value has been optimized out>) at 
filepoolcheck_main.c:80
   #12 0x000000004004f657 in pthread_startup (entry=<optimized out>, 
arg=<optimized out>) at pthread/pthread_create.c:65
   #13 0x0000000040054451 in pthread_start () at pthread/pthread_create.c:147
   #14 0x0000000000000000 in ?? ()
   
   Thread 1 (Thread 0x775953405740 (LWP 3114387) "nuttx"):
   #0  0x00007759530e5738 in __GI___clock_nanosleep (clock_id=clock_id@entry=0, 
flags=flags@entry=0, req=req@entry=0x7ffc1ea02520, rem=rem@entry=0x0) at 
../sysdeps/unix/sysv/linux/clock_nanosleep.c:78
   #1  0x00007759530ea5b7 in __GI___nanosleep (req=req@entry=0x7ffc1ea02520, 
rem=rem@entry=0x0) at ../sysdeps/unix/sysv/linux/nanosleep.c:25
   #2  0x000077595311bfff in usleep (useconds=<optimized out>) at 
../sysdeps/posix/usleep.c:31
   #3  0x0000000040054ded in host_sleepuntil (nsec=<optimized out>) at 
sim/posix/sim_hosttime.c:170
   #4  0x0000000040011e2e in sim_timer_update () at sim/sim_oneshot.c:313
   #5  0x0000000040010ca7 in up_idle () at sim/sim_idle.c:85
   #6  0x0000000040001a0d in nx_start () at init/nx_start.c:744
   #7  0x0000000040001405 in main (argc=<optimized out>, argv=<optimized out>, 
envp=<optimized out>) at sim/sim_head.c:236
   [Inferior 1 (process 3114387) detached]
   78   ../sysdeps/unix/sysv/linux/clock_nanosleep.c: 没有那个文件或目录.
   No symbol "g_file_mempool" in current context.
   
   [New LWP 3098763]
   [Thread debugging using libthread_db enabled]
   Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
   0x0000000040024536 in spin_lock_notrace (lock=<optimized out>) at 
/home/mi/test/nuttx_apache/fs-upstream/nuttx/include/nuttx/spinlock.h:178
   178    while (atomic_read_acquire(&lock->owner) != ticket)
   
   Thread 2 (Thread 0x791972e0fec0 (LWP 3098763) "nuttx"):
   #0  0x0000000040005346 in spin_lock_notrace (lock=0x4007b9d0 
<g_cpu_irqlock>) at 
/home/mi/test/nuttx_apache/fs-upstream/nuttx/include/nuttx/spinlock.h:178
   #1  enter_critical_section_notrace () at irq/irq_csection.c:234
   #2  0x000000004002520e in nxsem_post_slow (sem=0x40079440 <g_inode_lock>) at 
semaphore/sem_post.c:89
   #3  0x000000004000c3ed in nxsem_post (sem=<optimized out>) at 
semaphore/sem_post.c:178
   #4  0x00000000400164ae in inode_runlock () at inode/fs_inode.c:213
   #5  0x0000000040017905 in file_vopen (filep=filep@entry=0x791972e11ea0, 
path=path@entry=0x40056789 "/dev/null", oflags=oflags@entry=2, umask=0, 
ap=ap@entry=0x791972fbdeb0) at vfs/fs_open.c:258
   #6  0x0000000040017be9 in nx_vopen (list=0x791972f336d0, 
path=path@entry=0x40056789 "/dev/null", oflags=oflags@entry=2, 
ap=ap@entry=0x791972fbdeb0) at vfs/fs_open.c:402
   #7  0x0000000040017ec2 in open (path=0x40056789 "/dev/null", oflags=2) at 
vfs/fs_open.c:561
   #8  0x000000004002c810 in filepool_worker (arg=0x791972f7bf2c, 
arg@entry=<error reading variable: value has been optimized out>) at 
filepoolcheck_main.c:80
   #9  0x000000004004fe67 in pthread_startup (entry=<optimized out>, 
arg=<optimized out>) at pthread/pthread_create.c:65
   #10 0x0000000040054c61 in pthread_start () at pthread/pthread_create.c:147
   #11 0x0000000000000000 in ?? ()
   
   Thread 1 (Thread 0x7919770d0740 (LWP 3098762) "nuttx"):
   #0  0x0000000040024536 in spin_lock_notrace (lock=<optimized out>) at 
/home/mi/test/nuttx_apache/fs-upstream/nuttx/include/nuttx/spinlock.h:178
   #1  nxsched_process_delivered (cpu=cpu@entry=0) at 
sched/sched_process_delivered.c:78
   #2  0x000000004001196b in sim_smp_sched_handler (irq=<optimized out>, 
context=<optimized out>, arg=<optimized out>) at sim/sim_smpsignal.c:79
   #3  0x0000000040001651 in sim_doirq (irq=10, context=<optimized out>) at 
sim/sim_doirq.c:76
   #4  <signal handler called>
   #5  __GI___pthread_sigmask (how=how@entry=2, newmask=<optimized out>, 
newmask@entry=0x7ffef821f610, oldmask=oldmask@entry=0x0) at 
./nptl/pthread_sigmask.c:43
   #6  0x000000004005529d in up_irq_restore (flags=0) at 
sim/posix/sim_hostirq.c:121
   #7  0x0000000040010cb5 in up_idle () at sim/sim_idle.c:93
   #8  0x0000000040001a0d in nx_start () at init/nx_start.c:744
   #9  0x0000000040001405 in main (argc=<optimized out>, argv=<optimized out>, 
envp=<optimized out>) at sim/sim_head.c:236
   $1 = {initialbase = 0x4007bf60 <g_file_pool_buffer>, initialsize = 776, 
blocksize = 32, interruptsize = 0, expandsize = 200, maxalloc = 0, wait = 
false, priv = 0x0, name = 0x40059e72 "file", alloc = 0x40015250 
<file_pool_alloc>, free = 0x40015290 <file_pool_free>, check = 0x400158e0 
<file_pool_check>, ibase = 0x0, queue = {head = 0x4007c1d0 
<g_file_pool_buffer+624>, tail = 0x791972e11d90}, iqueue = {head = 0x0, tail = 
0x0}, equeue = {head = 0x791972e114b0, tail = 0x791972e11e50}, nalloc = 0, lock 
= {owner = 40279, next = 40279}, waitsem = {val = {semcount = 0, mholder = 0}, 
flags = 0 '\000', waitlist = {head = 0x0, tail = 0x0}}, init = true, procfs = 
{name = 0x40059e72 "file", next = 0x40079348 <g_file_mempool+200>, backtrace = 
false}}
   [Inferior 1 (process 3098762) detached]
   ```
   </details>
   
   <details>
   <summary>Local test source: Kconfig</summary>
   
   ```text
   config EXAMPLES_FILEPOOLCHECK
        bool "Local file object pool checks"
        default n
        depends on BUILD_FLAT
   ```
   </details>
   
   <details>
   <summary>Local test source: Make.defs</summary>
   
   ```text
   ifeq ($(CONFIG_EXAMPLES_FILEPOOLCHECK),y)
   CONFIGURED_APPS += $(APPDIR)/examples/filepoolcheck
   endif
   ```
   </details>
   
   <details>
   <summary>Local test source: Makefile</summary>
   
   ```text
   include $(APPDIR)/Make.defs
   
   PROGNAME = filepoolcheck
   PRIORITY = SCHED_PRIORITY_DEFAULT
   STACKSIZE = 32768
   MODULE = $(CONFIG_EXAMPLES_FILEPOOLCHECK)
   MAINSRC = filepoolcheck_main.c
   CSRCS = filepool_internal.c
   CFLAGS += -I$(abspath $(APPDIR)/../nuttx/fs)
   CFLAGS += -I$(abspath $(APPDIR)/../nuttx)
   CFLAGS += -I$(abspath $(APPDIR)/../nuttx/sched)
   
   include $(APPDIR)/Application.mk
   ```
   </details>
   
   <details>
   <summary>Local test source: filepool_internal.c</summary>
   
   ```c
   #include <nuttx/config.h>
   #include <stdio.h>
   #include <string.h>
   #include <time.h>
   #include "fs_heap.h"
   
   #ifndef CONFIG_FS_PREALLOC_FILES
   #define CONFIG_FS_PREALLOC_FILES 0
   #endif
   #ifndef CONFIG_FS_ALLOC_FILES
   #define CONFIG_FS_ALLOC_FILES 0
   #endif
   
   static int g_heap_allocations;
   static int g_heap_releases;
   static int g_fail_allocation;
   static int g_failures;
   static int g_checks;
   
   static void *filetest_malloc(size_t size)
   {
     g_heap_allocations++;
     return g_fail_allocation ? NULL : fs_heap_malloc(size);
   }
   
   static void *filetest_zalloc(size_t size)
   {
     g_heap_allocations++;
     return g_fail_allocation ? NULL : fs_heap_zalloc(size);
   }
   
   static void filetest_free(void *buffer)
   {
     g_heap_releases++;
     fs_heap_free(buffer);
   }
   
   #undef fs_heap_malloc
   #undef fs_heap_zalloc
   #undef fs_heap_free
   #define fs_heap_malloc filetest_malloc
   #define fs_heap_zalloc filetest_zalloc
   #define fs_heap_free filetest_free
   #define fdlist_dup3 filetest_fdlist_dup3
   #define fdlist_init filetest_fdlist_init
   #ifdef CONFIG_SCHED_DUMP_FDLIST
   #define fdlist_dump filetest_fdlist_dump
   #endif
   #define fdlist_free filetest_fdlist_free
   #define fdlist_count filetest_fdlist_count
   #define fdlist_get2 filetest_fdlist_get2
   #define fdlist_dupfile filetest_fdlist_dupfile
   #define fdlist_copy filetest_fdlist_copy
   #define fdlist_dup2 filetest_fdlist_dup2
   #define fdlist_close filetest_fdlist_close
   #define file_allocate_from_inode filetest_allocate_from_inode
   #define file_allocate filetest_allocate
   #define file_deallocate filetest_deallocate
   #define file_get2 filetest_get2
   #define file_ref filetest_ref
   #define file_put filetest_put
   
   struct file *file_allocate(void);
   void file_deallocate(struct file *filep);
   int file_get2(int fd, struct file **filep, struct fd **fdp);
   void file_ref(struct file *filep);
   int file_put(struct file *filep);
   int fdlist_count(struct fdlist *list);
   int fdlist_get2(struct fdlist *list, int fd, struct file **filep,
                   struct fd **fdp);
   
   #include "../../../nuttx/fs/inode/fs_files.c"
   
   static void filetest_check(const char *name, int condition)
   {
     g_checks++;
     if (!condition)
       {
         g_failures++;
       }
   
     printf("INTERNAL %s: %s\n", name, condition ? "PASS" : "FAIL");
   }
   
   static int filetest_iszero(const void *buffer, size_t size)
   {
     const unsigned char *bytes = buffer;
     size_t offset;
   
     for (offset = 0; offset < size; offset++)
       {
         if (bytes[offset] != 0)
           {
             return 0;
           }
       }
   
     return 1;
   }
   
   int filepool_internal(int require_pool)
   {
     struct file *files[128];
     struct file *filep;
     struct timespec start;
     struct timespec stop;
     long long elapsed;
     int initial_allocations;
     int initial_releases;
     int count;
     int index;
     int zeroed;
   
     g_failures = 0;
     g_checks = 0;
     g_heap_allocations = 0;
     g_heap_releases = 0;
     filep = file_allocate();
     filetest_check("allocate", filep != NULL);
     if (filep == NULL)
       {
         return 1;
       }
   
     filetest_check("zero-initialized", filetest_iszero(filep, sizeof(*filep)));
     memset(filep, 0xa5, sizeof(*filep));
     file_deallocate(filep);
     filep = file_allocate();
     filetest_check("reuse-zeroed", filep != NULL &&
                    filetest_iszero(filep, sizeof(*filep)));
     if (filep != NULL)
       {
         file_ref(filep);
         file_ref(filep);
         initial_releases = g_heap_releases;
         filetest_check("first-reference-keeps-object", file_put(filep) == 0 &&
                        atomic_read(&filep->f_refs) == 1 &&
                        g_heap_releases == initial_releases);
         filetest_check("last-reference-releases-object", file_put(filep) == 0);
       }
   
     initial_allocations = g_heap_allocations;
     initial_releases = g_heap_releases;
     zeroed = 1;
     clock_gettime(CLOCK_MONOTONIC, &start);
     for (index = 0; index < 20000; index++)
       {
         filep = file_allocate();
         if (filep == NULL || !filetest_iszero(filep, sizeof(*filep)))
           {
             zeroed = 0;
             break;
           }
   
         memset(filep, 0xa5, sizeof(*filep));
         file_deallocate(filep);
       }
   
     clock_gettime(CLOCK_MONOTONIC, &stop);
     elapsed = (stop.tv_sec - start.tv_sec) * 1000000000ll +
               stop.tv_nsec - start.tv_nsec;
     printf("BENCH cycles=%d heap_allocations=%d heap_releases=%d ns=%lld\n",
            index, g_heap_allocations - initial_allocations,
            g_heap_releases - initial_releases, elapsed);
     filetest_check("repeated-zeroing", zeroed && index == 20000);
     if (require_pool)
       {
         filetest_check("no-heap-in-steady-state",
                        g_heap_allocations == initial_allocations &&
                        g_heap_releases == initial_releases);
       }
   
     zeroed = 1;
     for (count = 0; count < 64; count++)
       {
         files[count] = file_allocate();
         if (files[count] == NULL)
           {
             break;
           }
   
         zeroed &= filetest_iszero(files[count], sizeof(*files[count]));
         memset(files[count], 0xa5, sizeof(*files[count]));
       }
   
     printf("CAPACITY count=%d heap_allocations=%d heap_releases=%d\n",
            count, g_heap_allocations, g_heap_releases);
     filetest_check("bulk-zeroed", zeroed);
   #if CONFIG_FS_PREALLOC_FILES > 0 && CONFIG_FS_ALLOC_FILES == 0
     filetest_check("fixed-capacity", count == CONFIG_FS_PREALLOC_FILES);
     filetest_check("full-pool-fails", file_allocate() == NULL);
     filetest_check("static-pool-never-uses-heap", g_heap_allocations == 0);
   #else
     filetest_check("bulk-capacity", count == 64);
   #endif
     for (index = count - 1; index >= 0; index--)
       {
         file_deallocate(files[index]);
       }
   
     filep = file_allocate();
     filetest_check("bulk-reuse", filep != NULL &&
                    filetest_iszero(filep, sizeof(*filep)));
     if (filep != NULL)
       {
         file_deallocate(filep);
       }
   
     g_fail_allocation = 1;
     initial_allocations = g_heap_allocations;
     for (count = 0; count < 128; count++)
       {
         files[count] = file_allocate();
         if (files[count] == NULL)
           {
             break;
           }
       }
   
     filetest_check("allocator-failure-or-retained-pool", count == 0 ||
                    require_pool);
     filetest_check("exhausted-pool-failure", count < 128 &&
                    file_allocate() == NULL);
   #if CONFIG_FS_ALLOC_FILES > 0
     filetest_check("expansion-failure-reached-heap",
                    g_heap_allocations > initial_allocations);
   #endif
     for (index = count - 1; index >= 0; index--)
       {
         file_deallocate(files[index]);
       }
   
     g_fail_allocation = 0;
     (void)initial_allocations;
     filep = file_allocate();
     filetest_check("recovery-after-failure", filep != NULL);
     if (filep != NULL)
       {
         file_deallocate(filep);
       }
   
   #if CONFIG_FS_PREALLOC_FILES > 0 || CONFIG_FS_ALLOC_FILES > 0
     {
       struct mempoolinfo_s info;
       mempool_info(&g_file_mempool, &info);
       printf("POOL allocated=%lu free=%lu size=%lu\n", info.aordblks,
              info.ordblks, info.arena);
       filetest_check("pool-no-live-objects", info.aordblks == 0);
     }
   #endif
     printf("INTERNAL RESULT checks=%d failures=%d\n", g_checks, g_failures);
     return g_failures;
   }
   ```
   </details>
   
   <details>
   <summary>Local test source: filepoolcheck_main.c</summary>
   
   ```c
   #include <nuttx/config.h>
   #include <nuttx/fs/fs.h>
   #include <nuttx/sched.h>
   #include <sys/resource.h>
   #include <sys/wait.h>
   #include <errno.h>
   #include <fcntl.h>
   #include <pthread.h>
   #include <sched.h>
   #include <stdio.h>
   #include <string.h>
   #include <unistd.h>
   
   int filepool_internal(int require_pool);
   
   static int g_checks;
   static int g_failures;
   
   static int filepool_available(void)
   {
     struct file *files[128];
     int count;
     int index;
   
     for (count = 0; count < 128; count++)
       {
         files[count] = file_allocate();
         if (files[count] == NULL)
           {
             break;
           }
       }
   
     for (index = count - 1; index >= 0; index--)
       {
         file_deallocate(files[index]);
       }
   
     return count;
   }
   
   static void filepool_check(const char *name, int condition)
   {
     g_checks++;
     if (!condition)
       {
         g_failures++;
       }
   
     printf("VFS %s: %s\n", name, condition ? "PASS" : "FAIL");
   }
   
   static int filepool_child(int argc, char **argv)
   {
     int descriptor;
     int index;
   
     for (index = 0; index < 4; index++)
       {
         descriptor = open("/dev/null", O_RDWR);
         if (descriptor < 0)
           {
             return 1;
           }
       }
   
     return 0;
   }
   
   static void *filepool_worker(void *arg)
   {
     int *failures = arg;
     char buffer[8];
     int descriptor;
     int duplicate;
     int index;
   
     for (index = 0; index < 500; index++)
       {
         descriptor = open("/dev/null", O_RDWR);
         if (descriptor < 0)
           {
             (*failures)++;
             continue;
           }
   
         duplicate = dup(descriptor);
         if (duplicate < 0 || read(descriptor, buffer, sizeof(buffer)) != 0 ||
             write(descriptor, "x", 1) != 1)
           {
             (*failures)++;
           }
   
         if (duplicate >= 0)
           {
             close(duplicate);
           }
   
         close(descriptor);
       }
   
     return NULL;
   }
   
   int filepoolcheck_main(int argc, char **argv)
   {
     struct file *original;
     struct file *copied;
     struct rlimit limit;
     struct rlimit reduced;
     pthread_t threads[4];
     int thread_failures[4] = {0};
     int created = 0;
     char buffer[8];
     int descriptor;
     int duplicate;
     int status;
     int index;
     int result;
     int available;
     pid_t child;
   
     g_checks = 0;
     g_failures = 0;
     g_failures += filepool_internal(argc > 1);
     available = filepool_available();
     descriptor = open("/dev/null", O_RDWR);
     filepool_check("open", descriptor >= 0);
     if (descriptor >= 0)
       {
         duplicate = dup(descriptor);
         filepool_check("dup", duplicate >= 0);
         if (duplicate >= 0)
           {
             original = NULL;
             copied = NULL;
             result = file_get(descriptor, &original);
             result |= file_get(duplicate, &copied);
             filepool_check("dup-shares-open-file", result == 0 &&
                            original == copied);
             if (original != NULL)
               {
                 file_put(original);
               }
   
             if (copied != NULL)
               {
                 file_put(copied);
               }
   
             filepool_check("close-original", close(descriptor) == 0);
             filepool_check("read-duplicate-after-close",
                            read(duplicate, buffer, sizeof(buffer)) == 0);
             filepool_check("close-duplicate", close(duplicate) == 0);
           }
         else
           {
             close(descriptor);
           }
       }
   
     for (index = 0; index < 100; index++)
       {
         errno = 0;
         descriptor = open("/filepool-missing/path", O_RDONLY);
         if (descriptor >= 0 || errno != ENOENT)
           {
             break;
           }
       }
   
     filepool_check("failed-open-releases-object", index == 100);
     if (getrlimit(RLIMIT_NOFILE, &limit) == 0)
       {
         reduced = limit;
         reduced.rlim_cur = 3;
         result = setrlimit(RLIMIT_NOFILE, &reduced);
         errno = 0;
         descriptor = open("/dev/null", O_RDWR);
         status = errno;
         result |= setrlimit(RLIMIT_NOFILE, &limit);
         filepool_check("fd-install-failure", result == 0 && descriptor < 0 &&
                        status == EMFILE);
         if (descriptor >= 0)
           {
             close(descriptor);
           }
       }
   
     for (index = 0; index < 4; index++)
       {
         if (pthread_create(&threads[index], NULL, filepool_worker,
                            &thread_failures[index]) != 0)
           {
             break;
           }
   
         created++;
       }
   
     for (index = 0; index < created; index++)
       {
         pthread_join(threads[index], NULL);
       }
   
     filepool_check("concurrent-open-dup-close", created == 4 &&
                    thread_failures[0] + thread_failures[1] +
                    thread_failures[2] + thread_failures[3] == 0);
     result = 0;
     for (index = 0; index < 20; index++)
       {
         child = task_create("filepool-child", 100, 8192, filepool_child, NULL);
         if (child <= 0 || waitpid(child, &status, 0) != child ||
             !WIFEXITED(status) || WEXITSTATUS(status) != 0)
           {
             result++;
           }
       }
   
     filepool_check("task-exit-closes-files", result == 0);
     descriptor = open("/dev/null", O_RDWR);
     filepool_check("pool-remains-usable", descriptor >= 0);
     if (descriptor >= 0)
       {
         close(descriptor);
       }
   
     filepool_check("available-capacity-restored",
                    filepool_available() == available);
   
     printf("FILEPOOL RESULT checks=%d failures=%d\n", g_checks, g_failures);
     return g_failures == 0 ? 0 : 1;
   }
   ```
   </details>
   
   ## PR verification Self-Check
   
   * [x] This PR introduces only one functional change.
   * [x] I have updated all required description fields above.
   * [x] My PR adheres to Contributing Guidelines and Documentation (git commit 
title and message, coding standard, etc).
   * [ ] My PR is still work in progress (not ready for review).
   * [x] My PR is ready for review and can be safely merged into a codebase.
   


-- 
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