From cb1565ebc96d7bcecfdc1db1216b07a244d57f8d Mon Sep 17 00:00:00 2001 From: Andrey Kamaev Date: Tue, 10 Apr 2012 08:26:46 +0000 Subject: [PATCH] Added Android-specific implementation of getNumberOfCPUs --- modules/core/src/system.cpp | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/modules/core/src/system.cpp b/modules/core/src/system.cpp index 3d337cf514..b8c46fad78 100644 --- a/modules/core/src/system.cpp +++ b/modules/core/src/system.cpp @@ -374,6 +374,46 @@ int getThreadNum(void) #endif } +#if ANDROID +static inline int getNumberOfCPUsImpl() +{ + FILE* cpuPossible = fopen("/sys/devices/system/cpu/possible", "r"); + if(!cpuPossible) + return 1; + + char buf[2000]; //big enough for 1000 CPUs in worst possible configuration + char* pbuf = fgets(buf, sizeof(buf), cpuPossible); + fclose(cpuPossible); + if(!pbuf) + return 1; + + //parse string of form "0-1,3,5-7,10,13-15" + int cpusAvailable = 0; + + while(*pbuf) + { + const char* pos = pbuf; + bool range = false; + while(*pbuf && *pbuf != ',') + { + if(*pbuf == '-') range = true; + ++pbuf; + } + if(*pbuf) *pbuf++ = 0; + if(!range) + ++cpusAvailable; + else + { + int rstart = 0, rend = 0; + sscanf(pos, "%d-%d", &rstart, &rend); + cpusAvailable += rend - rstart + 1; + } + + } + return cpusAvailable ? cpusAvailable : 1; +} +#endif + int getNumberOfCPUs(void) { #if defined WIN32 || defined _WIN32 @@ -381,6 +421,10 @@ int getNumberOfCPUs(void) GetSystemInfo( &sysinfo ); return (int)sysinfo.dwNumberOfProcessors; +#elif ANDROID + static int ncpus = getNumberOfCPUsImpl(); + printf("CPUS= %d\n", ncpus); + return ncpus; #elif defined __linux__ return (int)sysconf( _SC_NPROCESSORS_ONLN ); #elif defined __APPLE__