go语言获取设备cpu golang获取硬件信息( 二 )


}
static public String getCpuString(){
if(Build.CPU_ABI.equalsIgnoreCase("x86")){
return "Intel";
}
String strInfo = "";
try
{
byte[] bs = new byte[1024];
RandomAccessFile reader = new RandomAccessFile("/proc/cpuinfo", "r");
reader.read(bs);
String ret = new String(bs);
int index = ret.indexOf(0);
if(index != -1) {
strInfo = ret.substring(0, index);
} else {
strInfo = ret;
}
}
catch (IOException ex){
ex.printStackTrace();
}
return strInfo;
}
static public String getCpuType(){
String strInfo = getCpuString();
String strType = null;
if (strInfo.contains("ARMv5")) {
【go语言获取设备cpu golang获取硬件信息】strType = "armv5";
} else if (strInfo.contains("ARMv6")) {
strType = "armv6";
} else if (strInfo.contains("ARMv7")) {
strType = "armv7";
} else if (strInfo.contains("Intel")){
strType = "x86";
}else{
strType = "unknown";
return strType;
}
if (strInfo.contains("neon")) {
strType += "_neon";
}else if (strInfo.contains("vfpv3")) {
strType += "_vfpv3";
}else if (strInfo.contains(" vfp")) {
strType += "_vfp";
}else{
strType += "_none";
}
return strType;
}
/**
* @hide
* @return
*/
public static CPUInfo getCPUInfo() {
String strInfo = null;
try
{
byte[] bs = new byte[1024];
RandomAccessFile reader = new RandomAccessFile("/proc/cpuinfo", "r");
reader.read(bs);
String ret = new String(bs);
int index = ret.indexOf(0);
if(index != -1) {
strInfo = ret.substring(0, index);
} else {
strInfo = ret;
}
}
catch (IOException ex)
{
strInfo = "";
ex.printStackTrace();
}
CPUInfo info = parseCPUInfo(strInfo);
info.mCPUMaxFreq = getMaxCpuFreq();
return info;
}
private final static String kCpuInfoMaxFreqFilePath = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
private static int getMaxCpuFreq() {
int result = 0;
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(kCpuInfoMaxFreqFilePath);
br = new BufferedReader(fr);
String text = br.readLine();
if (text != null) {
result = Integer.parseInt(text.trim());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null)
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (br != null)
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
public static class CPUInfo{
public CPUInfo(){
}
public static final int CPU_TYPE_UNKNOWN = 0x00000000;
public static final int CPU_TYPE_ARMV5TE = 0x00000001;
public static final int CPU_TYPE_ARMV6 = 0x00000010;
public static final int CPU_TYPE_ARMV7 = 0x00000100;
public static final int CPU_FEATURE_UNKNOWS = 0x00000000;
public static final int CPU_FEATURE_VFP = 0x00000001;
public static final int CPU_FEATURE_VFPV3 = 0x00000010;
public static final int CPU_FEATURE_NEON = 0x00000100;
public int mCPUType;
public int mCPUCount;
public int mCPUFeature;
public double mBogoMips;
public long mCPUMaxFreq;
}
/**
*
* @param cpuInfo
* @return
* @hide
*/
private static CPUInfo parseCPUInfo(String cpuInfo) {
if (cpuInfo == null || "".equals(cpuInfo)) {
return null;
}
CPUInfo ci = new CPUInfo();

推荐阅读