Query with CPU_ALLOC rather than cpu_set_t: this must observe CPUs beyond CPU_SETSIZE too.
| 56 | |
| 57 | // Query with CPU_ALLOC rather than cpu_set_t: this must observe CPUs beyond CPU_SETSIZE too. |
| 58 | static bool get_affinity(std::vector<int>& ids) { |
| 59 | size_t count = initial_cpu_count(); |
| 60 | for (int attempt = 0; attempt < 8; ++attempt) { |
| 61 | DynamicCpuSet set(count); |
| 62 | if (!set) return false; |
| 63 | const int error = pthread_getaffinity_np(pthread_self(), set.bytes(), set.get()); |
| 64 | if (error == 0) { |
| 65 | ids.clear(); |
| 66 | for (size_t i = 0; i < set.count(); ++i) |
| 67 | if (CPU_ISSET_S(i, set.bytes(), set.get())) ids.push_back((int) i); |
| 68 | return !ids.empty(); |
| 69 | } |
| 70 | if (error != EINVAL || count > (size_t(1) << 20)) { |
| 71 | std::fprintf(stderr, "pthread_getaffinity_np failed: %s\n", std::strerror(error)); |
| 72 | return false; |
| 73 | } |
| 74 | count *= 2; |
| 75 | } |
| 76 | std::fprintf(stderr, "pthread_getaffinity_np still needs a larger CPU set\n"); |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | static bool set_affinity(const std::vector<int>& ids) { |
| 81 | if (ids.empty()) return false; |
no test coverage detected