Realtime (RT) Kernel

Note: this a "backport" or better a "Gentoo'ification" of the RT Kernel howto originally written by gimpel, contributed to the openSUSE jacklab community.
 * the original version can be found here (German): http://www.linux-club.de/ftopic31817.html
 * the translated and edited version can be found here: http://en.opensuse.org/RT_Kernel_Instructions

RT Kernel - why?
The main goal in audio-processing is the latency. The latency is the delay between signal-creating and reaching the processing in the system (e.g time between pressing a key on the keyboard and hearing the tone). The standard kernels have the disadvantage that the processing is slower (~11ms) than in Windows or on a MAC. The faster the system, i.e. the lower the latency, the more things can happen simultaneously on a the system (e.g. more synths...) Ingo Molnar, a RedHat developer and some others have started a project to minimize the latency in Linux: Realtime Preemption. By using excellent audio-hardware the JACK daemon (Jack Audio Connection Kit) can reach latency < 1ms, similar to the MacOS X „coreaudio“ system. For interested people this is a little step-by-step HowTo to patch a vanilla kernel with Realtime Preemption stuff and to compile and install this new kernel.

Note
This is an experimental kernel. the -rt patch is under heavy development all the time. So if you hit a bad day, you'll get a unstable/unusable kernel. See http://lkml.org for support, and check for a new version some days later if you happen to run into problems. Sample kernel is the stable linux-2.6.16 (stable at the time of writing, just see kernel.org and ingo's page for the latest versions).

The simple approach
Evermind's overlay provides an ebuild for rt-sources. So to get the latest kernel source including the -rt patch, just type emerge -av rt-sources After that continue with the configuration part.

Download the sources
wget http://kernel.org/pub/linux/kernel/v2.6/linux-2.6.16.tar.bz2 wget http://people.redhat.com/mingo/realtime-preempt/patch-2.6.16-rt22 Note: this is an example, the version of the -rt patch changes almost daily. So just take a look at http://people.redhat.com/mingo/realtime-preempt, choose the latest version and replace -rt22 with the current version throughout the whole guide. Note the realtime kernel patch has moved to http://kernel.org/pub/linux/kernel/projects/rt.
 * download current vanilla kernel
 * download Realtime Preemption patch

unpack and patch kernel
cd /usr/src/linux su tar xvjf /path/to/linux-2.6.16.tar.bz2 linux-2.6.16 linux-2.6.16-rt22 cd linux-2.6.16-rt22 patch -p1 < /path/to/patch-2.6.16-rt22

Clone the config
It's a good thing to clone your current (working) kernel configuration Then run This step will cause a ton of questions. You can basically say No to everything you don't need or for which you have no idea, because in the next step we will adjust the important settings.
 * if you have support for access to the config via  enabled:
 * 1) zcat /proc/config.gz > .config
 * if you don't, then copy the config from the current kernel's source tree
 * 1) cp ../linux-`uname -r`/.config .config
 * 1) make oldconfig

Note that section 10 of the Gentoo Linux Kernel Upgrade Guide warns on the use of " ", which is suitable for a change in kernel fix number, or for a change in the release candidate token, within the same kernel revision number, whereas it is not so safe for a change in kernel version; for example, given a kernel number, where C is the version number (see the Version numbering of the Linux kernel in Wikipedia), you can safely use " " for a change in the kernel fix number D, or for a change from, say, " ", to " ". As an example, " " is perfectly suitable for building your  based on that of.

In other situations, copy the  file as instructed above and run " " directly.

Adjust important settings
Enter the configuration menu make menuconfig and make the following settings:
 * on x86, activate high resolution timer

Processor type and features ---> (*) High Resolution Timer Support Note that since kernel 2.6.18-rt8, you may have to activate ACPI option to activate high resolution timer.

Power management options ---> ACPI ---> (*) ACPI Support (*) Power Management Timer Support
 * activate rt full preempt

Processor type and features ---> Preemption Mode (Complete Preemption (Real-Time)) ---> No Forced Preemption (Server) Voluntary Kernel Preemption (Desktop) Preemptible Kernel (Low-Latency Desktop) (X) Complete Preemption (Real-Time)

Processor type and features ---> Timer frequency (1000 HZ) ---> (X) 1000 HZ
 * set the internal timer frequency

Device drivers ---> Character devices ---> <*> Enhanced Real Time Clock Support
 * enable realtime clock support

Device drivers ---> Sound ---> Advanced Linux Sound Architecture ---> <*>  RTC Timer support [*]    Use RTC as default sequencer timer
 * ALSA should have RTC support too

(note: need to look at a -rt kernel config for a full list) Kernel hacking ---> [*] Magic SysRq key #that one is always good to have ;) [ ] Kernel debugging
 * make sure you get no overhead by debug features

Note that the kernel config option is only available when the kernel is patched with rt-lsm patch. If it isn't, you can emerge realtime-lsm to get the module after the kernel is compiled an installed. Only activate "Default Linux Capabilities" in that case. Security options ---> [*] Enable different security models   Default Linux Capabilities   Realtime Capabilities Leave the menu with "Exit" and save configuration.
 * activate realtime-lsm (deprecated)

Compile and install new kernel
make && make modules_install
 * compile the kernel:

Now get a coffee or something...

cp arch/i386/boot/bzImage /boot/kernel-2.6.16-rt22 cp System.map /boot/System.map-2.6.16-rt22
 * install the kernel:

cd /boot mkinitrd -k kernel-2.6.16-rt22 -i initrd-2.6.16-rt22
 * if you don't have support for your chipset and / filesystem built into the kernel, you will most likely need an intrd:

NOTE: mkinitrd has changed, at least on my system. The format is: mkinitrd initrd-2.6.16-rt29 2.6.16-rt29-tglx4 where "2.6.16-rt29-tglx4" is the directory under /lib/modules


 * Configure the bootloader

nano -w /boot/grub/grub.conf

Copy the existing entry for your old kernel and edit the names. The name should match your new kernel.


 * pray and reboot... :)

Activate and test RT
In order to use realtime features with your user account, you will have to grant privileges to your user. This can be done with the old rt-lsm kernel module, or preferably with PAM Rlimits.

The realtime-lsm module will give you better performance than PAM Rlimits. The difference is not big but for really heavy multimedia work, the use of the realtime-lsm can make a difference. Anyways rt-lsm doesn't care at all about limits and grants rights on all resources that it can find. Means if a RT process runs wild, chances your box will crash/hardlock are higher with rt-lsm than with PAM Rlimits.

realtime-lsm (deprecated)
emerge -av realtime-lsm id that spits out something, the important one is the gid of the audio group uid=1000(tom) gid=100(users)...,18(audio) if your user is not in the audio group yet, as root do gpasswd -a audio and re-login the user afterwards. modprobe commoncap modprobe realtime gid=18 echo "realtime" >> /etc/modules.autoload.d/kernel-2.6 echo "options realtime gid=18" >> /etc/modules.d/realtime modules-update
 * if you haven't compiled realtime-lsm as module in the kernel (needs kernel-patch), you can emerge it. Note that the link /usr/src/linux needs to point to the right kernel-source.
 * determine the gid of the audio group
 * Then load realtime-lsm
 * To ensure this gets loaded automagically after reboot:

Now the normal user is able to run audio with realtime capabilities.

lsmod|grep realtime realtime 5512 0 commoncap 7040 1 realtime

with PAM
make sure you have the latest version of PAM installed from the overlay equery which pam the path to the ebuild should point to where you've put the proaudio overlay. If you don't have equery installed: emerge -av gentoolkit Then edit /etc/security/limits.conf @audio - rtprio 90 @audio - nice -5 @audio - memlock 500000 This sets the RT priority to 90, nices RT tasks to -5, and grants a memlock of ~500MB. It is probably necessary to logout and log back in after doing this. TODO: explain those values more in depth
 * 1) rtprio

without PAM
If you do not use PAM, there is a tool to set RLIMITS priorities: emerge set_rlimits or wget http://www.physics.adelaide.edu.au/~jwoithe/set_rlimits-1.2.0.tgz @audio /usr/bin/jackd          nice=-1 rtprio=85 @audio /usr/bin/qjackctl       nice=-1 rtprio=84 @audio /usr/bin/ardour         nice=-1 rtprio=83 @audio /usr/bin/hydrogen       nice=-1 rtprio=82 @audio /usr/bin/jackeq         nice=-1 rtprio=81 @audio /usr/bin/jack-rack      nice=-1 rtprio=80 @audio /usr/bin/jamin          nice=-1 rtprio=79 @audio /usr/bin/qsynth         nice=-1 rtprio=78 @audio /usr/bin/rosegarden     nice=-1 rtprio=77 @audio /usr/bin/seq24          nice=-1 rtprio=76 @audio /usr/bin/specimen       nice=-1 rtprio=75 @audio /usr/bin/vkeybd         nice=-1 rtprio=74 @audio /usr/bin/zynaddsubfx    nice=-1 rtprio=73 @audio	/usr/bin/ams		nice=-1	rtprio=72 @audio	/usr/bin/amsynth	nice=-1	rtprio=71 Note: this sample is respectfully taken from: http://ubuntustudio.com/wiki/index.php/Breezy:Using_set_rlimits
 * Untar it and see the included README and manpage what to do with it ;)
 * A sample set_rlimits.conf entry could look like this:

testing RT audio
with qjackctl and zynaddsubfx

Hopefully you will hear the beautifull sound of ZynAddSubFX in all its glory ;)
 * Start qjackctl and make your config
 * press Start -> if JACK starts successfully,
 * start zynaddsubfx
 * in qjackctl open Connect
 * and connect zynadd to the alsa playback devices
 * then play around with zynadd

IRQ tweaking
This is a boot script that fix the priority of the hardware. It assign different priorities to the diffent IRQ, so at the rtc get the highest priority, followed by the sound card(s), the usb and the i8042.


 * So far read the excellent document written by Daniel Robbins, Chief architect of Gentoo GNU/Linux and CEO of Gentoo Technologies, Inc.:
 * Linux hardware stability guide, Part 2

emerge -av rtirq To have it started at boot time, do a rc-update add rtirq default
 * and take a look at the rtirq tool

Known issues
no Gentoo specific listed atm, but i'm sure there are issues.

USB and jack
The USB interrupt period is 1 msec. To be able to get lower latency with jack when using it with an USB device, you have to use a setting as 48kHz and 3 period. It will makes the buffer time a multiple of 1 msec and you will get a much lower latency as with the default 2 period. Additionaly, loading the snd-usb-audio with the parameter "nrpacks=1" will give you a much lower latency (for this to work take care that CONFIG_USB_BANDWIDTH is not set and CONFIG_USB_DYNAMIC_MINORS is not set in your running kernel).

Big soundfont and EMU10k1
The soundblaster Live and Audigy are not so good for audio pro work because they use an internal sample format of 16 bits and the new vesions with an internal format of 24 bits are not well supported by Alsa. But they are very popular and they have an internal soundfont synth.

By default, you can not load for more as 128MB soundfont with those cards. If you want to load more, it is very easy.

options snd-emu10k1 max_buffer_size=size_in_MB
 * 1) Edit /etc/modules.d/alsa and add the following option:
 * 2) run
 * 1) update-modules
 * 3) Reboot
 * 4) Load your(s) soundfont(s) as usual with asfxload.

It is best to reboot, because it is some limitations, and amoung them, the kernel might be not able to allocate the memory if you have already used for other purposes.

It is a hadrware limitation with max_buffer_size of 1GB (maybe 2? I don't know for sure). It will not harm if you try to allocate more as what the card can do. You can check at all is working (or not) as expected with
 * 1)  cat /proc/asound/card0/wavetableD1

TODO

 * PAM RLIMITS - explain the values more in depth
 * IRQ tweakage with rtirq - why and how, etc..
 * fix typos and grammar :P

Related Links

 * RT Preempt Howto