affinity.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Copyright (c) 2019, jtgrassie
  3. Copyright (c) 2019, tevador <tevador@gmail.com>
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of the copyright holder nor the
  13. names of its contributors may be used to endorse or promote products
  14. derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <sstream>
  27. #if defined(_WIN32) || defined(__CYGWIN__)
  28. #include <windows.h>
  29. #else
  30. #ifdef __APPLE__
  31. #include <mach/thread_act.h>
  32. #include <mach/thread_policy.h>
  33. #endif
  34. #include <pthread.h>
  35. #endif
  36. #include "affinity.hpp"
  37. int
  38. set_thread_affinity(const unsigned &cpuid)
  39. {
  40. std::thread::native_handle_type thread;
  41. #if defined(_WIN32) || defined(__CYGWIN__)
  42. thread = reinterpret_cast<std::thread::native_handle_type>(GetCurrentThread());
  43. #else
  44. thread = static_cast<std::thread::native_handle_type>(pthread_self());
  45. #endif
  46. return set_thread_affinity(thread, cpuid);
  47. }
  48. int
  49. set_thread_affinity(std::thread::native_handle_type thread,
  50. const unsigned &cpuid)
  51. {
  52. int rc = -1;
  53. #ifdef __APPLE__
  54. thread_port_t mach_thread;
  55. thread_affinity_policy_data_t policy = { static_cast<integer_t>(cpuid) };
  56. mach_thread = pthread_mach_thread_np(thread);
  57. rc = thread_policy_set(mach_thread, THREAD_AFFINITY_POLICY,
  58. (thread_policy_t)&policy, 1);
  59. #elif defined(_WIN32) || defined(__CYGWIN__)
  60. rc = SetThreadAffinityMask(reinterpret_cast<HANDLE>(thread), 1ULL << cpuid) == 0 ? -2 : 0;
  61. #else
  62. cpu_set_t cs;
  63. CPU_ZERO(&cs);
  64. CPU_SET(cpuid, &cs);
  65. rc = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cs);
  66. #endif
  67. return rc;
  68. }
  69. unsigned
  70. cpuid_from_mask(uint64_t mask, const unsigned &thread_index)
  71. {
  72. static unsigned lookup[64];
  73. static bool init = false;
  74. if (init)
  75. return lookup[thread_index];
  76. unsigned count_found = 0;
  77. for (unsigned i=0; i<64; i++)
  78. {
  79. if (1ULL & mask)
  80. {
  81. lookup[count_found] = i;
  82. count_found++;
  83. }
  84. mask >>= 1;
  85. }
  86. init = true;
  87. return lookup[thread_index];
  88. }
  89. std::string
  90. mask_to_string(uint64_t mask)
  91. {
  92. std::ostringstream ss;
  93. unsigned len = 0;
  94. unsigned v = 0;
  95. unsigned i = 64;
  96. while (i--)
  97. {
  98. v = mask >> i;
  99. if (1ULL & v)
  100. {
  101. if (len == 0) len = i + 1;
  102. ss << '1';
  103. }
  104. else
  105. if (len > 0) ss << '0';
  106. }
  107. return ss.str();
  108. }