Configure governors

A CPU governor controls how the CPU raises and lowers its frequency in response to the demands the user is placing on their device. Governors have a large impact on performance and power save.

Consider the following before selecting a governor:

Governor types

You can use one of several governor types to configure the CPU, all of which are contained in a enumerator called GovernorType. Each GovernorType is also represented by a class in the API.

Governor Type Class Description
Performance GovernorType.PERFORMANCE GovernorPerformance Sets the CPU statically to the highest frequency
Powersave GovernorType.POWERSAVE GovernorPowerSave

Sets the CPU statically to the lowest frequency

Userspace GovernorType.USERSPACE GovernorUserSpace Sets the CPU statically to the user-specified frequencies
Ondemand GovernorType.ONDEMAND GovernorOnDemand

Sets the CPU depending on the current usage.

It jumps to max speed when there is load on the CPU. If the CPU load abates, it slowly steps back down through the kernel's frequency steppings until it settles at the lowest frequency.

Conservative GovernorType.CONSERVATIVE GovernorConservative

Sets the CPU depending on the current usage.

It gracefully increases and decreases the CPU speed rather than jumping to the maximum the moment there is load on the CPU.

Interactive GovernorType.INTERACTIVE GovernorInteractive

Sets the CPU depending on the current usage.

This governor is more aggressive about scaling the CPU speed up in response to CPU-intensive activity.

 

To obtain the list of available governor types supported by the CPU, use the getAvaliableGovernorTypes() method of CPUManager.

Get available governor types
import com.digi.android.system.cpu.CPUManager;
import com.digi.android.system.cpu.GovernorType;
  
[...]
  
// Get the CPU manager.
CPUManager cpuManager = new CPUManager(context);
  
// Get the available governor types.
ArrayList<GovernorType> governorTypes = cpuManager.getAvailableGovernorTypes();
for (GovernorType type: governorTypes) {
    System.out.println(type.getID() + ": " + type.getDescription());
}
  
[...]

Get and set the CPU governor

You can get and set the current CPU governor using the following methods:

Method Description
getGovernorType() Returns the governor type the CPU is configured with
getGovernor() Returns a Governor object corresponding to the governor that the CPU is configured with
setGovernorType(GovernorType) Configures the CPU with the provided governor type and returns the corresponding Governor object

If an error occurs during the reading or configuration process these methods may fail, throwing a CPUException.

Get and set current governor type
import com.digi.android.system.cpu.CPUManager;
import com.digi.android.system.cpu.Governor;
import com.digi.android.system.cpu.GovernorType;
  
[...]
 
CPUManager cpuManager = ...;
  
// Get the CPU governor type.
GovernorType governorType = cpuManager.getGovernorType();
System.out.println("Current CPU governor: " + governorType.getID());
  
// Configure the CPU to use the Powersave governor.
Governor governor = cpuManager.setGovernorType(GovernorType.POWERSAVE);
System.out.println("Configured new CPU governor: " + governor.getGovernorType());
 
[...]

The getGovernor() and setGovernorType(GovernorType) methods return a Governor object. You can cast this object to the corresponding governor class to access its specific features.

Governor classes
import com.digi.android.system.cpu.CPUManager;
import com.digi.android.system.cpu.Governor;
import com.digi.android.system.cpu.GovernorType;
  
[...]
 
CPUManager cpuManager = ...;
  
// Get the CPU governor type.
Governor governor = cpuManager.getGovernor();
  
// Cast governor depending on its type.
switch (governor.getGovernorType()) {
    case GovernorType.PERFORMANCE:
        GovernorPerformance performanceGovernor = (GovernorPerformance)governor;
        // TODO Custom implementation.
        break;
    case GovernorType.POWERSAVE:
        GovernorPowerSave powerSaveGovernor = (GovernorPowerSave)governor;
        // TODO Custom implementation.
        break;
    case GovernorType.USERSPACE:
        GovernorUserSpace userSpaceGovernor = (GovernorUserSpace)governor;
        // TODO Custom implementation.
        break;
    case GovernorType.ONDEMAND:
        GovernorOnDemand onDemandGovernor = (GovernorOnDemand)governor;
        // TODO Custom implementation.
        break;
    case GovernorType.CONSERVATIVE:
        GovernorConservative conservativeGovernor = (GovernorConservative)governor;
        // TODO Custom implementation.
        break;
    case GovernorType.INTERACTIVE:
        GovernorInteractive interactiveGovernor = (GovernorInteractive)governor;
        // TODO Custom implementation.
        break;
    default:
        // TODO Custom implementation.
        break;
}
 
[...]

Configure governor parameters

Some governors are configurable and specify different parameters that can be read and set.

Performance, powersave, and userspace governors do not define any parameter. They just configure the CPU statically to the maximum, minimum, or user-specified frequency respectively when they are selected.

The configurable governors are:

All of them define some common and specific accessible parameters:

Configure governor parameters
import com.digi.android.system.cpu.CPUManager;
import com.digi.android.system.cpu.GovernorType;
import com.digi.android.system.cpu.GovernorInteractive;
  
[...]
 
CPUManager cpuManager = ...;
 
// Set the interactive governor.
GovernorInteractive interactiveGovernor = (GovernorInteractive)cpuManager.setGovernorType(GovernorType.INTERACTIVE);
  
// Configure the governor.
interactiveGovernor.setMinSampleTime(80000);
interactiveGovernor.setTimerRate(20000);
  
[...]
  
// Read some governor parameters.
System.out.println("High speed frequency: " + interactiveGovernor.getHiSpeedFreq());
System.out.println("Boost enabled: " + interactiveGovernor.getBoost());
 
[...]
Example: CPU management

The CPU Management Sample Application demonstrates the CPU API. In this example, you can enable and disable the CPU cores, configure frequencies, and select and set up the governor while monitoring the total CPU usage as well as the usage of each core.

 

You can import the example using Digi's Android Studio plugin. For more information, see Import a Digi sample application. To view the application source code, go to the GitHub repository.