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