Skip to content

Commit

Permalink
feat(cpus): add information about cpus
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Del Pino <idelpino@teclib.com>
  • Loading branch information
Ivan Del Pino authored and rafaelje committed Sep 27, 2018
1 parent c3d5af7 commit 963fac5
Showing 1 changed file with 18 additions and 35 deletions.
53 changes: 18 additions & 35 deletions inventory/src/main/java/org/flyve/inventory/categories/Cpus.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
package org.flyve.inventory.categories;

import android.content.Context;
import android.os.Build;

import org.flyve.inventory.FILog;
import org.flyve.inventory.Utils;
Expand All @@ -38,13 +37,11 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Map;
import java.util.regex.Pattern;

/**
* This class get all the information of the Cpus
Expand Down Expand Up @@ -87,6 +84,7 @@ public Cpus(Context xCtx) {
c.put("MODEL", new CategoryValue(getModel(), "MODEL", "model"));
c.put("NAME", new CategoryValue(getCpuName(), "NAME", "name"));
c.put("SPEED", new CategoryValue(getCpuFrequency(), "SPEED", "cpuFrequency"));
c.put("THREAD", new CategoryValue(getCpuThread(), "THREAD", "thread"));

this.add(c);

Expand All @@ -96,50 +94,27 @@ public Cpus(Context xCtx) {

}

/**
* Get the CPU Architecture
*
* @return String with the Cpu Architecture
*/
public String getArch() {
return System.getProperty("os.arch");
}

/**
* Get the CPU Number Core
*
* @return String with the Cpu Core
*/
public String getCPUCore() {
if (Build.VERSION.SDK_INT >= 17) {
return String.valueOf(Runtime.getRuntime().availableProcessors());
} else {
return String.valueOf(getNumCoresOldPhones());
int i = 0;
String a = Utils.getCatInfo("/sys/devices/system/cpu/present");
if (a != null && a.contains("-")) {
i = Integer.parseInt(a.split("-")[1]);
}
return String.valueOf(++i);
}

/**
* Gets the number of cores available in this device, across all processors.
* Get the CPU Architecture
*
* @return The number of cores, or 1 if failed to get result
* @return String with the Cpu Architecture
*/
private int getNumCoresOldPhones() {
class CpuFilter implements FileFilter {
@Override
public boolean accept(File pathname) {
return Pattern.matches("cpu[0-9]+", pathname.getName());
}
}

try {
File dir = new File("/sys/devices/system/cpu/");
File[] files = dir.listFiles(new CpuFilter());
//Return the number of cores (virtual CPU devices)
return files.length;
} catch (Exception e) {
//Default to return 1 core
return 1;
}
public String getArch() {
return System.getProperty("os.arch");
}

/**
Expand Down Expand Up @@ -284,4 +259,12 @@ public String getCpuFrequency() throws IOException {
}
return cpuFrequency;
}

/**
* Gets the number of threads available in this device.é
* @return The number of threads
*/
private String getCpuThread() {
return String.valueOf(Runtime.getRuntime().availableProcessors());
}
}

0 comments on commit 963fac5

Please sign in to comment.