Difference between revisions of "Low latency howto"

From AlsaProject
Jump to: navigation, search
(All-in-one real-time linux audio howto)
 
(fat fingers)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
= Low latency audio with Linux 2.6 =
 
= Low latency audio with Linux 2.6 =
 
== Overview ==
 
== Overview ==
Real-time audio work is of course a relative idea, latency is inherent when processing audio in the digital domain, however it is certainly possible to obtain latencies far below perceptible levels. The steps needed to get such results is exactly what this article will address.
+
Real-time audio work is a relative idea, latency is inherent when processing audio in the digital domain, however it is possible to get latencies far below perceptible levels. The steps needed to get such results is what this article will address.
  
 
== The Kernel ==
 
== The Kernel ==
As you most likely already know, much of the configuration needed to be able to reach low latency scheduling is done within the Linux kernel. For the sake of being recent and for ease of use, this article will use the latest 2.6 kernel source. If you are new to building custom kernels it would be wise to go read up on the steps needed to build a kernel that will boot on your system with necessary drivers. [http://kernel-handbook.alioth.debian.org/index.html Debian Kernel Handbook]
+
The kernel is critical, since it has to respond quickly to interrupts. Much of the configuration needed for low latency audio is done within the kernel.
 +
 
 +
For the sake of being recent and for ease of use, this article will use the 2.6 kernel source.
 +
 
 +
Be familiar with building custom kernels. It is easy to stop the system from booting. Read up on the steps needed to build a kernel that will boot on the system with necessary drivers.
 +
 
 +
[http://kernel-handbook.alioth.debian.org/index.html Debian Kernel Handbook]
  
 
=== Get, Extract, Patch ===
 
=== Get, Extract, Patch ===
Obviously the first step in building a new kernel is to get the source to it. This can be had from [http://www.kernel.org Kernel.org] (at the time of writing the latest stable version is 2.6.22). Next you will need to obtain the latest real-time kernel patch by Ingo Molnar, which is available from [http://people.redhat.com/mingo/realtime-preempt/ his website]. You will want to download the latest version for your choice of kernel version. I.E: patch-2.6.22-rt9 for kernel version 2.6.22. Next un-gzip the patch in the directory of the kernel source and apply it. This is usually done with “patch -p1 -i patch-2.6.xx.x-rtx”. If all goes well you will be ready to configure your kernel.
+
To build a new kernel, get the source to it. This can be had from [http://www.kernel.org Kernel.org] (at the time of writing the latest stable version is 2.6.22). Next get the corresponding real-time kernel patch by Ingo Molnar, which is available from [http://people.redhat.com/mingo/realtime-preempt/ his website]. Download the latest version for the choice of kernel version. For example, patch-2.6.22-rt9 for kernel version 2.6.22. Next un-gzip the patch in the directory of the kernel source and apply it. This is usually done with ''patch -p1 -i patch-2.6.xx.x-rtx''.
 +
 
 +
# gunzip patch-2.6.22-rt9.gz
 +
# patch --strip 1 < patch-2.6.22-rt9
  
 
=== Config, Build, Boot ===
 
=== Config, Build, Boot ===
The next step is to configure the newly patched kernel. This is easily done with the “make menuconfig” command. Users can also use “make xconfig” for an x11 interface to the configuration. You will want to first make sure that all necessary drivers to boot your computer are built in, usually this is an ide driver and ext3 file system support. Also you will want to go ahead and configure other drivers such as network and video as well as usb/firewire/parallel drivers. I prefer leaving the ALSA drivers for my sound card out of the initial kernel configuration, as I like to download the latest version from the [http://www.alsa-project.org/ ALSA website] and build them later, this is just a personal preference. You will still want to enable sound card support within “Device Drivers”. Building and installing the ALSA drivers will be covered later.
+
Configure the newly patched kernel. Use the ''make menuconfig'' command on a text console, or ''make xconfig'' for a GUI.
  
Now for the good stuff. Navigate to the “Processor type and features” category. There will be an option titled “Preemption mode”. Set this to “Complete Preemption (Real Time)”. It is also commonly recommended to enable the high resolution timer option. There are also some additional tweaks which are commonly recommended by the [http://lad.linuxaudio.org/subscribe/lau.html Linux Audio Users] community. These include selecting  deadline as the default IO scheduler (Block Layer -> IO Schedulers), enabling HPET timer support (Processor type and features), turning on enhanced real time clock support (Device Drivers -> Character Devices), turning off priority inheritance debugging (Device Drivers -> Character Devices), and enabling the high precision event timer (Device Drivers -> Character Devices).
+
Make sure that the drivers needed to boot the system are built in. Usually this is an IDE driver and ext3 file system support. Configure other drivers such as network and video as well as USB/firewire/parallel drivers.
Once you have your options and drivers configured you can safely exit and save the configuration. Next you will want to compile or build the kernel image. Your reading on kernel compiling and instillation should have made you familiar with this, however a quick make-kpkg kernel_image in Debian or Ubuntu will start the process. After your kernel is built and installed it's time to reboot into the new kernel. If everything boots properly then welcome to your new real-time capable system.  
+
 
 +
Some prefer leaving the ALSA drivers for their sound card out of the initial kernel configuration, as they like to download the latest version from the [http://www.alsa-project.org/ ALSA website] and build them later. Enable sound card support within “Device Drivers”. Building and installing the ALSA drivers will be covered later.
 +
 
 +
Navigate to the “Processor type and features” category. There will be an option “Preemption mode”. Set this to “Complete Preemption (Real Time)”.
 +
 
 +
There are also some additional tweaks which are commonly recommended by the [http://lad.linuxaudio.org/subscribe/lau.html Linux Audio Users] community:
 +
*enable deadline as the default IO scheduler (Block Layer -> IO Schedulers),
 +
*enable HPET timer support (Processor type and features),
 +
*enable enhanced real time clock support (Device Drivers -> Character Devices),
 +
*disable priority inheritance debugging (Device Drivers -> Character Devices), and
 +
*enable the high precision event timer (Device Drivers -> Character Devices).
 +
 
 +
After changes are made, exit and save the configuration.
 +
 
 +
Build the kernel. Exactly how to do this depends on the Linux distribution, but on Debian or Ubuntu use ''make-kpkg'' like this:
 +
# make-kpkg kernel_image
 +
which makes a package that you can then install.
 +
 
 +
After installing the kernel, reboot, and the system should be real-time capable.
  
 
== System Tuneage ==
 
== System Tuneage ==
Even though your kernel may be real-time capable, you will most likely still not be able to run jackd with a frame size of 16 (.667 ms latency with 2 periods/buffers). This is where a little software tweaking is needed.
+
 
 +
Even though the kernel may be real-time capable, jackd may not run with a frame size of 16, which is 0.667ms latency with 2 periods/buffers. Further tweaking is needed.
  
 
=== Building ALSA ===
 
=== Building ALSA ===
  
First you will want to build the ALSA sound drivers (and libraries if necessary) for your new kernel. The latest version can be had from the  [http://alsa-project.org ALSA project website]. Note: If you are installing ALSA for the first time you will also want to install the libraries and utilities from the ALSA website as well. After extracting the dirvers package navigate into the new directory (alsa-driver-1.0.14 at the time of writing) and issue the following command:
+
Build the ALSA sound drivers (and libraries if necessary) for the new kernel.
<blockquote>./configure –-with-sequencer=yes –-with-oss=yes –-with-cards=<drivers> --with-card-options=seq-rtctimer-default
+
</blockquote>
+
you will want to replace <drivers> in the above command to include a comma separated list of the drivers for the sound card(s) on your system. ./configure –-help will give you a full list of drivers and options, you can also find out which driver you need through the sound card matrix [[Matrix:Main]].
+
  
Once the ALSA system is configured simply issue the “make” command and watch as your new driver system is built before your eyes. After it's built, as root issue “make install” to install the modules into the /lib/modules/<kernel version> directory. After this you can install the libraries and utilities if necessary with simple “./configure” and “make&&make install” commands. When all this is done you should be able to adjust settings and volumes with the alsamixer utility. A few modprobes may be necessary to get all the drivers loaded properly but they are usually loaded automatically.
+
The latest version is on the [http://alsa-project.org ALSA project website].
 +
 
 +
Note: if this is the first time ALSA has been installed on this system, install the libraries and utilities from the ALSA website as well. After extracting the drivers package navigate into the new directory (alsa-driver-1.0.14 at the time of writing) and issue the following command:
 +
 
 +
# ./configure –-with-sequencer=yes –-with-oss=yes –-with-cards=<drivers>
 +
 
 +
replace <drivers> in the above command to include a comma separated list of the drivers for the sound card(s) on the system. ./configure –-help will give a full list of drivers and options, or find out which driver is needed through the sound card matrix [[Matrix:Main]].
 +
 
 +
Once the ALSA system is configured type the ''make'' command to build it.
 +
 
 +
Then, as root type ''make install'' to install the ALSA modules into the /lib/modules/<kernel version> directory.
 +
 
 +
Then, install the libraries and utilities (if necessary) by typing ''./configure && make && make install'' commands.
 +
 
 +
Reboot to allow the drivers to be loaded, or load them manually.
 +
 
 +
To verify, use the ''alsamixer'' utility to adjust settings and volumes. ''alsamixer'' may not show settings unless the correct drivers are loaded.
  
 
=== PAM ===
 
=== PAM ===
After ALSA is installed we will need to modify the PAM security system to allow regular users to operate software at a high priority (real time) level. The file that will allow this is “/etc/security/limits.conf” (in Debian based systems). You will need to add the following lines as root to limits.conf.
+
 
<blockquote>@audio - rtprio 95 <br />
+
After ALSA is installed modify the PAM security system to allow regular users to operate software at a high priority (real time) level. Edit ''/etc/security/limits.conf'' as root (in Debian based systems). Add the following lines to limits.conf:
@audio - memlock 512000 <br />
+
 
@audio - nice -19 <br />
+
@audio - rtprio 95
</blockquote>
+
@audio - memlock 512000
This example is from [http://tapas.affenbande.org/wordpress/?page_id=73 Florian Schmidt's website] and is a recommended default only, though it should work nicely for any system. The “rtprio” setting is the maximum priority a user of the “audio” group can run a task. The “memlock” setting is the maximum amount of memory that a member of the “audio” group can lock with a realtime task. This should be less than your maximum physical amount of memory, some recommend it to be half. “nice” is the minimum “nice level” a task can be run as (the willingness of a task to give up it's cpu time). These settings will get jackd, ardour and other RT capable programs runing at higher priorities, but there is still a bit more to do.
+
@audio - nice -19
 +
 
 +
This is from [http://tapas.affenbande.org/wordpress/?page_id=73 Florian Schmidt's website] and is a starting point only, though it should work nicely for any system. The ''rtprio'' setting is the maximum priority a user of the ''audio'' group can run a task. The ''memlock'' setting is the maximum amount of memory that a member of the ''audio'' group can lock with a realtime task. This should be less than the maximum physical amount of memory, some recommend it to be half. ''nice'' is the minimum “nice level” a task can be run as (the willingness of a task to give up it's cpu time). These settings will get jackd, ardour and other RT capable programs runing at higher priorities, but there is still a bit more to do.
  
 
=== Thread Priorities ===
 
=== Thread Priorities ===
  
The next and final step is to set a few thread priorities. The real time patch should have created several IRQ threads as well as added a “hrtimer” thread. These can be viewed with a quick ps -e. You will need the chrt command to set thread priorities (in Debian this is part of the [http://packages.debian.org/lenny/schedutils schedutils package]). It doesn't really matter which order you set the priorities just that it is done properly. Scan the output of the ps -e command looking for threads like “softirq-timer” and “softirq-hrtimer”, making note of the pid of each one (on my dual core system there are two of each, one for each cpu). Also you will want to find the pid of the IRQ thread that your sound card resides on. Looking at the output of cat /proc/interrupts should reveal this. Then all you have to do is issue a few commands and you're done. The basic format for these are: chrt -f -p 99 <pid> So your commands might be as follows.
+
The next and final step is to set a few thread priorities. The real time patch should have created several IRQ threads as well as added a “hrtimer” thread. These can be viewed with ''ps -e''. Use the ''chrt'' command to set thread priorities (in Debian this is part of the [http://packages.debian.org/lenny/schedutils schedutils package]). It doesn't matter which order the priorities are set. Scan the output of the ''ps -e'' command looking for threads like “softirq-timer” and “softirq-hrtimer”, taking note of the pid of each one (on a dual core system there are two of each, one for each CPU). Find the pid of the IRQ thread that the sound card resides on. Looking at the output of ''cat /proc/interrupts'' should reveal this. Set new priorities for each thread:
<blockquote>
+
 
chrt -f -p 99 6 <br />
+
chrt -f -p 99 6
chrt -f -p 99 12 <br />
+
chrt -f -p 99 12
chrt -f -p 99 818 <br />
+
chrt -f -p 99 818
</blockquote>
+
 
These will set the threads identified (6, 12, and 818) to a real-time priority level (99), of course that high of a priority may not be necessary and a bit insecure, feel free to tweak as you see fit. These commands are the last settings that need to be changed and allow the kernel timers to operate on a much higher priority, thus insuring more satisfactory scheduling latencies. The priorities of these processes will need to be reset after each reboot, so a simple init script would suffice for doing this as the pids shouldn't change.
+
These will set the threads identified (6, 12, and 818) to a real-time priority level (99). That high of a priority may not be necessary and a bit insecure, feel free to tweak as you see fit. These commands are the last settings that need to be changed and allow the kernel timers to operate on a much higher priority, thus insuring more satisfactory scheduling latencies.
 +
 
 +
The priorities of these processes will need to be reset after each reboot, so a simple init script would suffice for doing this as the pids shouldn't change. Or use the ''pgrep'' command to locate the threads:
 +
# chrt -f -p 99 `pgrep softirq-timer`
 +
 
 +
A more detailed explanation of thread priorities can be found over at the ffado project's wiki: [http://subversion.ffado.org/wiki/IrqPriorities IRQ priorities How-To]
  
 
== Start recording ==
 
== Start recording ==
The only thing left to do of course is to start recording and making music. You should be able to run jackd with much lower frame sizes than before and much fewer xruns. Enjoy!
+
Now start recording and making music. It should now be possible to jackd with much lower frame sizes than before and much fewer xruns. Enjoy!
  
 
== Notes on APIC ==
 
== Notes on APIC ==
It is a common problem to experience problems of many types when dealing with APIC enabled kernels and motherboards. These can range from dificulties in setting IRQs to full system lockups when using certain IRQs. If you experience any new problems after booting a real-time patched kernel you might try adding the noapic boot option to grub and see if that solves the problem, also, if you are running a single core processor, or for some reason don't want a kernel optimized for dual core processors then you can try disabling IO-APIC in the kernel config.
+
It is a common problem to experience problems of many types when dealing with APIC enabled kernels and motherboards. These can range from dificulties in setting IRQs to full system lockups when using certain IRQs. If you experience any new problems after booting a real-time patched kernel try adding the noapic boot option to grub and see if that solves the problem, also, if the processor is single core, or there is no need for a kernel optimized for dual core processors then you can try disabling IO-APIC in the kernel config.
  
 
= Relevant Links =
 
= Relevant Links =
 
* [http://www.silenceisdefeat.org/~atomic Authors Website]
 
* [http://www.silenceisdefeat.org/~atomic Authors Website]
 
* [http://debian.org/ Debian Linux]
 
* [http://debian.org/ Debian Linux]

Latest revision as of 17:13, 5 November 2009

Contents

[edit] Low latency audio with Linux 2.6

[edit] Overview

Real-time audio work is a relative idea, latency is inherent when processing audio in the digital domain, however it is possible to get latencies far below perceptible levels. The steps needed to get such results is what this article will address.

[edit] The Kernel

The kernel is critical, since it has to respond quickly to interrupts. Much of the configuration needed for low latency audio is done within the kernel.

For the sake of being recent and for ease of use, this article will use the 2.6 kernel source.

Be familiar with building custom kernels. It is easy to stop the system from booting. Read up on the steps needed to build a kernel that will boot on the system with necessary drivers.

Debian Kernel Handbook

[edit] Get, Extract, Patch

To build a new kernel, get the source to it. This can be had from Kernel.org (at the time of writing the latest stable version is 2.6.22). Next get the corresponding real-time kernel patch by Ingo Molnar, which is available from his website. Download the latest version for the choice of kernel version. For example, patch-2.6.22-rt9 for kernel version 2.6.22. Next un-gzip the patch in the directory of the kernel source and apply it. This is usually done with patch -p1 -i patch-2.6.xx.x-rtx.

# gunzip patch-2.6.22-rt9.gz
# patch --strip 1 < patch-2.6.22-rt9

[edit] Config, Build, Boot

Configure the newly patched kernel. Use the make menuconfig command on a text console, or make xconfig for a GUI.

Make sure that the drivers needed to boot the system are built in. Usually this is an IDE driver and ext3 file system support. Configure other drivers such as network and video as well as USB/firewire/parallel drivers.

Some prefer leaving the ALSA drivers for their sound card out of the initial kernel configuration, as they like to download the latest version from the ALSA website and build them later. Enable sound card support within “Device Drivers”. Building and installing the ALSA drivers will be covered later.

Navigate to the “Processor type and features” category. There will be an option “Preemption mode”. Set this to “Complete Preemption (Real Time)”.

There are also some additional tweaks which are commonly recommended by the Linux Audio Users community:

  • enable deadline as the default IO scheduler (Block Layer -> IO Schedulers),
  • enable HPET timer support (Processor type and features),
  • enable enhanced real time clock support (Device Drivers -> Character Devices),
  • disable priority inheritance debugging (Device Drivers -> Character Devices), and
  • enable the high precision event timer (Device Drivers -> Character Devices).

After changes are made, exit and save the configuration.

Build the kernel. Exactly how to do this depends on the Linux distribution, but on Debian or Ubuntu use make-kpkg like this:

# make-kpkg kernel_image

which makes a package that you can then install.

After installing the kernel, reboot, and the system should be real-time capable.

[edit] System Tuneage

Even though the kernel may be real-time capable, jackd may not run with a frame size of 16, which is 0.667ms latency with 2 periods/buffers. Further tweaking is needed.

[edit] Building ALSA

Build the ALSA sound drivers (and libraries if necessary) for the new kernel.

The latest version is on the ALSA project website.

Note: if this is the first time ALSA has been installed on this system, install the libraries and utilities from the ALSA website as well. After extracting the drivers package navigate into the new directory (alsa-driver-1.0.14 at the time of writing) and issue the following command:

# ./configure –-with-sequencer=yes –-with-oss=yes –-with-cards=<drivers>

replace <drivers> in the above command to include a comma separated list of the drivers for the sound card(s) on the system. ./configure –-help will give a full list of drivers and options, or find out which driver is needed through the sound card matrix Matrix:Main.

Once the ALSA system is configured type the make command to build it.

Then, as root type make install to install the ALSA modules into the /lib/modules/<kernel version> directory.

Then, install the libraries and utilities (if necessary) by typing ./configure && make && make install commands.

Reboot to allow the drivers to be loaded, or load them manually.

To verify, use the alsamixer utility to adjust settings and volumes. alsamixer may not show settings unless the correct drivers are loaded.

[edit] PAM

After ALSA is installed modify the PAM security system to allow regular users to operate software at a high priority (real time) level. Edit /etc/security/limits.conf as root (in Debian based systems). Add the following lines to limits.conf:

@audio - rtprio 95
@audio - memlock 512000
@audio - nice -19

This is from Florian Schmidt's website and is a starting point only, though it should work nicely for any system. The rtprio setting is the maximum priority a user of the audio group can run a task. The memlock setting is the maximum amount of memory that a member of the audio group can lock with a realtime task. This should be less than the maximum physical amount of memory, some recommend it to be half. nice is the minimum “nice level” a task can be run as (the willingness of a task to give up it's cpu time). These settings will get jackd, ardour and other RT capable programs runing at higher priorities, but there is still a bit more to do.

[edit] Thread Priorities

The next and final step is to set a few thread priorities. The real time patch should have created several IRQ threads as well as added a “hrtimer” thread. These can be viewed with ps -e. Use the chrt command to set thread priorities (in Debian this is part of the schedutils package). It doesn't matter which order the priorities are set. Scan the output of the ps -e command looking for threads like “softirq-timer” and “softirq-hrtimer”, taking note of the pid of each one (on a dual core system there are two of each, one for each CPU). Find the pid of the IRQ thread that the sound card resides on. Looking at the output of cat /proc/interrupts should reveal this. Set new priorities for each thread:

chrt -f -p 99 6
chrt -f -p 99 12
chrt -f -p 99 818

These will set the threads identified (6, 12, and 818) to a real-time priority level (99). That high of a priority may not be necessary and a bit insecure, feel free to tweak as you see fit. These commands are the last settings that need to be changed and allow the kernel timers to operate on a much higher priority, thus insuring more satisfactory scheduling latencies.

The priorities of these processes will need to be reset after each reboot, so a simple init script would suffice for doing this as the pids shouldn't change. Or use the pgrep command to locate the threads:

# chrt -f -p 99 `pgrep softirq-timer`

A more detailed explanation of thread priorities can be found over at the ffado project's wiki: IRQ priorities How-To

[edit] Start recording

Now start recording and making music. It should now be possible to jackd with much lower frame sizes than before and much fewer xruns. Enjoy!

[edit] Notes on APIC

It is a common problem to experience problems of many types when dealing with APIC enabled kernels and motherboards. These can range from dificulties in setting IRQs to full system lockups when using certain IRQs. If you experience any new problems after booting a real-time patched kernel try adding the noapic boot option to grub and see if that solves the problem, also, if the processor is single core, or there is no need for a kernel optimized for dual core processors then you can try disabling IO-APIC in the kernel config.

[edit] Relevant Links

Custom Search
Personal tools
Namespaces

Variants
Actions
Navigation
wiki
Toolbox