I am trying to get info about smartphone's CPU. I can get that from displaying /proc/cpuinfo file. However its output is poor and unreadable. I would like to have output like from "lscpu" command but when I execute command "lscpu" from my app I have no permissions to do that (however I can do that from Termux console on my phone and output is readable). Do you have any ideas how to get human readable?
Here is code to view /proc/cpuinfo file:
static class CMDExecute {
synchronized String run(String[] cmd)
throws IOException {
String result = "";
try {
ProcessBuilder builder = new ProcessBuilder(cmd);
builder.redirectErrorStream(true);
Process process = builder.start();
InputStream in = process.getInputStream();
byte[] re = new byte[1024];
while (in.read(re) != -1) {
System.out.println(new String(re));
result = result + new String(re);
}
in.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
}
private static StringBuffer buffer;
public static void fetch_cpu_info() {
String result = null;
CMDExecute cmdexe = new CMDExecute();
try {
String[] args = {"/system/bin/cat", "/proc/cpuinfo"};
result = cmdexe.run(args);
System.out.println("RESULT\n" + result);
} catch (IOException ex) {
ex.printStackTrace();
}
}
and its output:
I/System.out: RESULT
processor : 0
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt lpae evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 1
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt lpae evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 2
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt lpae evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 3
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt lpae evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
As you can see it's very poor output. When I try to perform "lscpu" command from my code, changing only one line:
String[] args = {"/system/bin/cat", "/proc/cpuinfo"};
to
String[] args = {"lscpu"};
I am getting error:
W/System.err: java.io.IOException: Cannot run program "lscpu": error=13, Permission denied
However, I have application Termux on my phone. It's shell console and I don't know why but I can perform "lscpu" in this console (I cannot from app code). Check this out:
I would like to get that output into my app. Do you have any ideas how to get that nice human readable output about CPU info?
I'd appreciate any help!
OK. I was able to solve the problem for the most part. Focusing on /proc/cpuinfo only was not good idea. Instead, I managed to get interesting hardware info like that:
Get smartphone manufacturer and model. In my case the output is: "samsung SM-J730F":
String manufacturer = Build.MANUFACTURER;
String model = Build.MODEL;
This command shows number of CPU cores:
Runtime.getRuntime().availableProcessors()
From this file you can get your CPU frequency. Remember to use .trim() because this file may contain whitespace character like it was in my case and I had problem to convert result to float for further operations:
("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq").trim()
File containing number of online CPUs:
/sys/devices/system/cpu/online
File containing number of offline CPUs. If all CPUs are online, this file seems to be empty but it may contain whitespace char:
("/sys/devices/system/cpu/offline").trim()
Command to get architecture:
System.getProperty("os.arch")
With all of this I created nice, human readable output. I guess this topic can be closed. Best Regards!