Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _GNU_SOURCE
- #define _GNU_SOURCE
- #endif
- #include <cstdint>
- #include <tuple>
- #include <iostream>
- #include <system_error>
- // For `sysconf`.
- #include <unistd.h>
- // For `CPU_SET`.
- #include <sched.h>
- void check(int r)
- {
- if (r == 0) return;
- throw std::system_error{r, std::system_category()};
- }
- auto cpuid(uint32_t leaf, uint32_t arg) ->
- std::tuple<uint32_t, uint32_t, uint32_t, uint32_t>
- {
- uint32_t r1, r2, r3, r4;
- asm("cpuid"
- : "=a" (r1), "=b" (r2), "=c" (r3), "=d" (r4)
- : "a" (leaf), "c" (arg)
- );
- return std::make_tuple(r1, r2, r3, r4);
- }
- void print_x2apic_id()
- {
- uint32_t r1, r2, r3, r4;
- std::tie(r1, r2, r3, r4) = cpuid(11, 0);
- std::cout << r4 << std::endl;
- }
- int main()
- {
- const auto _ = std::ignore;
- auto nprocs = ::sysconf(_SC_NPROCESSORS_ONLN);
- auto set = ::cpu_set_t{};
- std::cout << "Processors online: " << nprocs << std::endl;
- for (auto i = 0; i != nprocs; ++i) {
- CPU_SET(i, &set);
- check(::sched_setaffinity(0, sizeof(::cpu_set_t), &set));
- CPU_CLR(i, &set);
- print_x2apic_id();
- //uint32_t r4;
- //std::tie(_, _, _, r4) = cpuid(11, 0);
- //std::cout << r4 << std::endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement