Sep 20
2010

Reading the cycle counter on an HTC Desire

I wanted to do some performance monitoring on an HTC Desire phone -- nothing complex, just to be able to read the CPU cycle counter. Unfortunately this is more complicated than one might expect, because

  • The performance monitor unit can't be accessed from user-space (even by root), and
  • The stock kernel doesn't include support for performance monitoring.

To cut a long story short, it turns out the reason for the second bullet point above is that the kernel doesn't include support for the perf_events framework for the QSD 8250, which is the SOC present in the HTC Desire. Fortunately, adding the support is easy to do, because the QST 8250 is similar to the already-supported Cortex M8.

So, to do it:

  1. Root your Desire
  2. Install Cyanogenmod 6.0.1
  3. Download the Cyanogen kernel and patch it
  4. Install the new kernel.

I'm going to focus on step 3. Get the kernel -- currently you can do that this way: (source):

git clone git://github.com/teamdesire/cm-kernel.git
cd cm-kernel
make cyanogen_bravo_defconfig
make

Apply the patch I made: qst_8250_perf_events.patch.

Rebuild the kernel according the above instructions and generate a boot.img. If you're going to do this via mkbootimg, the command-line to use is this:

mkbootimg --cmdline 'no_console_suspend=1 msmsdcc_sdioirq=1
wire.search_count=5' --kernel zImage --ramdisk boot.img-ramdisk.gz
--base 0x20000000 -o new.boot.img

Flash it from the clockwordmod recovery console by mounting the sdcard, pushing the new image using adb and then using the flash_image command:

adb shell flash_image boot /sdcard/new.boot.img

To test it you'll need perf. I was not in the mood to compile perf, so I wrote a small tool. This tool is named miniperf, but that's a rather ostentatious name because all it does is print the cycle count, sleep for five seconds, print the cycle count again, and exit.

Download it here: miniperf.tar.gz

It compiles with agcc. You will have to change the obvious in the Makefile. Note that the cycle counter is reset when you start miniperf. I have tested it for all of five minutes, so give no guarantees that this works or does anything at all. :)

Happy cycle counting!