public class CPUManager
extends java.lang.Object
This manager allows applications to enable/disable the CPU cores, configure the different frequency values and set the CPU governor.
Unless noted, all CPU API methods require the
com.digi.android.permission.CPU
permission. If your application does
not have this permission it will not have access to any CPU service
feature.
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_USAGE_ELAPSE
Default timeout get usage calls block:
300 milliseconds.
|
static int |
MINIMUM_TEMP_INTERVAL
Minimum interval between temperature reads:
1000 milliseconds.
|
Constructor and Description |
---|
CPUManager(android.content.Context context)
Class constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
boostPulse()
Immediately boosts the speed of all CPUs to high speed frequency for the
period of time specified by the 'Boost pulse duration' property of the
'interactive' governor.
|
void |
disableCore(int coreIndex)
Disables the provided core.
|
void |
enableCore(int coreIndex)
Enables the provided core.
|
java.util.ArrayList<java.lang.Integer> |
getAvailableFrequencies()
Returns a list with the available frequencies in KHz.
|
java.util.ArrayList<GovernorType> |
getAvailableGovernorTypes()
Returns a list with the available governor types supported by the CPU.
|
float |
getCoreUsage(int coreIndex)
Returns the usage of the provided core, in percent.
|
float |
getCoreUsage(int coreIndex,
long elapse)
Returns the usage of the provided core, in percent.
|
float |
getCPUUsage()
Returns the total CPU usage, in percent.
|
float |
getCPUUsage(long elapse)
Returns the total CPU usage, in percent.
|
float |
getCriticalTemperature()
Retrieves the critical CPU temperature value.
|
float |
getCurrentTemperature()
Retrieves the current CPU temperature.
|
int |
getFrequency()
Returns the current frequency (in kHz) of the CPU.
|
Governor |
getGovernor()
Returns the governor that the CPU is configured with.
|
GovernorType |
getGovernorType()
Returns the governor type the CPU is configured with.
|
float |
getHotTemperature()
Retrieves the hot CPU temperature value.
|
int |
getMaxFrequency()
Returns the maximum frequency (in kHz) that the CPU supports.
|
int |
getMaxScalingFrequency()
Returns the maximum frequency (in kHz) the device may scale the CPU to.
|
int |
getMinFrequency()
Returns the minimum frequency (in kHz) that the CPU supports.
|
int |
getMinScalingFrequency()
Returns the minimum frequency (in kHz) the device may scale the CPU to.
|
int |
getNumberOfCores()
Returns the number of cores available in the device.
|
int |
getTransitionLatency()
Returns the transition latency of the CPU in nano seconds (10^⁻9).
|
java.util.ArrayList<java.lang.Float> |
getUsage()
Returns a list with all the CPU usage percentages (from CPU and cores).
|
java.util.ArrayList<java.lang.Float> |
getUsage(long elapse)
Returns a list with all the CPU usage percentages (from CPU and cores).
|
boolean |
isCoreEnabled(int coreIndex)
Returns whether the provided core is enabled or disabled.
|
void |
registerListener(ICPUTemperatureListener listener,
long interval)
Registers for temperature updates using the given time interval.
|
float |
setCriticalTemperature(float temp)
Sets the critical CPU temperature value.
|
void |
setFrequency(int frequency)
Configures the CPU with a custom frequency (in kHz).
|
Governor |
setGovernorType(GovernorType governorType)
Configures the CPU with the provided governor type.
|
float |
setHotTemperature(float temp)
Sets the hot CPU temperature value.
|
void |
setMaxScalingFrequency(int frequency)
Sets the maximum frequency (in kHz) the device may scale the CPU to.
|
void |
setMinScalingFrequency(int frequency)
Sets the minimum frequency (in kHz) the device may scale the CPU to.
|
void |
unregisterListener(ICPUTemperatureListener listener)
Removes all temperature updates for the specified listener.
|
public static final int MINIMUM_TEMP_INTERVAL
public static final int DEFAULT_USAGE_ELAPSE
public CPUManager(android.content.Context context)
Instantiates a new CPUManager
for the given application.
context
- Context of the application.public int getNumberOfCores()
enableCore(int)
,
disableCore(int)
,
isCoreEnabled(int)
public void enableCore(int coreIndex) throws NoSuchCoreException, CPUException
coreIndex
- The index of the core to enable.CPUException
- If there is an error during the configuration
process.java.lang.IllegalArgumentException
- If coreIndex < 1
.NoSuchCoreException
- If the core does not exist.disableCore(int)
,
getNumberOfCores()
,
isCoreEnabled(int)
public void disableCore(int coreIndex) throws NoSuchCoreException, CPUException
coreIndex
- The index of the core to disable.CPUException
- If there is an error during the configuration
process.java.lang.IllegalArgumentException
- If coreIndex < 1
.NoSuchCoreException
- If the core does not exist.enableCore(int)
,
getNumberOfCores()
,
isCoreEnabled(int)
public boolean isCoreEnabled(int coreIndex) throws NoSuchCoreException, CPUException
coreIndex
- The index of the core to check if it is enabled.true
if the core is enabled, false
otherwise.CPUException
- If there is an error checking for the core status.NoSuchCoreException
- If the core does not exist.enableCore(int)
,
disableCore(int)
,
getNumberOfCores()
public float getCPUUsage() throws CPUException
DEFAULT_USAGE_ELAPSE
milliseconds.CPUException
- If there is an error during the configuration
process.DEFAULT_USAGE_ELAPSE
,
getCoreUsage(int)
,
getCoreUsage(int, long)
,
getCPUUsage(long)
,
getUsage()
,
getUsage(long)
public float getCPUUsage(long elapse) throws CPUException
elapse
- The time in milliseconds between reads.CPUException
- If there is an error getting the usage.DEFAULT_USAGE_ELAPSE
,
getCoreUsage(int)
,
getCoreUsage(int, long)
,
getCPUUsage()
,
getUsage()
,
getUsage(long)
public float getCoreUsage(int coreIndex) throws NoSuchCoreException, CPUException
DEFAULT_USAGE_ELAPSE
milliseconds.coreIndex
- The index of the core to read its usage.CPUException
- If there is an error getting the usage.NoSuchCoreException
- If the core does not exist.DEFAULT_USAGE_ELAPSE
,
getCoreUsage(int, long)
,
getCPUUsage()
,
getCPUUsage(long)
,
getUsage()
,
getUsage(long)
public float getCoreUsage(int coreIndex, long elapse) throws NoSuchCoreException, CPUException
coreIndex
- The index of the core to read its usage.elapse
- The time in milliseconds between reads.CPUException
- If there is an error getting the usage.NoSuchCoreException
- If the core does not exist.DEFAULT_USAGE_ELAPSE
,
getCoreUsage(int)
,
getCPUUsage()
,
getCPUUsage(long)
,
getUsage()
,
getUsage(long)
public java.util.ArrayList<java.lang.Float> getUsage() throws CPUException
DEFAULT_USAGE_ELAPSE
milliseconds.
null
if the he values are not available.CPUException
- If there is an error getting the usage.DEFAULT_USAGE_ELAPSE
,
getCoreUsage(int)
,
getCoreUsage(int, long)
,
getCPUUsage()
,
getCPUUsage(long)
,
getUsage(long)
public java.util.ArrayList<java.lang.Float> getUsage(long elapse) throws CPUException
elapse
- The time in milliseconds between reads.null
if the he values are not available.CPUException
- If there is an error getting the usage.DEFAULT_USAGE_ELAPSE
,
getCoreUsage(int)
,
getCoreUsage(int, long)
,
getCPUUsage()
,
getCPUUsage(long)
,
getUsage()
public java.util.ArrayList<java.lang.Integer> getAvailableFrequencies() throws CPUException
CPUException
- If there is an error during the configuration
process.getFrequency()
,
getMaxFrequency()
,
getMaxScalingFrequency()
,
getMinFrequency()
,
getMinScalingFrequency()
,
setFrequency(int)
,
setMaxScalingFrequency(int)
,
setMinScalingFrequency(int)
public void setFrequency(int frequency) throws UnsupportedCommandException, CPUException
This operation can be performed only if the CPU is configured with a 'userspace' governor.
frequency
- The frequency that the CPU is configured with in kHz.CPUException
- If there is an error during the configuration
process.java.lang.IllegalArgumentException
- If frequency > getMaxFrequency()
or frequency < getMinFrequency()
or the frequency is not contained in the
list of available frequencies.UnsupportedCommandException
- If the GovernorType
configured in the CPU is different
from GovernorType.USERSPACE
.getAvailableFrequencies()
,
getFrequency()
,
getMaxFrequency()
,
getMaxScalingFrequency()
,
getMinFrequency()
,
getMinScalingFrequency()
,
setMaxScalingFrequency(int)
,
setMinScalingFrequency(int)
public int getFrequency() throws CPUException
CPUException
- If there is an error during the configuration
process.getAvailableFrequencies()
,
getMaxFrequency()
,
getMaxScalingFrequency()
,
getMinFrequency()
,
getMinScalingFrequency()
,
setFrequency(int)
,
setMaxScalingFrequency(int)
,
setMinScalingFrequency(int)
public int getMaxFrequency() throws CPUException
CPUException
- If there is an error during the configuration
process.getAvailableFrequencies()
,
getFrequency()
,
getMaxScalingFrequency()
,
getMinFrequency()
,
getMinScalingFrequency()
,
setFrequency(int)
,
setMaxScalingFrequency(int)
,
setMinScalingFrequency(int)
public int getMinFrequency() throws CPUException
CPUException
- If there is an error during the configuration
process.getAvailableFrequencies()
,
getFrequency()
,
getMaxFrequency()
,
getMaxScalingFrequency()
,
getMinScalingFrequency()
,
setFrequency(int)
,
setMaxScalingFrequency(int)
,
setMinScalingFrequency(int)
public int getMinScalingFrequency() throws CPUException
CPUException
- If there is an error during the configuration
process.getAvailableFrequencies()
,
getFrequency()
,
getMaxFrequency()
,
getMaxScalingFrequency()
,
getMinFrequency()
,
setFrequency(int)
,
setMaxScalingFrequency(int)
,
setMinScalingFrequency(int)
public void setMinScalingFrequency(int frequency) throws CPUException
frequency
- The minimum scaling frequency in kHz.CPUException
- If there is an error during the configuration
process.java.lang.IllegalArgumentException
- If frequency > getMaxFrequency()
or frequency < getMinFrequency()
or frequency > getMaxScalingFrequency()
or the frequency is not contained in the
list of available frequencies.getAvailableFrequencies()
,
getFrequency()
,
getMaxFrequency()
,
getMaxScalingFrequency()
,
getMinFrequency()
,
getMinScalingFrequency()
,
setFrequency(int)
,
setMaxScalingFrequency(int)
public int getMaxScalingFrequency() throws CPUException
CPUException
- If there is an error during the configuration
process.getAvailableFrequencies()
,
getFrequency()
,
getMaxFrequency()
,
getMinFrequency()
,
getMinScalingFrequency()
,
setFrequency(int)
,
setMaxScalingFrequency(int)
,
setMinScalingFrequency(int)
public void setMaxScalingFrequency(int frequency) throws CPUException
frequency
- The maximum scaling frequency in kHz.CPUException
- If there is an error during the configuration
process.java.lang.IllegalArgumentException
- If frequency > getMaxFrequency()
or frequency < getMinFrequency()
or frequency < getMinScalingFrequency()
or the frequency is not contained in the
list of available frequencies.getAvailableFrequencies()
,
getFrequency()
,
getMaxFrequency()
,
getMaxScalingFrequency()
,
getMinFrequency()
,
getMinScalingFrequency()
,
setFrequency(int)
,
setMinScalingFrequency(int)
public java.util.ArrayList<GovernorType> getAvailableGovernorTypes() throws CPUException
CPUException
- If there is an error during the configuration
process.getGovernor()
,
getGovernorType()
,
setGovernorType(GovernorType)
,
GovernorType
public Governor setGovernorType(GovernorType governorType) throws CPUException
governorType
- The GovernorType
to configure in the CPU.Governor
corresponding to the provided
GovernorType
and that has been set in the manager.CPUException
- If there is an error during the configuration
process.java.lang.IllegalArgumentException
- If the governor type is not supported
by the CPU,
(see getAvailableGovernorTypes()
).getAvailableGovernorTypes()
,
getGovernor()
,
getGovernorType()
,
Governor
,
GovernorType
public GovernorType getGovernorType() throws CPUException
GovernorType
that the CPU is configured with.CPUException
- If there is an error during the configuration
process.getAvailableGovernorTypes()
,
getGovernor()
,
setGovernorType(GovernorType)
,
GovernorType
public Governor getGovernor() throws CPUException
GovernorType.UNKNOWN
if the governor type does not match
any of the known types.CPUException
- If there is an error during the configuration
process.getAvailableGovernorTypes()
,
getGovernorType()
,
setGovernorType(GovernorType)
,
Governor
,
GovernorType
public int getTransitionLatency() throws CPUException
If it is unknown or known to be that high that the driver does not work with the 'ondemand' governor, it returns -1.
CPUException
- If there is an error during the configuration
process.public void boostPulse() throws UnsupportedCommandException, CPUException
This operation can be performed only if the CPU is configured with 'interactive' governor.
CPUException
- If there is an error during the configuration
process.UnsupportedCommandException
- If the GovernorType
configured in the CPU is different
from GovernorType.INTERACTIVE
.GovernorInteractive.getBoostPulseDuration()
,
GovernorInteractive.setBoostPulseDuration(long)
public float getCurrentTemperature() throws CPUTemperatureException
CPUTemperatureException
- If there is an error reading the current
temperature.public float getHotTemperature() throws CPUTemperatureException
Hot CPU temperature is the temperature limit at which system will reduce CPU and GPU frequency to avoid system overheating.
CPUTemperatureException
- If there is an error reading the hot
temperature.setHotTemperature(float)
public float setHotTemperature(float temp) throws CPUTemperatureException
Hot CPU temperature is the temperature limit at which system will reduce CPU and GPU frequency to avoid system overheating.
Note that in some scenarios there might be problems setting the requested temperature. Use the returned value to verify the temperature that was finally set.
temp
- New hot CPU temperature value.CPUTemperatureException
- If there is an error setting the hot
temperature.java.lang.IllegalArgumentException
- If the given temperature is equal or
greater than the critical temperature.getCriticalTemperature()
,
getHotTemperature()
public float getCriticalTemperature() throws CPUTemperatureException
Critical CPU temperature is the temperature limit at which system will halt to avoid system damage caused by overheating. This always occurs after reaching hot temperature.
CPUTemperatureException
- If there is an error reading the critical
temperature.setCriticalTemperature(float)
,
getHotTemperature()
public float setCriticalTemperature(float temp) throws CPUTemperatureException
Critical CPU temperature is the temperature limit at which system will halt to avoid system damage caused by overheating. This always occurs after reaching hot temperature.
Note that in some scenarios there might be problems setting the requested temperature. Use the returned value to verify the temperature that was finally set.
temp
- New critical CPU temperature value.CPUTemperatureException
- If there is an error setting the critical
temperature.java.lang.IllegalArgumentException
- If the given temperature is equal or
lesser than the hot temperature.getCriticalTemperature()
,
getHotTemperature()
public void registerListener(ICPUTemperatureListener listener, long interval)
listener
- A ICPUTemperatureListener
whose
ICPUTemperatureListener.onTemperatureUpdate(float)
method will be called for each temperature update.interval
- Time interval between temperature updates, in milliseconds.java.lang.IllegalArgumentException
- If interval < MINIMUM_TEMP_INTERVAL
.java.lang.NullPointerException
- If listener == null
.MINIMUM_TEMP_INTERVAL
,
getCurrentTemperature()
,
unregisterListener(ICPUTemperatureListener)
,
ICPUTemperatureListener
public void unregisterListener(ICPUTemperatureListener listener)
Following this call, updates will no longer occur for this listener.
listener
- Listener object that no longer needs temperature updates.java.lang.NullPointerException
- If listener == null
.registerListener(ICPUTemperatureListener, long)
,
ICPUTemperatureListener