Bootstrap

20、【OS】【Nuttx】建一个最小系统工程配置

背景

接之前wiki
18、【OS】【Nuttx】用gdb调试nuttx os
19、【OS】【Nuttx】【Python】gdb调试优化,python实现构建脚本
已经开始用gdb开始调试了,但一直调试别人的demo工程也不是个事儿,哪有调试属于自己的工程舒服。这里问题来了,什么是自己的工程,自己的工程需要什么?
想明白自己的工程需要什么,刚开始不容易,不过可以用一个最小系统为起点,搭建一个最小系统工程后,再去想自己需要什么,往上加功能就行了!

目标

搭建一个最小系统工程配置,可使用configure.sh列表搜索并对整个项目进行配置

解决方案

首先思考什么是Nuttx最小系统,哪些功能是不需要的,也能正常把Nuttx跑起来?
打开之前调试的OSTEST Demo的配置文件defconfig

#
# This file is autogenerated: PLEASE DO NOT EDIT IT.
#
# You can use "make menuconfig" to make any modifications to the installed .config file.
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
# modifications.
#
CONFIG_ARCH="sim"
CONFIG_ARCH_BOARD="sim"
CONFIG_ARCH_BOARD_SIM=y
CONFIG_ARCH_CHIP="sim"
CONFIG_ARCH_SIM=y
CONFIG_BOARDCTL=y
CONFIG_BOARDCTL_POWEROFF=y
CONFIG_BOARD_LOOPSPERMSEC=100
CONFIG_CANCELLATION_POINTS=y
CONFIG_DEBUG_ASSERTIONS=y
CONFIG_DEBUG_FEATURES=y
CONFIG_DEBUG_SYMBOLS=y
CONFIG_FS_NAMED_SEMAPHORES=y
CONFIG_IDLETHREAD_STACKSIZE=4096
CONFIG_INIT_ENTRYPOINT="ostest_main"
CONFIG_MM_KASAN=y
CONFIG_MM_UBSAN=y
CONFIG_MM_UBSAN_TRAP_ON_ERROR=y
CONFIG_PRIORITY_INHERITANCE=y
CONFIG_PTHREAD_MUTEX_TYPES=y
CONFIG_RAM_START=0x00000000
CONFIG_SCHED_HAVE_PARENT=y
CONFIG_SCHED_WAITPID=y
CONFIG_SIM_WALLTIME_SIGNAL=y
CONFIG_START_DAY=27
CONFIG_START_MONTH=2
CONFIG_START_YEAR=2007
CONFIG_TESTING_OSTEST=y
CONFIG_TESTING_OSTEST_LOOPS=5
CONFIG_TESTING_OSTEST_POWEROFF=y
CONFIG_TLS_NCLEANUP=3

可发现里面的配置项很多,里面启用了包括FS,MM,POWEROFF等众多功能,以这个配置项为核心,在项目中搜索defconfig,按配置项数量从少到多进行排序

bash中输入命令

find . -name "defconfig" -exec wc -l {} + | sort -n | head

关键参数解释:

  • -exec 表示对找到的文件执行一次后面的命令
  • wc(word count)是一个统计命令,-l 表示行数,{} 表示占位符,即find找到的文件,+ 表示找到的多个文件进行处理,而不是单个文件。即 wc -l {} + 表示统计找到所有文件的行数
  • | 为管道符,将前面命令的标准输出(即统计到的文件行数)传递给下一个命令,如sort -n
  • sort -n 按数值顺序对输入进行排序,确保数字较小的行排在前面。如果想要数字较大的行排在前面,可调整为 sort -nr,r表示反序
  • head 默认显示前10行,如果想显示不同数量的行,可以用 -n,如 head -n 5 显示前5行

在项目路径下输入命令后,可看到在sim模拟器中,mount的配置项是最少的

adminpc@adminpc:~/nuttx_pdt/nuttx$ find . -name "defconfig" -exec wc -l {} + | sort -n | head
      6 ./boards/xtensa/esp32s3/esp32s3-devkit/configs/txtable/defconfig
     13 ./boards/xtensa/esp32s3/esp32s3-devkit/configs/usbmsc/defconfig
     21 ./boards/arm/lpc17xx_40xx/pnev5180b/configs/nsh/defconfig
     21 ./boards/sim/sim/sim/configs/mount/defconfig
     23 ./boards/arm/stm32wl5/nucleo-wl55jc/configs/nsh/defconfig
     23 ./boards/xtensa/esp32s3/esp32s3-devkit/configs/adb/defconfig
     25 ./boards/arm/lpc17xx_40xx/pnev5180b/configs/knsh/defconfig
     26 ./boards/sim/sim/sim/configs/fb/defconfig
     28 ./boards/arm/lpc17xx_40xx/pnev5180b/configs/usbnsh/defconfig
     28 ./boards/sim/sim/sim/configs/cxxtest/defconfig

打开mount目录下的defconfig

#
# This file is autogenerated: PLEASE DO NOT EDIT IT.
#
# You can use "make menuconfig" to make any modifications to the installed .config file.
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
# modifications.
#
CONFIG_ARCH="sim"
CONFIG_ARCH_BOARD="sim"
CONFIG_ARCH_BOARD_SIM=y
CONFIG_ARCH_CHIP="sim"
CONFIG_ARCH_SIM=y
CONFIG_BOOT_RUNFROMSDRAM=y
CONFIG_EXAMPLES_MOUNT=y
CONFIG_EXAMPLES_MOUNT_BLOCKDEVICE=y
CONFIG_EXAMPLES_MOUNT_DEVNAME="/dev/ram0"
CONFIG_FS_FAT=y
CONFIG_IDLETHREAD_STACKSIZE=4096
CONFIG_INIT_ENTRYPOINT="mount_main"
CONFIG_START_MONTH=6
CONFIG_START_YEAR=2008

以此为基础,去掉mount相关的配置项,去掉FS和其他多余的配置项,把时间改成当前时间,把CONFIG_INIT_ENTRYPOINT设置为自己的app函数入口,CONFIG_MYAPP设置为y(该配置项不了解的参考wiki 15、【OS】【Nuttx】OS裁剪,运行指定程序,周期打印当前任务),可得到最小系统的配置工程

#
# This file is autogenerated: PLEASE DO NOT EDIT IT.
#
# You can use "make menuconfig" to make any modifications to the installed .config file.
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
# modifications.
#
CONFIG_ARCH="sim"
CONFIG_ARCH_BOARD="sim"
CONFIG_ARCH_BOARD_SIM=y
CONFIG_ARCH_CHIP="sim"
CONFIG_ARCH_SIM=y
CONFIG_INIT_ENTRYPOINT="myapp_main"
CONFIG_START_DAY=31
CONFIG_START_MONTH=12
CONFIG_START_YEAR=2024
CONFIG_MYAPP=y

在sim模拟器的config目录下,新建文件夹myapp,里面添加defconfig
在这里插入图片描述
此时configure.sh将自动识别,并能搜索到我们添加的最小系统配置项

tools/configure.sh -L | grep sim:m

在这里插入图片描述
对新建的最小系统配置项进行配置

adminpc@adminpc:~/nuttx_pdt/nuttx$ tools/configure.sh -l sim:myapp
  Copy files
  Select CONFIG_HOST_LINUX=y
  Refreshing...
CP: arch/dummy/Kconfig to /home/adminpc/nuttx_pdt/nuttx/arch/dummy/dummy_kconfig
CP: boards/dummy/Kconfig to /home/adminpc/nuttx_pdt/nuttx/boards/dummy/dummy_kconfig
LN: platform/board to /home/adminpc/nuttx_pdt/nuttx-apps/platform/dummy
LN: include/arch to arch/sim/include
LN: include/arch/board to /home/adminpc/nuttx_pdt/nuttx/boards/sim/sim/sim/include
LN: drivers/platform to /home/adminpc/nuttx_pdt/nuttx/drivers/dummy
LN: include/arch/chip to /home/adminpc/nuttx_pdt/nuttx/arch/sim/include/sim
LN: arch/sim/src/chip to /home/adminpc/nuttx_pdt/nuttx/arch/sim/src/sim
LN: arch/sim/src/board to /home/adminpc/nuttx_pdt/nuttx/boards/sim/sim/sim/src
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/audioutils
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/benchmarks
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/boot
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/canutils
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/crypto
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/database
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/examples/mcuboot
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/examples/module
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/examples/sotest
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/examples
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/fsutils
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/games
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/graphics
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/industry
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/inertial
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/interpreters/luamodules
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/interpreters
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/logging
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/lte
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/math
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/mlearning
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/netutils
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/sdr
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/system
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/testing
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/videoutils
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/wireless/bluetooth
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/wireless/ieee802154
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps/wireless
mkkconfig in /home/adminpc/nuttx_pdt/nuttx-apps
#
# configuration written to .config
#

构建

adminpc@adminpc:~/nuttx_pdt/nuttx$ make clean; make
Create version.h
LN: platform/board to /home/adminpc/nuttx_pdt/nuttx-apps/platform/dummy
Register: myapp
CP:  /home/adminpc/nuttx_pdt/nuttx/include/nuttx/config.h
CP:  /home/adminpc/nuttx_pdt/nuttx/include/nuttx/fs/hostfs.h
LD:  nuttx
Pac SIM with dynamic libs..
'/lib/x86_64-linux-gnu/libc.so.6' -> 'sim-pac/libs/libc.so.6'
'/lib64/ld-linux-x86-64.so.2' -> 'sim-pac/ld-linux-x86-64.so.2'
SIM elf with dynamic libs archive in nuttx.tgz

查看输出件nuttx大小

adminpc@adminpc-M600:~/nuttx_pdt/nuttx$ ll | grep nuttx
lrwxrwxrwx  1 adminpc adminpc      75 Dec 31 22:32 Make.defs -> /home/adminpc/nuttx_pdt/nuttx/tools/../boards/sim/sim/sim/scripts/Make.defs
-rwxrwxr-x  1 adminpc adminpc  128632 Dec 31 22:33 nuttx*
-rw-rw-r--  1 adminpc adminpc      27 Dec 31 22:33 nuttx.manifest
-rw-rw-r--  1 adminpc adminpc  123915 Dec 31 22:33 nuttx.map
-rw-rw-r--  1 adminpc adminpc 1128472 Dec 31 22:33 nuttx.tgz

只有128632字节,即125.62K,比之前wiki 15、【OS】【Nuttx】OS裁剪,运行指定程序,周期打印当前任务 裁剪后的1.63M,大幅减少了90%的空间!不愧是最小系统的Nuttx!
查看内核大小,只有106992字节,即104.48K,和之前wiki 裁剪后的325K,也大幅减少了60%的空间!可以说是最小系统了!
在这里插入图片描述

最终效果

前面忘贴应用程序了,参考前面的wiki 15、【OS】【Nuttx】OS裁剪,运行指定程序,周期打印当前任务
myapp_main.c

#include <nuttx/config.h>
#include <stdio.h>

int myapp_main(int argc, char *argv[])
{
    printf("Hello from myapp!\n");
    return 0;
}

运行程序,打印ok

adminpc@adminpc:~/nuttx_pdt/nuttx$ ./nuttx 
Hello from myapp!
;