android_additional.h 949 字节
Newer Older
A
Andrey Kamaev 已提交
1
#include <cstdio>
2

A
Andrey Kamaev 已提交
3 4 5 6 7
static inline int getPossibleCPUs()
{
   FILE* cpuPossible = fopen("/sys/devices/system/cpu/possible", "r");
   if(!cpuPossible)
       return 1;
8

A
Andrey Kamaev 已提交
9 10 11 12 13
   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;
14

A
Andrey Kamaev 已提交
15 16
   //parse string of form "0-1,3,5-7,10,13-15"
   int cpusAvailable = 0;
17

A
Andrey Kamaev 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
   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;
      }
36

A
Andrey Kamaev 已提交
37 38
   }
   return cpusAvailable ? cpusAvailable : 1;
39 40
}

A
Andrey Kamaev 已提交
41
#define __TBB_HardwareConcurrency() getPossibleCPUs()