| 170 | } |
| 171 | |
| 172 | static bool round_trip_high_cpu(const std::vector<int>& allowed, bool require_high) { |
| 173 | const auto it = std::find_if(allowed.begin(), allowed.end(), [](int id) { return id >= 64; }); |
| 174 | if (it == allowed.end()) { |
| 175 | if (require_high) { |
| 176 | std::fprintf(stderr, "Linux affinity test: --require-high-cpu requested, but no allowed CPU ID is >= 64\n"); |
| 177 | return false; |
| 178 | } |
| 179 | std::puts("SKIP high-CPU affinity path: no allowed CPU ID is >= 64 (use --require-high-cpu to require it)"); |
| 180 | return true; |
| 181 | } |
| 182 | const int target = allowed.front() == *it ? (allowed.size() > 1 ? allowed.back() : *it) : allowed.front(); |
| 183 | if (target == *it) { |
| 184 | if (require_high) { |
| 185 | std::fprintf(stderr, "Linux affinity test: --require-high-cpu needs at least two allowed CPUs to test restoration\n"); |
| 186 | return false; |
| 187 | } |
| 188 | std::puts("SKIP high-CPU affinity round trip: only one CPU is allowed, so no meaningful pin transition is possible"); |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | // Restrict the caller to one high-numbered CPU, then pin to a different originally allowed CPU. |
| 193 | // Restoring the high-only mask catches truncation even when the original process mask is sparse. |
| 194 | const std::vector<int> high_only{*it}; |
| 195 | if (!set_affinity(high_only) || !expect_affinity(high_only, "establish high-only mask") || |
| 196 | !topology_respects_current_mask()) return false; |
| 197 | const auto previous = cpu::pin_current_thread(target); |
| 198 | const bool pinned = previous.valid && expect_affinity({target}, "pin away from high-only mask"); |
| 199 | cpu::restore_thread_affinity(previous); |
| 200 | const bool restored = pinned && expect_affinity(high_only, "restore high-only mask"); |
| 201 | if (restored) std::printf("High-CPU affinity round trip: saved CPU %d, pin target %d\n", *it, target); |
| 202 | return restored; |
| 203 | } |
| 204 | |
| 205 | static bool negative_cpu_is_noop() { |
| 206 | std::vector<int> before; |
no test coverage detected