Wednesday, 5 December 2012

Manage CPU cores in Linux

This is going to be a short post. There are two things I need to do frequently with manycores machines that I have:

* Switch them on/off to perform experiments in a more controlled environment. 
* Scale their frequency to match their performance.  

For first, there is a simple trick. All CPU cores in linux show up in sysfs 

#ls /sys/devices/system/cpu/
cpu0/       cpu1/       cpu2/       cpu3/       cpufreq/    cpuidle/    kernel_max  microcode/  modalias    offline     online      possible    present     probe       release     uevent      

As you can see there are 4 directories for 4 core system that I have. In each of the core* directories I have

#ls /sys/devices/system/cpu/cpu0/
cache  cpufreq crash_notes  microcode node0  online  subsystem thermal_throttle  topology  uevent

So one just have to echo 1 (for ON) or 0 (for OFF) into online file. This is a sysfs file, writing to which triggers some action inside kernel. In our case this would be switching off the CPU. 

#echo 0 > /sys/devices/system/cpu/cpu0/online

For this to work, you kernel must support dynamic hotplugging of CPUs. Mind that CPU0 has a special status and you can not switch if off. Although, Linux is smart enough when running on a single core, it switches to uniprocessor (UP) code. 

For second, you need to have associated processor power state driver. Most of the modern Intel processor can work with P state acpi_cpufreq.ko driver. For other driver options check in: 

'make menuconfig' -> Power Management and ACPI Options -> CPU Frequency Scaling
-> x86 CPU Frequency Scaling Drivers  

Now get the driver in. To regulate frequency from userspace you need a userspace tool called 'cpufrequtils'. Install it. With the driver and tool in, we get something like: 

cpufrequtils 007: cpufreq-info (C) Dominik Brodowski 2004-2009
Report errors and bugs to, please.
analyzing CPU 0:
  driver: acpi-cpufreq
  CPUs which run at the same hardware frequency: 0 1 2 3
  CPUs which need to have their frequency coordinated by software: 0
  maximum transition latency: 10.0 us.
  hardware limits: 1.60 GHz - 2.53 GHz
  available frequency steps: 2.53 GHz, 2.39 GHz, 2.26 GHz, 2.13 GHz, 2.00 GHz, 1.86 GHz, 1.73 GHz, 1.60 GHz
  available cpufreq governors: conservative, ondemand, userspace, performance
  current policy: frequency should be within 1.60 GHz and 2.53 GHz.
                  The governor "userspace" may decide which speed to use
                  within this range.
  current CPU frequency is 1.86 GHz (asserted by call to hardware).

As you can it there are multiple frequencies I can choose from. You can not arbitrarily set one frequency, it has to be one from the set. To set the frequency: 

#cpufreq-set -c 0 -f 1.8GHz

where -C is core number. And -f is frequency. 

And thats about it. For more details see Linux kernel documentation at 

and most of other ACPI and power/performance related options are in 
'make menuconfig' -> Power Management and ACPI Options



  1. The major clients of such service providers are advertising and marketing agencies,CPU
    photography studios, graphic design studios, web designers and in some cases
    printing companies and lithographers too.

  2. This is an amazing post you have shared with us . I appreciate you excellent post . Remvoe White Background | Clipping Path | Product Photo Editing