Skip to content

Conversation

@jlaitine
Copy link
Contributor

@jlaitine jlaitine commented Mar 21, 2025

Summary

This further optimizes mutexes:

  1. If priority inheritance or priority protect are enabled in nuttx config, the fast atomic mutex path should be used, if priority adjusting is not enabled for the mutex. The sem->flags can be read/checked without locking
  2. If CONFIG_LIBC_ARCH_ATOMIC is defined, the atomic mutex path should NOT be used, this defaults to spinlock implementation which makes the mutexes heavy
  3. The atomic fast mutexes can be used directly from userspace, so add that logic into the libc. This optimizes the pthread_mutexes heavily in protected builds, as the priority inheritance and priority protection are disabled by default in those, and typically there is no need to do the syscall to the kernel at all.

Impact

Has no functional impact, but increases performance especially for:

  • pthread_mutexes in CONFIG_BUILD_KERNEL
  • pthread_mutexes when the CONFIG_PRIORITY_INHERITANCE and/or CONFIG_PRIORITY_PROTECT are enabled
  • in case CONFIG_LIBC_ARCH_ATOMIC is defined

Testing

  • rv-virt:nsh: ostest passes
  • rv-virt64:nsh: ostest passes
    • log: "$ qemu-system-riscv64 -semihosting -M virt,aclint=on -cpu rv64 -bios none -kernel nuttx -nographic 2>&1 | tee log.txt"
      log.txt
  • with TII custom mpfs targets w. application sw, with SMP 4 cores, CONFIG_BUILD_FLAT and CONFIG_BUILD_KERNEL

@github-actions github-actions bot added Area: OS Components OS Components issues Size: M The size of the change in this PR is medium labels Mar 21, 2025
@nuttxpr
Copy link

nuttxpr commented Mar 21, 2025

[Experimental Bot, please feedback here]

Yes, this PR appears to meet the NuttX requirements, although some minor improvements could be made.

Strengths:

  • Clear Summary: The summary clearly explains the "why," "what," and "how" of the changes.
  • Impact Section Addressed: All impact categories are addressed, and the performance improvements are highlighted.
  • Testing Provided: Testing logs are included and multiple architectures/configurations are covered. The link to the log file is helpful.

Areas for Improvement:

  • Missing Issue References: If this PR addresses a specific NuttX or NuttX Apps issue, those should be linked in the Summary. Even if it's a performance optimization without a directly linked issue, consider creating one to track it.
  • Build Host Details: The Testing section mentions targets but lacks detail on the build host environment (OS, compiler version, etc.). This information is important for reproducibility.
  • "Before" vs. "After" Logs Clarity: While logs are provided, it's unclear how to directly compare the performance improvement. Consider including specific metrics (e.g., timings for relevant operations) in the logs or providing a summary of the performance gains observed. Simply saying "ostest passes" doesn't quantify the improvement.
  • Clarity on CONFIG_LIBC_ARCH_ATOMIC: The summary mentions not using the atomic mutex path if CONFIG_LIBC_ARCH_ATOMIC is defined. Explain why this is the case (e.g., "because it defaults to spinlocks, which are less efficient in this context").

Recommendation: Add the missing build host details, clarify the CONFIG_LIBC_ARCH_ATOMIC reasoning, and provide more quantifiable performance data in the testing section. Linking a related issue (even a newly created one for performance tracking) would also be beneficial.

@pussuw
Copy link
Contributor

pussuw commented Mar 21, 2025

This partially completes the discussion from here #14465
@zyfeier can you take a look?

The next optimization is to move the mutex holder to userspace

@jlaitine jlaitine force-pushed the optimze_mutex_atomic_fast_path branch from 0362941 to 7f733ba Compare March 21, 2025 12:31
@jlaitine
Copy link
Contributor Author

There seems to be some issue with sim:citest target, and of course I can't even build the citest locally ( The same issue as in #12974 ). Need to get the docker build environment and try to resolve that ...

@jlaitine jlaitine force-pushed the optimze_mutex_atomic_fast_path branch from 7f733ba to 2ba7c1b Compare March 21, 2025 14:15
@jlaitine
Copy link
Contributor Author

Changed the way how atomic.h is included, now it should compile. Re-tested on mpfs.

@jlaitine jlaitine force-pushed the optimze_mutex_atomic_fast_path branch from 2ba7c1b to 50b2e31 Compare March 21, 2025 14:56
@jlaitine jlaitine force-pushed the optimze_mutex_atomic_fast_path branch 3 times, most recently from ac60716 to bc30151 Compare March 24, 2025 08:09
@jlaitine
Copy link
Contributor Author

Re-tested on mpfs and qemu targets

@jlaitine jlaitine force-pushed the optimze_mutex_atomic_fast_path branch from bc30151 to 5908cdc Compare March 24, 2025 10:41
@jlaitine
Copy link
Contributor Author

Rebased against current master and cleaned up one comment.

@lupyuen
Copy link
Member

lupyuen commented Mar 24, 2025

@nuttxpr test avaota-a1:nsh

@nuttxpr
Copy link

nuttxpr commented Mar 24, 2025

[Experimental Bot, please feedback here]

Build and Test Successful (avaota-a1:nsh)
https://gitlab.com/lupyuen/nuttx-build-log/-/snippets/4828942

$ git clone https://github.com/tiiuae/nuttx nuttx --branch optimze_mutex_atomic_fast_path
$ git clone https://github.com/apache/nuttx-apps apps --branch master
$ pushd nuttx
$ git reset --hard HEAD
HEAD is now at 5908cdc578 libc/semaphore: Add fast mutex wait/post paths to libc
$ popd
$ pushd apps
$ git reset --hard HEAD
HEAD is now at e336ebad5 cmake: fix cmake build for FreeModBus
$ popd
NuttX Source: https://github.com/apache/nuttx/tree/5908cdc578e8d515bc7183d7ae558973c08e6023
NuttX Apps: https://github.com/apache/nuttx-apps/tree/e336ebad5e4da1cfd9d99ac04bccaf15cc09249f
$ cd nuttx
$ tools/configure.sh avaota-a1:nsh
$ make -j
$ make -j export
$ pushd ../apps
$ ./tools/mkimport.sh -z -x ../nuttx/nuttx-export-10.4.0.tar.gz
$ make -j import
$ popd
$ genromfs -f initrd -d ../apps/bin -V NuttXBootVol
$ head -c 65536 /dev/zero
$ cat nuttx.bin /tmp/nuttx.pad initrd
$ scp Image thinkcentre:/tmp/Image
$ ssh thinkcentre sudo /home/user/copy-image.sh
$ sd-mux-ctrl --device-serial=sd-wire_02-09 --ts
$ mkdir -p /tmp/sda1
$ mount /dev/sda1 /tmp/sda1
$ cp /tmp/Image /tmp/sda1/
$ rm /tmp/Image
$ umount /tmp/sda1
$ sd-mux-ctrl --device-serial=sd-wire_02-09 --dut
$ cd /home/luppy/nuttx-build-farm
$ ssh thinkcentre
nsh> uname -a
NuttX 10.4.0 5908cdc578 Mar 24 2025 18:58:32 arm64 avaota-a1
nsh> ostest
arena        a000    28000
ordblks         2        4
mxordblk     5ff8    1bff8
uordblks     27e8     6700
fordblks     7818    21900
user_main: Exiting
ostest_main: Exiting with status 0
Now running https://github.com/lupyuen/nuttx-build-farm/blob/main/avaota-power.sh off
----- Power off Avaota-A1
[]

@lupyuen
Copy link
Member

lupyuen commented Mar 24, 2025

@nuttxpr test milkv_duos:nsh

@nuttxpr
Copy link

nuttxpr commented Mar 24, 2025

[Experimental Bot, please feedback here]

Build and Test Successful (milkv_duos:nsh)
https://gitlab.com/lupyuen/nuttx-build-log/-/snippets/4828943

$ git clone https://github.com/tiiuae/nuttx nuttx --branch optimze_mutex_atomic_fast_path
$ git clone https://github.com/apache/nuttx-apps apps --branch master
$ pushd nuttx
$ git reset --hard HEAD
HEAD is now at 5908cdc578 libc/semaphore: Add fast mutex wait/post paths to libc
$ popd
$ pushd apps
$ git reset --hard HEAD
HEAD is now at e336ebad5 cmake: fix cmake build for FreeModBus
$ popd
NuttX Source: https://github.com/apache/nuttx/tree/5908cdc578e8d515bc7183d7ae558973c08e6023
NuttX Apps: https://github.com/apache/nuttx-apps/tree/e336ebad5e4da1cfd9d99ac04bccaf15cc09249f
$ cd nuttx
$ tools/configure.sh milkv_duos:nsh
$ make -j
$ make -j export
$ pushd ../apps
$ ./tools/mkimport.sh -z -x ../nuttx/nuttx-export-10.4.0.tar.gz
$ make -j import
$ popd
$ genromfs -f initrd -d ../apps/bin -V NuttXBootVol
$ head -c 65536 /dev/zero
$ cat nuttx.bin /tmp/nuttx.pad initrd
$ scp Image tftpserver:/tftpboot/Image-sg2000
$ cd /home/luppy/nuttx-build-farm
$ ssh tftpserver
OpenSBI v0.9
nsh> uname -a
NuttX 10.4.0 5908cdc578 Mar 24 2025 19:02:56 risc-v milkv_duos
nsh> ostest
arena       81000    81000
ordblks         2        3
mxordblk    7cff8    78ff8
uordblks     2660     4570
fordblks    7e9a0    7ca90
user_main: Exiting
ostest_main: Exiting with status 0
nsh> Now running https://github.com/lupyuen/nuttx-build-farm/blob/main/oz64-power.sh off
----- Power off Oz64
[]

@nuttxpr
Copy link

nuttxpr commented Mar 26, 2025

[Experimental Bot, please feedback here]

Build and Test Successful (milkv_duos:nsh)
https://gitlab.com/lupyuen/nuttx-build-log/-/snippets/4829490

$ git clone https://github.com/tiiuae/nuttx nuttx --branch optimze_mutex_atomic_fast_path
$ git clone https://github.com/apache/nuttx-apps apps --branch master
$ pushd nuttx
$ git reset --hard HEAD
HEAD is now at 603b3c82b3 libc/semaphore: Add fast mutex wait/post paths to libc
$ popd
$ pushd apps
$ git reset --hard HEAD
HEAD is now at 7b65b2bb4 drivertest_uart: Pass in the specified parameters to test
$ popd
NuttX Source: https://github.com/apache/nuttx/tree/603b3c82b3a3da6281016ef48348b07192bb5a8e
NuttX Apps: https://github.com/apache/nuttx-apps/tree/7b65b2bb41763207dcfb965c3eb2223595986779
$ cd nuttx
$ tools/configure.sh milkv_duos:nsh
$ make -j
$ make -j export
$ pushd ../apps
$ ./tools/mkimport.sh -z -x ../nuttx/nuttx-export-10.4.0.tar.gz
$ make -j import
$ popd
$ genromfs -f initrd -d ../apps/bin -V NuttXBootVol
$ head -c 65536 /dev/zero
$ cat nuttx.bin /tmp/nuttx.pad initrd
$ scp Image tftpserver:/tftpboot/Image-sg2000
$ cd /home/luppy/nuttx-build-farm
$ ssh tftpserver
OpenSBI v0.9
nsh> uname -a
NuttX 10.4.0 603b3c82b3 Mar 26 2025 16:11:52 risc-v milkv_duos
nsh> ostest
arena       81000    81000
ordblks         2        3
mxordblk    7cff8    78ff8
uordblks     2660     4570
fordblks    7e9a0    7ca90
user_main: Exiting
ostest_main: Exiting with status 0
nsh> Now running https://github.com/lupyuen/nuttx-build-farm/blob/main/oz64-power.sh off
----- Power off Oz64
[]

Copy link
Member

@lupyuen lupyuen left a comment

Choose a reason for hiding this comment

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

Retested OK with OSTest on Real Hardware. Thanks :-)

…TECT or CONFIG_PRIORITY_INHERITANCE

When the semaphore priority flags is set to NONE, and the semaphore
is a mutex, the fast locking path can be used, even when
priority inheritance or priority protect are enabled globally.

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
@jlaitine jlaitine force-pushed the optimze_mutex_atomic_fast_path branch 3 times, most recently from 3854169 to 427d51e Compare March 28, 2025 11:17
@jlaitine
Copy link
Contributor Author

Modified the PR acc. to the review comments, re-tested on rv-virt:nsh64 (ostest) and on multiple real hw & build configurations (MPFS, CONFIG_BUILD_KERNEL and CONFIG_BUILD_FLAT, SMP and non SMP).

This avoids unnecessary syscalls in memory protected builds, when mutex
lock/unlock can be done with only atomic counter access

Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
@jlaitine jlaitine force-pushed the optimze_mutex_atomic_fast_path branch from 427d51e to 2c8f0cf Compare March 28, 2025 11:49
@jlaitine
Copy link
Contributor Author

And had to modify still a bit to pass CI with debug assertions....

@lupyuen
Copy link
Member

lupyuen commented Mar 28, 2025

Hmmm wonder why Avaota-A1 Arm64 SBC is failing...
https://gist.github.com/lupyuen/75bee0db56674f2a90c4be99a3baa15a

dump_assert_info: Current Version: NuttX  10.4.0 427d51e07e Mar 28 2025 19:51:10 arm64
dump_assert_info: Assertion failed ret > 0: at file: init/nx_bringup.c:380 task: AppBringUp process: Kernel 0x408065c8

Update: Seems OK now :-)

@lupyuen
Copy link
Member

lupyuen commented Mar 28, 2025

@nuttxpr test avaota-a1:nsh

@jlaitine
Copy link
Contributor Author

Hmmm wonder why Avaota-A1 Arm64 SBC is failing... https://gist.github.com/lupyuen/75bee0db56674f2a90c4be99a3baa15a

dump_assert_info: Current Version: NuttX  10.4.0 427d51e07e Mar 28 2025 19:51:10 arm64
dump_assert_info: Assertion failed ret > 0: at file: init/nx_bringup.c:380 task: AppBringUp process: Kernel 0x408065c8

Update: Seems OK now :-)

Ok, what happened? I re-checked the PR, and did some extra tests on i.MX93 (arm64) CONFIG_BUILD_KERNEL, but didn't find anything...

@nuttxpr
Copy link

nuttxpr commented Mar 28, 2025

[Experimental Bot, please feedback here]

Build and Test Successful (avaota-a1:nsh)
https://gitlab.com/lupyuen/nuttx-build-log/-/snippets/4830105

$ git clone https://github.com/tiiuae/nuttx nuttx --branch optimze_mutex_atomic_fast_path
$ git clone https://github.com/apache/nuttx-apps apps --branch master
$ pushd nuttx
$ git reset --hard HEAD
HEAD is now at 2c8f0cf574 libc/semaphore: Move fast mutex wait/post paths to libc
$ popd
$ pushd apps
$ git reset --hard HEAD
HEAD is now at 369729eea testing/drivers/drivertest: Uniform test case name
$ popd
NuttX Source: https://github.com/apache/nuttx/tree/2c8f0cf57465f7751eac07e5aade0cb5ae2c3b01
NuttX Apps: https://github.com/apache/nuttx-apps/tree/369729eea4f18b61ab899f84948acfa4820d0518
$ cd nuttx
$ tools/configure.sh avaota-a1:nsh
$ make -j
$ make -j export
$ pushd ../apps
$ ./tools/mkimport.sh -z -x ../nuttx/nuttx-export-10.4.0.tar.gz
$ make -j import
$ popd
$ genromfs -f initrd -d ../apps/bin -V NuttXBootVol
$ head -c 65536 /dev/zero
$ cat nuttx.bin /tmp/nuttx.pad initrd
$ scp Image thinkcentre:/tmp/Image
$ ssh thinkcentre sudo /home/user/copy-image.sh
$ sd-mux-ctrl --device-serial=sd-wire_02-09 --ts
$ mkdir -p /tmp/sda1
$ mount /dev/sda1 /tmp/sda1
$ cp /tmp/Image /tmp/sda1/
$ rm /tmp/Image
$ umount /tmp/sda1
$ sd-mux-ctrl --device-serial=sd-wire_02-09 --dut
$ cd /home/luppy/nuttx-build-farm
$ ssh thinkcentre
nsh> uname -a
NuttX 10.4.0 2c8f0cf574 Mar 28 2025 20:14:34 arm64 avaota-a1
nsh> ostest
arena        a000    28000
ordblks         2        4
mxordblk     5ff8    1bff8
uordblks     27e8     6700
fordblks     7818    21900
user_main: Exiting
ostest_main: Exiting with status 0
nsh> Now running https://github.com/lupyuen/nuttx-build-farm/blob/main/avaota-power.sh off
----- Power off Avaota-A1
[]

@lupyuen
Copy link
Member

lupyuen commented Mar 28, 2025

@nuttxpr test milkv_duos:nsh

@nuttxpr
Copy link

nuttxpr commented Mar 28, 2025

[Experimental Bot, please feedback here]

Build and Test Successful (milkv_duos:nsh)
https://gitlab.com/lupyuen/nuttx-build-log/-/snippets/4830107

$ git clone https://github.com/tiiuae/nuttx nuttx --branch optimze_mutex_atomic_fast_path
$ git clone https://github.com/apache/nuttx-apps apps --branch master
$ pushd nuttx
$ git reset --hard HEAD
HEAD is now at 2c8f0cf574 libc/semaphore: Move fast mutex wait/post paths to libc
$ popd
$ pushd apps
$ git reset --hard HEAD
HEAD is now at 369729eea testing/drivers/drivertest: Uniform test case name
$ popd
NuttX Source: https://github.com/apache/nuttx/tree/2c8f0cf57465f7751eac07e5aade0cb5ae2c3b01
NuttX Apps: https://github.com/apache/nuttx-apps/tree/369729eea4f18b61ab899f84948acfa4820d0518
$ cd nuttx
$ tools/configure.sh milkv_duos:nsh
$ make -j
$ make -j export
$ pushd ../apps
$ ./tools/mkimport.sh -z -x ../nuttx/nuttx-export-10.4.0.tar.gz
$ make -j import
$ popd
$ genromfs -f initrd -d ../apps/bin -V NuttXBootVol
$ head -c 65536 /dev/zero
$ cat nuttx.bin /tmp/nuttx.pad initrd
$ scp Image tftpserver:/tftpboot/Image-sg2000
$ cd /home/luppy/nuttx-build-farm
$ ssh tftpserver
OpenSBI v0.9
nsh> uname -a
NuttX 10.4.0 2c8f0cf574 Mar 28 2025 20:19:48 risc-v milkv_duos
nsh> ostest
arena       81000    81000
ordblks         2        3
mxordblk    7cff8    78ff8
uordblks     2660     4570
fordblks    7e9a0    7ca90
user_main: Exiting
ostest_main: Exiting with status 0
nsh> Now running https://github.com/lupyuen/nuttx-build-farm/blob/main/oz64-power.sh off
----- Power off Oz64
[]

Copy link
Member

@lupyuen lupyuen left a comment

Choose a reason for hiding this comment

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

Retested OK with OSTest on Real Hardware. Thanks :-)

@jlaitine jlaitine closed this Mar 31, 2025
@jlaitine jlaitine reopened this Mar 31, 2025
@jlaitine
Copy link
Contributor Author

Sorry, clicked "close by accident" :)

@jlaitine
Copy link
Contributor Author

I will still do the following chages based on comments from @pussuw :

  1. Will have separate functions for non-cancellation or errno in libc without the nx-prefix and functions in kernel with the nx-prefix
  2. Have the common code (counter handling) as before as an inline function and include that in both
  3. replace all the existing calls of nxsem* inside libc with calls to the functions without the nx-prefix

@xiaoxiang781216 xiaoxiang781216 merged commit 19a8e24 into apache:master Apr 1, 2025
78 checks passed
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: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants