Performance tuning

Note that the information in this section pertains to Shark, a performance tool that is only available on Mac OS X as part of the CHUD Tools. It is not possible to use it on a pure Darwin installation.

How can Shark be used to profile the code in a driver that exists solely in supervisor space?

Shark has several modes for capturing data, but most of them pertain to userspace code. The best mode to use for capturing data is the "Marked Cycles" mode. To use this, the cycles used by the driver need to be marked for collection. This is done by setting the Machine Status Register (MSR) marked bit (bit 29). Shark will sample all cycles with this bit set while ignoring all userspace activity. However, if the driver execution is preempted to run another task then Shark will also capture that performance data.

The code to set and unset this bit should look similar to this:

void driver_class_name::_setPMM( bool mark )
{
        unsigned int msr_val;

        __asm__ volatile("mfmsr %0" : "=r" (msr_val));

        if(mark) {
                msr_val |= 0x4; // note: least significant bit is bit #31 on PPC, so mask for bit 29 is 0x4
        } else {
                msr_val &= 0xFFFFFFFB;
        }
        __asm__ volatile("mtmsr %0" : : "r" (msr_val));
}
        

Simply call this function with parameter "true" at every entry point into the driver. At each exit point, call the function again with "false" as the parameter.

When I run Shark on my code, it can't look up the symbols in my driver. Every line in the profile has the form "NO_SYMBOL_0xSOMEHEXADDR". How do I fix this?

There are two possible explanations for this error.

First, verify that the code is being compiled with debugging symbols turned on. For gcc this corresponds to the -g option. Also verify that the linker is not stripping all symbols. By default it will not, but look for options similar to -S on the linker command line.

Second, Shark must be able to find the driver so it can do the symbol lookup during its processing step. By default it will look for it in /System/Library/Extensions. It caches all this information in a preference file stored at ~/Library/Preferences/com.apple.shark.kextpaths. Look for the driver name in that file. If it doesn't exist, either add the absolute path or delete the file and relaunch Shark. It will rebuild the file and rediscover all kexts in the Extensions directory.

Double-clicking on a trouble spot in Shark doesn't bring up the offending source code. How do I fix this

This is a preference setting within the Shark application. To fix, launch Shark and open the preferences panel. Select the "Search Paths" tab and click the plus sign to add the directories containing the source code. Relaunch Shark to be certain the settings take effect.