Monday, September 24, 2012

Real-time Linux



I installed the RT Linux kernel on both of my Ubuntu laptops recently and I am happy to report that thus far I have not noticed any negative side-effects. The RT patch seems to be stable -- video, sound, networking etc. continue to work as before. My intention is to experiment with the RT kernel. In what way is the network latency affected ? Is the packet latency from the ethernet port to user space predictable ?
Many software applications benefit from a real time operating system. I used to work on aircraft simulators. In order for the pilot to experience smooth motion, frames would have to be updated every 16 milliseconds (60 FPS). Too many consecutive frames dropped, often produced unacceptable jerky motion. A single frame dropped from time to time had no appreciable effect. This is an example of a soft real-time system. The system met the desired deadlines on average. A soft real-time system does not guarantee maximum response time. On the other hand, hard real-time systems meet the desired deadlines at all times, even under a worst-case system load. 
The Linux kernel can be made into a real-time kernel by applying Ingo Molnar’s PREEMPT RT patch. This patch makes a regular Linux kernel into a fully Preemptible OS - an OS that has the capability to stop/hold a low priority task in favour of a high priority task. 

I copy the instructions I used to build and install the patch below.


To build the RT kernel by hand, first install the required software packages:

sudo apt-get install kernel-package fakeroot build-essential libncurses5-dev

Then fetch the vanilla kernel and RT patch (the version numbers are somewhat old, tweak as necessary):

mkdir -p ~/tmp/linux-rt
cd ~/tmp/linux-rt
wget http://www.kernel.org/pub/linux/kernel/v3.x/linux-3.4.tar.bz2
wget http://www.kernel.org/pub/linux/kernel/projects/rt/3.4/patch-3.4-rt7.patch.bz2
tar xjvf linux-3.4.tar.bz2
cd linux-3.4
patch -p1 < <(bunzip2 -c ../patch-3.4-rt7.patch.bz2)

Then configure the kernel using:

cp /boot/config-$(uname -r) .config && make oldconfig

where you should select "full preemption" (option 5) when prompted, and leave everything else at its default value by pressing enter at every prompt. The config from the -lowlatency kernel might be a better starting point than that of the -generic kernel.

Then build the kernel with: 

sed -rie 's/echo "\+"/#echo "\+"/' scripts/setlocalversion
make-kpkg clean
CONCURRENCY_LEVEL=$(getconf _NPROCESSORS_ONLN) fakeroot make-kpkg --initrd --revision=0 kernel_image kernel_headers

And finally install your new kernel with:

sudo dpkg -i ../linux-{headers,image}-3.4-rt7_0_*.deb