MacBook Rev. F Audio Skipping in Vista Analisys and Solution – Part 1

Last week I got the latest Apple’s 13.3″ MacBook Rev. F. (aluminum unibody model)

This is an amazing looking laptop, and is one of the fastest performing laptops out there. I decided to have dual boot OS and installed Vista and Apple’s BootCamp.
Here is MacBook’s performance index in Vista:

As you can see, the overall rating is pretty high and the graphics card has pretty high rating (5.4) as well. One nice thing is that at the back Apple gave us easy access to the hard drive on the back. This makes a very easy upgrade. The thing that I found is that this is the only laptop that can fit new 500GB SATA laptop drives which are thicker (12.5mm) than standard 250GB laptop drive (9.5mm). So I managed to easily upgrade mine to 500GB 5400rpm Hitachi laptop drive.

The Problem

After playing with MacBook for a while on Vista, I noticed that when playing music, frequently there are audible clicks and audio skipping. This was even more audible when I was listening streamed radio stations over shoutcast network. On top of this, I think that there is significant reduction of the MacBook battery run time as well (comparing to OSX). I decided to investigate this more closely.

In short, I found, the reason why this happens is because of the exceptionally high DPC latency.

Why Audio Drop-outs Occur?

(Taken from http://www.thesycon.de/deu/latency_check.shtml)

Processing of streaming data in real-time is a very challenging task for Windows based applications and device drivers. This is because by design Windows is not a real-time operating system. There is no guarantee that certain (periodic) actions can be executed in a timely manner.

Audio or video data streams transferred from or to an external device are typically handled by a kernel-mode device driver. Data processing in such device drivers is interrupt-driven. Typically, the external hardware periodically issues interrupts to request the driver to transfer the next block of data. In Windows NT based systems (Windows 2000 and better) there is a specific interrupt handling mechanism. A device driver cannot process data immediately in its interrupt routine. It has to schedule a Deferred Procedure Call (DPC) which basically is a callback routine that will be called by the operating system as soon as possible. Any data transfer performed by the device driver takes place in the context of this callback routine, named DPC for short.

The operating system maintains DPCs scheduled by device drivers in a queue. There is one DPC queue per CPU available in the system. At certain points the kernel checks the DPC queue and if no interrupt is to be processed and no DPC is currently running the first DPC will be un-queued and executed. DPC queue processing happens before the dispatcher selects a thread and assigns the CPU to it. So, a Deferred Procedure Call has a higher priority than any thread in the system.

Note that the Deferred Procedure Call concept exists in kernel mode only. Any user-mode code (Windows applications) runs in the context of a thread. Threads are managed and scheduled for execution by the dispatcher.

While there is a pre-emptive multitasking for threads, DPCs are executed sequentially according to the first in, first out nature of a DPC queue. Thus, a sort of cooperative multitasking scheme exists for Deferred Procedure Calls. If any DPC runs for an excessive amount of time then other DPCs will be delayed by that amount of time. Consequently, the latency of a particular DPC is defined as the sum of the execution time of all DPCs queued in front of that DPC. In order to achieve reasonable DPC latencies, in the Windows Device Driver Kit (DDK) documentation Microsoft recommends to return from a DPC routine as quick as possible. Any lengthy operation and specifically loops that wait for a hardware state change (polling) are strongly discouraged.

Unfortunately, many existing device drivers do not conform to this advice. Such drivers spend an excessive amount of time in their DPC routines, causing an exceptional large latency for any other driver’s DPCs. For a device driver that handles data streams in real-time it is crucial that a DPC scheduled from its interrupt routine is executed before the hardware issues the next interrupt. If the DPC is delayed and runs after the next interrupt occurred, typically a hardware buffer overrun occurs and the flow of data is interrupted. A drop-out occurs.

The Solution

After running the DPC Latency Checker program, I found that the excessive DPC latency was indeed the cause of all the audio skipping. 

Here is the DPC Latency with my MacBook idling:

At moments the latency was as high as 30.5ms!

After running the Process Explorer program I was able to track down the program that was causing the problem. It was the Apple’s KbdMgr.exe. This program is responsible for managing the keyboard, handling the special keys and displaying the OSD (on-screen-display) when you, for example, change screen brightness. 

For a program that is mostly idle the KbdMgr.exe consumes quite a lot of CPU cycles.

You can see that KbdMgr has eight threads, but only one of them constantly consumes high number of CPU cycles. After inspecting the API calls that thread (TID 1352) makes, I found that the main cause for the high CPU cycles is the bad KbdMgr’s multiprocessor implementation (deadlock situation betwen CPU cores in User/Kernel mode). In another words, by simply limiting KbdMgr to a CPU core #1 (CPU affinity) and optionally setting KdbMgr’s priority to Idle, the DPC latency of my MacBook improved dramatically:

As you can see the latency went from whopping high 30.5ms down to 1ms. The result was smooth audio playback with no skipping, much more responsive machine and improved laptop’s battery life.

Here is the fix: KbdMgr.rar
The new fixed version will automatically run on CPU #1 with low priority and will reduce the DPC latency of MacBook.

Installation:
1. Go to Task Manager and kill KbdMgr.exe process
2. Download and extract the contents of the KbdMgr.rar file to a temporary directory
3. Backup the original KbdMgr.exe file located in C:\Program Files\Boot Camp directory
4. Copy new KbdMgr.exe file to C:\Program Files\Boot Camp directory
5. Restart your machine

Advertisements

~ by Alexander Popovich on October 30, 2008.

40 Responses to “MacBook Rev. F Audio Skipping in Vista Analisys and Solution – Part 1”

  1. […] …Read More […]

  2. […] MacBook Rev. F Audio Skipping in Vista Analisys and Solution … […]

  3. […] MacBook Rev. F Audio Skipping in Vista Analisys and Solution You can see that KbdMgr has eight threads, but only one of them constantly consumes high number of CPU cycles. After inspecting the API calls that thread (TID 1352) makes, I found that the main cause for the high CPU cycles is the bad … […]

  4. Best Vista Theme For XP

  5. […] Original post by unknown […]

  6. is it possible to set the affinity permanently? or do we need to do this every time we log into Vista?

  7. Great article and fix.

  8. I could swear apple releases terribly “buggy” “drivers” on purpose…

  9. I tested this on Windows XP SP3 on a 15 inch Macbook Pro. (old type)
    Had exactly the same problem, and this also solved the problem for me. Just extra information for anyone who has similar problems and want to fix the problem.

  10. Alex, I’m selfishly excited about your new mac. I’m hoping more and more of your software might end up mac compatible. Keep up the good work. And thanks for everything.

  11. I’m using Vista on a MacBook Pro, with the same Issues. Unfortunately, your fixed kbdmgr.exe is not working.
    The Error Message says: “Invalid side-by-side configuration.”
    Any Ideas?

  12. Mick, you need to install VS 2005 SP1 redistributable. You can find it here: http://www.microsoft.com/downloads/details.aspx?familyid=200b2fd9-ae1a-4a14-984d-389c36f85647&displaylang=en

  13. Thanks a lot, that did it! 🙂

  14. I’m having the same DPC issue on my MBP running Vista. Looks like it’s not the Boot Camp Manager. I’m pretty positive that the Broadcom WIFI driver is causing it on my system, however, there’s something else that causes the seldom spike. I have no idea what that could be though.

  15. Hey Alex, i tried out your kbdmgr.exe on Mac Book Pro 2,5 Ghz and WinXP.
    It works so far, but the eject button did not work anymore.
    It is possible to fix that?
    thanx a lot.

  16. Hey there, thanks for this great solution. I included a link to your site and solution in my threads about how to get DPC Latency below 100us (even below 50us) on Mac Rumors, Gearslutz and RME Audio forums.

    Here’s the link to the Gearslutz one: http://www.gearslutz.com/board/music-computers/348951-howto-low-dpc-latencies-100-us-bootcamped-macbooks-pro.html

  17. I also checked my MBP Early 2008 using DPC – same issue. I compared this to Sony VAIO FW-21E where results are OK. Alex could you add also patch for Vista 64-bit ? Current patch does not work 😦

  18. Unfortunately it turned out that your fix does not help with professional low latency Audio applications. The only way to use those is to completely kill KBDMGR.EXE it seems. Time to report to Apple and hope for a fix anytime soon. Thanks anyway!

  19. I already downloaded and replaced your fixed kdbmgr.exe with the existing one I have. However, I am still experiencing sound glitch in Vista, probably every 40-50 secs. Please help 😦

    • Ed,
      This fix only partially fixes the problem. The second part is as you said the glitch that occurs every minute or so. To fix this you will have to disable the WiFi on your laptop. The glitch happens every time Vista tries to scan for new access points. There is a command line fix for this and I’ll be posting about this soon.

  20. Thank you for the help, saved me alot of frustration causing troubleshooting. the dpc latency checked dropped from 8000+ constant to rarely above 1000.

  21. […] Rev. F Audio Skipping in Vista Analisys and Solution – Part 2 In my previous post I talked about reducing the DPC latency in order to prevent frequent audible clicks and audio […]

  22. Hi Alex,
    Thanks for the Patch, it works great here.
    But i want to run Vista 64bit in the future.
    Would be nice if you can port it to 64bit 🙂

    THX micro

  23. Hi Alex, eliminating the original kbdmgr.exe solved both audio drop outs and randon keyboard+mouse hangs on my MacBook (same model as yours), *but* using your homegrown kbdmgr.exe only gave me audio controls on keys F10-F12. It didn’t seem to provide screen backlight and keyb backlight control as per the original. Do you find the same?

  24. […] setting tap options for the trackpad. It has broken multithreading as pointed out by this website: MacBook Rev. F Audio Skipping in Vista Analisys and Solution – Part 1 AlexP’s Weblog You can also change the CPU affinity of KBDMGR.EXE to CPU 1 (not 0) which will help DPC Latencies […]

  25. Here’s a small toolkit I put together. Installation instructions are included in the README.TXT for your convinience. 😉

    Boot CampED download page:

    http://www.screencast.com/t/OGJm9jwKim

    Direct Download:

    content.screencast.com/users/Timursen/folders/Default/media/220ace76-09df-4e1c-b59c-6eb975a4ab28/Boot%20CampED.zip?downloadOnly=true

  26. Alexander, is it possible you can create a x64 version of KbdMgr.exe? I tried myself with imagecfg, but somehow, after a reboot, the affinity is set to CPU0 instead of CPU1.
    Thanks!

  27. Hi, does this fix help reducing the overall temperature of the macbook, which is reportedly higher in Vista compared to OSX? Improved battery life was mentioned, and I guess the two issues (battery+heat) could be related. Thanks!

  28. I just wanted to thank you for being so awesome. I’ve had the audio issue for a while, and had been searching for answers, and none had helped until you actually did the gruntwork and figured out what the issue was, and took care of it. Bravo, Alex.

  29. (Sorry, first link went dead!)

    Here’s a small toolkit I put together that allows you to conviniently enable/disable Apple’s “Boot Camp” tray application (KBDMGR.EXE) via an icon link and/or keyboard shortcut. Optionally it will switch the function of the F-Keys automatically for you depending on whether Boot Camp is loaded or not. Should work on all versions of Windows (32/64) as long as Windows Scripting Host is enabled.

    Furthermore it automatically turns Boot Camp’s CPU priority to “Idle” and CPU affinity to CPU1 in order to turn down the bug induced DPC Latencies and prevent dropouts with Windows sounds and Media Player playback. Professional Audio users will find that only turning off Boot Camp will allow low audio latency usage. Installation instructions are included in the README.TXT for your convinience.

    Boot CampED – Download Page:

    http://www.screencast.com/t/7HhqtFZLpf

  30. I love your site!

    _____________________
    Experiencing a slow PC recently? Fix it now!

  31. This lacks a decent video card, and, I have to say, considering they stop the ratings system at 5.9, no matter how awesome your hardware is, I’m not impressed. That has to be a really bad hard drive to score a 5.3, and even an 8800M GPU would score a 5.8.

  32. […] Alexander Popovich on Dec.22, 2008, under MacBook Hacks In my previous post I talked about reducing the DPC latency in order to prevent frequent audible clicks and audio […]

  33. I’m experiencing the same issues with Windows 7 beta (build 7000). When I try to run the kdbmgr.exe file provided by this article, I get the following error:

    The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail.

    The application event log says:

    Activation context generation failed for “C:\Downloads\KbdMgr.exe”. Dependent Assembly Microsoft.VC80.MFC,processorArchitecture=”x86″,publicKeyToken=”1fc8b3b9a1e18e3b”,type=”win32″,version=”8.0.50727.762″ could not be found. Please use sxstrace.exe for detailed diagnosis.

    Any ideas?

  34. hi alex
    i am still facing the problem when i listen music on windows media player the sound goes off after some time

  35. Hi all,

    I managed to get a working 64bit version of the KBDMGR.exe that autoloads in CPU1. I do that using a MS software, called imagecfg.

    The only issue is that logging off and re-logging in, it loads on CPU0. Rebooting will fix this.

    The program could be found at http://www.l4tn.net/junk/teejay/KbdMgr.zip

    Hope to be useful!

  36. YAA Adding this to my bookmarks. Thank You

  37. Thanks for this, I noticed the same thing myself. Although I still have a massive problem with the track pad. Using it in my DAW causes the cpu to spike to 99% and totally kill the sound. Running the the DAW with a USB mouse works perfectly. Has anyone been able to fix this – find another trackpad driver that would work? I can live with using the mouse for most cases, but I do a lot of work on airplanes etc which makes it hard to use a mouse…

  38. i need a usbeyecam driver

  39. Hi, you have a great web here! I’m definitely going to bookmark you! Thank you for your info.And this is radio online site. It pretty much covers streaming radio online related stuff.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: