Skip to content

Linux/Ubuntu CPU Perf Scaling with Modern Intel CPUs

I’m posting this largely because all the documentation I can find, and the discussions around this appear to be out of date, or at least not entirely accurate.

I run a Ubuntu server as my home NAS, storage server, general do things host, and for development work on some sites I maintain. It’s built around an Intel Xeon E3-1220 v2, 16 GB of DDR3 RAM and storage running on ZFS.

By default, Ubuntu runs a process on boot called ondemand (/etc/init.d/ondemand). The process is simple enough, it’s a 73 line shell script that basically looks at the available CPU governors (/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors).

It then looks at what governors are available and if either “interactive”, “ondemand”, or “powersave” are, it sets those governors in that order.

So for example, if you have an E3-1220 v2; then the output of /sys/.../scaling_available_governors will be powersave perforamnce.

Since powersave shows up in the available list, it will then echo that to /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor.

So far all this seems reasonable, in a sense, and for older CPUs or maybe AMD or ARM cpus (I don’t know about this one for sure, as I don’t have any AMD or ARM systems) this may be the ideal way to go about getting the clocks on the CPU to scale dynamically.

Which brings me to the documentation. According to this (arch linux wiki) page, and the Linux kernel documentation the governors should behave roughly as follows:

Governor Behavior
powersave Run the CPU at the minimum clock frequency
ondemand Scale the CPU’s clock according to load; Jumps to highest frquency first, then scales the clocks back as idle time increases
performance Run the CPU at the maximum clock frequency

Conventionally, unless you had a better plan you’d want to run the CPU with the ondemand governor, as that would provide the most performance with the most power savings.

Since Intel released SandyBridge, they’ve also provided a p_state driver that controls the performance of the clock speeds of their CPUs. With the p_state driver running an Intel CPU will always dynamically scale it’s clock speed, regardless of the Linux governor set. However, changing the governor does have a measurable impact on the response rate for the p_state changes.

Which brings me to the point of this post. By default, the Ubuntu system sets the cpu governor to powersave every boot, and while this might be an ideal solution for a laptop or desktop as it would likely produce the lowest possible load, it also has the slowest switching delay.

How does this impact actual performance?

While not a comprehensive test, here’s an example of what I’m seeing. Loading the home page of a custom WordPress theme. This is running the latest version of WordPress, MySQL 5.7, and PHP 7.2 with opcaching enable and the cache primed.

Governor Render Time # SQL Queries
powersave 70 ms (averaged over 5 runs) 17
perforance 38 ms (averaged over 5 runs) 17

And the CPU clock rate is still scaling: through out this test, the clock rates of the cores ranged from 1.6 GHz to 3.5 GHz.

Now presumably the reason for wanting to use the powersave governor is to save power. The easiest way I can currently gather power data is by looking at my UPS’s statistics. (Yes, I know there are other power counters available in Linux itself, but to use them would require more work than I’m willing to invest in this.)

According to my UPS, with the CPU set to either state, and the system idle, the system as a whole is using 7% of the UPS’s capacity (works out to 60W total load, though this includes the system itself, and smattering of network equipment [Edge Router X, 16-port gig-e switch, WAP, and DSL Modem] too).

Note, I only care about power consumption at idle. Under load, in either case the CPU’s clocks and power consumption scaled up to a higher value and will consume more power.

The point here is that with modern Intel CPUs, better performance, especially in terms of responsiveness, at similar idle power levels is achieviable by running the performance governor. Moreover, because the p_state driver is modifying the clock speeds, the documented behaviors of the performance and powersave governors doesn’t really apply.

In Ubuntu, if you want to turn the governors off I used this procedure:

  1. Install cpufrequtils (sudo apt-get install cpufrequtils)
  2. Set the default governor for CPU frequtils by creating the file /etc/defaults/cpufrequtils and setting the contents to GOVERNOR="performance"
  3. Disable the ondemand daemon by running sudo update-rc.d ondemand disable
  4. Reboot (or set the governor manually)
Published inComputersLinux