COVER STORY Realtime

Multimedia applications and the realtime priority GETTING REAL provides tools and patches for speeding up the priority of multimedia applications. So if you're not

getting the performance you expect, try shifting into overdrive. BY OLIVER FROMMEL

espite the presence of multiple does not assign the full power of the sys- default kernel (such as the Rlimits fea- Gigahertz clock speeds and mul- tem to a single program. Today’s Linux ture, discussed later in this article, which Dtiple Gigabytes of memory, TV computers, however, often belong to a is enabled in kernel version 2.6.14 or on the computer screen sometimes single user, and that user can sometimes newer). doesn’t work as smoothly as you might benefit from dialing up the priority for If you are thinking about experiment- like. In some cases, the slow perfor- resource-hungry multimedia applica- ing with re-assigning system priority lev- mance of a multimedia application may tions. els, you probably want to enable kernel result from the priority assigned to the Modern Linux kernels allow users to preemption when compiling the kernel. application. assign higher priorities to individual ap- You can check the kernel configuration Actually, one of the biggest advantages plications, giving them preferential treat- file, typically stored in the /boot/grub di- of Linux often causes problems when it ment when it comes to CPU cycles. rectory, to see if your comes to prioritizing multimedia. Be- There are many different solutions for adds kernel preemption by default. The cause Linux is designed as a multi-user revving up the application priority. If file for Ubuntu is titled config-2.6.12-9- , the emphasis is on you have the latest version of your pre- k7, for example. Use Grep to search keeping many different applications run- ferred distribution, you may find that PREEMPT. If you discover that CONFIG_ ning concurrently. By default, Linux many of the features are enabled in the PREEMPT is not set or PREEMPT_NONE Ford Motor Company

26 ISSUE 65 APRIL 2006 WWW.LINUX - MAGAZINE.COM Realtime COVER STORY

is set, you can recompile your kernel with modified settings (Figure 1). However, CONFIG_PREEMPT_ VOLUNTARY=y is acceptable, and PRE- EMPT is better still. If you really want to crank up the pri- ority of a Linux multimedia application, the realtime priority level is the ultimate destination. Realtime priority gives the application almost complete control of system resources, which could have the Figure 1: If you are looking for a system with quick response, you will want to enable the effect of bringing other applications to a “CONFIG_PREEMPT” setting when you build the kernel. standstill. If your kernel supports realtime prior- other distributions will require the ker- realtime priority in the /etc/security/lim- ity, you still need to find out how to en- nel sources, or at least the headers, de- its.conf file. Each entry starts with the able this feature. Normally, only root is pending on their choice of distribution. group, followed by the resource, and allowed to execute a program with real- After satisfying the requirements, go then by the value. The second column time priority. After all, you don’t want to on to compile and install realtime-LSM typically specifies whether the user can give every single user the ability to bring by running make and make install. If modify the preset value (soft if so, hard the computer to its knees by running his you then additionally run depmod -a, if not). Resources include realtime prior- or her software with realtime priority. you can enter modprobe realtime-lsm to ity rt_priority, the nice level nice, and However, running end-user multimedia load the module. realtime-LSM under- the amount of memory to lock memlock. applications as root doesn’t make much stands a few parameters that you can set sense either. For a responsible single to influence the module’s behavior. For @audio hard rt_priority 80 user, therefore, the best solution is to set example, any=1 tells the module to as- @audio hard nice -10 up Linux so that it will support realtime sign realtime priority to any requesting speeds without requiring root privileges. program. For more control, the gid pa- If you prefer not to update the whole This article discusses some techniques rameter lets you specify the group ID of PAM subsystem on your current distribu- for running your applications in realtime authorized programs. For example, if tion, you can try a special utility that without root access. your audio programs belong to the audio only handles realtime priority: the aptly with a GID of 33, you can load the ker- named set_rtlimits tool [4]. To avoid any Realtime without Root nel module as follows: modprobe real- user assigning priorities at will, the ad- Serious audio users have long since de- time-lsm gid=33. This lets you run ministrator can use the /etc/set_rtlimits. vised an approach to the realtime re- Jackd in realtime mode with the -R conf configuration file to specify which quirements of the Jack [2] audio server: option without root privileges. users and groups will have this ability. the realtime-lsm [3] kernel module. The Additionally, the file actually lists the kernel developers have not added the Rlimits programs permitted to assume realtime module to the standard kernel due to Instead of the workaround I just de- priority. The following line lets a user various misgivings; but it has proved its scribed, most kernel developers prefer to called joey give the Jack server /usr/bin/ value in practical audio applications. use the Rlimit mechanism to assign real- jackd realtime priority: What the module aims to do is give real- time priority, and this is why Rlimit be- time priority to programs not running came part of standard kernel 2.6.14. The joey /usr/bin/jackd -1 80 with root privileges. This is important R in Rlimits does not stand for realtime, for Jack, as it has to run under the same however, but for resource. It lets you The next number in this entry sets the user ID as the audio programs that ac- specify the use of various system re- maximum nice value (see the manpage cess it – and it is not a good idea to run sources, such as memory usage, and, for nice), the second number sets the every single synthesizer program as root. more interestingly for this article, the maximum realtime priority. A negative priority. number means that the user is not per- Realtime LSM The approach to Rlimits control that mitted to change the assigned value. The Ubuntu has the realtime-lsm kernel most developers would like to see in- program detects authorized user groups module in its repository, and users of volves the PAM (Pluggable Authentica- based on a leading @, for example other distributions can download the tion Module) subsystem, which handles @audio. You can then run the required source code from the website. To use re- password management in many distribu- program by passing the absolute path- altime-lsm, you need Capabilities as a tions (Fedora, Suse, …). Unfortunately, name to the command line tool: module, not built-in to the kernel. This is only the latest (0.79 or newer) and the case with Suse 9.3, but not with Fe- patched PAM versions actually support set_rlimits -r=80 U dora Core 4, so Fedora users will need to Rlimits. If you happen to have a PAM /usr/bin/mythfrontend recompile the kernel and set up Capabili- that is up to the job, you can specify the ties as a module (Figure 2). Users with users and/ or groups allowed to assign As Rlimits is now part of the standard

WWW.LINUX - MAGAZINE.COM ISSUE 65 APRIL 2006 27 COVER STORY Realtime

kernel, the next versions of most Linux for realtime, for example, SCHED_ISO In our lab, the schedtool command line distributions will support it. Let’s hope and SCHED_BATCH. Batch priority is program proved useful for setting priori- this leads to less complex tools that useful for processes that take a long ties. To use the tool, just launch the pro- let users prioritize applications in a time to complete on server machines. gram with the required priority, as fol- simpler way. This class allows server programs to lows: run awhile longer, before the scheduler Con Kolivas’ Patchset interrupts them. This in turn means schedtool -R -p 50 -e U Not long ago, an Australian doctor by less time lost on task switching. On the mplayer file.avi the name of Con Kolivas caused a stir on downside, the server will not respond as the mailing list, by learning quickly to user input, but this is proba- The -R switch sets the realtime priority; C programming in next to no time, then bly something you can live with on a -p 50 is the value. If the -e is missing, moving on to kernel hacking, and finally server machine. Schedtool expects the ID of the active by optimizing the kernel scheduler. The . The schedutils package by Rob- scheduler is the component that assigns Realtime Lite ert Love [6] does a similar thing to CPU time to active programs. The less The SCHED_ISO priority class is some- Schedtool, but it doesn’t support the full time the scheduler needs to do this, the thing like a “lite” version of the realtime set of scheduler priorities. quicker programs can run, quasi-simul- priority. It does not require root privi- taneously. Con Kolivas scheduler is now leges, but neither does it assign the high- Working on the Kernel part of the standard kernel, and Con has est priority. If a user without root privi- In our lab, the patch set by Con Kolivas a number of other performance boosting leges attempts to assign realtime priority proved to be the most effective ap- patches on his homepage. to an application, the Kolivas scheduler proach. Using the patch, MythTV played Among other things, the patch set in- automatically puts the program in the a decoded DVB stream without video or cludes a number of new priority classes SCHED_ISO class. sound dropouts on a machine with a heavy system load. As the changes are not yet part of the standard kernel, users still need to compile the optimized ker- nel themselves. Fedora users can check out [7] for binary packages, which are not quite up to date. Conclusion Of course, there are limits to the tech- niques described in this article, and the results this kind of tweaking will give you depend on the current system load. Realtime priority should be considered a matter of making the “best effort,” but it doesn’t always clear up all your prob- lems. This said, and assuming normal circumstances, the techniques described

INFO [1] Con Kolivas’ patch set: http:// members. optusnet. com. au/ ckolivas/ kernel/ [2] Jack audio server: http:// jackit. sourceforge. net [3] Realtime-LSM: http:// sourceforge. net/ projects/ realtime-lsm [4] set_rtlimits: http:// www. physics. adelaide. edu. au/ ~jwoithe [5] schedtool: http:// freequaos. host. sk/ schedtool [6] schedutils: http:// rlove. org/ schedutils [7] Desktop kernel for Fedora: http:// apt. bea. ki. se/ kernel-desktop [8] Ingo Molnar’s Voluntary Preemption: Figure 2: You need Capabilities compiled as a module (opposed to compiled into the kernel) in http:// people. redhat. com/ mingo/ realtime-preempt order to use realtime-lsm.

28 ISSUE 65 APRIL 2006 WWW.LINUX - MAGAZINE.COM