Wednesday, August 1, 2018

Fast boot with Raspberry Pi

I am hoping to have a raspberry pi power a wildlife camera. This camera will have to rely on battery and solar power. As a result, it would be beneficial if the camera was off when no wildlife is present. To aid in this regard, I hope to use a motion sensor that can trigger the raspberry pi to turn on and take a picture. For this to work, the time from motion detection to picture snap is heavily influenced by the boot time of the raspberry pi. Here is a video of what I've been able to accomplish:



I am starting with the stock Raspbian Stretch Lite distribution on a Pi 3B. Boot times out of the box are on the order of 1 minute. Boot time is influenced by the following:

1. Hardware
2. Bootloader
3. Kernel
4. Userspace

The Raspberry Pi hardware and bootloader are essentially out of my control. There was an effort to open source the boot loader, however the proprietary binary blob is the only reasonable option at this point. The Hardware and bootloader take approximately a minimum of 1.5-2 seconds to run. This is explained in an excellent post on the Raspberry Pi Forums. The author tested boot times with various minimal boot loaders. The fastest any code could be run on the ARM processor was around 1.5 seconds.

I was able to get the kernel and userspace boot times down to about 0.6 second and 0.8 seconds respectively. As a result my total boot time is on the order of 3.5 to 4 seconds (from power on to picture taken).

To be able to control the Raspberry Pi without SSH, I used serial (UART) communications. See my previous post to learn how.

I reduced the kernel and userspace boot times by doing the following (in order highest yield to lowest yield):

1. Editing the /boot/config.txt with the following changes:


# Disable the rainbow splash screen
disable_splash=1

# Disable bluetooth
dtoverlay=pi3-disable-bt

#Disable Wifi
dtoverlay=pi3-disable-wifi
 

# Overclock the SD Card from 50 to 100MHz
# This can only be done with at least a UHS Class 1 card
dtoverlay=sdtweak,overclock_50=100
 

# Set the bootloader delay to 0 seconds. The default is 1s if not specified.
boot_delay=0

# Overclock the raspberry pi. This voids its warranty. Make sure you have a good power supply.
force_turbo=1


2. Make the kernel output less verbose by adding the "quiet" flag to the kernel command line in file /boot/cmdline.txt 


dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=32e07f87-02 rootfstype=ext4 elevator=deadline fsck.repair=yes quiet rootwait

3. Use systemd-analyze blame, systemd-analyze critical-chain to disable services I didn't need


sudo systemctl disable dhcpcd.service
sudo systemctl disable networking.service
sudo systemctl disable ssh.service
sudo systemctl disable ntp.service
sudo systemctl disable dphys-swapfile.service
sudo systemctl disable keyboard-setup.service
sudo systemctl disable apt-daily.service
sudo systemctl disable wifi-country.service
sudo systemctl disable hciuart.service
sudo systemctl disable raspi-config.service
sudo systemctl disable avahi-daemon.service
sudo systemctl disable triggerhappy.service


See the references below to learn about a primer on systemd and the new linux init system to learn about how to interpret and write the above services.

4. Add a service that runs the code you would like to run as fast as possible. For example if you wanted to add a service called "1ylapse", create the following file: /etc/systemd/system/1ylapse.service


[Unit]
Description=Starts 1 Year Lapse Service

[Service]
ExecStart=/home/pi/foo.sh
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=piservice
User=pi
Group=pi
WorkingDirectory=/root/1ylapse/

[Install]
WantedBy=basic.target

5. Analyze the kernel for unnecessary work being done at boot.

To do this you need to compile your kernel with "CONFIG_PRINTK_TIME" and "CONFIG_KALLSYMS". This should be enabled on the default raspberry pi kernel. This allows you to add "initcall_debug" to the kernel command line. The kernel will now output start and end time information for every init call. You can use "bootgraph.pl" which is included with the linux kernel to analyze the output of dmesg.

On the raspberry pi:

$ dmesg > boot.log

On the cross-compile host:

$ linux/scripts/bootgraph.pl boot.log > boot.sv

This will output an graph of what is taking the most time when initializing the kernel. I noticed that a routine used by the USB driver was taking around 0.3s. I don't need USB for my project so I disabled USB support when re-compiling the  kernel (see below). This saved around 0.3s.

6. Re-compile the Linux kernel

Remove stuff that is wasting time during initialization. I used the guide from the Raspberry Pi Foundation to learn how to re-compile the kernel.

7. Use LZO compression for kernel

When compiling the Linux kernel, select "LZO" compression instead of "GZip". This saved around 0.3s.

8. Don't re-mount the /boot partition

Edit the /etc/fstab file and comment out the line that re-mounts the /boot partition. This saved around 0.2s.


The final systemd-analyze shows:

Startup finished in 669ms (kernel) + 1.225s (userspace) = 1.894s

It should be noted that my camera service starts before systemd is finished initializing. You can find out when your service starts by using systemd-analyze crritical-chain. You can see below that my service starts at 836ms after the kernel is finished initializing, rather than the total of 1.225s.


$ systemd-analyze critical-chain 1ylapse.service
1ylapse.service @836ms
└─basic.target @832ms
  └─sockets.target @832ms
    └─dbus.socket @831ms
      └─sysinit.target @826ms
        └─systemd-update-utmp.service @784ms +41ms
          └─systemd-tmpfiles-setup.service @748ms +33ms
            └─systemd-journal-flush.service @658ms +87ms
              └─systemd-remount-fs.service @585ms +64ms
                └─systemd-fsck-root.service @444ms +137ms
                  └─systemd-journald.socket @433ms
                    └─-.slice @376ms



9. Remove plymouth to disable systemd init messages


sudo apt-get purge --remove plymouth

I haven't seen anyone boot a raspberry pi faster than this using full Raspbian. Bare metal is obviously faster however. However having full Raspbian available at this boot up speed is a good compromise.

Things that failed to improve boot time included making the root partition read only.


Hopefully this helps others in my predicament.

References:
  1. Presentation by Jan Altenberg on booting linux in less than 1 second. Powerpoint here. Youtube of presentation here.
  2. Excellent powerpoint on boot time optimization using a beagle bone as a prototype here.
  3. Excellent powerpoint on speeding up raspberry pi boot time here.
  4. Excellent primer on systemd-anzlyze.
  5. Good stackoverflow question on using sytemd-analyze. 

36 comments:

  1. Amazing. Do you think an Rpi zero can reach the similar boot time?

    ReplyDelete
  2. Awesome tutorial, maybe you should male a longer and more detailed video on this, kudos!!

    ReplyDelete
  3. Very interesting. What camera software are you using?

    ReplyDelete
  4. Your File is Very Usefull File... 100% working... Thanks For Uploading.... Please More File Uploading...

    Please Visit Our Website,Mobile Flash tool,,FastBoot Frp Remover Tool,Country Unlock Tools,iPhone Unlock tools,PC Software Download,
    Usb Flash Tool Mtk,Spd,Qualcoumm,Samsung, ADB Tool Free Download,Iphone Unlock Tool Download,
    All Flash Box Driver Download, Android BackUp Software Tool, Mobile flashing Tools , Android Data Recovery Tools,Cracked Box,EMMC Rom Read And Write Tool Download,Huawei Frp Tool Download,Iphone Icloud Download,
    LG Root Tools Download,LG FRP Unlock TOOL,Motorola Frp Unlocker, Nokia unlock tool Download, Oppo Unlock Tools Download,Qualcomm IMEI Repiar tools,
    Qualcoum cpu frp tools Download,Android Root Software Download,Samsung FRP Tools Download,Box Setup File Download,
    Spd Imei Repair Tools, Xiaomi Unlocker Tools ,Xiaomi Usb Drive Download, Etc File Tool Free Download BY www.AndroidtipsBD71.xyz
    <a

    ReplyDelete

  5. We are a Quickbooks Support company. We always Provides Solution for any error. The quickbooks support phone number can be easily solved if you call us on +18009016679.

    ReplyDelete
  6. Nice article admin thanks for share your atricle keep share your knowledge i am waiting for your new post check the north face school uniform kindly review and reply me

    ReplyDelete
  7. Instant help for your Quickbooks Accounting software you can dial Quickbooks Desktop Support Phone Number 800-901-6679 for USA. We are always available for your help.

    ReplyDelete
  8. Nice Blog It’s a really informative for all. QuickBooks is one of the best accounting software which helps in managing your company finances and accounting. We are providing technical support in Quickbooks Desktop Support Phone Number. Please call us our Toll-free Number + 1-800-986-4607.

    ReplyDelete
  9. Quickbooks accounting software is mostly used by small business owner. If you are using this software and sometimes face errors. Avail instant & effective solution for your queries & issues while using Quickbooks accounting, simply dial Dial Quickbooks Payroll Support Phone Number 1-800-901-6679.

    ReplyDelete
  10. Nice Blog It’s a really informative for all. Quickbooks accounting software helps you to solve accounting problems. We are providing technical support in Quickbooks Support Phone Number 1800 . We also provide guidance & all types of information about Quickbooks. So if you need any issue Please call us our Toll-free Number + 1-800-986-4607.

    ReplyDelete
  11. Nice Blog Our support team at QuickBooks Support Phone Number 1-800-986-4591 resolve your all issue in Quickbooks like installation & upgrading. We are providing 24*7 technical support so if you need any query.

    ReplyDelete
  12. Nice Blog If you need Quickbooks Tech Support Phone Number then you can dial +1-800-986-4591 for help and support. Our technical support team always provides you the best technical help.

    ReplyDelete
  13. What is Quickbooks Support Phone Number 800-901-6679 for instant help. In quickbooks accounting software if you face any type of issue in this. Our Highly skilled Technicians are there for you, to resolve your queries.

    ReplyDelete
  14. are everyone looking for ways online to get help solving their pregnancy and infertility problems when most of every native American is talking online about the help of Dr Mandaker Alamun. I checked him out when my husband who could not get me pregnant for over 9 years of marriage as a result of low sperm count became fertile and now, I am 5 months pregnant and it is this man known as Dr Mandaker who helped my husband solve his problem. My name is Alecia from CA USA. I would advise anyone and everyone who needs the help of any spell caster in love marriage,finance, job promotion,lottery spell,poker spell,golf spell,Law & Court case Spells,money spell,weigh loss spell,diabetic spell,hypertensive spell,high cholesterol spell,Trouble in marriage,Barrenness(need spiritual marriage separation),good Luck, Money Spells,it's all he does or looking for breakthrough in your political career to meet this Dr Mandaker the link to his website copy this link (witch-doctor.page4.me)His email contact witchhealing@outlook.com for He is a Reliable and trustworthy. I and my husband have gone to different hospitals having the thinking that I was at fault for not getting pregnant. But at the Federal hospital, they examined him too and his sperm count was low and unable to get a woman pregnant as a result of male infertility. It was then I sort out,thanks to Dr Mandaker.

    ReplyDelete
  15. Our Quickbooks Support Phone Number USA 800-901-6679 for instant help. In quickbooks accounting software if you face any type of issue in this. Our Highly skilled Technicians are there for you, to resolve your queries.

    ReplyDelete
  16. Thanks For Sharing!
    Quickbooks application can easy to use. It is user friendly software if yur are facing issue dial our Quickbooks Support in New York 1-800-986-4591.

    ReplyDelete
  17. Avail instant & effective solutions from our Quickbooks Support Phone Number. Our support team constitutes of highly skilled & trained technicians who have years of experience in handling technical defects. It doesn’t matter how complex the issues would be. Get it resolved, from our Support team. As they are available for you, 24*7. Whenever you face any trouble, feel free to contact Quickbooks support Phone Number 800-901-6679.

    ReplyDelete
  18. The most recent comments here have been spam so I am unfollowing what could have been interesting followup.

    ReplyDelete
  19. Facing problems in deleting or Editing paychecks in Quickbooks ? Feel Free to contact on Quickbooks Payroll Support Phone Number 1-800-986-4607. Get Immediate & effective solution for problems regarding Quickbooks Payrolls. Avail round the clock technical assistance from Highly skilled & Trained experts who have years of experience in resolving the Quickbooks Technical glitches.

    ReplyDelete
  20. Quickbooks is mostly used by smart business owner. If you are using this software and sometimes face errors. Avail instant & effective solution for your queries & issues while using Quickbooks Payroll accounting, simply dial Dial Quickbooks Helpline Number 800-901-6679.

    ReplyDelete
  21. QuickBooks Support +1-877-651-8034 QuickBooks is considered as the best accounting software which has the ability to integrate with other sorts of programs. This software is offering a lot of time in the matter of managing any type of complex business accounting process in less time. Quickbooks Billing Support Number Simply put, you can easily rely on its high productivity and automating accounting software. If you are one of those who are running their own business and using this QuickBooks accounting software, Quickbooks Enterprise Support Number you should know QuickBooks Technical Support Phone Number is available 24*7 or even on public holidays. When you will dial QuickBooks Technical Support Number, our technicians will coordinate you to discuss your problems and resolve them in lesser time. Quickbooks Error Support NumberWhen you will dial QuickBooks Technical Support Number to consult your problems, you will be able to get valuable and reliable solutions to your problems. Our team knows how to fix any sort of technical problem; be it complex or not. If you are dealing with some sorts of problems while using this QuickBooks accounting software, you are free to dial Quickbooks Online Support NumberQuickBooks Technical Support Phone Number which is mentioned on our website. This will be more convenient for you to reach easily at our QuickBooks Technical Support channel. It will definitely help you in answering your questions which are puzzling in your mind. QuickBooks Support +1-877-651-8034

    ReplyDelete
  22. QuickBooks Support +1-877-651-8034 is accounting software which is fully responsible to handle banking transactions. If you are one of those who are driving their own business then you can choose this accounting and finance management tool in the matter of managing your business finance. Suppose, you are dealing with some issues while using then you can dial Quickbooks Support Number QuickBooks Enterprise Support Phone NumberIf you are a customer then you can be thinking about the quality of QuickBooks Support Services before contacting any type of technical help, it is always recommended that you should dialQuickbooks Payroll Support Number QuickBooks Customer Support Phone Number to talk to our team of experts. If you are one of those who are worried the quality of QuickBooks Desktop Support channel and hesitating to dial our QuickBooks Desktop Support Phone Number, Quickbooks Pro Support Number or you are worried about to take the availability of our technicians, you just need to focus on us. This is because we have an option of mailing and messaging which areto discuss your queries with our team of technicians who have immense knowledge in this stream. When you will dial QuickBooks Enterprise Phone Number to talk to our experts then you will definitely get quick and reliable solutions to your problems. They have great knowledge in this stream so, you can You can easily obtain 24*7 QuickBooks Payroll Support by calling at QuickBooks Support +1-877-651-8034

    ReplyDelete
  23. Get your issues resolved from our highly enthusiastic technicians, as they provide effective solution with an amicable approach. Don’t remain preoccupied with the technical issue. Do let us know & get immediate assistance.
    Call now, on Quickbooks Toll Free Number 1-800-986-4607.

    ReplyDelete
  24. all you quickbooks experts, this is not the right forum to advertise

    ReplyDelete
  25. Wow! this is Amazing! Do you know your hidden name meaning ? Click here to find your hidden name meaning

    ReplyDelete