port_posix.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. #if !defined(OS_WIN)
  10. #include "port/port_posix.h"
  11. #include <cassert>
  12. #if defined(__i386__) || defined(__x86_64__)
  13. #include <cpuid.h>
  14. #endif
  15. #include <sched.h>
  16. #include <sys/resource.h>
  17. #include <sys/time.h>
  18. #include <unistd.h>
  19. #include <cerrno>
  20. #include <csignal>
  21. #include <cstdio>
  22. #include <cstdlib>
  23. #include <cstring>
  24. #include <fstream>
  25. #include <string>
  26. #include "util/string_util.h"
  27. namespace ROCKSDB_NAMESPACE {
  28. // We want to give users opportunity to default all the mutexes to adaptive if
  29. // not specified otherwise. This enables a quick way to conduct various
  30. // performance related experiements.
  31. //
  32. // NB! Support for adaptive mutexes is turned on by definining
  33. // ROCKSDB_PTHREAD_ADAPTIVE_MUTEX during the compilation. If you use RocksDB
  34. // build environment then this happens automatically; otherwise it's up to the
  35. // consumer to define the identifier.
  36. #ifdef ROCKSDB_DEFAULT_TO_ADAPTIVE_MUTEX
  37. const bool kDefaultToAdaptiveMutex = true;
  38. #else
  39. const bool kDefaultToAdaptiveMutex = false;
  40. #endif
  41. namespace port {
  42. static int PthreadCall(const char* label, int result) {
  43. if (result != 0 && result != ETIMEDOUT && result != EBUSY) {
  44. fprintf(stderr, "pthread %s: %s\n", label, errnoStr(result).c_str());
  45. abort();
  46. }
  47. return result;
  48. }
  49. Mutex::Mutex(bool adaptive) {
  50. (void)adaptive;
  51. #ifdef ROCKSDB_PTHREAD_ADAPTIVE_MUTEX
  52. if (!adaptive) {
  53. PthreadCall("init mutex", pthread_mutex_init(&mu_, nullptr));
  54. } else {
  55. pthread_mutexattr_t mutex_attr;
  56. PthreadCall("init mutex attr", pthread_mutexattr_init(&mutex_attr));
  57. PthreadCall("set mutex attr", pthread_mutexattr_settype(
  58. &mutex_attr, PTHREAD_MUTEX_ADAPTIVE_NP));
  59. PthreadCall("init mutex", pthread_mutex_init(&mu_, &mutex_attr));
  60. PthreadCall("destroy mutex attr", pthread_mutexattr_destroy(&mutex_attr));
  61. }
  62. #else
  63. PthreadCall("init mutex", pthread_mutex_init(&mu_, nullptr));
  64. #endif // ROCKSDB_PTHREAD_ADAPTIVE_MUTEX
  65. }
  66. Mutex::~Mutex() { PthreadCall("destroy mutex", pthread_mutex_destroy(&mu_)); }
  67. void Mutex::Lock() {
  68. PthreadCall("lock", pthread_mutex_lock(&mu_));
  69. #ifndef NDEBUG
  70. locked_ = true;
  71. #endif
  72. }
  73. void Mutex::Unlock() {
  74. #ifndef NDEBUG
  75. locked_ = false;
  76. #endif
  77. PthreadCall("unlock", pthread_mutex_unlock(&mu_));
  78. }
  79. bool Mutex::TryLock() {
  80. bool ret = PthreadCall("trylock", pthread_mutex_trylock(&mu_)) == 0;
  81. #ifndef NDEBUG
  82. if (ret) {
  83. locked_ = true;
  84. }
  85. #endif
  86. return ret;
  87. }
  88. void Mutex::AssertHeld() const {
  89. #ifndef NDEBUG
  90. assert(locked_);
  91. #endif
  92. }
  93. CondVar::CondVar(Mutex* mu) : mu_(mu) {
  94. PthreadCall("init cv", pthread_cond_init(&cv_, nullptr));
  95. }
  96. CondVar::~CondVar() { PthreadCall("destroy cv", pthread_cond_destroy(&cv_)); }
  97. void CondVar::Wait() {
  98. #ifndef NDEBUG
  99. mu_->locked_ = false;
  100. #endif
  101. PthreadCall("wait", pthread_cond_wait(&cv_, &mu_->mu_));
  102. #ifndef NDEBUG
  103. mu_->locked_ = true;
  104. #endif
  105. }
  106. bool CondVar::TimedWait(uint64_t abs_time_us) {
  107. struct timespec ts;
  108. ts.tv_sec = static_cast<time_t>(abs_time_us / 1000000);
  109. ts.tv_nsec = static_cast<suseconds_t>((abs_time_us % 1000000) * 1000);
  110. #ifndef NDEBUG
  111. mu_->locked_ = false;
  112. #endif
  113. int err = pthread_cond_timedwait(&cv_, &mu_->mu_, &ts);
  114. #ifndef NDEBUG
  115. mu_->locked_ = true;
  116. #endif
  117. if (err == ETIMEDOUT) {
  118. return true;
  119. }
  120. if (err != 0) {
  121. PthreadCall("timedwait", err);
  122. }
  123. return false;
  124. }
  125. void CondVar::Signal() { PthreadCall("signal", pthread_cond_signal(&cv_)); }
  126. void CondVar::SignalAll() {
  127. PthreadCall("broadcast", pthread_cond_broadcast(&cv_));
  128. }
  129. RWMutex::RWMutex() {
  130. PthreadCall("init mutex", pthread_rwlock_init(&mu_, nullptr));
  131. }
  132. RWMutex::~RWMutex() {
  133. PthreadCall("destroy mutex", pthread_rwlock_destroy(&mu_));
  134. }
  135. void RWMutex::ReadLock() {
  136. PthreadCall("read lock", pthread_rwlock_rdlock(&mu_));
  137. }
  138. void RWMutex::WriteLock() {
  139. PthreadCall("write lock", pthread_rwlock_wrlock(&mu_));
  140. }
  141. void RWMutex::ReadUnlock() {
  142. PthreadCall("read unlock", pthread_rwlock_unlock(&mu_));
  143. }
  144. void RWMutex::WriteUnlock() {
  145. PthreadCall("write unlock", pthread_rwlock_unlock(&mu_));
  146. }
  147. int PhysicalCoreID() {
  148. #if defined(ROCKSDB_SCHED_GETCPU_PRESENT) && defined(__x86_64__) && \
  149. (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 22))
  150. // sched_getcpu uses VDSO getcpu() syscall since 2.22. I believe Linux offers
  151. // VDSO support only on x86_64. This is the fastest/preferred method if
  152. // available.
  153. int cpuno = sched_getcpu();
  154. if (cpuno < 0) {
  155. return -1;
  156. }
  157. return cpuno;
  158. #elif defined(__x86_64__) || defined(__i386__)
  159. // clang/gcc both provide cpuid.h, which defines __get_cpuid(), for x86_64 and
  160. // i386.
  161. unsigned eax, ebx = 0, ecx, edx;
  162. if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
  163. return -1;
  164. }
  165. return ebx >> 24;
  166. #else
  167. // give up, the caller can generate a random number or something.
  168. return -1;
  169. #endif
  170. }
  171. void InitOnce(OnceType* once, void (*initializer)()) {
  172. PthreadCall("once", pthread_once(once, initializer));
  173. }
  174. void Crash(const std::string& srcfile, int srcline) {
  175. fprintf(stdout, "Crashing at %s:%d\n", srcfile.c_str(), srcline);
  176. fflush(stdout);
  177. kill(getpid(), SIGTERM);
  178. }
  179. int GetMaxOpenFiles() {
  180. #if defined(RLIMIT_NOFILE)
  181. struct rlimit no_files_limit;
  182. if (getrlimit(RLIMIT_NOFILE, &no_files_limit) != 0) {
  183. return -1;
  184. }
  185. // protect against overflow
  186. if (static_cast<uintmax_t>(no_files_limit.rlim_cur) >=
  187. static_cast<uintmax_t>(std::numeric_limits<int>::max())) {
  188. return std::numeric_limits<int>::max();
  189. }
  190. return static_cast<int>(no_files_limit.rlim_cur);
  191. #else
  192. return -1;
  193. #endif
  194. }
  195. void* cacheline_aligned_alloc(size_t size) {
  196. #if __GNUC__ < 5 && defined(__SANITIZE_ADDRESS__)
  197. return malloc(size);
  198. #elif (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 || defined(__APPLE__))
  199. void* m;
  200. errno = posix_memalign(&m, CACHE_LINE_SIZE, size);
  201. return errno ? nullptr : m;
  202. #else
  203. return malloc(size);
  204. #endif
  205. }
  206. void cacheline_aligned_free(void* memblock) { free(memblock); }
  207. static size_t GetPageSize() {
  208. #if defined(OS_LINUX) || defined(_SC_PAGESIZE)
  209. long v = sysconf(_SC_PAGESIZE);
  210. if (v >= 1024) {
  211. return static_cast<size_t>(v);
  212. }
  213. #endif
  214. // Default assume 4KB
  215. return 4U * 1024U;
  216. }
  217. const size_t kPageSize = GetPageSize();
  218. void SetCpuPriority(ThreadId id, CpuPriority priority) {
  219. #ifdef OS_LINUX
  220. sched_param param;
  221. param.sched_priority = 0;
  222. switch (priority) {
  223. case CpuPriority::kHigh:
  224. sched_setscheduler(id, SCHED_OTHER, &param);
  225. setpriority(PRIO_PROCESS, id, -20);
  226. break;
  227. case CpuPriority::kNormal:
  228. sched_setscheduler(id, SCHED_OTHER, &param);
  229. setpriority(PRIO_PROCESS, id, 0);
  230. break;
  231. case CpuPriority::kLow:
  232. sched_setscheduler(id, SCHED_OTHER, &param);
  233. setpriority(PRIO_PROCESS, id, 19);
  234. break;
  235. case CpuPriority::kIdle:
  236. sched_setscheduler(id, SCHED_IDLE, &param);
  237. break;
  238. default:
  239. assert(false);
  240. }
  241. #else
  242. (void)id;
  243. (void)priority;
  244. #endif
  245. }
  246. int64_t GetProcessID() { return getpid(); }
  247. bool GenerateRfcUuid(std::string* output) {
  248. output->clear();
  249. std::ifstream f("/proc/sys/kernel/random/uuid");
  250. std::getline(f, /*&*/ *output);
  251. if (output->size() == 36) {
  252. return true;
  253. } else {
  254. output->clear();
  255. return false;
  256. }
  257. }
  258. } // namespace port
  259. } // namespace ROCKSDB_NAMESPACE
  260. #endif