How to Read CPU Frequency on Android Device

How to read cpu frequency on android device

To have frequency on Android, just read these special files in /sys directory:

#cat "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"
#cat "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq"
#cat "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"

You will have current, min and max Frequency allowed.

How to detect android cpu speed?

The best way to do it in my opinion is to monitor the time it takes to do these actions. If it is taking too much time, then the system is too slow and you can disable fancy features until it is fast enough.

Reading the CPU speed or other specs and attempting to judge the system speed is a bad idea. Future hardware changes might make these specs meaningless.

Look at the Pentium 4 vs the Core 2 for example. Which is the faster CPU, a 2.4 GHz Pentium 4, or the 1.8 GHz Core 2? Is a 2 GHz Opteron faster than a 1.4 GHz Itanium 2? How are you going to know what kind of ARM CPU is actually faster?

To get system speed ratings for Windows Vista and 7 Microsoft actually benchmarks the machine. This is the only halfway accurate method to determine system capabilities.

It looks like a good method is to use SystemClock.uptimeMillis().

How do I determine the max frequency of the processor (Android)

For me,this code works:

String cpuMaxFreq = "";
RandomAccessFile reader = new RandomAccessFile("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", "r");
cpuMaxFreq = reader.readLine();
reader.close();

How to Get CPU usage programmatically (With the cores frequency)

You should see this SO post here.
Basically, you want to use the RandomAccessFile class to parse the /proc/stat file.

EDIT: The above solution won't work for Android O and above. See this link for more information. The Reddit user fornwall filed a report to Google about this and got:

Status: Won't Fix (Intended Behavior) Thank you for filing this bug
report.

The removal of /proc/stat was intentional. /proc/stat leaks side
channel information about applications which could allow one
application to infer the state of other applications on the device.
See
https://www.cl.cam.ac.uk/~lmrs2/publications/interrupts_pets16.pdf
for example.

EDIT2: I've prepared an example (not using CpuStasCollector)

package com.k3.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {

private static int sLastCpuCoreCount = -1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.txtv);
textView.setText("");
for (int i = 0; i < calcCpuCoreCount(); i++) {
textView.append(takeCurrentCpuFreq(i) +"\n");
}
}

private static int readIntegerFile(String filePath) {

try {
final BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream(filePath)), 1000);
final String line = reader.readLine();
reader.close();

return Integer.parseInt(line);
} catch (Exception e) {
return 0;
}
}

private static int takeCurrentCpuFreq(int coreIndex) {
return readIntegerFile("/sys/devices/system/cpu/cpu" + coreIndex + "/cpufreq/scaling_cur_freq");
}

public static int calcCpuCoreCount() {

if (sLastCpuCoreCount >= 1) {
// キャッシュさせる
return sLastCpuCoreCount;
}

try {
// Get directory containing CPU info
final File dir = new File("/sys/devices/system/cpu/");
// Filter to only list the devices we care about
final File[] files = dir.listFiles(new FileFilter() {

public boolean accept(File pathname) {
//Check if filename is "cpu", followed by a single digit number
if (Pattern.matches("cpu[0-9]", pathname.getName())) {
return true;
}
return false;
}
});

// Return the number of cores (virtual CPU devices)
sLastCpuCoreCount = files.length;

} catch(Exception e) {
sLastCpuCoreCount = Runtime.getRuntime().availableProcessors();
}

return sLastCpuCoreCount;
}
}

Get current cpu usage for each core on Android

The thread you have specified has only how to get the CPU Usage....If you read this answer carefully you can see there's method as "getCpuUsage". This method will return you the current CPU usage per each core once you pass the cpuid of the core.



Related Topics



Leave a reply



Submit