2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #include <sys/system_properties.h>
13 #include <machine/cpu-features.h>
21 #include "webrtc/system_wrappers/source/android/cpu-features.h"
23 static pthread_once_t g_once;
24 static AndroidCpuFamily g_cpuFamily;
25 static uint64_t g_cpuFeatures;
26 static int g_cpuCount;
28 static const int android_cpufeatures_debug = 0;
31 # define DEFAULT_CPU_FAMILY ANDROID_CPU_FAMILY_ARM
32 #elif defined __i386__
33 # define DEFAULT_CPU_FAMILY ANDROID_CPU_FAMILY_X86
35 # define DEFAULT_CPU_FAMILY ANDROID_CPU_FAMILY_UNKNOWN
40 if (android_cpufeatures_debug) { \
41 printf(__VA_ARGS__); fflush(stdout); \
46 static __inline__ void x86_cpuid(int func, int values[4])
49 /* We need to preserve ebx since we're compiling PIC code */
50 /* this means we can't use "=b" for the second output register */
51 __asm__ __volatile__ ( \
56 : "=a" (a), "=r" (b), "=c" (c), "=d" (d) \
66 /* Read the content of /proc/cpuinfo into a user-provided buffer.
67 * Return the length of the data, or -1 on error. Does *not*
68 * zero-terminate the content. Will not read more
69 * than 'buffsize' bytes.
72 read_file(const char* pathname, char* buffer, size_t buffsize)
76 fd = open(pathname, O_RDONLY);
81 len = read(fd, buffer, buffsize);
82 } while (len < 0 && errno == EINTR);
89 /* Extract the content of a the first occurence of a given field in
90 * the content of /proc/cpuinfo and return it as a heap-allocated
91 * string that must be freed by the caller.
93 * Return NULL if not found
96 extract_cpuinfo_field(char* buffer, int buflen, const char* field)
98 int fieldlen = strlen(field);
99 char* bufend = buffer + buflen;
104 /* Look for first field occurence, and ensures it starts the line.
107 bufend = buffer + buflen;
109 p = memmem(p, bufend-p, field, fieldlen);
113 if (p == buffer || p[-1] == '\n')
119 /* Skip to the first column followed by a space */
121 p = memchr(p, ':', bufend-p);
122 if (p == NULL || p[1] != ' ')
125 /* Find the end of the line */
127 q = memchr(p, '\n', bufend-p);
131 /* Copy the line into a heap-allocated buffer */
133 result = malloc(len+1);
137 memcpy(result, p, len);
144 /* Count the number of occurences of a given field prefix in /proc/cpuinfo.
147 count_cpuinfo_field(char* buffer, int buflen, const char* field)
149 int fieldlen = strlen(field);
150 const char* p = buffer;
151 const char* bufend = buffer + buflen;
158 p = memmem(p, bufend-p, field, fieldlen);
162 /* Ensure that the field is at the start of a line */
163 if (p > buffer && p[-1] != '\n') {
169 /* skip any whitespace */
171 while (q < bufend && (*q == ' ' || *q == '\t'))
174 /* we must have a colon now */
175 if (q < bufend && *q == ':') {
185 /* Like strlen(), but for constant string literals */
186 #define STRLEN_CONST(x) ((sizeof(x)-1)
189 /* Checks that a space-separated list of items contains one given 'item'.
190 * Returns 1 if found, 0 otherwise.
193 has_list_item(const char* list, const char* item)
195 const char* p = list;
196 int itemlen = strlen(item);
205 while (*p == ' ' || *p == '\t')
208 /* find end of current list item */
210 while (*q && *q != ' ' && *q != '\t')
213 if (itemlen == q-p && !memcmp(p, item, itemlen))
216 /* skip to next item */
224 android_cpuInit(void)
229 g_cpuFamily = DEFAULT_CPU_FAMILY;
233 cpuinfo_len = read_file("/proc/cpuinfo", cpuinfo, sizeof cpuinfo);
234 D("cpuinfo_len is (%d):\n%.*s\n", cpuinfo_len,
235 cpuinfo_len >= 0 ? cpuinfo_len : 0, cpuinfo);
237 if (cpuinfo_len < 0) /* should not happen */ {
241 /* Count the CPU cores, the value may be 0 for single-core CPUs */
242 g_cpuCount = count_cpuinfo_field(cpuinfo, cpuinfo_len, "processor");
243 if (g_cpuCount == 0) {
244 g_cpuCount = count_cpuinfo_field(cpuinfo, cpuinfo_len, "Processor");
245 if (g_cpuCount == 0) {
250 D("found cpuCount = %d\n", g_cpuCount);
254 char* features = NULL;
255 char* architecture = NULL;
257 /* Extract architecture from the "CPU Architecture" field.
258 * The list is well-known, unlike the the output of
259 * the 'Processor' field which can vary greatly.
261 * See the definition of the 'proc_arch' array in
262 * $KERNEL/arch/arm/kernel/setup.c and the 'c_show' function in
265 char* cpuArch = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "CPU architecture");
267 if (cpuArch != NULL) {
272 D("found cpuArch = '%s'\n", cpuArch);
274 /* read the initial decimal number, ignore the rest */
275 archNumber = strtol(cpuArch, &end, 10);
277 /* Here we assume that ARMv8 will be upwards compatible with v7
278 * in the future. Unfortunately, there is no 'Features' field to
279 * indicate that Thumb-2 is supported.
281 if (end > cpuArch && archNumber >= 7) {
285 /* Unfortunately, it seems that certain ARMv6-based CPUs
286 * report an incorrect architecture number of 7!
288 * See http://code.google.com/p/android/issues/detail?id=10812
290 * We try to correct this by looking at the 'elf_format'
291 * field reported by the 'Processor' field, which is of the
292 * form of "(v7l)" for an ARMv7-based CPU, and "(v6l)" for
296 char* cpuProc = extract_cpuinfo_field(cpuinfo, cpuinfo_len,
298 if (cpuProc != NULL) {
299 D("found cpuProc = '%s'\n", cpuProc);
300 if (has_list_item(cpuProc, "(v6l)")) {
301 D("CPU processor and architecture mismatch!!\n");
309 g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_ARMv7;
312 /* The LDREX / STREX instructions are available from ARMv6 */
313 if (archNumber >= 6) {
314 g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_LDREX_STREX;
320 /* Extract the list of CPU features from 'Features' field */
321 char* cpuFeatures = extract_cpuinfo_field(cpuinfo, cpuinfo_len, "Features");
323 if (cpuFeatures != NULL) {
325 D("found cpuFeatures = '%s'\n", cpuFeatures);
327 if (has_list_item(cpuFeatures, "vfpv3"))
328 g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3;
330 else if (has_list_item(cpuFeatures, "vfpv3d16"))
331 g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_VFPv3;
333 if (has_list_item(cpuFeatures, "neon")) {
334 /* Note: Certain kernels only report neon but not vfpv3
335 * in their features list. However, ARM mandates
336 * that if Neon is implemented, so must be VFPv3
337 * so always set the flag.
339 g_cpuFeatures |= ANDROID_CPU_ARM_FEATURE_NEON |
340 ANDROID_CPU_ARM_FEATURE_VFPv3;
345 #endif /* __ARM_ARCH__ */
348 g_cpuFamily = ANDROID_CPU_FAMILY_X86;
352 /* According to http://en.wikipedia.org/wiki/CPUID */
353 #define VENDOR_INTEL_b 0x756e6547
354 #define VENDOR_INTEL_c 0x6c65746e
355 #define VENDOR_INTEL_d 0x49656e69
358 int vendorIsIntel = (regs[1] == VENDOR_INTEL_b &&
359 regs[2] == VENDOR_INTEL_c &&
360 regs[3] == VENDOR_INTEL_d);
363 if ((regs[2] & (1 << 9)) != 0) {
364 g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_SSSE3;
366 if ((regs[2] & (1 << 23)) != 0) {
367 g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_POPCNT;
369 if (vendorIsIntel && (regs[2] & (1 << 22)) != 0) {
370 g_cpuFeatures |= ANDROID_CPU_X86_FEATURE_MOVBE;
377 android_getCpuFamily(void)
379 pthread_once(&g_once, android_cpuInit);
385 android_getCpuFeatures(void)
387 pthread_once(&g_once, android_cpuInit);
388 return g_cpuFeatures;
393 android_getCpuCount(void)
395 pthread_once(&g_once, android_cpuInit);