Skip to content

Assert size optimizations#19061

Open
jlaitine wants to merge 2 commits into
apache:masterfrom
tiiuae:assert_size_optimizations
Open

Assert size optimizations#19061
jlaitine wants to merge 2 commits into
apache:masterfrom
tiiuae:assert_size_optimizations

Conversation

@jlaitine

@jlaitine jlaitine commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Summary

Here is one proposal, how we could save a bit of flash on small systems.

First commit (not too significant one) doesn't change functionality, just cleans up a tiny amount of flash (64 bytes on 32-bit arm) by removing a duplicate string and an extra function call. The duplicate string was not cleaned up by linker because the string was only almost the same, the other one had one extra space.

The second one adds two new CONFIG flags giving more granularity to configure what to dump at crash:

CONFIG_SCHED_DUMP_STACK : Output stack dump at crash, consumes ~1kB flash ( If disabled, it still dumps the current stack, irq stack and kernel stack base and size )
CONFIG_SCHED_DUMP_TASKS : Output tasks dump at crash, consumes ~0.5kB flash

By removing these two, 1.6kB can be saved in flash size.

Both flags are defined !CONFIG_DEFAULT_SMALL, so with normal configurations the functionality is unchanged.

Impact

Impacts boards definining CONFIG_BUILD_SMALL, reducing flash consumption and output of kernel panic dump by default. On these systems, restoring the original panic dump would require adding CONFIG_SCHED_DUMP_STACK=y, CONFIG_SCHED_DUMP_TASKS=y to the defconfig.

Testing

Tested on stm32-f765II (pixhawk4 board)

With both the new flags disabled, the crash dump looks like this:

Hard Fault escalation:
PANIC!!! Hard Fault!:
	IRQ: 3 regs: 0x2007dac8
	BASEPRI: 00000080 PRIMASK: 00000000 IPSR: 00000003 CONTROL: 00000000
	CFSR: 00008200 HFSR: 40000000 DFSR: 00000000 BFAR: 0000016c AFSR: 00000000
Hard Fault Reason:
Current Version: NuttX  12.12.0 97e9848b92 Jun  8 2026 13:41:23 arm
Assertion failed panic: at file: :0 task: px4_entry process: px4_entry 0x8100039
R0: 00000000 R1: 00000000 R2: 00000080  R3: 00000000
R4: 00000000 R5: 00000000 R6: 00000220  FP: 00000000
R8: 00000220 SB: 00000260 SL: 00000020 R11: 00000000
IP: 00000000 SP: 2007dba0 LR: 0801c345  PC: 0801c348
xPSR: 61000000 BASEPRI: 00000080 CONTROL: 00000000
EXC_RETURN: ffffffed
IRQ Stack: base: 0x200212c0 size: 768
User Stack: base: 0x2007d110 size: 3152

With CONFIG_SCHED_DUMP_TASKS=y, CONFIG_SCHED_DUMP_STACK=n:

Hard Fault escalation:
PANIC!!! Hard Fault!:
	IRQ: 3 regs: 0x2007dac8
	BASEPRI: 00000080 PRIMASK: 00000000 IPSR: 00000003 CONTROL: 00000000
	CFSR: 00008200 HFSR: 40000000 DFSR: 00000000 BFAR: 0000016c AFSR: 00000000
Hard Fault Reason:
Current Version: NuttX  12.12.0 648d0d828a Jun  8 2026 14:57:34 arm
Assertion failed panic: at file: :0 task: px4_entry process: px4_entry 0x8100039
R0: 00000000 R1: 00000000 R2: 00000080  R3: 00000000
R4: 00000000 R5: 00000000 R6: 00000220  FP: 00000000
R8: 00000220 SB: 00000260 SL: 00000020 R11: 00000000
IP: 00000000 SP: 2007dba0 LR: 0801c595  PC: 0801c598
xPSR: 61000000 BASEPRI: 00000080 CONTROL: 00000000
EXC_RETURN: ffffffed
IRQ Stack: base: 0x200212c0 size: 768
User Stack: base: 0x2007d110 size: 3152
   PID GROUP PRI POLICY   TYPE    NPX STATE   EVENT      SIGMASK          STACKBASE  STACKSIZE      USED   FILLED    COMMAND
  ----   --- --- -------- ------- --- ------- ---------- ---------------- 0x200212c0       768       600    78.1%    irq
      0     0   0 FIFO     Kthread -   Ready              0000000000000000 0x2002b3cc       734       472    64.3%    Idle_Task
      1     0 249 FIFO     Kthread -   Waiting Semaphore  0000000000000000 0x20041988      1216       336    27.6%    hpwork 0x20020b88 0x20020bd8
      2     0  50 FIFO     Kthread -   Waiting Semaphore  0000000000000000 0x20041f80      1568       336    21.4%    lpwork 0x20020b18 0x20020b68
      3     3 100 FIFO     Task    -   Waiting Semaphore  0000000000000000 0x2007c498      2016       336    16.6%    uwork
      4     4 100 FIFO     Task    -   Running            0000000000000000 0x2007d110      3152       776    24.6%    px4_entry
      5     0 255 FIFO     Kthread -   Waiting Semaphore  0000000000000000 0x200430b0      1240       344    27.7%    wq:manager
      6     0 205 FIFO     Kthread -   Waiting Semaphore  0000000000000000 0x200436b8      3440       448    13.0%    wq:lp_default 80f6350

And the current functionality, both flags = y:

Hard Fault escalation:
PANIC!!! Hard Fault!:
	IRQ: 3 regs: 0x2007dac8
	BASEPRI: 00000080 PRIMASK: 00000000 IPSR: 00000003 CONTROL: 00000000
	CFSR: 00008200 HFSR: 40000000 DFSR: 00000000 BFAR: 0000016c AFSR: 00000000
Hard Fault Reason:
Current Version: NuttX  12.12.0 648d0d828a Jun  8 2026 15:04:19 arm
Assertion failed panic: at file: :0 task: px4_entry process: px4_entry 0x8100039
R0: 00000000 R1: 00000000 R2: 00000080  R3: 00000000
R4: 00000000 R5: 00000000 R6: 00000220  FP: 00000000
R8: 00000220 SB: 00000260 SL: 00000020 R11: 00000000
IP: 00000000 SP: 2007dba0 LR: 0801c715  PC: 0801c718
xPSR: 61000000 BASEPRI: 00000080 CONTROL: 00000000
EXC_RETURN: ffffffed
User Stack:
  base: 0x2007d110
  size: 00003152
    sp: 0x2007dba0
0x2007db80: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 0800eef9
0x2007dba0: 00000260 00000000 00000220 0801c74d 00000088 2007e2b0 00000220 080b3497
0x2007dbc0: 00000088 2007e2b0 2007dc40 2007e2b4 00000220 00000000 00000000 080b341f
0x2007dbe0: 00000088 080b2499 2002641c 00000000 080f7dcc 00000000 080b2389 080f7dcc
0x2007dc00: 00000000 080b251d 00000003 2007dc40 00000001 080b2e1b 00000000 00000000
0x2007dc20: 00000000 2007e2b0 00000000 00000003 080fe28c 08008000 080fe218 080a75f3
0x2007dc40: 000009b4 00000000 696e6906 6c616974 64657a69 524f7520 6f6c2042 6e696767
0x2007dc60: 00000067 00000000 00000000 00000000 00000000 00000000 00000000 00000000
0x2007dc80: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
0x2007dca0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
0x2007dcc0: 00000000 00000000 00000003 080aa097 00000000 00000001 40021800 00000000
0x2007dce0: 2007d0f8 2007d0f8 00000000 080a91b1 00000000 00000000 00000000 00000000
0x2007dd00: 00000000 00000000 2007d0f8 0800aaa9 00000001 08009e33 00000001 2007d0f8
0x2007dd20: 2007d0f8 08100049 00000018 00000000 00000001 08100039 00000001 08104719
0x2007dd40: 2007d0f8 0801928b 08100039 0801928b 00000000 00000000 00000000 00000000
0x2007dd60: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
   PID GROUP PRI POLICY   TYPE    NPX STATE   EVENT      SIGMASK          STACKBASE  STACKSIZE      USED   FILLED    COMMAND
  ----   --- --- -------- ------- --- ------- ---------- ---------------- 0x200212c0       768       656    85.4%!   irq
      0     0   0 FIFO     Kthread -   Ready              0000000000000000 0x2002b3cc       734       472    64.3%    Idle_Task
      1     0 249 FIFO     Kthread -   Waiting Semaphore  0000000000000000 0x20041988      1216       336    27.6%    hpwork 0x20020b88 0x20020bd8
      2     0  50 FIFO     Kthread -   Waiting Semaphore  0000000000000000 0x20041f80      1568       336    21.4%    lpwork 0x20020b18 0x20020b68
      3     3 100 FIFO     Task    -   Waiting Semaphore  0000000000000000 0x2007c498      2016       336    16.6%    uwork
      4     4 100 FIFO     Task    -   Running            0000000000000000 0x2007d110      3152       776    24.6%    px4_entry
      5     0 255 FIFO     Kthread -   Waiting Semaphore  0000000000000000 0x200430b0      1240       344    27.7%    wq:manager
      6     0 205 FIFO     Kthread -   Waiting Semaphore  0000000000000000 0x200436b8      3440       448    13.0%    wq:lp_default 80f6528

- Use shared string for "stack pointer out of range" to avoid duplicate in flash
- Re-use already calculated stack_used instead of calling up_check_tcbstack again

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
@github-actions github-actions Bot added Area: OS Components OS Components issues Size: S The size of the change in this PR is small labels Jun 8, 2026
Comment thread sched/misc/assert.c Outdated

_alert("IRQ Stack: base: %p size: %u\n",
(FAR void *)up_get_intstackbase(cpu),
(unsigned)CONFIG_ARCH_INTERRUPTSTACK);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the cast

Comment thread sched/misc/assert.c Outdated
#ifdef CONFIG_ARCH_KERNEL_STACK
_alert("Kernel Stack: base: %p size: %u\n",
rtcb->xcp.kstack,
(unsigned)CONFIG_ARCH_KERNEL_STACKSIZE);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the cast

Comment thread sched/misc/assert.c Outdated

_alert("User Stack: base: %p size: %zu\n",
rtcb->stack_base_ptr,
(size_t)rtcb->adj_stack_size);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the cast

Comment thread sched/misc/assert.c
@@ -168,6 +168,8 @@ static int assert_tracecallback(FAR struct usbtrace_s *trace, FAR void *arg)

#ifdef CONFIG_ARCH_STACKDUMP

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need both CONFIG_ARCH_STACKDUMP and CONFIG_SCHED_DUMP_STACK?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I interpreted the ARCH flag meaning that the architecture supports it, and not typically to be set in boards own config? It looks like I messed up something as this didn't pass the CI; I'll revisit this later today

@jlaitine jlaitine Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that ARCH_STACKDUMP is indeed set for each board individually. So the difference here is just that disabling ARCH_STACKDUMP would remove printing the whole stack info.

I wanted to only suppress dumping the stack content, but leave the relatively lightweight printing of stack information.

Perhaps some other CONFIG_ syntax would be cleaner?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I think so. so, should we just keep one Kconfig? the basic stack info should be always printed, but the stack content is printed only Kconfig is enabled.

Comment thread sched/misc/assert.c Outdated
int cpu = this_cpu();
# endif

_alert("IRQ Stack: base: %p size: %u\n",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not add #ifdef CONFIG_SCHED_DUMP_STACK/#endif in the origin code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tried to make it as small as possible; I can refactor this for sure!

@linguini1

Copy link
Copy Markdown
Contributor

I like this optimization, and thanks @jlaitine for working on so many size improvements lately!

Maybe you could add these two new options to the documentation somewhere so it is easier for other users to take advantage of them?

@jlaitine jlaitine force-pushed the assert_size_optimizations branch from 4f8c4c6 to 339121d Compare June 8, 2026 16:30
@jlaitine

jlaitine commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

I re-factored this to be a minimal code change. Total size reduction with reduced output is still > 1.3KB. This now always keeps the functionality for printing out the stack pointer info for user/kernel & irq stacks.

Would this approach look better @xiaoxiang781216 ?

If this is preferable to the previous version, I'll test tomorrow and add new test outputs here. I don't have all the HW at hand atm.

…STACK

Add more refined options for sched/misc/assert to control how verbose
crash dumps are printed out:

- SCHED_DUMP_TASKS
- SCHED_DUMP_STACK

These default to y unless DEFAULT_SMALL is defined. The options can
be undefined to save flash space on a small system.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
@jlaitine jlaitine force-pushed the assert_size_optimizations branch from 339121d to f93360b Compare June 8, 2026 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues 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