port_posix.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #include "port/port_posix.h"
  10. #include <assert.h>
  11. #if defined(__i386__) || defined(__x86_64__)
  12. #include <cpuid.h>
  13. #endif
  14. #include <errno.h>
  15. #include <sched.h>
  16. #include <signal.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <sys/resource.h>
  20. #include <sys/time.h>
  21. #include <unistd.h>
  22. #include <cstdlib>
  23. #include "logging/logging.h"
  24. namespace ROCKSDB_NAMESPACE {
  25. // We want to give users opportunity to default all the mutexes to adaptive if
  26. // not specified otherwise. This enables a quick way to conduct various
  27. // performance related experiements.
  28. //
  29. // NB! Support for adaptive mutexes is turned on by definining
  30. // ROCKSDB_PTHREAD_ADAPTIVE_MUTEX during the compilation. If you use RocksDB
  31. // build environment then this happens automatically; otherwise it's up to the
  32. // consumer to define the identifier.
  33. #ifdef ROCKSDB_DEFAULT_TO_ADAPTIVE_MUTEX
  34. extern const bool kDefaultToAdaptiveMutex = true;
  35. #else
  36. extern const bool kDefaultToAdaptiveMutex = false;
  37. #endif
  38. namespace port {
  39. static int PthreadCall(const char* label, int result) {
  40. if (result != 0 && result != ETIMEDOUT) {
  41. fprintf(stderr, "pthread %s: %s\n", label, strerror(result));
  42. abort();
  43. }
  44. return result;
  45. }
  46. Mutex::Mutex(bool adaptive) {
  47. (void) adaptive;
  48. #ifdef ROCKSDB_PTHREAD_ADAPTIVE_MUTEX
  49. if (!adaptive) {
  50. PthreadCall("init mutex", pthread_mutex_init(&mu_, nullptr));
  51. } else {
  52. pthread_mutexattr_t mutex_attr;
  53. PthreadCall("init mutex attr", pthread_mutexattr_init(&mutex_attr));
  54. PthreadCall("set mutex attr",
  55. pthread_mutexattr_settype(&mutex_attr,
  56. PTHREAD_MUTEX_ADAPTIVE_NP));
  57. PthreadCall("init mutex", pthread_mutex_init(&mu_, &mutex_attr));
  58. PthreadCall("destroy mutex attr",
  59. pthread_mutexattr_destroy(&mutex_attr));
  60. }
  61. #else
  62. PthreadCall("init mutex", pthread_mutex_init(&mu_, nullptr));
  63. #endif // ROCKSDB_PTHREAD_ADAPTIVE_MUTEX
  64. }
  65. Mutex::~Mutex() { PthreadCall("destroy mutex", pthread_mutex_destroy(&mu_)); }
  66. void Mutex::Lock() {
  67. PthreadCall("lock", pthread_mutex_lock(&mu_));
  68. #ifndef NDEBUG
  69. locked_ = true;
  70. #endif
  71. }
  72. void Mutex::Unlock() {
  73. #ifndef NDEBUG
  74. locked_ = false;
  75. #endif
  76. PthreadCall("unlock", pthread_mutex_unlock(&mu_));
  77. }
  78. void Mutex::AssertHeld() {
  79. #ifndef NDEBUG
  80. assert(locked_);
  81. #endif
  82. }
  83. CondVar::CondVar(Mutex* mu)
  84. : mu_(mu) {
  85. PthreadCall("init cv", pthread_cond_init(&cv_, nullptr));
  86. }
  87. CondVar::~CondVar() { PthreadCall("destroy cv", pthread_cond_destroy(&cv_)); }
  88. void CondVar::Wait() {
  89. #ifndef NDEBUG
  90. mu_->locked_ = false;
  91. #endif
  92. PthreadCall("wait", pthread_cond_wait(&cv_, &mu_->mu_));
  93. #ifndef NDEBUG
  94. mu_->locked_ = true;
  95. #endif
  96. }
  97. bool CondVar::TimedWait(uint64_t abs_time_us) {
  98. struct timespec ts;
  99. ts.tv_sec = static_cast<time_t>(abs_time_us / 1000000);
  100. ts.tv_nsec = static_cast<suseconds_t>((abs_time_us % 1000000) * 1000);
  101. #ifndef NDEBUG
  102. mu_->locked_ = false;
  103. #endif
  104. int err = pthread_cond_timedwait(&cv_, &mu_->mu_, &ts);
  105. #ifndef NDEBUG
  106. mu_->locked_ = true;
  107. #endif
  108. if (err == ETIMEDOUT) {
  109. return true;
  110. }
  111. if (err != 0) {
  112. PthreadCall("timedwait", err);
  113. }
  114. return false;
  115. }
  116. void CondVar::Signal() {
  117. PthreadCall("signal", pthread_cond_signal(&cv_));
  118. }
  119. void CondVar::SignalAll() {
  120. PthreadCall("broadcast", pthread_cond_broadcast(&cv_));
  121. }
  122. RWMutex::RWMutex() {
  123. PthreadCall("init mutex", pthread_rwlock_init(&mu_, nullptr));
  124. }
  125. RWMutex::~RWMutex() { PthreadCall("destroy mutex", pthread_rwlock_destroy(&mu_)); }
  126. void RWMutex::ReadLock() { PthreadCall("read lock", pthread_rwlock_rdlock(&mu_)); }
  127. void RWMutex::WriteLock() { PthreadCall("write lock", pthread_rwlock_wrlock(&mu_)); }
  128. void RWMutex::ReadUnlock() { PthreadCall("read unlock", pthread_rwlock_unlock(&mu_)); }
  129. void RWMutex::WriteUnlock() { PthreadCall("write unlock", pthread_rwlock_unlock(&mu_)); }
  130. int PhysicalCoreID() {
  131. #if defined(ROCKSDB_SCHED_GETCPU_PRESENT) && defined(__x86_64__) && \
  132. (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 22))
  133. // sched_getcpu uses VDSO getcpu() syscall since 2.22. I believe Linux offers VDSO
  134. // support only on x86_64. This is the fastest/preferred method if available.
  135. int cpuno = sched_getcpu();
  136. if (cpuno < 0) {
  137. return -1;
  138. }
  139. return cpuno;
  140. #elif defined(__x86_64__) || defined(__i386__)
  141. // clang/gcc both provide cpuid.h, which defines __get_cpuid(), for x86_64 and i386.
  142. unsigned eax, ebx = 0, ecx, edx;
  143. if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
  144. return -1;
  145. }
  146. return ebx >> 24;
  147. #else
  148. // give up, the caller can generate a random number or something.
  149. return -1;
  150. #endif
  151. }
  152. void InitOnce(OnceType* once, void (*initializer)()) {
  153. PthreadCall("once", pthread_once(once, initializer));
  154. }
  155. void Crash(const std::string& srcfile, int srcline) {
  156. fprintf(stdout, "Crashing at %s:%d\n", srcfile.c_str(), srcline);
  157. fflush(stdout);
  158. kill(getpid(), SIGTERM);
  159. }
  160. int GetMaxOpenFiles() {
  161. #if defined(RLIMIT_NOFILE)
  162. struct rlimit no_files_limit;
  163. if (getrlimit(RLIMIT_NOFILE, &no_files_limit) != 0) {
  164. return -1;
  165. }
  166. // protect against overflow
  167. if (static_cast<uintmax_t>(no_files_limit.rlim_cur) >=
  168. static_cast<uintmax_t>(std::numeric_limits<int>::max())) {
  169. return std::numeric_limits<int>::max();
  170. }
  171. return static_cast<int>(no_files_limit.rlim_cur);
  172. #endif
  173. return -1;
  174. }
  175. void *cacheline_aligned_alloc(size_t size) {
  176. #if __GNUC__ < 5 && defined(__SANITIZE_ADDRESS__)
  177. return malloc(size);
  178. #elif ( _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 || defined(__APPLE__))
  179. void *m;
  180. errno = posix_memalign(&m, CACHE_LINE_SIZE, size);
  181. return errno ? nullptr : m;
  182. #else
  183. return malloc(size);
  184. #endif
  185. }
  186. void cacheline_aligned_free(void *memblock) {
  187. free(memblock);
  188. }
  189. static size_t GetPageSize() {
  190. #if defined(OS_LINUX) || defined(_SC_PAGESIZE)
  191. long v = sysconf(_SC_PAGESIZE);
  192. if (v >= 1024) {
  193. return static_cast<size_t>(v);
  194. }
  195. #endif
  196. // Default assume 4KB
  197. return 4U * 1024U;
  198. }
  199. const size_t kPageSize = GetPageSize();
  200. } // namespace port
  201. } // namespace ROCKSDB_NAMESPACE