Lac2006 Lee Revell
Upcoming SlideShare
Loading in...5
×
 

Lac2006 Lee Revell

on

  • 469 views

Paper presented at Linux Audio Conference 2006 on realtime kernel performance

Paper presented at Linux Audio Conference 2006 on realtime kernel performance

Statistics

Views

Total Views
469
Views on SlideShare
457
Embed Views
12

Actions

Likes
0
Downloads
0
Comments
0

2 Embeds 12

http://www.linkedin.com 9
https://www.linkedin.com 3

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Lac2006 Lee Revell Lac2006 Lee Revell Document Transcript

  • Realtime Audio vs. Linux 2.6 Lee Revell Mindpipe Audio 305 S. 11th St. 2R Philadelphia, PA, 19107 USA, rlrevell@joe-job.comAbstract (and therefore good audio performance) with-From the beginning of its development kernel 2.6 out custom patches, as kernel preemption waspromised latency as low as a patched 2.4 kernel. available by default. However early 2.6 ker-These claims proved to be premature when testing of nels (2.6.0 through approximately 2.6.7) werethe 2.6.7 kernel showed it was much worse than 2.4. I tested by the Linux audio development com-present here a review of the most significant latency munity and found to be a significant regres-problems discovered and solved by the kernel devel- sion from 2.4+ll. These concerns were com-opers with the input of the Linux audio community municated privately to kernel developer Ingobetween the beginning of this informal collaboration Molnar and 2.6 kernel maintainer Andrew Mor-in July 2004 around kernel 2.6.7 through the most re-cent development release, 2.6.16-rc5. Most of these ton; Molnar and Arjan van de Ven respondedsolutions went into the mainline kernel directly or in July 2004 with the ”Voluntary Kernel Pre-via the -mm, voluntary-preempt, realtime-preempt, emption patch” (Molnar, 2004). The name isand -rt patch sets maintained by Ingo Molnar (Mol- actually misleading - ’Voluntary’ only refers tonar, 2004) and many others. the feature of turning might sleep() debugging checks into scheduling points if preemption isKeywords disabled. The interesting features for realtimeLatency, Preemption, Kernel, 2.6, Realtime audio users, who will always enable preemption, are the additional rescheduling points with lock1 Introduction breaks that Molnar and van de Ven added wher- ever they found a latency over 1ms.In mid-2004 Paul Davis, and other Linux au-dio developers found that the 2.6 kernel, despite 3 Latency debugging mechanismspromises of low latency without custom patches, The first requirement to beat Linux 2.6 intowas essentially unusable as an audio platform shape as an audio platform was to develop adue to large gaps in scheduling latency. They mechanism to determine the source of an xrun.responded with a letter to the kernel developers Although kernel 2.6 claims to be fully pre-which ignited intense interest among the kernel emptible, there are many situations that pre-developers (Molnar, 2004) in solving this prob- vent preemption, such as holding a spinlock, thelem. Massive progress was made, and recent 2.6 BKL, or explicitly calling preempt disable(), orreleases like 2.6.14 provide latency as good or any code that executes in hard or soft interruptbetter than the proprietary alternatives. This context (regardless of any locks held).is a review of some of the problems encountered The first method used was ALSA’s ”xrun de-and how they were solved. . . . bug” feature, about the crudest imaginable la- tency debugging tool, by which ALSA simply2 Background calls dump stack() when an xrun is detected, inThe main requirements for realtime audio on a the hope that some clue to the kernel code pathgeneral purpose PC operating system are appli- responsible remains on the stack. This crudecation support, driver support, and low schedul- mechanism found many bugs, but an improveding latency. Linux audio began in earnest method was quickly developed.around 2000 when these three requirements In the early days of the voluntary preemp-were met by (respectively) JACK, ALSA, and tion patch, Molnar developed a latency trac-the low latency patches for Linux 2.4 (”2.4+ll”). ing mechanism. This causes the kernel to traceThe 2.6 kernel promised low scheduling latency every function call, along with any operation LAC2006 21
  • that affects the ”preempt count”. The pre- duration of the console switch to prevent dis-empt count is how the kernel knows whether play corruption. This problem which had beenpreemption is allowed - it is incremented or known since the 2.4 low latency patches wasdecremented according to the rules above (tak- also resolved with the introduction of the pre-ing spinlock or BKL increments it, releasing emptible BKL.decrements, etc) and preemption is only allowedwhen the count is zero. The kernel tracks the 6 Hardirq contextmaximum latency (amount of time the preempt Another issue discovered in the very early test-count is nonzero) and if it exceeds the previous ing of the voluntary preemption patches was ex-value, saves the entire call stack from the time cessive latency caused by large IO requests bythe preempt count became positive to when it the ATA driver. It had previously been knownbecame negative to /proc/latency trace). that with IDE IO completions being handled So rather than having to guess which kernel in hard IRQ context and a maximum requestcode path caused an xrun we receive an ex- size of 32MB (depending on whether LBA48act record of the code path. This mechanism is in effect which in turn depends on the sizehas persisted more or less unchanged from the of the drive), scheduling latencies of many mil-beginning of the voluntary preemption patches liseconds occurred when processing IO in IRQ(Molnar, 2004) to the present, and within a context.week of being ported to the mainline kernel This was fixed by adding the sysfs tunables:had identified at least one latency regression /sys/block/hd*/queue/max sectors kb(from 2.6.14 to 2.6.15, in the VM), and has which can be used to limit the amount of IObeen used by the author to find another (in processed in a single disk interrupt, eliminatingfree swap cache()) in the past week. Dozens of excessive scheduling latencies at a small price inlatency problems have been fixed with Molnar’s disk throughput.tracer (everything in this paper, unless other- Another quite humorous hardirq latency bugwise noted); it is the one of the most successful occurred when toggling Caps, Scroll, or Numkernel debugging tools ever. Lock - the PS/2 keyboard driver actually spun in the interrupt handler polling for LED status4 The BKL: ReiserFS 3 (!). Needless to say this was quickly and quietlyOne of the very first issues found was that Reis- fixed.erFS 3.x was not a good choice for low la-tency systems. Exactly why was never really 7 Process context - VFS and VMestablished, as the filesystem was in mainte- issuesnance mode, so any problems were unlikely to Several issues were found in the VFS and VMbe fixed. One possibility is that reiser3’s exten- subsystems of the kernel, which are invokedsive use of the BKL (big kernel lock - a coarse quite frequently in process context, such asgrained lock which dates from the first SMP im- when files are deleted or a process exits. Theseplementations of Linux, where it was used to often involve operations on large data struc-provide quick and dirty locking for code with tures that can run for long enough to causeUP assumptions which otherwise would have to audio dropouts and were most easily triggeredbe rewritten for SMP). ReiserFS 3.x uses the by heavy disk benchmarks (bonnie, iozone,BKL for all write locking. The BKL at the tiobench, dbench).time disabled preemption, which is no longer One typical VFS latency issue involvedthe case, so the suitability of ReiserFS 3.x for shrinking the kernel’s directory cache when alow latency audio systems may be worth revisit- directory with thousands of files was deleted;ing. Hans Reiser claims that ReiserFS 4.x solves a typical VM latency problem would cause au-these problems. dio dropouts at process exit when the kernel unmapped all of that processes virtual mem-5 The BKL: Virtual console ory areas with preemption disabled. The sync() switching syscall also caused xruns if large amounts ofOne of the oldest known latency issues involved dirty data was flushed.virtual console (VC) switching (as with Alt-Fn), One significant process-context latency bugas like ReiserFS 3.x this process relies on the was discovered quite accidentally, when the au-BKL for locking which must be held for the thor was developing an ALSA driver that re- LAC2006 22
  • quired running separate JACK instances for quires limiting the amount of time spent inplayback and capture. A large xrun would be softirq context. Softirqs are used heavily by theinduced in the running JACK process when an- networking system, for example looping over aother was started. The problem was identified list of packets delivered by the network adapter,as mlockall() calling into make pages present() as well as SCSI and for kernel timers (Love,which in turn called get user pages() causing 2003). Fortunately the Linux networking stackthe entire address space to be faulted in with provides numerous sysctls that can be tuned topreemption disabled. limit the number of packets processed at once, Process-context latency problems were fortu- and the block IO fixes described elsewhere fornately the easiest to solve, by the addition of a IDE also apply to SCSI, which does IO comple-reschedule with lock break within the problem- tion in softirq context.atic loop. Softirqs are the main source of excessive scheduling latencies that, while rare, can still8 Process context - ext3fs occur in the latest 2.6 kernel as of this writ-While ReiserFS 3.x did not get any latency ing (2.6.16-rc5). Timer based route cache flush-fixes as it was in maintenance mode, EXT3FS ing can still produce latencies over 10ms, anddid require several changes to achieve accept- is the most problematic remaining softirq as noable scheduling latencies. At least three latency workaround seems to be available; however theproblems in the EXT3 journalling code (a mech- problem is known by the kernel developers andanism for preserving file system integrity in the a solution has been proposed (Dumazet, 2006).event of power loss without lengthy file sys-tem checks at reboot) and one in the reserva- 10 Performance issuestion code (a mechanism by which the filesystem The problems described so far mostly fit thespeeds allocation by preallocating space in an- pattern of too much work being done at once inticipation that a file will grow) were fixed by the some non-preemptible context and were solvedmaintainers. by doing the same work in smaller units. How- ever several areas where the kernel was simply9 Softirq context - the struggle inefficient were resolved, to the benefit of all continues users.Having covered process and hardirq contexts we One such problem was kallsyms lookup(), in-come to the stickiest problem - softirqs (aka voked in cases like printk(), which did a lin-”Bottom Halves”, known as ”DPCs” in the ear search over thousands of symbols, caus-Windows world - all the work needed to han- ing excessive scheduling latency. Paulo Mar-dle an interrupt that can be delayed from the ques solved this problem by rewriting kall-hardirq, and run later, on another processor, syms lookup() to use a more efficient search al-with interrupts enabled, etc). Full discussion of gorithm. The frequent invocation of SHATrans-softirqs is outside the scope (see (Love, 2003)) form() in non-preemptible contexts to add toof this paper but an important feature of the the entropy pool was another latency problemLinux implementation is that while softirqs nor- solved by rewriting the code to be more efficient.mally run immediately after the hardirq that en-abled them on the same processor in interrupt 11 Non-kernel factorscontext, under load, all softirq handling can be The strangest latency problem identified wasoffloaded to a ”softirqd” thread, for scalability found to have an origin completely outside thereasons. kernel. Testing revealed that moving windows An important side effect is that the kernel on the desktop reliably caused JACK to reportcan be trivially modified to unconditionally run excessive delays. This is a worse situation thansoftirqs in process context, which results in a an xrun as it indicates the audio device stoppeddramatic improvement in latency if the audio producing/consuming data or a hardware levelsystem runs at a higher priority than the softirq timing glitch occurred, while an xrun merelythread(s). This is the approach taken by the -rt indicates that audio was available but JACKkernel, and by many independent patches that was not scheduled in time to process it. Thepreceded it. problem disappeared when 2D acceleration was The mainline Linux kernel lacks this feature, disabled in the X configuration which pointedhowever, so minimizing scheduling latency re- clearly to the X display driver - on Linux all LAC2006 23
  • hardware access is normally mitigated by the soft realtime applications, like IRQ threading,kernel except 2D XAA acceleration by the X was pioneered by Solaris engineers in the earlyserver. 1990s (Vahalia, 1996). The VIA Unichrome video card used in test-ing has a command FIFO and a status register. 14 AcknowledgementsThe status register tells the X server when the My thanks go to Ingo Molnar, Paul Davis, An-FIFO is ready to accept more data. (Jones and drew Morton, Linus Torvalds, Florian Schmidt,Regehr, 1999) describes certain Windows video and everyone who helped to evolve Linux 2.6drivers which improve benchmark scores by ne- into a world class realtime audio platform.glecting to check the status register before writ-ing to the FIFO; the effect is to stall the CPU if Referencesthe FIFO was full. The symptoms experienced Jonathan Corbet. 2004. Another lookwere identical to (Jones and Regehr, 1999) - the at the new development model.machine stalled when the user dragged a win- https://lwn.net/Articles/95312/.dow. Communication with the maintainer of Eric Dumazet. 2006. Re: Rcu la-the VIA unichrome driver (which had been sup- tency regression in 2.6.16-rc1.plied by the vendor) confirmed that the driver http://lkml.org/lkml/2006/1/28/111.was in fact failing to check the status register Michael B. Jones and John Regehr. 1999. Theand was easily fixed. problems you’re having may not be the prob- lems you think you’re having: Results from a12 The -rt kernel and the future latency study of windows nt. In ProceedingsThe above solutions all have in common that of the 7th Workshop on Hot Topics in Oper-they reduce scheduling latencies by minimiz- ating Systems (HotOS VII), pages 96–101.ing the time the kernel spends with a spin- Robert Love. 2003. Linux Kernel Development.lock held, with preemption manually disabled, Sams Publishing, Indianapolis, Indiana.and in hard and soft IRQ contexts, but do not Ingo Molnar. 2004. [announce] [patch]change the kernels behavior regarding which voluntary kernel preemption patch.contexts are preemptible. Modulo a few re- http://lkml.org/lkml/2004/7/9/138.maining, known bugs, this approach is capable Uresh Vahalia. 1996. Unix Internals: The Newof reducing the worst case scheduling latencies Frontiers. Prentice Hall, Upper Saddle River,to the 1-2ms range, which is adequate for au- New Jersey.dio applications. Reducing latencies further re-quired deep changes to the kernel and the rulesabout when preemption is allowed. The -rt ker-nel eliminates the spinlock problem by turningthem into mutexes, the softirq by the softirqmethod previously described, and the hardirqissue by creating a set of kernel threads, one perinterrupt line, and running all interrupt han-dlers in these threads. These changes result ina worst case scheduling latency close to 50 mi-croseconds which approaches hardware limits.13 ConclusionsOne of the significant implications of the storyof low latency in kernel 2.6 is that I believe itvindicates the controversial ”new kernel devel-opment process” (Corbet, 2004) - it is hard toimagine Linux 2.6 evolving into a world class au-dio platform as rapidly and successfully as it didunder a development model that valued stabil-ity over progress. Another lesson is that in op-erating systems as in life, history repeats itself.Much of the work done on Linux 2.6 to support LAC2006 24