In Linux, there will be some file like this, to show the CPUs frequency.
/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq
...
Run adb command:
$ adb shell ls /sys/devices/system/cpu/
It will show some directory named cpu0, cpu1..., it is the number of cpu in your system.
Run adb command, to show the current frequency of cpu0:
$ adb shell cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
package com.example.androidcpu;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Pattern;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
TextView textAvProc, textNumOfCpu, textMsg;
Button btnReadCpuFreq;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textAvProc = (TextView)findViewById(R.id.avproc);
textNumOfCpu = (TextView)findViewById(R.id.numofcpu);
textMsg = (TextView)findViewById(R.id.msg);
btnReadCpuFreq = (Button)findViewById(R.id.readfreq);
Runtime runtime = Runtime.getRuntime();
int availableProcessors = runtime.availableProcessors();
textAvProc.setText("availableProcessors = " + availableProcessors);
btnReadCpuFreq.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
readCpuFreqNow();
}});
readCpuFreqNow();
}
private void readCpuFreqNow(){
File[] cpuFiles = getCPUs();
textNumOfCpu.setText("number of cpu: " + cpuFiles.length);
String strFileList = "";
for(int i=0; i<cpuFiles.length; i++){
String path_scaling_cur_freq =
cpuFiles[i].getAbsolutePath()+"/cpufreq/scaling_cur_freq";
String scaling_cur_freq = cmdCat(path_scaling_cur_freq);
strFileList +=
i + ": "
+ path_scaling_cur_freq + "\n"
+ scaling_cur_freq
+ "\n";
}
textMsg.setText(strFileList);
}
//run Linux command
//$ cat f
private String cmdCat(String f){
String[] command = {"cat", f};
StringBuilder cmdReturn = new StringBuilder();
try {
ProcessBuilder processBuilder = new ProcessBuilder(command);
Process process = processBuilder.start();
InputStream inputStream = process.getInputStream();
int c;
while ((c = inputStream.read()) != -1) {
cmdReturn.append((char) c);
}
return cmdReturn.toString();
} catch (IOException e) {
e.printStackTrace();
return "Something Wrong";
}
}
/*
* Get file list of the pattern
* /sys/devices/system/cpu/cpu[0..9]
*/
private File[] getCPUs(){
class CpuFilter implements FileFilter {
@Override
public boolean accept(File pathname) {
if(Pattern.matches("cpu[0-9]+", pathname.getName())) {
return true;
}
return false;
}
}
File dir = new File("/sys/devices/system/cpu/");
File[] files = dir.listFiles(new CpuFilter());
return files;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.androidcpu.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<TextView
android:id="@+id/avproc"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/readfreq"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Read CPUs Frequency now"/>
<TextView
android:id="@+id/numofcpu"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Related:
- Java SE version on Raspberry Pi and Linux
2 comments:
Good but does it run with non root privileges?
Thanks for the code snippet
Yes it runs without root previleges, cpu_cur_freq would require root permission though
Post a Comment