Skip to content

sched: default NFILE_DESCRIPTORS_PER_BLOCK to 4 on DEFAULT_SMALL - #22419

Merged
GUIDINGLI merged 1 commit into
apache:masterfrom
Swatantra-66:fix/sched-file-descriptors-default-small
Nov 28, 2026
Merged

GUIDINGLI merged 1 commit into
apache:masterfrom
Swatantra-66:fix/sched-file-descriptors-default-small

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

Addresses Issue #20414

In the NuttX VFS implementation, file descriptors are allocated in chunks of CONFIG_NFILE_DESCRIPTORS_PER_BLOCK. The initial block is statically embedded in struct fdlist (fl_prefds) inside each task group (struct task_group_s) to avoid heap allocation overhead during task creation and standard I/O initialization.

Previously, NFILE_DESCRIPTORS_PER_BLOCK defaulted unconditionally to 8, unlike other scheduler pre-allocation parameters (SEM_PREALLOCHOLDERS, SIG_PREALLOC_IRQ_ACTIONS, PREALLOC_MQ_MSGS, PREALLOC_TIMERS, PREALLOC_IRQCHAIN) which scale down to 4 when DEFAULT_SMALL is enabled.

This patch updates CONFIG_NFILE_DESCRIPTORS_PER_BLOCK to default to 4 when DEFAULT_SMALL is enabled and 8 otherwise. On 32-bit targets, each struct fd consumes 8 bytes (4-byte file pointer plus flags and alignment padding), reducing the pre-allocated descriptor table from 64 bytes to 32 bytes and saving 32 bytes of static RAM per active task group. On 64-bit targets, each struct fd is 16 bytes, saving 64 bytes of RAM per task group.

Standard POSIX streams (stdin 0, stdout 1, stderr 2) and one additional file descriptor are accommodated in the initial pre-allocated block without runtime heap allocation. When a task requires additional descriptors, fdlist_extend() in fs/inode/fs_files.c dynamically allocates further blocks of 4 descriptors, preserving full functional compatibility.

Redundant CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=4 entries are removed from 11 board defconfigs (10 AVR configurations and stm32vldiscovery:nsh) to keep defconfig files canonical, and Documentation/os/scheduling/tasks_vs_threads.rst is updated to reference the modern configuration symbol.

Impact

  • Is new feature added? No.
  • User: Saves 32 bytes SRAM per task group on 32-bit (64 bytes on 64-bit) for all DEFAULT_SMALL targets. Standard builds (!DEFAULT_SMALL) retain default 8.
  • Build: 11 board defconfigs refreshed to remove redundant entries.
  • Documentation: Updated Documentation/os/scheduling/tasks_vs_threads.rst to reference CONFIG_NFILE_DESCRIPTORS_PER_BLOCK.

Testing

Before and After Comparison

Configuration Target Board Before (master) After (this patch) Memory Impact
DEFAULT_SMALL=y (32-bit) stm32f103-minimum:nsh NFILE_DESCRIPTORS_PER_BLOCK=8 NFILE_DESCRIPTORS_PER_BLOCK=4 -32 B SRAM / task group
DEFAULT_SMALL=y (AVR) arduino-mega2560:nsh NFILE_DESCRIPTORS_PER_BLOCK=4 (manual override) NFILE_DESCRIPTORS_PER_BLOCK=4 (default) 0 B (canonical cleanup)
DEFAULT_SMALL=n (standard) sam4s-xplained-pro:nsh NFILE_DESCRIPTORS_PER_BLOCK=8 NFILE_DESCRIPTORS_PER_BLOCK=8 Unchanged (retains 8)

Verification Logs

Target with DEFAULT_SMALL=y (stm32f103-minimum:nsh):

$ ./tools/configure.sh -l stm32f103-minimum:nsh
Loaded configuration '.config'
Configuration saved to '.config'

$ grep -E '(CONFIG_DEFAULT_SMALL|CONFIG_NFILE_DESCRIPTORS_PER_BLOCK)' .config
CONFIG_DEFAULT_SMALL=y
CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=4

Target with DEFAULT_SMALL=n (sam4s-xplained-pro:nsh):

$ ./tools/configure.sh -l sam4s-xplained-pro:nsh
Loaded configuration '.config'
Configuration saved to '.config'

$ grep -E '(CONFIG_DEFAULT_SMALL|CONFIG_NFILE_DESCRIPTORS_PER_BLOCK)' .config
# CONFIG_DEFAULT_SMALL is not set
CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=8

Canonical defconfig check for cleaned board (arduino-mega2560:nsh):

$ ./tools/configure.sh -l arduino-mega2560:nsh
Loaded configuration '.config'
Configuration saved to '.config'

$ grep -E '(CONFIG_DEFAULT_SMALL|CONFIG_NFILE_DESCRIPTORS_PER_BLOCK)' .config
CONFIG_DEFAULT_SMALL=y
CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=4

Code style check:

$ git diff --check
(clean, 0 warnings / errors)

In the NuttX VFS implementation, file descriptors are allocated in chunks
of CONFIG_NFILE_DESCRIPTORS_PER_BLOCK. The initial block is statically
embedded in struct fdlist (fl_prefds) inside each task group
(struct task_group_s) to avoid heap allocation overhead during task creation
and standard I/O initialization.

Previously, NFILE_DESCRIPTORS_PER_BLOCK defaulted unconditionally to 8,
unlike other scheduler pre-allocation parameters (SEM_PREALLOCHOLDERS,
SIG_PREALLOC_IRQ_ACTIONS, PREALLOC_MQ_MSGS, PREALLOC_TIMERS,
PREALLOC_IRQCHAIN) which scale down to 4 when DEFAULT_SMALL is enabled.

Defaulting NFILE_DESCRIPTORS_PER_BLOCK to 4 on DEFAULT_SMALL targets
reduces the preallocated descriptor table by 4 entries, saving 32 bytes of
SRAM per active task group on 32-bit architectures and 64 bytes on 64-bit
architectures. Standard POSIX streams (stdin 0, stdout 1, stderr 2) plus one
additional descriptor remain available without heap overhead; opening
further descriptors continues to extend dynamically in blocks of 4 via
fdlist_extend().

Also remove redundant CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=4 settings from 11
board defconfigs (10 AVR targets and stm32vldiscovery:nsh) that now inherit
the default, and update documentation in tasks_vs_threads.rst.

Assisted-by: Claude:claude-opus-4.6
Signed-off-by: Swatantra Yadav <[email protected]>
@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: S The size of the change in this PR is small Board: arm Board: avr labels Nov 26, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

mirtoo

  • kseg0_progmem: .text -4 B (-0.0%, 62,128 B / 131,072 B, total: 47% used)
  • kseg1_datamem: .bss -32 B (-1.2%, 2,604 B / 32,256 B, total: 8% used)

stm32-nucleo-f103rb

  • flash: .text -4 B (-0.0%, 33,980 B / 131,072 B, total: 26% used)
  • sram: .bss -32 B (-1.3%, 2,468 B / 20,480 B, total: 12% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@GUIDINGLI
GUIDINGLI merged commit 18d5513 into apache:master Nov 28, 2026
59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation Board: arm Board: avr Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants