MCPcopy Create free account
hub / github.com/bshoshany/thread-pool / check_os_process_affinity

Function check_os_process_affinity

tests/BS_thread_pool_test.cpp:2999–3037  ·  view source on GitHub ↗

* @brief Check that getting and setting OS process affinity works. */

Source from the content-addressed store, hash-verified

2997 * @brief Check that getting and setting OS process affinity works.
2998 */
2999void check_os_process_affinity()
3000{
3001 logln("Checking OS process affinity...");
3002
3003 log("Obtaining initial process affinity ");
3004 const std::optional<std::vector<bool>> initial_affinity = BS::get_os_process_affinity();
3005 check(initial_affinity.has_value());
3006 logln("Initial affinity is: ", affinity_to_string(initial_affinity));
3007 const std::size_t num_bits = initial_affinity.has_value() ? initial_affinity->size() : std::thread::hardware_concurrency();
3008
3009 log("Setting affinity to CPU 1 only ");
3010 std::vector<bool> cpu_1_in(num_bits, false);
3011 cpu_1_in[0] = true;
3012 check(BS::set_os_process_affinity(cpu_1_in));
3013 log("Obtaining new affinity ");
3014 const std::optional<std::vector<bool>> cpu_1_out = BS::get_os_process_affinity();
3015 check(cpu_1_out.has_value());
3016 check(affinity_to_string(cpu_1_in), affinity_to_string(cpu_1_out));
3017
3018 log("Setting affinity to alternating CPUs ");
3019 std::vector<bool> alternating_in(num_bits, false);
3020 for (std::size_t i = 0; i < num_bits; ++i)
3021 alternating_in[i] = (i % 2 == 1);
3022 check(BS::set_os_process_affinity(alternating_in));
3023 log("Obtaining new affinity ");
3024 const std::optional<std::vector<bool>> alternating_out = BS::get_os_process_affinity();
3025 check(alternating_out.has_value());
3026 check(affinity_to_string(alternating_in), affinity_to_string(alternating_out));
3027
3028 if (initial_affinity.has_value())
3029 {
3030 log("Setting affinity back to initial value ");
3031 check(BS::set_os_process_affinity(*initial_affinity));
3032 log("Obtaining new affinity ");
3033 const std::optional<std::vector<bool>> initial_out = BS::get_os_process_affinity();
3034 check(initial_out.has_value());
3035 check(affinity_to_string(initial_affinity), affinity_to_string(initial_out));
3036 }
3037}
3038
3039/**
3040 * @brief Check that getting and setting OS thread affinity works.

Callers 1

mainFunction · 0.85

Calls 7

loglnFunction · 0.85
logFunction · 0.85
get_os_process_affinityFunction · 0.85
checkFunction · 0.85
affinity_to_stringFunction · 0.85
set_os_process_affinityFunction · 0.85
sizeMethod · 0.80

Tested by

no test coverage detected