If you're on a Linux system, sysprof is your best friend. First, make sure you've got the kernel development headers, and debugging information for the things you're trying to profile. On a Fedora system, that looks like:
% sudo yum -y install kernel-devel % sudo debuginfo-install -y xorg-x11-server-Xorg xorg-x11-drv-savage
Then, build sysprof:
% git clone git://git.gnome.org/sysprof % cd sysprof % ./autogen.sh % make % sudo make install % sudo sysprof &
Click "Start", do some stuff, click "Profile", and revel in the glorious CPU time accounting.
Note that sysprof is itself an X application, so you may want to run it forwarded to another display so it doesn't end up profiling its own animation.