From 5aaff34b6febfd36888caba6cea09c6af5b21338 Mon Sep 17 00:00:00 2001 From: Mark Peng Date: Mon, 16 May 2016 15:36:56 -0700 Subject: [PATCH] Added a few more methods to detect number of CPU cores on device --- .../facebook/device/yearclass/DeviceInfo.java | 47 +++++++++++++++++-- .../device/yearclass/DeviceInfoTest.java | 31 ++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 yearclass/src/test/java/com/facebook/device/yearclass/DeviceInfoTest.java diff --git a/yearclass/src/main/java/com/facebook/device/yearclass/DeviceInfo.java b/yearclass/src/main/java/com/facebook/device/yearclass/DeviceInfo.java index 72a7511..ee9fd23 100644 --- a/yearclass/src/main/java/com/facebook/device/yearclass/DeviceInfo.java +++ b/yearclass/src/main/java/com/facebook/device/yearclass/DeviceInfo.java @@ -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. */ @@ -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) { @@ -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) { @@ -209,4 +250,4 @@ private static int extractValue(byte[] buffer, int index) { } return DEVICEINFO_UNKNOWN; } -} \ No newline at end of file +} diff --git a/yearclass/src/test/java/com/facebook/device/yearclass/DeviceInfoTest.java b/yearclass/src/test/java/com/facebook/device/yearclass/DeviceInfoTest.java new file mode 100644 index 0000000..72d4fa2 --- /dev/null +++ b/yearclass/src/test/java/com/facebook/device/yearclass/DeviceInfoTest.java @@ -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); + } +} \ No newline at end of file