Profiling an application in Mac OS X
Finding which process to profile
If your system is running slowly, perhaps a process is using too much CPU time and won't let other processes run smoothly. To find out which processes are taking up a lot of CPU time, you can use Apple's Activity Monitor.
The CPU pane shows how processes are affecting CPU (processor) activity:
Click the top of the “% CPU” column to sort by the percentage of CPU capability used by each process. If one of the top processes displays a large number (80-100% or more!) then this process may be hogging the CPU and preventing the other processes from doing their tasks. This can result in slow response times, beachballing and even some applications freezing.
A process with a high "% CPU" value is worth profiling.
A process that often freezes or beachballs, even though all other applications run smoothly and "% CPU" is very low, is also worth profiling.
Xcode Command-Line Tools
Profiling is much better with the Xcode tools installed. Instruments has a command-line interface to provide detailed profiling that can be opened in the Instruments application for investigation.
To check if Xcode Command-Line Tools are installed, open a Terminal and type:
to print the path of the current developer directory. If the command-line tools are installed, the output should be a path like the following:
If not, an error message is output:
xcode-select: error: unable to get active developer directory, use `xcode-select --switch` to set one (or see `man xcode-select`)
To install Xcode Command-Line Tools, type:
Profiling with Instruments
If Xcode Command-Line Tools are installed, it is best to use Instruments to profile your application. If Xcode Command-Line Tools are not installed and you would rather not install them, skip to the next section for profiling with
sample. In this example, we will show how to profile
First, we need to find the process ID (PID) of
ecore. Open a Terminal and type:
ps aux | grep ecore
and you should see something like this:
user 5773 0.0 0.0 3183424 9088 ?? S 2:47PM 0:09.00 /Applications/Lima.app/Contents/MacOS/ecore [...arguments...] user 6543 0.0 0.0 2432772 648 s006 S+ 4:32PM 0:00.00 grep ecore
The first entry is the currently running instance of
ecore. (We'll ignore the second entry as it is only an artifact of the actual search). The second column is our PID., in this case
Now that we know the PID, it's time to create a time profile for
ecore. Type the following in the Terminal:
instruments -l 30000 -t Time\ Profiler -p 5773
After 30s (30'000ms),
instruments will save the profile to the Desktop and exit with a message indicating the name of the latest profile file. This file can be opened in Instruments or forwarded to a developer to understand what performance issues have arisen in
ecore on your computer.
sample is a much simpler tool than Instruments. However, its simplicity is also why it is included in a standard Mac OS X installation. To profile
sample, first find
ecore's PID (see above). For this example, I will assume that the PID is still
5773. Open a Terminal and type:
sample 5773 30 -file ~/Desktop/ecore.sample.txt
This will sample process
5773 for 30s and write the profile to the given path. This file can be opened as text or forwarded to a developer to understand what performance issues have arisen in
ecore on your computer.
Improving your sample
Under the hood, sampling checks your process every 1 ms and reports what your process was doing at the time. The data might not always be pertinent.
For example, if your process
sleeps a lot or is never scheduled to run by the operating system, then no sample is taken. Luckily, you can always tell the kernel that your process is important and should be scheduled more often than others. Use
renice for that. For example:
sudo renice -20 -p 5773
This will set the scheduling priority of your process to the maximum.
If you are using profiling to diagnose a specific operation in your process like drawing a view or listing files, you need to make sure the specific code is triggered as often as possible (by refreshing the view a lot, or performing a lot of
ls) during the sampling period.
If none of these options yield better results, or do not apply to your case, it is always possible to extend the sampling duration. 30 seconds seems a bare minimum and you will always get more data if you sample longer.