## Friday, December 10, 2010

### Problems with cooling the MacBook Pro (1,2)

I have a first generation MacBook Pro which, after living in shame with OS X for several years, I finally bit the bullet and installed Ubuntu on it. And it was certainly biting a bullet; Apple computers are some of the least supported computers on the market when it comes to Ubuntu and the Linux kernel in general. I imagine this is strongly linked with the closed nature of their design. Despite this, the gains I have seen in capability of the machine far outweigh the annoyances of the setup, the reduced battery life, and when using 9.04, the many graphics errors that made compiz and anything with OpenGL unusable (the new ATI drivers are much better, at least for me).

One of the most annoying problems, however, is that (at least) the first generation MacBook Pros run very hot with Ubuntu. Largely this seems to be due to a bug in the fan speed control. It does increase the fan speed with temperature, but it seems that the temperature of the CPU needs to reach upwards of 100 degrees C before you will notice the fans kicking in. At 100 degrees C the outside case (aluminum and often in contact with your skin) may reach an uncomfortable temperature. I suspect that this might be the result of the fan control module reading its temperature data from the wrong sensor, i.e. scaling the fan based on the case temperature instead of the CPU, but let's ignore the root of the problem and consider how to fix it.

Several people have come up with methods of controlling the temperature, like this. I wrote my own because I thought this was too complicated, but mine has grown in complexity with time. I just have this running in the background as root.

#!/bin/bash

let 'speed=1000'

while true
do
temp=cat /sys/devices/platform/applesmc.768/temp3_input
let 'new_speed=(1000 + (5000*((temp-35000)/(100*(60-30))))/10)'
if [ $new_speed -lt$speed ]
then
# Go down in speed quickly
let 'speed=new_speed'
fi
# Average to provide some damping
let 'speed=(speed+new_speed)/2'
if [ $speed -gt 6000 ]; then speed=6000; fi if [$speed -lt 1000 ]; then speed=1000; fi
echo $speed > /sys/devices/platform/applesmc.768/fan1_min echo$speed > /sys/devices/platform/applesmc.768/fan2_min
sleep 2
done 

But even this wasn't enough to keep the temperature under control. I have read in many places that the maximum fan speed for the MacBook Pro (1,2) is 6000 RPM (although it seems to go up to 7000 RPM). Even when the fan speed is pegged at 6000 RPM, one process at 100% CPU will raise the temperature up to 100 degrees C (already too hot for laptop work), and anything running on the other core will often times raise the temperature enough to cause the system to shutdown from overheating. This is a big problem: it seems that the fans on the MacBook Pro (1,2) are not able to cool the CPU appropriately at maximum load.

Today I finally found a way to keep the temperature under control: by changing the CPU scaling behavior to powersave mode. This basically locks each core to its minimal clock frequency, or 1 GHz for me. Interestingly, this doesn't seem to extend my battery life at all. What it does do is hold the CPU temperature at below 60 degrees C when the above script is despite the system load. Of course, CPU bound programs run at half the speed.

Never mind the matter of why Ubuntu's built in thermal throttling isn't kicking in, setting the powersave governor will allow us to script our own thermal throttling. I plan, in the near future, to hack together a solution using cpufreq-selector that will switch the CPU scaling governor when the system gets too hot or if the system is running on battery power (as stated earlier, this isn't for extended battery life. It's just that when I am on battery power it is more likely that the computer is sitting on my lap or I'm in a meeting and a noisy fan would bother others). This sort of thing makes me wonder if Apple used thermal throttling when they configured OS X for the first gen MBPros. The computer certainly ran hot in OS X, but it never crashed due to overheating. The current CPU frequency was not readily apparent from with OS X (or at least I didn't see it). I wonder if all those times I was running at maximum CPU, I was actually running at 1 GHz or 1.33 GHz so that the system wouldn't shutdown. Makes me pretty happy that my place of work went with my suggestion to buy the lowest clock speed computer. I could probably test this by booting into OS X and running a benchmark and comparing to the result under Ubuntu.

I did a little research and it seems that the Core2 Duos dropped support for ACPI Thermal Zone, which is responsible for thermal throttling. The first gen MBP has a Core Duo, so maybe it is similar. Turns out you can switch between thermal throttling CPU states by writing the desired state (say T1) to /proc/acpi/processor/CPU0/throttling. Even at the most limiting throttle states, the CPU still runs hotter than running with the powersave governor at T0. That doesn't make a lot of sense.