Raspberry Pi
With RealTime Patch
ELECTGON
www.electgon.com
[email protected]
01.05.2018
Contents
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 Install Evaluation Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2.1 Cyclictest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2.2 LTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
3 Adding Realtime Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
4 RealTime Patch Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Abstract
Realtime applications are used widely in many industrial ields. It is connected more with
embedded devices which are built mainly on ARM processors. Raspberry Pi is using ARM
processor as its core CPU. Thus, using Raspberry Pi for developing or prototyping Realtime
applications is reasonable approach specially with the low cost of the Raspberry Pi. This
document illustrates then how to use Realtime patch in Raspberry Pi and issues that can be
found during development.
RPi RealTime 1. Introduction
1 Introduction
Raspbian is built for Raspberry Pi as an operating system based on native Linux. This means,
user has the feasibility to tweak Linux kernel to customize it according to his inal system
preferences. Normally, Raspbian is provided on Raspberry Pi website in two versions; Lite
version with only console interface and another one with GUI interface. Both versions are
based in Linux without Realtime patch. Although this will perform actually with high ef iciency
in terms of latency and multitasking, However more harness sometimes is needed for critical
Realtime applications. Applying Realtime patch to kernel of Raspbian will adopt and give
more accuracy for the performance of Raspbian as will be shown in this document. Therefore,
steps needed for applying Realtime patch will be discussed here. As a prerequisite, steps
mentioned here assume that reader has already known about Raspbian and how to install it
as discussed in previous tutorial.
Realtime patch used here is PREEMPT_RT which is a patch that can be applied to a Linux
kernel so that the kernel will have more hard time requirements. Linux kernel without this
patch can be considered also as a Realtime kernel. The PREEMPT_RT adds more restrictions
on the latency of performed process as will be shown later.
2 Install Evaluation Packages
In order to be able to evaluate performance of Raspbian before and after applying Realtime
Patch, we need to install some packages that can be used to measure the performance. We
will use Two packages Cyclictest and LTP.
2.1 Cyclictest
Cyclictest is used to measure latency of the operating system. Download and build it by
$git clone git://git.kernel.org/pub/scm/linux/kernel/git/clrkwllms/rt-tests.git
$cd rt-tests make all
$sudo cp ./cyclictest /usr/bin/
Cyclictest can be used as
$sudo cyclictest -t5 -p80 -n -i10000 -l10000
Which means that cyclictest will generate 5 threads with priority 80. Each thread will be
iterated 10000, each iteration has 10000 loop. So Cyclic test is calculating minimum, average
and maximum latency of each iteration.
2.2 LTP
LTP has various scripts that can be used to test speci ic behavior of the operating system. So
we can measure Scheduling Overhead, Context Switching, Time to create and start a thread,
interprocess communication, etc. LTP can be installed using the following steps
1
RPi RealTime 3. Adding Realtime Features
$wget http://downloads.sourceforge.net/ltp/ltp-full-20150420.tar.bz2
$tar xvf ltp-full-20150420.tar.bz2
$cd ltp-full-20150420
$./configure --with-realtime-testsuite
$make
Note
You may face the following warning while installing any package
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LC_TIME = "de_DE.UTF-8",
LC_MONETARY = "de_DE.UTF-8",
LC_ADDRESS = "de_DE.UTF-8",
LC_TELEPHONE = "de_DE.UTF-8",
LC_NAME = "de_DE.UTF-8",
LC_MEASUREMENT = "de_DE.UTF-8",
LC_IDENTIFICATION = "de_DE.UTF-8",
LC_NUMERIC = "de_DE.UTF-8",
LC_PAPER = "de_DE.UTF-8",
LANG = "en_GB.UTF-8"
To get rid of this warning open the following ile
$sudo nano /etc/default/locale
then add this line
LC_ALL=en_GB.UTF-8
3 Adding Realtime Features
As mentioned before, although Raspbian version is highly optimized, however it can miss
some realtime requirements. Figure 1 shows latency of Raspbian without Realtime patch.
2
RPi RealTime 3. Adding Realtime Features
Figure 1: Raspbian Latency Without Realtime Patch
You can notice that there are some over latency recorded. This is what we meant previously
that Linux is a Realtime system but it can miss some timing. Therefore you may need to
control this latency. This can be done by con iguring the kernel. First we have to de ine which
requirement we need for our Realtime performance. In this tutorial, performance added by
PREEMPT_RT is suf icient. So all what we need is to apply this patch to a Linux kernel and
3
RPi RealTime 3. Adding Realtime Features
use this kernel instead of current Raspbian kernel.
Not any kernel version can work in Raspberry Pi, e, only ported kernel can work with
Raspberry Pi board. That is because processor architecture should be speci ied in the kernel
machines. Fortunately, you can ind Linux kernel ported for Raspberry Pi provided at their
website. Use the following command to get this kernel
$git clone --depth=1 https://github.com/raspberrypi/linux
Open the downloaded folder, we need now to apply PREEMPT_RT patch to that kernel.
To do that, we need to know irst which version of the kernel we have downloaded. You can
know that by opening Make ile and see VERSION, PATCHLEVEL, SUBLEVEL at the beginning
of the ile. Those indicate version of the kernel. For example, I had downloaded this when
writing this document
VERSION = 4
PATCHLEVEL = 1
SUBLEVEL = 15
EXTRAVERSION =
NAME = Series 4800
which means that this Linux kernel is 4.1.15. knowing that we can download matching
PREEMPT_RT patch from the following link
https://www.kernel.org/pub/Linux/kernel/projects/rt/
Extract the downloaded patch
$gunzip patch-4.1.13-rt15.patch.gz
Apply the patch using
$cd linux
$patch -p1 -b < ../path/to/patch-4.1.13-rt15.patch
I am assuming now that you are preparing this realtime version in another host machine
(not in Raspberry Pi). Which means that we need cross compiler in order to be able to compile
the new kernel.
Next is we need to make sure that we have cross compiler to compile this kernel for ARM
processor. For that purpose, you can use Codesourcery provided by MentorGraphics which
is available at the following link
https://sourcery.mentor.com/sgpp/lite/arm/portal/kbentry62
However it is advised to use linaro cross compiler which can be installed by getting source
iles
$git clone https://github.com/raspberrypi/tools
Then add /path/to/downloadDirectory/gcc‐linaro‐arm‐linux‐gnueabihf‐raspbian/bin to your
$PATH in the .bashrc in your home directory (or .pro ile or in etc/pro ile or etc/environment.).
Kernel is ready now for compilation, type the following commands
Create a folder to install compiled modules in it
$mkdir my_modules_folder
4
RPi RealTime 3. Adding Realtime Features
Then go to downloaded Linux folder
$cd linux
$export INSTALL_MOD_PATH=../my_modules_folder
$export KERNEL=kernel7 ##(with small 'k' in kernel7)
$make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig
$make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig
note that bcm2709_defcon ig is the con iguration ile for the Raspberry Pi2.
Most important at this step is to enable PREEMPT_RT feature. This can be found in kernel
feature section. You can then modify other features in the kernel as you need. After inishing
modi ications in the kernel, start the compile process
$make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage
$make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- modules
$make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- dtbs
$make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- modules_install
Then you have to generate the compiled kernel. While you are in Linux directory execute
the following command
$./scripts/mkknlimg ./arch/arm/boot/zImage $INSTALL_MOD_PATH/$KERNEL.img
Prepare now iles and folder that will be transferred to the Raspbian image
$mkdir $INSTALL_MOD_PATH/boot
$cp ./arch/arm/boot/dts/*.dtb $INSTALL_MOD_PATH/boot/
$cp -r ./arch/arm/boot/dts/overlays $INSTALL_MOD_PATH/boot
This compilation process should go ine. If successfully inished, start to transfer this
compiled kernel to the raspbian installed image. Insert SD card which has this installed
Raspbian into your hosting machine. open your SD card to execute the following commands
###Make Backup of the Original Kernel Files#######
$cd /boot
$sudo mkdir OriginalKernelFiles
$sudo cp kernel7.img OriginalKernelFiles/
$sudo cp *.dtb OriginalKernelFiles/
$sudo cp -r overlays OriginalKernelFiles/
###Make Backup of the Original Modules Files#######
$cd /lib sudo mkdir OriginalKernelFiles
$sudo cp -r modules OriginalKernelFiles/
$sudo cp -r firmware OriginalKernelFiles/
####Transfer Compiled Kernel####
$cd
$mkdir CompiledKernel
now in the host machine where we did cross compilation, we should transfer these compiled
kernel
$scp -rp $INSTALL_MOD_PATH/*
[email protected]:/home/pi/CompiledKernel
5
RPi RealTime 3. Adding Realtime Features
In Raspberry Pi, compiled kernel shall be transferred now. Install it
####Install Compiled Kernel####
$sudo cp /home/pi/CompiledKernel/my_modules_folder/kernel7.img /boot/kernel7.
img
$sudo cp /home/pi/CompiledKernel/my_modules_folder/boot/*.dtb /boot/.
$sudo cp -r /home/pi/CompiledKernel/my_modules_folder/boot/overlays/* /boot/
overlays/.
$sudo cp -r /home/pi/CompiledKernel/my_modules_folder/lib/modules/* /lib/
modules/.
$sudo cp -r /home/pi/CompiledKernel/my_modules_folder/lib/firmware/* /lib/
firmware/.
Note that:
• To copy kernel from host machine to Raspberry Pi we used ‐rp option.
• It is advised to run “sync” command before removing the SD card from host machine.
You should have Raspbian with PREEMPT_RT kernel now. To make sure that everything has
been setup correctly, you can restart the RaspberryPi and run the following command
$dmesg | grep error
You shouldn’t get any recorded error messages. Further you can now check performance
of your realtime Raspbian by running Cyclictest to test latency of the system. The following
Results in igure 2 are expected.
6
RPi RealTime 3. Adding Realtime Features
Figure 2: Raspbian Latency With Realtime Patch
There is also Ubuntu Mate that can be used with Raspberry Pi. The following results were
obtained after applying Preempt patch for Ubuntu Mate.
7
RPi RealTime 3. Adding Realtime Features
Figure 3: Ubuntu Mate Latency With Realtime Patch
Installing Ubuntu Mate is quite simple like Raspbian. Download it from Raspberry Pi
website. Then install it in your SD Card
$sudo dd bs=4M if=ubuntu-source-file.img of=/dev/sdb
##(note it is sdb or whatever the driver of the sd Card)
however Ubuntu mate doesn’t have default user account, so after installation, we have to
connect Raspberry Pi with a Monitor to create a user account and prepare needed settings
(Time, Keyboard, etc).
After that we need to expand the image on the full disk size
$sudo fdisk /dev/mmcblk0
This fdisk utility is used to reformat partitions of the SD Card, so run the following commands
8
RPi RealTime 4. RealTime Patch Issues
d
2
n
p
2
ENTER
ENTER
w
then reboot the system and issue the following command
$sudo resize2fs /dev/mmcblk0p2
It is advised also to disable the graphical interface of ubuntu mate and work with a console
interface. This can be done using this command
$graphical disable
4 RealTime Patch Issues
Adding this realtime patch wasn’t that easy task. One problem has been found that took a lot
of time to be igured out. This problem is Raspberry Pi board is “freezing” i.e. it is hanging
and stops working. By doing more investigation about this problem, it has been found that
it happens only when the realtime patch is applied to the kernel. Which can lead to a hint
that there is a mis‐compatibility problem between the operating system (Raspbian) and the
compiled kernel (Linux kernel) and the applied patch (PREEMPT‐RT). This incompatibility
may be due to incompatibility in used packages and libraries, or it is incompatibility between
hardware modules and irmware.
Another suggestion was pointing out that this freezing behavior is because of lack of
power resources in Raspberry Pi since its allowed voltage supply is 2 volt. The 4 USB Interfaces
and Ethernet Interface are transmitting/receiving data on the same bus (i.e. transmission
rate is distributed over these interfaces). These interfaces are driven by one driver which
makes this drive is a power consuming one. Not only but also the USB interface are driving
also attached devices. This powering issue was thought as a cause of the problem because
inappropriate power levels in the Raspberry Pi makes it stop working.
A Third suggestion is referring this freezing behavior to the con iguration of the PREEMPT‐
RT patch. This patch is using Spinlock for sharing resource between processes instead of
using mutexes. i.e. normal Linux kernel is using mutexes, by applying PREEMPT‐RT patch,
it will replace this mechanism by using Spinlock. Not only but also other con igurations in
the kernel like ’panic’ behavior of the kernel. The kernel can be set in case hang‐up or panic
situation to reboot automatically, or just wait, or reboot after some time. Other setting like
detect if there is a panic or not...etc. All these con igurations may cause disturbance in the
system after applying the PREEMPT‐ RT patch.
To sum up causes of this freezing problem, it might be because of
‐ Mis‐compatibility.
9
RPi RealTime 4. RealTime Patch Issues
‐ Lack of Power Resources.
‐ PREEMPT‐RT Con iguration.
Other causes might be possible also.
10
Bibliography
[1] https://www.raspberrypi.org/
[2] http://www.frank‐durr.de/?p=203
11