MCPcopy Create free account
hub / github.com/Niko1221/Strata / restriction

Function restriction

engine/src/kernels/cpu/card_node_test.cpp:294–329  ·  view source on GitHub ↗

restrict_process_cpus on this process: a second thread exists before the call and must be restricted too

Source from the content-addressed store, hash-verified

292#if defined(__linux__)
293// restrict_process_cpus on this process: a second thread exists before the call and must be restricted too
294static void restriction() {
295 const std::vector<int> mine = cpu::card_node_thread_cpus();
296 if (mine.size() < 2) {
297 std::printf("card_node_test: restriction skipped (this process may run on %zu CPU)\n", mine.size());
298 return;
299 }
300 std::atomic<bool> stop{false};
301 std::atomic<int> other_tid{0};
302 std::vector<int> other_after;
303 std::thread t([&] {
304 other_tid = (int) syscall(SYS_gettid);
305 while (!stop.load()) std::this_thread::sleep_for(std::chrono::milliseconds(2));
306 other_after = cpu::card_node_thread_cpus();
307 });
308 while (other_tid.load() == 0) std::this_thread::sleep_for(std::chrono::milliseconds(1));
309 const std::vector<int> half(mine.begin(), mine.begin() + (long) (mine.size() / 2));
310 std::vector<int> want = half;
311 want.push_back(100000); // a CPU the process does not have is ignored
312 int refused = -1;
313 const int changed = cpu::restrict_process_cpus(want, refused);
314 expect(changed >= 2 && refused == 0, "restriction: both threads changed (" + std::to_string(changed) + ", refused " +
315 std::to_string(refused) + ")");
316 expect(cpu::card_node_thread_cpus() == half, "restriction: the calling thread has the subset");
317 std::vector<int> created_after;
318 std::thread n([&] { created_after = cpu::card_node_thread_cpus(); });
319 n.join();
320 expect(created_after == half, "restriction: a thread created afterwards inherits the subset");
321 stop = true;
322 t.join();
323 expect(other_after == half, "restriction: the thread that existed before has the subset");
324 int refused2 = -1;
325 expect(cpu::restrict_process_cpus(half, refused2) == 0 && refused2 == 0, "restriction: a second call changes nothing");
326 std::vector<int> none = {100000};
327 expect(cpu::restrict_process_cpus(none, refused2) == 0 && cpu::card_node_thread_cpus() == half,
328 "restriction: no common CPU keeps the mask");
329}
330#endif
331
332int main() {

Callers 1

mainFunction · 0.85

Calls 9

card_node_thread_cpusFunction · 0.85
restrict_process_cpusFunction · 0.85
push_backMethod · 0.80
expectFunction · 0.70
to_stringFunction · 0.50
sizeMethod · 0.45
loadMethod · 0.45
beginMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected