It’s taken me a while to mess around with the Linux RT system, so here’s a somewhat full documentation of what I did. I assume some Linux compiling knowledge and a safe environment to actually do this, such as a chroot environment or a virtual machine or your roommate’s computer who isn’t going to know what happened.
I’ll start with a simple program in C to test the RT Kernel. Get it here, adapted from the official docs’ provided example. You can compile it on your regular stock kernel as such:
$ gcc -o test_rt test_rt.c -lrt
You can then run the program a number of times to see what the minimum run time is.
$ time sudo ./test_rt
My personal record is about 10 seconds on a stock kernel on VirtualBox running a single CPU. However, we know that testing this program on a standard machine with low load is not indicative of a `real` real time system. So we now run our program under high load. But first, we need to simulate that high load:
$ sudo stress -c 1000 -i 100 -m 2 --vm-keep -t 120
(note that the -m 2 uses (2 * 256)MB of memory, so make sure you have enough!)
And then, we’ll run the test_rt program again.
Patching the Kernel
In this example I’m working with kernel 3.8.4. You may want to use a different version of the kernel, in which case look for the highest version of the patch available here: https://www.kernel.org/pub/linux/kernel/projects/rt/
And download the corresponding vanilla kernel here: https://www.kernel.org/pub/linux/kernel/v3.x/
And moving along, here’s how we patch:
- In your home directory go to the Download folder and download the patch file in bz2 format (mandatory for this exercise) and the vanilla kernel.
- Unpack the kernel, and go into the directory
$ tar -xjvf linux-3.8.4
$ cd linux-3.8.4
- Apply the patch
$ patch -p1 < <(bunzip2 -c ../patches-3.8.4-rt2.tar.bz2)
- Configure the kernel using the config file from your existing kernel
$ cp /boot/config-$(uname -r) .config
$ make oldconfig
- when prompted for preemption model, select option 5 – Fully Preemptible.
- When prompted for debug options do not select it. Turning on the debug flags (which is the default option) will decrease performance.
- For every other prompt you can just press which selects the default
- Build the kernel
$ make-kpkg clean
$ CONCURRENCY_LEVEL=$(getconf _NPROCESSORS_ONLN) fakeroot make-kpkg --initrd --revision=0 kernel_image kernel_headers
- Install the .deb files
$ cd ../
$ sudo dpkg -i linux-headers-3.8.4-rt2_0_i386.deb linux-image-3.8.4-rt2_0_i386.deb
- Reboot into your new kernel. Note: the steps above do not make this kernel your default. You will need to select the appropriate kernel from your grub menu
At this point you can try running the benchmarking script again. My record was under 2 seconds, down from the 10 seconds mentioned above.
Note: If your performance actually decreases, you may want to check to make sure your debugging flags were turned off during compile.