| 99 | } |
| 100 | |
| 101 | int GetCurrentCPU() { |
| 102 | #if defined(__EMSCRIPTEN__) |
| 103 | return sched_getcpu(); |
| 104 | #elif defined(__linux__) && !defined(__ANDROID__) |
| 105 | return sched_getcpu(); |
| 106 | // Attempt to use cpuid on all other platforms. If that fails, perform a |
| 107 | // syscall. |
| 108 | #elif defined(__cpuid) && !defined(__APPLE__) |
| 109 | // TODO(b/120919972): __cpuid returns invalid APIC ids on OS X. |
| 110 | uint32_t eax = 0; |
| 111 | uint32_t ebx = 0; |
| 112 | uint32_t ecx = 0; |
| 113 | uint32_t edx = 0; |
| 114 | __cpuid(/*level=*/1, eax, ebx, ecx, edx); |
| 115 | if ((edx & /*bit_APIC=*/(1 << 9)) != 0) { |
| 116 | // EBX bits 24-31 are APIC ID |
| 117 | return (ebx & 0xFF) >> 24; |
| 118 | } |
| 119 | #elif defined(__NR_getcpu) |
| 120 | unsigned int cpu; |
| 121 | if (syscall(__NR_getcpu, &cpu, NULL, NULL) < 0) { |
| 122 | return kUnknownCPU; |
| 123 | } else { |
| 124 | return static_cast<int>(cpu); |
| 125 | } |
| 126 | #endif |
| 127 | return kUnknownCPU; |
| 128 | } |
| 129 | |
| 130 | int NumHyperthreadsPerCore() { |
| 131 | static const int ht_per_core = tensorflow::port::CPUIDNumSMT(); |