Start C++ program as fast as possible after power up

Hi! I have 2GB board version and i want to start my C++ program as fast as possible after power up. What can you suggest on my project needs:

  1. OS installed on eMMC in read only mode to prevent files damage when suddenly power off / reboot.
  2. C++ program uses UART and file system for logs writing.
  3. C++ program should start in 5-7 seconds after power up (or maybe less if its real).
  4. C++ program should not interrupt booting process.
  5. USB, network and GUI can be used after everything else boots up (no hurry).

I bench boot time on different OS’s on eMMC (DietPi, Debian, Armbian, slarm64 and crux-arm). DietPi was the fastest (boot took 12 seconds). Debian 15-16 seconds. Other OS’s boot even longer.
Can I just add line to some file that will launch my C++ program at the very beginning of booting process after UART and file system initialized but before everything else on Debian/DietPi?

If you need special things that are not important in consumer grade OS, you need to build your own:
https://www.yoctoproject.org
https://buildroot.org/
… which will ofc costs you a lot of.

2 Likes

5 -7 seconds are not hard to archive, you need to optimize from u-boot to kernel to app. You need some embedded Linux skills. check this:

https://bootlin.com/training/boot-time/

OK… If you are same SBC enthusiast as me and not familiar with Linux kernel editing to throw out things from it to speed-up booting process here is my quick and in most time acceptable solution of the problem witch costs 0$ :sweat_smile: and 0 lines of kernel edit :sweat_smile:. Just some hours spend around systemd services.
First of all, the basic idea is not to delete everything from OS to decrease boot time. The idea is to put Your program launch after and before some OS services in boot process. The sequence of service starting is shown here.
The basic OS functionality comes with sysinit.target loaded. You still can be missing some drivers (like SPI) so keep it in mind and wait for necessary drivers to start (with some bash script for example) but UART, GPIO and filesystem are available to interact.

To add your program (for example /usr/myprogram) as a service you should:

  1. sudo su if you are not root

  2. Create a service file. For example nano /etc/systemd/system/myserw.service:

    [Unit]
    Description=MyService
    DefaultDependencies=no

    [Service]
    Type=simple
    ExecStart=/usr/myprogram

    [Install]
    WantedBy=sysinit.target

  3. chmod +x /etc/systemd/system/myserw.service

  4. systemctl enable myserw.service
    creates symlink

  5. systemctl daemon-reload

  6. systemctl status myserw.service
    Now you should see some info about your service working.

  7. reboot and if your app can blink a LED - try to measure time between boot process starts and led turned on and be ready to be nicely surprised. If your program did not start at booting process systemctl status myserw.service should give you info about the problem occur.

PS: You should read about systemd service creation to be more flexible with your project needs.

Result: my C++ project on beagle board witch boots about 120! seconds till network up, now starts after 31 second after power up. Its almost 4 times faster!!!

I will try described method on radxa zero and will share the results.
And it would be nice if someone can help to solve 2 annoying things in raxda booting process:

  1. 2 seconds delay on U-boot.
  2. raid6 benchmark on kernel start. (1 second)

In general it waist 3 seconds and it cannot be skipped by systemd :crying_cat_face:.
Post created.

UPD. Problems solved:

Fix 1. u-boot 2 sec delay.
Fix 2. raid6 bench - 1 sec delay.

1 Like