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

   ## Summary
   
   Add an optional pool for the `struct socket` objects owned by file 
descriptors.
   A static reserve avoids individual heap allocations, and configurable batches
   allow growth from the selected FS heap. Socket creation, accept, socketpair,
   file duplication, close and failure rollback use the same allocation policy.
   This uses the existing mempool callbacks and the merged FS heap support.
   
   ## Impact
   
   * Configuration: YES — `CONFIG_FS_PREALLOC_SOCKETS` and
     `CONFIG_FS_ALLOC_SOCKETS`, both defaulting to zero. With both zero,
     allocation is unchanged.
   * Behavior: a reserve with no growth has a fixed capacity and fails 
immediately
     when empty. Budget startup sockets and concurrent operations. Expanded 
chunks
     are retained for reuse. Connection structures, file objects and descriptor
     tables still use their separate allocation policies.
   * Public API/ABI, build process, hardware/drivers and security: NO changes.
   * Documentation: YES — allocation policy, ownership and memory costs in
     `file_descriptors.rst`.
   * Performance: fewer heap calls; the simulator is slower in this test.
     The baseline 20,000-cycle allocate/zero/free loop took
     7,096,940 ns; fixed reserve 16 took
     11,617,921 ns; reserve 16/batch 4 took
     11,563,147 ns. These are individual host samples.
   
   ## Testing
   
   I confirm that the flat 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: `82a1289a9e6`; tested head: 
`8125d93f98148ad7fd4d680f1428abf524215683`.
   * `sim:nsh`: default allocation, fixed reserve 16, reserve 16/batch 4,
     dynamic-only batch 4, and shared-heap reserve 16/batch 4 — 208 checks pass.
   * A local harness compiles the complete production `socket.c`, `accept.c` and
     `socketpair.c` under private names. Only heap entry points, descriptor
     installation and explicit protocol/duplication error injection are wrapped;
     successful networking, file operations and mempool operations are real.
   * Coverage includes zeroing; fixed exhaustion/reuse; failed expansion and
     recovery; both socketpair allocation and descriptor-install failures;
     socket/accept/duplication rollback; shared `dup()` versus separate
     `file_dup2()` objects; UDP flags/data and TCP socketpair data; TCP loopback
     accept flags/data; four threads doing 500 socket/dup/close cycles each;
     and 20 tasks exiting with four unclosed sockets each.
   * The unmodified base intentionally fails one added requirement:
     `no-heap-steady-state`. Its other checks pass. Steady-state heap calls drop
     from 20,000 allocation/free pairs to zero with a pool. Holding 64 objects
     with reserve 16/batch 4 needs 12 expansion allocations.
   * Small `sim:nsh` configuration, no local harness or coverage 
instrumentation:
     disabled pool has identical text/data/bss to the base; reserve 16/batch 4
     adds text +440, data +224, bss +544 bytes.
   * `qemu-armv7r:pnsh` (protected) and `qemu-armv8a:knsh` (kernel) compile with
     TCP/UDP, the socket pool and an independent FS heap enabled.
   * Kconfig refresh, full touched-file/commit checks, Make/CMake and ostest 
pass.
   * No hardware, protected/kernel runtime, SMP stress, KASAN or optional
     mempool backtrace-recording validation. The injected error branches are
     local tests; no new public test interface is added by this feature.
   
   Testing logs before change:
   
   <details>
   <summary>Unmodified base: runtime output</summary>
   
   ```text
   nsh> socketpoolcheck require-pool
   CHECK raw-zero: PASS
   BENCH cycles=20000 heap_allocations=20000 heap_releases=20000 ns=7096940
   CHECK raw-reuse-zeroed: PASS
   CHECK no-heap-steady-state: FAIL
   BULK count=64 heap_allocations=64
   CHECK bulk-capacity: PASS
   CHECK heap-failure-or-fixed-exhaustion: PASS
   CHECK recovery-after-failure: PASS
   CHECK raw-no-live-objects: PASS
   CHECK socket-flags: PASS
   CHECK dup-shares-object: PASS
   CHECK file-dup-failure: PASS
   CHECK file-dup-success: PASS
   CHECK copied-file-remains-usable: PASS
   CHECK copied-file-close: PASS
   CHECK unsupported-domain-rollback: PASS
   CHECK socket-fd-install-rollback: PASS
   CHECK socketpair-null: PASS
   CHECK socketpair-protocol-rollback: PASS
   CHECK socketpair-first-fd-rollback: PASS
   CHECK socketpair-second-fd-rollback: PASS
   CHECK socketpair-flags: PASS
   CHECK socketpair-data: PASS
   CHECK tcp-socketpair: PASS
   CHECK tcp-socketpair-data: PASS
   CHECK listener: PASS
   CHECK accept-invalid-flags: PASS
   CHECK accept-protocol-rollback: PASS
   CHECK loopback-connect: PASS
   CHECK accept-fd-install-rollback: PASS
   CHECK loopback-connect: PASS
   CHECK accept-flags: PASS
   CHECK accept-data: PASS
   CHECK exhaustion-held-socket: PASS
   CHECK socket-pool-exhaustion: PASS
   CHECK accept-allocation-failure: PASS
   CHECK file-dup-allocation-failure: PASS
   CHECK pair-first-allocation-failure: PASS
   CHECK failure-capacity-restored: PASS
   CHECK four-threads-500-socket-dup-close: PASS
   CHECK twenty-task-exits-close-sockets: PASS
   CHECK still-usable: PASS
   CHECK final-no-live-objects: PASS
   SOCKETPOOL RESULT checks=41 failures=1
   nsh> poweroff
   ```
   </details>
   
   Testing logs after change:
   
   <details>
   <summary>sim:nsh — default</summary>
   
   ```text
   nsh> socketpoolcheck
   CHECK raw-zero: PASS
   BENCH cycles=20000 heap_allocations=20000 heap_releases=20000 ns=7261153
   CHECK raw-reuse-zeroed: PASS
   BULK count=64 heap_allocations=64
   CHECK bulk-capacity: PASS
   CHECK heap-failure-or-fixed-exhaustion: PASS
   CHECK recovery-after-failure: PASS
   CHECK raw-no-live-objects: PASS
   CHECK socket-flags: PASS
   CHECK dup-shares-object: PASS
   CHECK file-dup-failure: PASS
   CHECK file-dup-success: PASS
   CHECK copied-file-remains-usable: PASS
   CHECK copied-file-close: PASS
   CHECK unsupported-domain-rollback: PASS
   CHECK socket-fd-install-rollback: PASS
   CHECK socketpair-null: PASS
   CHECK socketpair-protocol-rollback: PASS
   CHECK socketpair-first-fd-rollback: PASS
   CHECK socketpair-second-fd-rollback: PASS
   CHECK socketpair-flags: PASS
   CHECK socketpair-data: PASS
   CHECK tcp-socketpair: PASS
   CHECK tcp-socketpair-data: PASS
   CHECK listener: PASS
   CHECK accept-invalid-flags: PASS
   CHECK accept-protocol-rollback: PASS
   CHECK loopback-connect: PASS
   CHECK accept-fd-install-rollback: PASS
   CHECK loopback-connect: PASS
   CHECK accept-flags: PASS
   CHECK accept-data: PASS
   CHECK exhaustion-held-socket: PASS
   CHECK socket-pool-exhaustion: PASS
   CHECK accept-allocation-failure: PASS
   CHECK file-dup-allocation-failure: PASS
   CHECK pair-first-allocation-failure: PASS
   CHECK failure-capacity-restored: PASS
   CHECK four-threads-500-socket-dup-close: PASS
   CHECK twenty-task-exits-close-sockets: PASS
   CHECK still-usable: PASS
   CHECK final-no-live-objects: PASS
   SOCKETPOOL RESULT checks=40 failures=0
   nsh> poweroff
   ```
   </details>
   
   <details>
   <summary>sim:nsh — fixed</summary>
   
   ```text
   nsh> socketpoolcheck require-pool
   CHECK raw-zero: PASS
   BENCH cycles=20000 heap_allocations=0 heap_releases=0 ns=11617921
   CHECK raw-reuse-zeroed: PASS
   CHECK no-heap-steady-state: PASS
   BULK count=16 heap_allocations=0
   CHECK fixed-capacity: PASS
   CHECK heap-failure-or-fixed-exhaustion: PASS
   CHECK recovery-after-failure: PASS
   CHECK raw-no-live-objects: PASS
   CHECK socket-flags: PASS
   CHECK dup-shares-object: PASS
   CHECK file-dup-failure: PASS
   CHECK file-dup-success: PASS
   CHECK copied-file-remains-usable: PASS
   CHECK copied-file-close: PASS
   CHECK unsupported-domain-rollback: PASS
   CHECK socket-fd-install-rollback: PASS
   CHECK socketpair-null: PASS
   CHECK socketpair-protocol-rollback: PASS
   CHECK socketpair-first-fd-rollback: PASS
   CHECK socketpair-second-fd-rollback: PASS
   CHECK socketpair-flags: PASS
   CHECK socketpair-data: PASS
   CHECK tcp-socketpair: PASS
   CHECK tcp-socketpair-data: PASS
   CHECK listener: PASS
   CHECK accept-invalid-flags: PASS
   CHECK accept-protocol-rollback: PASS
   CHECK loopback-connect: PASS
   CHECK accept-fd-install-rollback: PASS
   CHECK loopback-connect: PASS
   CHECK accept-flags: PASS
   CHECK accept-data: PASS
   CHECK exhaustion-held-socket: PASS
   CHECK socket-pool-exhaustion: PASS
   CHECK accept-allocation-failure: PASS
   CHECK file-dup-allocation-failure: PASS
   CHECK pair-first-allocation-failure: PASS
   CHECK pair-second-allocation-failure: PASS
   CHECK failure-capacity-restored: PASS
   CHECK four-threads-500-socket-dup-close: PASS
   CHECK twenty-task-exits-close-sockets: PASS
   CHECK still-usable: PASS
   CHECK final-no-live-objects: PASS
   SOCKETPOOL RESULT checks=42 failures=0
   nsh> poweroff
   ```
   </details>
   
   <details>
   <summary>sim:nsh — expanding</summary>
   
   ```text
   nsh> socketpoolcheck require-pool
   CHECK raw-zero: PASS
   BENCH cycles=20000 heap_allocations=0 heap_releases=0 ns=11563147
   CHECK raw-reuse-zeroed: PASS
   CHECK no-heap-steady-state: PASS
   BULK count=64 heap_allocations=12
   CHECK bulk-capacity: PASS
   CHECK heap-failure-or-fixed-exhaustion: PASS
   CHECK recovery-after-failure: PASS
   CHECK raw-no-live-objects: PASS
   CHECK socket-flags: PASS
   CHECK dup-shares-object: PASS
   CHECK file-dup-failure: PASS
   CHECK file-dup-success: PASS
   CHECK copied-file-remains-usable: PASS
   CHECK copied-file-close: PASS
   CHECK unsupported-domain-rollback: PASS
   CHECK socket-fd-install-rollback: PASS
   CHECK socketpair-null: PASS
   CHECK socketpair-protocol-rollback: PASS
   CHECK socketpair-first-fd-rollback: PASS
   CHECK socketpair-second-fd-rollback: PASS
   CHECK socketpair-flags: PASS
   CHECK socketpair-data: PASS
   CHECK tcp-socketpair: PASS
   CHECK tcp-socketpair-data: PASS
   CHECK listener: PASS
   CHECK accept-invalid-flags: PASS
   CHECK accept-protocol-rollback: PASS
   CHECK loopback-connect: PASS
   CHECK accept-fd-install-rollback: PASS
   CHECK loopback-connect: PASS
   CHECK accept-flags: PASS
   CHECK accept-data: PASS
   CHECK exhaustion-held-socket: PASS
   CHECK socket-pool-exhaustion: PASS
   CHECK accept-allocation-failure: PASS
   CHECK file-dup-allocation-failure: PASS
   CHECK pair-first-allocation-failure: PASS
   CHECK pair-second-allocation-failure: PASS
   CHECK failure-capacity-restored: PASS
   CHECK four-threads-500-socket-dup-close: PASS
   CHECK twenty-task-exits-close-sockets: PASS
   CHECK still-usable: PASS
   CHECK final-no-live-objects: PASS
   SOCKETPOOL RESULT checks=42 failures=0
   nsh> poweroff
   ```
   </details>
   
   <details>
   <summary>sim:nsh — dynamic</summary>
   
   ```text
   nsh> socketpoolcheck require-pool
   CHECK raw-zero: PASS
   BENCH cycles=20000 heap_allocations=0 heap_releases=0 ns=11583885
   CHECK raw-reuse-zeroed: PASS
   CHECK no-heap-steady-state: PASS
   BULK count=64 heap_allocations=15
   CHECK bulk-capacity: PASS
   CHECK heap-failure-or-fixed-exhaustion: PASS
   CHECK recovery-after-failure: PASS
   CHECK raw-no-live-objects: PASS
   CHECK socket-flags: PASS
   CHECK dup-shares-object: PASS
   CHECK file-dup-failure: PASS
   CHECK file-dup-success: PASS
   CHECK copied-file-remains-usable: PASS
   CHECK copied-file-close: PASS
   CHECK unsupported-domain-rollback: PASS
   CHECK socket-fd-install-rollback: PASS
   CHECK socketpair-null: PASS
   CHECK socketpair-protocol-rollback: PASS
   CHECK socketpair-first-fd-rollback: PASS
   CHECK socketpair-second-fd-rollback: PASS
   CHECK socketpair-flags: PASS
   CHECK socketpair-data: PASS
   CHECK tcp-socketpair: PASS
   CHECK tcp-socketpair-data: PASS
   CHECK listener: PASS
   CHECK accept-invalid-flags: PASS
   CHECK accept-protocol-rollback: PASS
   CHECK loopback-connect: PASS
   CHECK accept-fd-install-rollback: PASS
   CHECK loopback-connect: PASS
   CHECK accept-flags: PASS
   CHECK accept-data: PASS
   CHECK exhaustion-held-socket: PASS
   CHECK socket-pool-exhaustion: PASS
   CHECK accept-allocation-failure: PASS
   CHECK file-dup-allocation-failure: PASS
   CHECK pair-first-allocation-failure: PASS
   CHECK pair-second-allocation-failure: PASS
   CHECK failure-capacity-restored: PASS
   CHECK four-threads-500-socket-dup-close: PASS
   CHECK twenty-task-exits-close-sockets: PASS
   CHECK still-usable: PASS
   CHECK final-no-live-objects: PASS
   SOCKETPOOL RESULT checks=42 failures=0
   nsh> poweroff
   ```
   </details>
   
   <details>
   <summary>sim:nsh — shared-heap</summary>
   
   ```text
   nsh> socketpoolcheck require-pool
   CHECK raw-zero: PASS
   BENCH cycles=20000 heap_allocations=0 heap_releases=0 ns=11400795
   CHECK raw-reuse-zeroed: PASS
   CHECK no-heap-steady-state: PASS
   BULK count=64 heap_allocations=12
   CHECK bulk-capacity: PASS
   CHECK heap-failure-or-fixed-exhaustion: PASS
   CHECK recovery-after-failure: PASS
   CHECK raw-no-live-objects: PASS
   CHECK socket-flags: PASS
   CHECK dup-shares-object: PASS
   CHECK file-dup-failure: PASS
   CHECK file-dup-success: PASS
   CHECK copied-file-remains-usable: PASS
   CHECK copied-file-close: PASS
   CHECK unsupported-domain-rollback: PASS
   CHECK socket-fd-install-rollback: PASS
   CHECK socketpair-null: PASS
   CHECK socketpair-protocol-rollback: PASS
   CHECK socketpair-first-fd-rollback: PASS
   CHECK socketpair-second-fd-rollback: PASS
   CHECK socketpair-flags: PASS
   CHECK socketpair-data: PASS
   CHECK tcp-socketpair: PASS
   CHECK tcp-socketpair-data: PASS
   CHECK listener: PASS
   CHECK accept-invalid-flags: PASS
   CHECK accept-protocol-rollback: PASS
   CHECK loopback-connect: PASS
   CHECK accept-fd-install-rollback: PASS
   CHECK loopback-connect: PASS
   CHECK accept-flags: PASS
   CHECK accept-data: PASS
   CHECK exhaustion-held-socket: PASS
   CHECK socket-pool-exhaustion: PASS
   CHECK accept-allocation-failure: PASS
   CHECK file-dup-allocation-failure: PASS
   CHECK pair-first-allocation-failure: PASS
   CHECK pair-second-allocation-failure: PASS
   CHECK failure-capacity-restored: PASS
   CHECK four-threads-500-socket-dup-close: PASS
   CHECK twenty-task-exits-close-sockets: PASS
   CHECK still-usable: PASS
   CHECK final-no-live-objects: PASS
   SOCKETPOOL RESULT checks=42 failures=0
   nsh> poweroff
   ```
   </details>
   
   <details>
   <summary>Small configuration size output (base / disabled / reserve 16, 
batch 4)</summary>
   
   ```text
   text    data     bss     dec     hex filename
    627701         4048   24096  655845   a01e5 nuttx
   text    data     bss     dec     hex filename
    627701         4048   24096  655845   a01e5 nuttx
   text    data     bss     dec     hex filename
    628141         4272   24640  657053   a069d nuttx
   ```
   </details>
   
   <details>
   <summary>Protected/kernel build output excerpts</summary>
   
   ```text
   qemu-armv7r:pnsh: socket pool + networking + independent FS heap build PASS
   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:      222816 B       512 KB     42.50%
             uflash:          0 GB       512 KB      0.00%
              ksram:         20 KB         8 MB      0.24%
              usram:          0 GB         8 MB      0.00%
   CP: nuttx.bin
   qemu-armv8a:knsh: socket pool + networking + independent FS heap build PASS
   
   LD: nuttx
   Memory region         Used Size  Region Size  %age Used
               dram:        376 KB         2 MB     18.36%
              pgram:           0 B         4 MB      0.00%
   CP: nuttx.hex
   CP: nuttx.bin
   ```
   </details>
   
   <details>
   <summary>Final local gate</summary>
   
   ```text
   target   : nuttx
   branch   : fs/socket-mempool
   head     : 8125d93f98148ad7fd4d680f1428abf524215683
   subject  : fs: add mempool for socket objects
   commits  : 1
   base     : 82a1289a9e (merge-base with upstream/master)
   behind   : 3 commits of upstream/master
   nuttx    : 8125d93f98
   apps     : 53f5906de5
   options  : CONFIG_NET CONFIG_NET_IPv4 CONFIG_NET_TCP CONFIG_NET_UDP 
CONFIG_NET_LOOPBACK CONFIG_NET_TCPBACKLOG CONFIG_FS_HEAPSIZE=1048576 
CONFIG_FS_PREALLOC_SOCKETS=16 CONFIG_FS_ALLOC_SOCKETS=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>Local reproduction program</summary>
   
   Place these four files in `apps/examples/socketpoolcheck/` in a sibling apps
   tree. Configure `sim:nsh`; enable NET, NET_IPv4, NET_TCP, NET_UDP,
   NET_LOOPBACK, NET_TCPBACKLOG, FS_PROCFS, DEBUG_ASSERTIONS,
   EXAMPLES_SOCKETPOOLCHECK, and set FS_HEAPSIZE=1048576. Enable
   DEBUG_FULLOPT and COVERAGE_NONE for the recorded comparison. Set the pool
   reserve/growth counts for each case, build twice to refresh builtin
   registration, and run `socketpoolcheck require-pool` from NSH (omit
   `require-pool` when both counts are zero). Set FS_HEAPSIZE=0 to check
   the shared-heap case. On the unmodified base, the added zero-heap-call
   criterion intentionally fails; the normal lifecycle checks pass.
   
   `Kconfig`
   
   ```text
   config EXAMPLES_SOCKETPOOLCHECK
        bool "Local socket pool validation"
        default n
        depends on BUILD_FLAT && NET && NET_TCP && NET_UDP && NET_LOOPBACK
   ```
   
   `Make.defs`
   
   ```text
   ifeq ($(CONFIG_EXAMPLES_SOCKETPOOLCHECK),y)
   CONFIGURED_APPS += $(APPDIR)/examples/socketpoolcheck
   endif
   ```
   
   `Makefile`
   
   ```text
   include $(APPDIR)/Make.defs
   
   PROGNAME = socketpoolcheck
   PRIORITY = SCHED_PRIORITY_DEFAULT
   STACKSIZE = 32768
   MODULE = $(CONFIG_EXAMPLES_SOCKETPOOLCHECK)
   MAINSRC = socketpoolcheck_main.c
   CFLAGS += -I$(abspath $(APPDIR)/../nuttx/fs)
   CFLAGS += -I$(abspath $(APPDIR)/../nuttx)
   CFLAGS += -I$(abspath $(APPDIR)/../nuttx/sched)
   
   include $(APPDIR)/Application.mk
   ```
   
   `socketpoolcheck_main.c`
   
   ```c
   #include <nuttx/config.h>
   #include <nuttx/net/net.h>
   #include <nuttx/fs/fs.h>
   #include <nuttx/fs/ioctl.h>
   #include <nuttx/sched.h>
   #include <sys/socket.h>
   #include <sys/wait.h>
   #include <netinet/in.h>
   #include <arpa/inet.h>
   #include <errno.h>
   #include <fcntl.h>
   #include <pthread.h>
   #include <sched.h>
   #include <stdio.h>
   #include <string.h>
   #include <time.h>
   #include <unistd.h>
   #include "fs_heap.h"
   
   #ifndef CONFIG_FS_PREALLOC_SOCKETS
   #define CONFIG_FS_PREALLOC_SOCKETS 0
   #endif
   #ifndef CONFIG_FS_ALLOC_SOCKETS
   #define CONFIG_FS_ALLOC_SOCKETS 0
   #endif
   
   static int g_allocations;
   static int g_releases;
   static int g_fail_heap;
   static int g_fd_calls;
   static int g_fail_fd;
   static int g_fail_accept;
   static int g_fail_pair;
   static int g_fail_dup;
   static int g_checks;
   static int g_failures;
   
   static __attribute__((unused)) void *test_malloc(size_t size)
   {
     __atomic_fetch_add(&g_allocations, 1, __ATOMIC_RELAXED);
     return g_fail_heap ? NULL : fs_heap_malloc(size);
   }
   
   static __attribute__((unused)) void *test_zalloc(size_t size)
   {
     __atomic_fetch_add(&g_allocations, 1, __ATOMIC_RELAXED);
     return g_fail_heap ? NULL : fs_heap_zalloc(size);
   }
   
   static void test_free(void *buffer)
   {
     __atomic_fetch_add(&g_releases, 1, __ATOMIC_RELAXED);
     fs_heap_free(buffer);
   }
   
   static int test_install(struct inode *inode, int flags, off_t pos,
                           void *priv, int minfd)
   {
     if (g_fail_fd)
       {
         g_fd_calls++;
       }
   
     return g_fail_fd && g_fd_calls == g_fail_fd ? -EMFILE :
       file_allocate_from_inode(inode, flags, pos, priv, minfd);
   }
   
   static int test_accept(struct socket *sock, struct sockaddr *addr,
                          socklen_t *len, struct socket *newsock, int flags)
   {
     return g_fail_accept ? -EIO : psock_accept(sock, addr, len, newsock, 
flags);
   }
   
   static int test_pair(int domain, int type, int protocol, struct socket 
*socks[2])
   {
     return g_fail_pair ? -EIO : psock_socketpair(domain, type, protocol, 
socks);
   }
   
   static int test_dup(struct socket *src, struct socket *dest)
   {
     return g_fail_dup ? -EIO : psock_dup2(src, dest);
   }
   
   #undef fs_heap_malloc
   #undef fs_heap_zalloc
   #undef fs_heap_free
   #define fs_heap_malloc test_malloc
   #define fs_heap_zalloc test_zalloc
   #define fs_heap_free test_free
   #define file_allocate_from_inode test_install
   #define psock_accept test_accept
   #define psock_socketpair test_pair
   #define psock_dup2 test_dup
   #define socket(...) test_socket(__VA_ARGS__)
   #define socketpair test_socketpair
   #define accept4 test_accept4
   #define sockfd_allocate test_sockfd_allocate
   #define sockfd_socket test_sockfd_socket
   #define file_socket test_file_socket
   #if CONFIG_FS_PREALLOC_SOCKETS > 0 || CONFIG_FS_ALLOC_SOCKETS > 0
   #define sock_alloc test_sock_alloc
   #define sock_free test_sock_free
   #endif
   
   int test_socket(int domain, int type, int protocol);
   int test_socketpair(int domain, int type, int protocol, int sv[2]);
   int test_accept4(int fd, struct sockaddr *addr, socklen_t *len, int flags);
   int test_sockfd_allocate(struct socket *sock, int flags);
   int test_sockfd_socket(int fd, struct file **filep, struct socket **sock);
   struct socket *test_file_socket(struct file *filep);
   
   #include "../../../nuttx/fs/socket/socket.c"
   #include "../../../nuttx/fs/socket/accept.c"
   #include "../../../nuttx/fs/socket/socketpair.c"
   
   #ifndef __FS_SOCKET_SOCKET_H
   #define sock_alloc() test_zalloc(sizeof(struct socket))
   #define sock_free(p) test_free(p)
   #endif
   
   static void check(const char *name, int condition)
   {
     g_checks++;
     g_failures += !condition;
     printf("CHECK %s: %s\n", name, condition ? "PASS" : "FAIL");
   }
   
   static int iszero(const void *buffer, size_t size)
   {
     const unsigned char *bytes = buffer;
     size_t i;
     for (i = 0; i < size; i++)
       {
         if (bytes[i] != 0)
           {
             return 0;
           }
       }
   
     return 1;
   }
   
   static int live(void)
   {
   #if CONFIG_FS_PREALLOC_SOCKETS > 0 || CONFIG_FS_ALLOC_SOCKETS > 0
     struct mempoolinfo_s info;
     mempool_info(&g_socket_mempool, &info);
     return info.aordblks;
   #else
     return 0;
   #endif
   }
   
   static int child_main(int argc, char **argv)
   {
     int i;
     for (i = 0; i < 4; i++)
       {
         if (socket(AF_INET, SOCK_DGRAM, 0) < 0)
           {
             return 1;
           }
       }
   
     return 0;
   }
   
   static void *worker(void *arg)
   {
     int *failures = arg;
     int fd;
     int copy;
     int i;
     for (i = 0; i < 500; i++)
       {
         fd = socket(AF_INET, SOCK_DGRAM, 0);
         if (fd < 0)
           {
             (*failures)++;
             continue;
           }
   
         copy = dup(fd);
         if (copy < 0)
           {
             (*failures)++;
           }
         else
           {
             close(copy);
           }
   
         close(fd);
       }
   
     return NULL;
   }
   
   static void raw_checks(int require_pool)
   {
     struct socket *socks[128];
     struct socket *sock;
     struct timespec start;
     struct timespec stop;
     int before_alloc;
     int before_free;
     int count;
     int i;
     int zeroed = 1;
     sock = sock_alloc();
     check("raw-zero", sock != NULL && iszero(sock, sizeof(*sock)));
     if (sock == NULL)
       {
         return;
       }
   
     memset(sock, 0xa5, sizeof(*sock));
     sock_free(sock);
     before_alloc = g_allocations;
     before_free = g_releases;
     clock_gettime(CLOCK_MONOTONIC, &start);
     for (i = 0; i < 20000; i++)
       {
         sock = sock_alloc();
         if (sock == NULL || !iszero(sock, sizeof(*sock)))
           {
             zeroed = 0;
             break;
           }
   
         memset(sock, 0xa5, sizeof(*sock));
         sock_free(sock);
       }
   
     clock_gettime(CLOCK_MONOTONIC, &stop);
     printf("BENCH cycles=%d heap_allocations=%d heap_releases=%d ns=%lld\n",
            i, g_allocations - before_alloc, g_releases - before_free,
            (stop.tv_sec - start.tv_sec) * 1000000000ll +
            stop.tv_nsec - start.tv_nsec);
     check("raw-reuse-zeroed", zeroed && i == 20000);
     if (require_pool)
       {
         check("no-heap-steady-state", g_allocations == before_alloc &&
               g_releases == before_free);
       }
   
     before_alloc = g_allocations;
     for (count = 0; count < 64; count++)
       {
         socks[count] = sock_alloc();
         if (socks[count] == NULL)
           {
             break;
           }
       }
   
     printf("BULK count=%d heap_allocations=%d\n", count,
            g_allocations - before_alloc);
   #if CONFIG_FS_PREALLOC_SOCKETS > 0 && CONFIG_FS_ALLOC_SOCKETS == 0
     check("fixed-capacity", count == CONFIG_FS_PREALLOC_SOCKETS &&
           sock_alloc() == NULL && g_allocations == 0);
   #else
     check("bulk-capacity", count == 64);
   #endif
     for (i = 0; i < count; i++)
       {
         sock_free(socks[i]);
       }
   
     g_fail_heap = 1;
     for (count = 0; count < 128; count++)
       {
         socks[count] = sock_alloc();
         if (socks[count] == NULL)
           {
             break;
           }
       }
   
     check("heap-failure-or-fixed-exhaustion", count < 128 && sock_alloc() == 
NULL);
     for (i = 0; i < count; i++)
       {
         sock_free(socks[i]);
       }
   
     g_fail_heap = 0;
     sock = sock_alloc();
     check("recovery-after-failure", sock != NULL);
     if (sock)
       {
         sock_free(sock);
       }
   
     check("raw-no-live-objects", live() == 0);
   }
   
   static void lifecycle_checks(void)
   {
     struct sockaddr_in addr = {0};
     struct file *filep;
     struct file copied = {0};
     struct socket *socks[128];
     pthread_t threads[4];
     int failures[4] = {0};
     int sv[2];
     int fd;
     int client;
     int accepted;
     int copy;
     int saved;
     int i;
     int j;
     int status;
     int count;
     int before;
     char byte;
     pid_t pid;
   
     fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
     check("socket-flags", fd >= 0 && (fcntl(fd, F_GETFD) & FD_CLOEXEC) &&
           (fcntl(fd, F_GETFL) & O_NONBLOCK));
     before = live();
     copy = dup(fd);
     check("dup-shares-object", copy >= 0 && live() == before);
     if (copy >= 0)
       {
         close(copy);
       }
   
     if (fd >= 0 && file_get(fd, &filep) == 0)
       {
         g_fail_dup = 1;
         check("file-dup-failure", file_dup2(filep, &copied) == -EIO &&
               live() == before);
         g_fail_dup = 0;
         memset(&copied, 0, sizeof(copied));
         check("file-dup-success", file_dup2(filep, &copied) == 0 &&
               copied.f_priv != filep->f_priv);
         file_put(filep);
         close(fd);
         status = 1;
         check("copied-file-remains-usable", file_ioctl(&copied, FIONBIO,
               (unsigned long)(uintptr_t)&status) == 0);
         check("copied-file-close", file_close(&copied) == 0 && live() == 0);
       }
   
     before = live();
     check("unsupported-domain-rollback", socket(-1, SOCK_DGRAM, 0) < 0 &&
           live() == before);
     g_fd_calls = 0;
     g_fail_fd = 1;
     fd = socket(AF_INET, SOCK_DGRAM, 0);
     check("socket-fd-install-rollback", fd < 0 && errno == EMFILE && live() == 
0);
     g_fail_fd = 0;
   
     check("socketpair-null", socketpair(AF_INET, SOCK_STREAM, 0, NULL) < 0 &&
           errno == EINVAL);
     g_fail_pair = 1;
     check("socketpair-protocol-rollback", socketpair(AF_INET, SOCK_STREAM, 0, 
sv) < 0 &&
           errno == EIO && live() == 0);
     g_fail_pair = 0;
     for (j = 1; j <= 2; j++)
       {
         g_fd_calls = 0;
         g_fail_fd = j;
         status = socketpair(AF_INET, SOCK_STREAM, 0, sv);
         check(j == 1 ? "socketpair-first-fd-rollback" : 
"socketpair-second-fd-rollback",
               status < 0 && errno == EMFILE && live() == 0);
         g_fail_fd = 0;
       }
   
     status = socketpair(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, 
sv);
     check("socketpair-flags", status == 0 && (fcntl(sv[0], F_GETFL) & 
O_NONBLOCK) &&
           (fcntl(sv[1], F_GETFD) & FD_CLOEXEC));
     if (status == 0)
       {
         check("socketpair-data", write(sv[0], "x", 1) == 1 &&
               read(sv[1], &byte, 1) == 1 && byte == 'x');
         close(sv[0]);
         close(sv[1]);
       }
   
     status = socketpair(AF_INET, SOCK_STREAM, 0, sv);
     check("tcp-socketpair", status == 0);
     if (status == 0)
       {
         check("tcp-socketpair-data", write(sv[0], "t", 1) == 1 &&
               read(sv[1], &byte, 1) == 1 && byte == 't');
         close(sv[0]);
         close(sv[1]);
       }
   
     fd = socket(AF_INET, SOCK_STREAM, 0);
     addr.sin_family = AF_INET;
     addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
     addr.sin_port = htons(32546);
     status = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
     status |= listen(fd, 4);
     check("listener", fd >= 0 && status == 0);
     before = live();
     check("accept-invalid-flags", accept4(fd, NULL, NULL, -1) < 0 &&
           errno == EINVAL && live() == before);
     g_fail_accept = 1;
     check("accept-protocol-rollback", accept4(fd, NULL, NULL, 0) < 0 &&
           errno == EIO && live() == before);
     g_fail_accept = 0;
     for (j = 0; j < 2; j++)
       {
         client = socket(AF_INET, SOCK_STREAM, 0);
         status = connect(client, (struct sockaddr *)&addr, sizeof(addr));
         check("loopback-connect", client >= 0 && status == 0);
         before = live();
         g_fd_calls = 0;
         g_fail_fd = j == 0 ? 1 : 0;
         accepted = accept4(fd, NULL, NULL, SOCK_NONBLOCK | SOCK_CLOEXEC);
         saved = errno;
         g_fail_fd = 0;
         if (j == 0)
           {
             check("accept-fd-install-rollback", accepted < 0 && saved == 
ENFILE &&
                   live() == before);
           }
         else
           {
             check("accept-flags", accepted >= 0 &&
                   (fcntl(accepted, F_GETFL) & O_NONBLOCK) &&
                   (fcntl(accepted, F_GETFD) & FD_CLOEXEC));
             if (accepted >= 0)
               {
                 check("accept-data", write(client, "y", 1) == 1 &&
                       read(accepted, &byte, 1) == 1 && byte == 'y');
                 close(accepted);
               }
           }
   
         close(client);
       }
   
     close(fd);
     fd = socket(AF_INET, SOCK_DGRAM, 0);
     filep = NULL;
     check("exhaustion-held-socket", fd >= 0 && file_get(fd, &filep) == 0);
     g_fail_heap = 1;
     for (count = 0; count < 128; count++)
       {
         socks[count] = sock_alloc();
         if (socks[count] == NULL)
           {
             break;
           }
       }
   
     before = live();
     check("socket-pool-exhaustion", socket(AF_INET, SOCK_DGRAM, 0) < 0 &&
           errno == ENOMEM);
     check("accept-allocation-failure", accept4(fd, NULL, NULL, 0) < 0 &&
           errno == ENOMEM && live() == before);
     memset(&copied, 0, sizeof(copied));
     check("file-dup-allocation-failure", file_dup2(filep, &copied) == -ENOMEM 
&&
           live() == before);
     check("pair-first-allocation-failure", socketpair(AF_INET, SOCK_STREAM, 0, 
sv) < 0 &&
           errno == ENOMEM && live() == before);
     if (count > 0)
       {
         sock_free(socks[--count]);
         before = live();
         check("pair-second-allocation-failure", socketpair(AF_INET, 
SOCK_STREAM, 0, sv) < 0 &&
               errno == ENOMEM && live() == before);
       }
   
     for (i = 0; i < count; i++)
       {
         sock_free(socks[i]);
       }
   
     g_fail_heap = 0;
     if (filep)
       {
         file_put(filep);
       }
   
     close(fd);
     check("failure-capacity-restored", live() == 0);
   
     for (i = 0; i < 4; i++)
       {
         if (pthread_create(&threads[i], NULL, worker, &failures[i]) != 0)
           {
             break;
           }
       }
   
     count = i;
     for (i = 0; i < count; i++)
       {
         pthread_join(threads[i], NULL);
       }
   
     check("four-threads-500-socket-dup-close", count == 4 && live() == 0 &&
           failures[0] + failures[1] + failures[2] + failures[3] == 0);
     status = 0;
     for (i = 0; i < 20; i++)
       {
         pid = task_create("sockpool-child", 100, 8192, child_main, NULL);
         if (pid < 0 || waitpid(pid, &saved, 0) != pid ||
             !WIFEXITED(saved) || WEXITSTATUS(saved) != 0)
           {
             status++;
           }
       }
   
     check("twenty-task-exits-close-sockets", status == 0 && live() == 0);
     fd = socket(AF_INET, SOCK_DGRAM, 0);
     check("still-usable", fd >= 0);
     if (fd >= 0)
       {
         close(fd);
       }
   
     check("final-no-live-objects", live() == 0);
   }
   
   int socketpoolcheck_main(int argc, char **argv)
   {
     g_checks = 0;
     g_failures = 0;
     raw_checks(argc > 1);
     lifecycle_checks();
     printf("SOCKETPOOL RESULT checks=%d failures=%d\n", g_checks, g_failures);
     return g_failures ? 1 : 0;
   }
   ```
   
   </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]

  • [PR] fs: add mempool for sock... jingfei195887 (via GitHub) via commits

Reply via email to