Skip to content

Commit b54e2e1

Browse files
committed
lib: Introduce a way to set minimum file descriptors count
Instead of user trying to figure out what is the amount of file / socket descriptors in the system, let the various subsystems etc. specify their need using a Kconfig option. The build system will then add these smaller values together and set a suitable file descriptor count in the system. This works the same way as the heap size calculation introduced in commit 3fbf124 Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 2cfd924 commit b54e2e1

File tree

8 files changed

+63
-11
lines changed

8 files changed

+63
-11
lines changed

include/zephyr/sys/fdtable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ struct zvfs_pollfd {
245245
__syscall int zvfs_poll(struct zvfs_pollfd *fds, int nfds, int poll_timeout);
246246

247247
struct zvfs_fd_set {
248-
uint32_t bitset[(CONFIG_ZVFS_OPEN_MAX + 31) / 32];
248+
uint32_t bitset[(ZVFS_OPEN_SIZE + 31) / 32];
249249
};
250250

251251
/** @brief Number of file descriptors which can be added @ref zvfs_fd_set */

lib/os/CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,36 @@ zephyr_sources(
1313
thread_entry.c
1414
)
1515

16+
if(CONFIG_ZVFS_OPEN_IGNORE_MIN)
17+
set(final_fd_size ${CONFIG_ZVFS_OPEN_MAX})
18+
else()
19+
# Import all custom ZVFS_OPEN_ size requirements
20+
import_kconfig(CONFIG_ZVFS_OPEN_ADD_SIZE_ ${DOTCONFIG} add_size_keys)
21+
22+
# Calculate the sum of all "ADD_SIZE" requirements
23+
set(add_size_sum 0)
24+
foreach(add_size ${add_size_keys})
25+
math(EXPR add_size_sum "${add_size_sum} + ${${add_size}}")
26+
endforeach()
27+
28+
if(CONFIG_ZVFS_OPEN_MAX LESS "${add_size_sum}")
29+
# Only warn if default value 0 has been modified
30+
if(NOT CONFIG_ZVFS_OPEN_MAX EQUAL 0)
31+
message(WARNING "
32+
CONFIG_ZVFS_OPEN_MAX is less than requested minimum:
33+
${CONFIG_ZVFS_OPEN_MAX} < ${add_size_sum}
34+
Setting the file descriptor size to ${add_size_sum}")
35+
endif()
36+
37+
set(final_fd_size ${add_size_sum})
38+
else()
39+
# CONFIG_ZVFS_OPEN_MAX was greater than the sum of the requirements
40+
set(final_fd_size ${CONFIG_ZVFS_OPEN_MAX})
41+
endif()
42+
endif()
43+
44+
zephyr_compile_definitions(ZVFS_OPEN_SIZE=${final_fd_size})
45+
1646
zephyr_sources_ifdef(CONFIG_FDTABLE fdtable.c)
1747
zephyr_syscall_header_ifdef(CONFIG_FDTABLE
1848
${ZEPHYR_BASE}/include/zephyr/sys/fdtable.h

lib/os/Kconfig

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,27 @@ config FDTABLE
1212

1313
config ZVFS_OPEN_MAX
1414
int "Maximum number of open file descriptors"
15-
default 24 if NRF70_ENABLE_DUAL_VIF
16-
default 16 if WIFI_NM_WPA_SUPPLICANT
17-
default 16 if POSIX_API
18-
default 4
15+
default 0
1916
help
2017
Maximum number of open file descriptors, this includes
21-
files, sockets, special devices, etc.
18+
files, sockets, special devices, etc. If subsystems
19+
specify ZVFS_OPEN_ADD_SIZE_* options, these will be added together
20+
and the sum will be compared to the ZVFS_OPEN_MAX value.
21+
If the sum is greater than the ZVFS_OPEN_MAX option (even if this
22+
has the default 0 value), then the actual heap size will be rounded up
23+
to the sum of the individual requirements (unless the
24+
ZVFS_OPEN_IGNORE_MIN option is enabled). If the final value, after
25+
considering both this option as well as sum of the custom
26+
requirements, ends up being zero, then no file descriptors will be
27+
available.
28+
29+
config ZVFS_OPEN_IGNORE_MIN
30+
bool "Ignore the minimum fd size requirement"
31+
help
32+
This option can be set to force setting a smaller file descriptor
33+
size than what's specified by enabled subsystems. This can be useful
34+
when optimizing memory usage and a more precise minimum fd size
35+
is known for a given application.
2236

2337
config PRINTK_SYNC
2438
bool "Serialize printk() calls"

lib/os/fdtable.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ struct fd_entry {
3939
#if defined(CONFIG_POSIX_DEVICE_IO)
4040
static const struct fd_op_vtable stdinout_fd_op_vtable;
4141

42-
BUILD_ASSERT(CONFIG_ZVFS_OPEN_MAX >= 3, "CONFIG_ZVFS_OPEN_MAX >= 3 for CONFIG_POSIX_DEVICE_IO");
42+
BUILD_ASSERT(ZVFS_OPEN_SIZE >= 3, "ZVFS_OPEN_SIZE >= 3 for CONFIG_POSIX_DEVICE_IO");
4343
#endif /* defined(CONFIG_POSIX_DEVICE_IO) */
4444

45-
static struct fd_entry fdtable[CONFIG_ZVFS_OPEN_MAX] = {
45+
static struct fd_entry fdtable[ZVFS_OPEN_SIZE] = {
4646
#if defined(CONFIG_POSIX_DEVICE_IO)
4747
/*
4848
* Predefine entries for stdin/stdout/stderr.

modules/hostap/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ config WIFI_NM_WPA_SUPPLICANT
2424

2525
if WIFI_NM_WPA_SUPPLICANT
2626

27+
config ZVFS_OPEN_ADD_SIZE_WIFI_NM_WPA_SUPPLICANT
28+
int "Number of socket descriptors needed by hostap"
29+
default 12
30+
2731
config WIFI_NM_WPA_SUPPLICANT_GLOBAL_HEAP
2832
bool "Use Zephyr kernel heap for Wi-Fi driver"
2933
default y

modules/nrf_wifi/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
config ZEPHYR_NRF_WIFI_MODULE
55
bool
66

7+
config ZVFS_OPEN_ADD_SIZE_NRF70_ENABLE_DUAL_VIF
8+
int "Number of socket descriptors needed by nrf wifi driver"
9+
default 8
10+
711
source "modules/nrf_wifi/bus/Kconfig"

subsys/net/lib/dns/dispatcher.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ NET_BUF_POOL_DEFINE(dns_msg_pool, DNS_RESOLVER_BUF_CTR,
3131

3232
static struct socket_dispatch_table {
3333
struct dns_socket_dispatcher *ctx;
34-
} dispatch_table[CONFIG_ZVFS_OPEN_MAX];
34+
} dispatch_table[ZVFS_OPEN_SIZE];
3535

3636
static int dns_dispatch(struct dns_socket_dispatcher *dispatcher,
3737
int sock, struct sockaddr *addr, size_t addrlen,

subsys/net/lib/sockets/socket_obj_core.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ static K_MUTEX_DEFINE(sock_obj_mutex);
2020
/* Allocate some extra socket objects so that we can track
2121
* closed sockets and get some historical statistics.
2222
*/
23-
static struct sock_obj sock_objects[CONFIG_ZVFS_OPEN_MAX * 2] = {
24-
[0 ... ((CONFIG_ZVFS_OPEN_MAX * 2) - 1)] = {
23+
static struct sock_obj sock_objects[ZVFS_OPEN_SIZE * 2] = {
24+
[0 ... ((ZVFS_OPEN_SIZE * 2) - 1)] = {
2525
.fd = -1,
2626
.init_done = false,
2727
}

0 commit comments

Comments
 (0)