Advertisement
Guest User

Untitled

a guest
Aug 18th, 2014
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #ifndef _GNU_SOURCE
  2.         #define _GNU_SOURCE
  3. #endif
  4.  
  5. #include <cstdint>
  6. #include <tuple>
  7. #include <iostream>
  8. #include <system_error>
  9.  
  10. // For `sysconf`.
  11. #include <unistd.h>
  12. // For `CPU_SET`.
  13. #include <sched.h>
  14.  
  15. void check(int r)
  16. {
  17.         if (r == 0) return;
  18.         throw std::system_error{r, std::system_category()};
  19. }
  20.  
  21. auto cpuid(uint32_t leaf, uint32_t arg) ->
  22. std::tuple<uint32_t, uint32_t, uint32_t, uint32_t>
  23. {
  24.         uint32_t r1, r2, r3, r4;
  25.         asm("cpuid"
  26.                 : "=a" (r1), "=b" (r2), "=c" (r3), "=d" (r4)
  27.                 : "a" (leaf), "c" (arg)
  28.         );
  29.         return std::make_tuple(r1, r2, r3, r4);
  30. }
  31.  
  32. void print_x2apic_id()
  33. {
  34.         uint32_t r1, r2, r3, r4;
  35.         std::tie(r1, r2, r3, r4) = cpuid(11, 0);
  36.         std::cout << r4 << std::endl;
  37. }
  38.  
  39. int main()
  40. {
  41.         const auto _ = std::ignore;
  42.         auto nprocs = ::sysconf(_SC_NPROCESSORS_ONLN);
  43.         auto set = ::cpu_set_t{};
  44.         std::cout << "Processors online: " << nprocs << std::endl;
  45.  
  46.         for (auto i = 0; i != nprocs; ++i) {
  47.                 CPU_SET(i, &set);
  48.                 check(::sched_setaffinity(0, sizeof(::cpu_set_t), &set));
  49.                 CPU_CLR(i, &set);
  50.                 print_x2apic_id();
  51.                 //uint32_t r4;
  52.                 //std::tie(_, _, _, r4) = cpuid(11, 0);
  53.                 //std::cout << r4 << std::endl;
  54.         }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement