:FM

Dlck`d\[`XJlggfik`ek_\C`eloB\ie\c B

We’ll show you how to tune up your system for multimedia that apply to the topic (i.e., support for multimedia), and the following descrip- applications. BY DAVE PHILLIPS tions and explanations assume some experience in compiling programs from n the late 1960s, “multimedia” was a and software required by machines in- source code. However, even if you’re a new term connected typically to the tended for the production and presenta- complete novice, Google can direct you @work of artists such as Andy Warhol. tion of rich media formats. Because of to text and video guides for the process. Although Warhol was not the only artist space limitations, I can only touch on It’s not terribly difficult, and if you’re working in this domain, his Exploding some of the factors required to optimize patient and thorough, it might be fun. Plastic Inevitable performances defined that support, but I hope my efforts in- the multimedia event. An EPI show si- spire you to compile your own media- :GLKpg\ multaneously included film, projected optimized kernel. The source code is The first settings will be made in the Pro- images, dancers, music, recitations, and free, the build process isn’t terribly com- cessor type and features section. The pro- so on, for an effect calculated to over- plicated, and the results can yield a con- cessor type is an easy choice, but if whelm the senses. By comparison, the siderable improvement in audio and you’re not sure what CPU is in your ma- multimedia experience on your com- video performance. chine, just issue the uname -a command puter is relatively restrained, but it can at a terminal prompt. be colorful and exciting. :fe]`^li`e^k_\B\ie\c]fi This tool will respond by listing vari- A computer-based multimedia produc- Dlck`d\[`XDXZ_`e\j ous facts about your hardware and its tion comprises text, graphics, sound, The Linux kernel provides support for . For example, on my and video, but these elements can be multimedia with drivers for a variety notebook, I’m running 8.10 with combined into a media-rich presenta- of devices (sound cards, video boards, a kernel patched for real-time operation. tion, an interactive audio/ video installa- graphics tablets, etc.) and code for fea- The uname utility reports the following tion, or a stroke-inducing first-person ac- tures of various motherboard chipsets. information about my system: tion game. Each combination is a true The kernel's openness and modularity multimedia production. Like the original invite extension by developers who want dlphilp@maximus:~$ uname -a multimedia events, they require abun- to add new capabilities to the existing Linux maximus 2.6.27-3-rt #1 5 dant resources. Modern machines have kernel services. PREEMPT RT Mon Oct 27 5 plenty of drive space and RAM, fast Before you compile the kernel, you 03:05:19 UTC 2008 i686 GNU/Linux CPUs, and powerful video capabilities, must configure its options, usually with but modern multimedia software can the help of a menu-based utility that From this report, I learn that my kernel test the performance envelope of even presents the configuration options and is numbered 2.6.27-3-rt, that it’s running the most powerful desktop computer. preferences in an organized GUI (Figure on the first (#1) of a dual-core machine, In this article, I look at the Linux ker- 1). The scope of this article is restricted and that it has been compiled for full nel’s integral support for the hardware to those parts of the kernel configuration preemption. Also, I know that my CPU

38 ISSUE 100 MARCH 2009

038-041_realtime.indd 38 14.01.2009 17:09:08 Uhr Multimedia and the Kernel :FM

type is an i686, a post-Pentium processor trol software. The kernel type. Actually, it’s an AMD Turion-X2, module loader handles configured as a single-core 32-bit i686 everything. You did enable (with the CONFIG_X86 kernel option) so the support for loadable that I can use certain software that will modules, didn’t you? not run on a dual-core 64-bit machine. At this point, I’ll include a word about Ye Olde Way B\ie\cK`d\ij of configuring high-resolu- Successful multimedia performance of tion timing at the kernel any kind is critically dependent on tim- level. Users of pre-2.6 ker- ing. Audio and video need tight synchro- nels might not have access nization, and sound needs to be free of to the new timers, but they dropouts and spurious noise. Fortu- can still set up their ker- nately, the modern Linux kernel pro- nels for enhanced resolu- vides the necessary components, but tion. The CONFIG_HZ op- your distribution might not have the op- tion allows frequency set- tions enabled for those timers. To ac- tings for 100, 250, and quire the benefits of better timing, you 1,000Hz, the last of which might need to recompile your kernel. is the preferred resolution The high-resolution timers option for any system running (CONFIG_HIGH_RES_TIMERS) enables a audio and MIDI applica- “tickless” system with a timing accuracy tions. Conventional wis- =`^li\(1-+Jkl[`fËjb\ie\cZfe]`^liXk`fe>L@% of about 1msec on most contemporary dom advises compiling the machines – a considerable improvement RTC driver, again a necessity for high- be absolutely certain that no other pro- over the standard hertz-based timer res- resolution audio and MIDI event timing. cess is going to knock Ardour aside. The olution. Introduced in kernel 2.6.21, the Note that the timer frequency option is preemptive kernel saves the day. tickless system reduces the load on the available in the Processor type and fea- Preemption is available in four modes. system clock by shutting off the timer in- tures section of your kernel configura- The first sets no preemption at all and is terrupt (the tick) whenever the system tion, but the RTC driver is selected in the the default for servers. Voluntary pre- idles. This process saves power on lap- Device Drivers section. emption, the second mode, is the default tops and notebooks and improves multi- for normal desktop use without timing- tasking performance. The option for B\ie\cGi\\dgk`fe critical applications. The third option high-resolution timers can be enabled Timers aren’t the only items of interest provides a low-latency system for users without the tickless system, but in an in this section; you can also decide on who want an optimal system for multi- optimal multimedia system, make sure the degree of preemption you intend for media performance from applications that CONFIG_NO_HZ is set to true. your kernel, but first, consider the topic such as games and audio/ video players. The high-performance event timer of preemption in the Linux kernel. The fourth choice, complete preemption, (HPET) was once known as the multi- As users’ needs have become more so- is for users who want a media produc- media timer. According to its entry in phisticated, their demands on computer tion platform, either in a studio setting Wikipedia, this timer “… can produce hardware have become greater. Multi- or as a real-time performance system. periodic interrupts at a much higher res- tasking is an ordinary aspect of modern Your choice of preemption mode de- olution than the RTC [real-time clock] computing, and although multitasking is pends on your needs, so choose wisely. and is often used to synchronize multi- indeed a lovely thing, it also creates a As you shall see, a preemptive kernel media streams, providing smooth play- world of concerns for multimedia pro- can have its own problems. back and reducing the need to use other duction and playback. Without some timestamp calculations.” The option sort of process control, applications will ;\m`Z\;i`m\ij (CONFIG_HPET_TIMER) is machine clash over access to system resources Without the correct drivers, your shiny dependent and will not work on older and services, causing audio dropouts, new cards and USB add-ons won’t yield hardware or operating systems, includ- stuttering video, and even complete sys- so much as a squawk, so again be sure ing the Linux 2.4 series. It also requires tem lock-ups. Under normal circum- you’ve selected the right drivers for your the rtc-cmos driver instead of the tradi- stances, most users will be happy with a hardware. Although you can build all the tional RTC driver discussed next. non-preemptive kernel, but if your appli- available drivers as modules for the ker- Which of these timers should you use? cations need priority access to resources nel to load as needed, if you must be On my JAD 1.0 box (openSUSE 10.2), and services, the normal kernel sched- conscientious about space, you can build the HPET and the high-resolution timer uler might not suffice. Preemption is a only the modules you need for your cur- options are both compiled into the ker- way to guarantee an application’s prior- rent hardware. For a multimedia system, nel. Clocks and timers operate transpar- ity status, keeping other processes from look closely at kernel support for your ently on Linux, so don’t worry about interfering with its operations. For exam- graphics hardware, video devices, and loading modules or installing other con- ple, when I record with Ardour, I need to sound cards.

MARCH 2009 ISSUE 100 39

038-041_realtime.indd 39 14.01.2009 17:09:10 Uhr :FM

Kernel drivers for graphics adapters capture devices. If you work with a web- For a status report on the indicated de- have code for hardware from various cam, a television card, or a radio tuner, vice, run the utility without parameters. manufacturers. Just find your device and you’ll need to activate the Incidentally, if you know nothing about select the proper driver. However, bear (V4L) driver by selecting the CONFIG_ hdparm, its manual page is required in mind that these drivers work closely VIDEO_DEV option. Recent kernels in- reading before you use the tool. Some of with the X Window System, and the clude extensive support for a variety of hdparm’s parameters are dangerous, but overall performance of your graphics video capture devices, so be sure to scan fortunately, you need only the two flags display(s) will be determined by the op- the list of supported hardware. First se- shown above to improve the perfor- timal combination of X and the kernel lect the relevant option(s), and be sure mance of your IDE drives. driver. Other potentially relevant options to enable the CONFIG_V4L_USB_DRIV- include /dev/agpgart support for older ERS option if you plan to use a USB- D`jZ\ccXe\fljJ\kk`e^j machines, a direct rendering manage- connected capture or encoding device. The Block Layer configuration section in- ment system for certain video chipsets, As previously mentioned, the drivers cludes three options for choosing your and support for framebuffer devices. section includes the option to build the system’s I/ O scheduler. The Anticipatory The default kernel sound system is driver for the enhanced real-time clock scheduler is the normal system default, from the Advanced Linux Sound Archi- (CONFIG_SND_RTCTIMER). When this the Deadline scheduler is recommended tecture project (ALSA). To activate sound option is selected, you will be given the for systems running large databases, and support in the kernel, select the CON- opportunity to declare RTC as the default the CFQ scheduler is advised for load av- FIG_SOUND and CONFIG_SND options. timer for the ALSA sequencer (a MIDI eraging on normal desktop systems. If Next, choose the driver corresponding to dataflow manager). Consider this option you choose to build all three schedulers, your sound hardware. This step is poten- a necessity. This section also includes an you must declare one to be the system tially confusing: Your might option for building the driver for an IEEE default. The CFQ option is the scheduler not be listed, but its chipset could be 1394 port, more familiarly known as the of choice for low-latency systems. supported by the ALSA drivers. For a Firewire port. Firewire is an excellent The Kernel Hacking section includes complete list of supported devices tabu- data transfer protocol, designed to han- support for the Magic SysRq key (CON- lated by sound card, manufacturer, and dle high-capacity streams, but Linux FIG_MAGIC_SYSRQ), a handy amenity chipset names, see the ALSA website’s support is relatively new. This option in- for anyone using a preemptive kernel. SoundCard Matrix. cludes a stable and an experimental This option enables the use of the SysRq If your sound hardware is connected driver; choose the option that best ap- key in combination with other keys to to a USB port, you’ll need to enable the plies to your hardware. recover a frozen machine or reboot a CONFIG_SND_USB and CONFIG_SND_ Disk drive performance should be op- machine without corrupting the filesys- USB_AUDIO options. PCMCIA cards will timal. The drive type (IDE, SCSI) is not tem. The Magic SysRq Key page on require the CONFIG_SND_PCMCIA op- so important as it once was, but if your Wikipedia has a full list of magic key tion, and similar options exist for sound system includes IDE drives, you should combinations. support with PowerPC, Sparc, ARM, and check a few options in the subsection for other non-x86 Linux-capable platforms. ATA/ ATAPI/MFM/ RLL support. In the CXk\eZpXe[k_\I\Xc$K`d\ The kernel configuration utility lists IDE/ ATA/ ATAPI Block Devices section, B\ie\c Open Sound System (OSS) modules as you need to find and accept the options On the Wikipedia disambiguation page deprecated, but they are still usable. The for Generic PCI Bus-master DMA Support for “latency,” numerous references share older OSS system can be employed if and Use PCI DMA By Default When the common identifying factor of a time ALSA can’t be used, but I would advise Available. These options enable DMA delay between an event’s initiation and the use of the OSS/ Free package [1] in- (direct memory access) for your disks, its realization. Under normal circum- stead of the kernel’s OSS modules. OSS/ which is helpful with low-latency disk- stances, latency might not be an issue, Free is now open source software, and intensive work. However, even with but as playback and production de- the current package is an exceptionally these options, you might still need to op- mands increase, so does the possibility stable and powerful alternative to ALSA. timize your disk performance with the for disruptive latency. Much of the infor- Also, it is easy to install and can be used hdparm utility. The following command mation here is directed toward lowering within an otherwise ALSA-based system. enables 32-bit I/ O and turns on DMA latency, but a normal Linux kernel can- support for the IDE hard drive in my not reach the range of latency consid- Dlck`d\[`X;\m`Z\j JAD 1.0 machine: ered to be acceptable in professional ap- The Multimedia Devices section includes plications. Fortunately, thanks primarily essential support for video and radio hdparm -c 1 -d 1 /dev/hda to the work of kernel developer Ingo Molnar, a set of patches are available Table 1: Machine Details at Studio Dave that can dramatically reduce latency in Machine CPU Distribution Kernel nVidia Driver the kernel – down to and beyond profes- sional limits [2]. Most of the media-opti- Big Black AMD 3200+ (64-bit) 64 Studio 2.1 () 2.6.21-1 169.12 mized Linux distributions include a real- The3800 AMD 3800+ (32-bit) JAD 1.0 (openSUSE) 2.6.19-5 169.12 time kernel, and most of those kernels Maximus AMD Turion-X2 (32-bit) Ubuntu 8.10 (Debian) 2.6.27-3 177.80 are patched with Molnar’s work.

40 ISSUE 100 MARCH 2009

038-041_realtime.indd 40 14.01.2009 17:09:10 Uhr Multimedia and the Kernel :FM

Molnar has identified six traditional ity of 64-bit Flash and the 64-bit Java sign yourself to the possibility of inexpli- sources of latency in the Linux kernel: plugin. Maximus is my cutting-edge ma- cable problems when running a real- ฀ ฀฀฀฀฀ chine; it’s used mostly for testing soft- time kernel. Because of its closed source ฀ ฀฀ ware that requires the latest Qt and Gtk, nature, you can’t know how the video ฀ ฀฀฀/proc file system such as Qtractor and Ardour3. driver interacts with the kernel at levels ฀ ฀฀฀ Big Black and The3800 run on distri- that affect latency and real-time re- ฀ ฀฀฀฀ butions optimized specifically for en- sponse. To nVidia’s credit, they do try to ฀ ฀฀ hanced multimedia performance. Both keep up with Linux kernel development, These factors can delay the return of machines include M-Audio Delta 66 but the real-time patches are not official control to the scheduler for several milli- audio interfaces, and both report patches, and until nVidia opens their seconds, and with enough delay, audio 5.8msec latency in the JACK audio source code, kernel developers are un- dropouts and video frame loss will server. Maximus runs on a Molnar- able to help users who experience per- occur. Molnar’s patches tune these fac- patched real-time kernel; otherwise, its formance problems on real-time and tors until kernel latency reduces to less system is unmodified Ubuntu. It is also low-latency systems. than 5msec, a very dramatic reduction the least stable of the three systems, and Also, you should keep your X window- from the latency of an unpatched kernel I don’t plan to use it for audio or video ing system current. X works closely with and within professional limits. However, production purposes. Maximus is a note- the kernel video drivers, so to ensure low latency also depends on audio hard- book with an Intel HDA audio chipset, maximum performance, make sure that ware capabilities, and in some instances, but I’ve added an Edirol UA25 USB inter- X is up to date. latency might not be so reducible. face to its audio capabilities. Alas, I have The term “real-time,” with regard to yet to bring latency below 11msec with- Flkif the kernel, has two rather different out xruns (ALSA-speak for buffer excess This topic goes much deeper than I’ve meanings. The real-time kernel I’ve been or insufficiencies), but I still have some been able to discuss here, but the web is configuring here is “soft”; that is, it can module options to explore. rich in relevant resources. For example, promise to deliver the goods within a All of my machines are set up to run you should have no trouble finding out certain time, but it can’t make a guaran- Jean-Pierre Lemoine’s AVSynthesis, a more about kernel configuration [6], low tee. By contrast, a hard real-time system program that manipulates and blends latency [7], and real-time optimizations guarantees the delivery within a speci- image and sound into fantastic anima- [8], and I hope that some of you will fied time frame. In neither of these us- tions. Because it needs abundant re- take the next steps toward compiling ages do you find any necessary reference sources, it's an excellent application to your own kernel. Rolling your own is a to low latency, although typically, a real- use to test multimedia capabilities. Its time-honored Linux tradition, and you time system operates within tight time sound production relies on Csound5 don’t need an engineering degree to do constraints, even down to microsecond compiled for high-definition audio, its it. Just be sure to keep your old kernel levels. The essential difference between video capabilities depend on OpenGL around for booting into in case things hard and soft real-time systems is criti- and hardware-accelerated 3D graphics, go wrong, breathe slowly and deeply, cality: If Ardour suffers an audio buffer and it can be operated in real time. be patient and brave, ask questions, and, overrun, I might get upset over the re- I also looked for information regarding above all, have fun. p sulting glitch in my recording, but if the kernels used by other optimized distri- real-time control system software for a butions. Fedora-based Planet CCRMA [3] INFO nuclear power plant misses a few beats, offers stable real-time kernels from the [1] OSS/ Free: then we’re all in serious trouble. 2.6.24 series and testing kernels from the http:// www. 4front-tech. com/ usslite/ 2.6.26 releases. Debian-derived Musix [2] Ingo Molnar’s real-time kernel K_\I\Xc$K`d\B\ie\c`e [4] uses a stable 2.6.21 real-time kernel patches: http://www. kernel. org/ pub/ I\XcC`]\ in its 1.0 R2 release. Gentoo-based Bar- linux/ kernel/ projects/ rt/ Table 1 compares the three machines I dix 0.1 [5] employs a real-time 2.6.25 [3] Planet CCRMA: http://www-ccrma. use here at Studio Dave. All systems are kernel. From this information, you can stanford. edu/ planetccrma/ software/ configured for PREEMPT and RT, and all see that distributions optimized for mul- [4] Musix: http:// www. musix. org. ar/ en/ machines have nVidia graphics chipsets. timedia production and performance index. html Big Black is my main production box, clearly prefer the 2.6 kernel series. [5] Bardix: http://www. linuxmusicians. com/ viewtopic. php? f=4& t=696?? which I use for recording with Ardour, composing with Csound5, producing J`^e`]`ZXekEfe$B\ie\c [6] LinuxForums’ guide to compiling =XZkfij the Linux kernel: CD/ DVDs, and managing all my teach- http:// www. linuxforums. org/ forum/ ing-related activities. The3800 is used When considering your hardware, you linux-kernel/ 55612-mini-howto-com mostly for composing with MIDI, watch- should exercise wisdom. Bigger and pile-linux-kernel-2-6-a. html ing movies and DVDs, and running Win- faster are the watchwords for a powerful [7] The low-latency HOWTO: dows music and sound software under multimedia system, but some caveats re- http:// lowlatency. linuxaudio. org/ Wine. Until recently, it has also been my main. Video is an especially tricky fac- [8] The Real-Time Linux wiki: box of choice for surfing the video web, tor. If, like myself, you need to use nVid- http://rt. wiki. kernel. org/ index. php/ but that could change with the availabil- ia’s closed source drivers, you must re- Main_Page

MARCH 2009 ISSUE 100 41

038-041_realtime.indd 41 14.01.2009 17:09:10 Uhr