Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #16 from lozzle/master
Browse files Browse the repository at this point in the history
Added a few more methods to detect number of CPU cores on device
  • Loading branch information
zlern2k committed May 18, 2016
2 parents 3c12f8f + 5aaff34 commit d590884
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public class DeviceInfo {
public static final int DEVICEINFO_UNKNOWN = -1;

/**
* Reads the number of CPU cores from {@code /sys/devices/system/cpu/}.
* Reads the number of CPU cores from the first available information from
* {@code /sys/devices/system/cpu/possible}, {@code /sys/devices/system/cpu/present},
* then {@code /sys/devices/system/cpu/}.
*
* @return Number of CPU cores in the phone, or DEVICEINFO_UKNOWN = -1 in the event of an error.
*/
Expand All @@ -41,7 +43,13 @@ public static int getNumberOfCPUCores() {
}
int cores;
try {
cores = new File("/sys/devices/system/cpu/").listFiles(CPU_FILTER).length;
cores = getCoresFromFileInfo("/sys/devices/system/cpu/possible");
if (cores == DEVICEINFO_UNKNOWN) {
cores = getCoresFromFileInfo("/sys/devices/system/cpu/present");
}
if (cores == DEVICEINFO_UNKNOWN) {
cores = getCoresFromCPUFileList();
}
} catch (SecurityException e) {
cores = DEVICEINFO_UNKNOWN;
} catch (NullPointerException e) {
Expand All @@ -50,6 +58,39 @@ public static int getNumberOfCPUCores() {
return cores;
}

/**
* Tries to read file contents from the file location to determine the number of cores on device.
* @param fileLocation The location of the file with CPU information
* @return Number of CPU cores in the phone, or DEVICEINFO_UKNOWN = -1 in the event of an error.
*/
private static int getCoresFromFileInfo(String fileLocation) {
try {
InputStream is = new FileInputStream(fileLocation);
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
String fileContents = buf.readLine();
return getCoresFromFileString(fileContents);
} catch (IOException e) {
return DEVICEINFO_UNKNOWN;
}
}

/**
* Converts from a CPU core information format to number of cores.
* @param str The CPU core information string, in the format of "0-N"
* @return The number of cores represented by this string
*/
static int getCoresFromFileString(String str) {
if (str == null || !str.matches("0-[\\d]+$")) {
return DEVICEINFO_UNKNOWN;
}
int cores = Integer.valueOf(str.substring(2)) + 1;
return cores;
}

private static int getCoresFromCPUFileList() {
return new File("/sys/devices/system/cpu/").listFiles(CPU_FILTER).length;
}

private static final FileFilter CPU_FILTER = new FileFilter() {
@Override
public boolean accept(File pathname) {
Expand Down Expand Up @@ -209,4 +250,4 @@ private static int extractValue(byte[] buffer, int index) {
}
return DEVICEINFO_UNKNOWN;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.device.yearclass;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.junit.Assert.*;

@RunWith(PowerMockRunner.class)
public class DeviceInfoTest {

@Test
public void testFileStringValid() {
assertEquals(DeviceInfo.getCoresFromFileString("0-3"), 4);
assertEquals(DeviceInfo.getCoresFromFileString("0-11"), 12);
}

@Test
public void testFileStringInvalid() {
assertEquals(DeviceInfo.getCoresFromFileString("INVALIDSTRING"), -1);
assertEquals(DeviceInfo.getCoresFromFileString("0-2a"), -1);
assertEquals(DeviceInfo.getCoresFromFileString("035"), -1);
}
}

0 comments on commit d590884

Please sign in to comment.