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 beetween 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) as in Windows or using a MAC . The faster the system, means the lower the latency, the more things can happen simular 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 demon (Jack Audio Connection Kit) reach latency < 1ms, similar to the MacOS X „coreaudio“ system. For interested people follows a little step-by-step HowTo to patch a vanilla kernel with Realtime Preemption stuff and how to compile and install this new kernel.
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.
Obtain the kernel-source and necessary patches
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
- download current vanilla kernel
- download Realtime Preemption patch
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.
unpack and patch kernel
cd /usr/src/linux su <password> 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
- if you have support for access to the config via /proc/config.gz enabled:
zcat /proc/config.gz > .config
- if you don't, then copy the config from the current kernel's source tree
cp /usr/src/linux-`uname -r`/.config .config
Note: this step will cause a ton of questions. You can basically say No to everthing you don't need or you have no idea of what it is. In the next step we will adjust all important settings.
Adjust important settings
Enter the configuration menu
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)
- set the internal timer frequency
Processor type and features ---> Timer frequency (1000 HZ) ---> (X) 1000 HZ
- enable realtime clock support
Device drivers ---> Character devices ---> <*> Enhanced Real Time Clock Support
- ALSA should have RTC support too
Device drivers ---> Sound ---> Advanced Linux Sound Architecture ---> <*> RTC Timer support [*] Use RTC as default sequencer timer
- make sure you get no overhead by debug features
(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
- activate realtime-lsm (deprecated)
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 <M> Default Linux Capabilities <M> Realtime Capabilities
Leave the menu with "Exit" and save configuration.
Compile and install new kernel
- compile the kernel:
make && make modules_install
Now get a coffee or something...
- install the kernel:
cp arch/i386/boot/bzImage /boot/kernel-2.6.16-rt22 cp System.map /boot/System.map-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:
cd /boot mkinitrd -k kernel-2.6.16-rt22 -i initrd-2.6.16-rt22
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
after reboot it's time to activate the realtime-lsm module:
- 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.
emerge -av realtime-lsm
- determine the gid of the audio group
that spits out something, the important one is the gid of the audio group
if your user is not in the audio group yet, as root do
gpasswd -a <username> audio
and re-login the user afterwards.
- Then load realtime-lsm
modprobe commoncap modprobe realtime gid=18
- To ensure this gets loaded automagically after reboot:
echo "realtime" >> /etc/modules.autoload.d/kernel-2.6 echo "options realtime gid=18" >> /etc/modules.d/realtime modules-update
Now the normal user is able to run audio with realtime capabilities.
lsmod|grep realtime realtime 5512 0 commoncap 7040 1 realtime
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
# rtprio @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. TODO: explain those values more in depth
If you do not use PAM, there is a tool to set RLIMITS priorities:
- Untar it and see the included README and manpage what to do with it ;)
- A sample set_rlimits.conf entry could look like this:
@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
testing RT audio
with qjackctl and zynaddsubfx
- 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
Hopefully you will hear the beautifull sound of ZynAddSubFX in all its glory ;)
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.:
- and take a look at the rtirq tool
emerge -av rtirq
To have it started at boot time, do a
rc-update add rtirq default
no Gentoo specific listed atm, but i'm sure there are issues.
Realtime-lsm and PAM-Rlimits
The realtime-lsm module will give you better performances than Pam-Rlimits. The difference is not big but for really heavy multimedia work, the use of the realtime-lsm can make a difference.
On the other hand, you have to test carefully any new application with the realtime-lsm before using it in a production environment, because it can crash the whole system (even the magic keys sometime) in some cases (especially with an old application such as bristol used with a wrong configuration).
It does not make sense to use both. PAM-Rlimits will take the priority over the realtime-lsm module. In consequence, if you are using PAM-Rlimits, you must not load the realtime-lsm module, and if you are using the realtime-lsm and PAM, you must be sure at PAM is not Rlimits aware, it mean at PAM's version must be < 0.80 and not the version in this overlay.
A good thing to do if you are using the realtime-lsm, is to tell portage to not upgrade pam when you do an "emerge --world" or "--deep". Just add the following line in /etc/portage/package.mask
A recent discussion on the alsa users email list showed me at it must be possible to archive exactly the same result with pam-rlimits as with the realtime-lsm. It is only 2 different ways to do the same thing, so more test is needed on that issue. This discussion showed me at it is possible to have PAM-rlimits installed and use the realtime-lsm at the same time. All that is needed is to comment out the lines
@audio - rtprio 90 @audio - nice -5 @audio - memlock 500000
in /etc/security/limits.conf and reboot.
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.
- 1) Edit /etc/modules.d/alsa and add the following option:
options snd-emu10k1 max_buffer_size=size_in_MB
- 2) run
- 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
# cat /proc/asound/card0/wavetableD1
- PAM RLIMITS - explain the values more in depth
- IRQ tweakage with rtirq - why and how, etc..
- fix typos and grammar :P