如何查询Android设备核心相对性能及运行时获取不同类型核心数量?
在Android运行时获取不同性能层级的CPU核心数量
可以通过读取系统文件或使用Android原生API实现,以下是两种可行方案:
方案1:读取/sys文件系统的核心频率信息
Android设备的每个CPU核心最大频率存储在/sys/devices/system/cpu/路径下,通过读取该路径下的文件可划分核心性能层级:
- 遍历
/sys/devices/system/cpu/下以cpu开头的核心目录(如cpu0、cpu1) - 读取每个核心目录下
cpufreq/scaling_max_freq文件的内容,得到该核心的最大运行频率(单位:kHz) - 根据频率数值分组:最高频的归为fast,中间区间的归为intermediate,最低频的归为slow
示例代码:
import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class CPUCoreInfo { public static Map<String, List<Integer>> getCoreGroups() { Map<String, List<Integer>> coreGroups = new HashMap<>(); List<Integer> allMaxFreqs = new ArrayList<>(); File cpuDir = new File("/sys/devices/system/cpu/"); File[] cpuCoreDirs = cpuDir.listFiles((dir, name) -> name.startsWith("cpu") && Character.isDigit(name.charAt(3))); if (cpuCoreDirs == null) return coreGroups; // 收集所有核心的最大频率 for (File coreDir : cpuCoreDirs) { File freqFile = new File(coreDir, "cpufreq/scaling_max_freq"); if (freqFile.exists()) { try { String freqStr = Files.readString(freqFile.toPath()).trim(); int maxFreq = Integer.parseInt(freqStr); allMaxFreqs.add(maxFreq); } catch (IOException | NumberFormatException e) { e.printStackTrace(); } } } if (allMaxFreqs.isEmpty()) return coreGroups; // 计算阈值并分组 int max = allMaxFreqs.stream().max(Integer::compare).orElse(0); int min = allMaxFreqs.stream().min(Integer::compare).orElse(0); int midThreshold = (max + min) / 2; List<Integer> fastCores = new ArrayList<>(); List<Integer> intermediateCores = new ArrayList<>(); List<Integer> slowCores = new ArrayList<>(); for (int i = 0; i < allMaxFreqs.size(); i++) { int freq = allMaxFreqs.get(i); if (freq == max) { fastCores.add(i); } else if (freq <= midThreshold) { slowCores.add(i); } else { intermediateCores.add(i); } } coreGroups.put("fast", fastCores); coreGroups.put("intermediate", intermediateCores); coreGroups.put("slow", slowCores); return coreGroups; } }
注意:部分定制ROM可能限制
/sys目录的访问权限,一般情况下无需额外权限即可读取。
方案2:使用Android 10+原生API
从Android 10(API 29)开始,可直接通过Process.getCpuInfo()获取核心信息,无需读取系统文件:
import android.os.Process; import android.os.CpuInfo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class CPUCoreInfoApi29 { public static Map<String, List<Integer>> getCoreGroups() { Map<String, List<Integer>> coreGroups = new HashMap<>(); CpuInfo[] cpuInfos = Process.getCpuInfo(); if (cpuInfos == null || cpuInfos.length == 0) return coreGroups; List<Integer> allMaxFreqs = new ArrayList<>(); for (CpuInfo info : cpuInfos) { allMaxFreqs.add(info.getMaxFreqKhz()); } // 同方案1的分组逻辑 int max = allMaxFreqs.stream().max(Integer::compare).orElse(0); int min = allMaxFreqs.stream().min(Integer::compare).orElse(0); int midThreshold = (max + min) / 2; List<Integer> fastCores = new ArrayList<>(); List<Integer> intermediateCores = new ArrayList<>(); List<Integer> slowCores = new ArrayList<>(); for (int i = 0; i < cpuInfos.length; i++) { int freq = cpuInfos[i].getMaxFreqKhz(); if (freq == max) { fastCores.add(i); } else if (freq <= midThreshold) { slowCores.add(i); } else { intermediateCores.add(i); } } coreGroups.put("fast", fastCores); coreGroups.put("intermediate", intermediateCores); coreGroups.put("slow", slowCores); return coreGroups; } }
额外提示
- 不同厂商的核心架构差异(如大小核、三档核)可通过频率分组统一适配,无需依赖设备型号列表
- 若需绑定线程到特定核心,可使用
Process.setThreadAffinityMask()方法,传入对应核心的掩码(例如绑定cpu0和cpu1,掩码为0b11)
内容的提问来源于stack exchange,提问作者John Dallman
相关产品推荐
相关产品推荐

