2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2013-10-16 23:59:46 +02:00
|
|
|
//
|
2011-03-18 23:37:00 +01:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
//
|
|
|
|
// See port_example.h for documentation for the following types/functions.
|
|
|
|
|
2015-02-24 07:05:06 +01:00
|
|
|
#pragma once
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
#include <thread>
|
2015-07-13 21:11:05 +02:00
|
|
|
// size_t printf formatting named in the manner of C99 standard formatting
|
|
|
|
// strings such as PRIu64
|
2015-07-02 01:13:49 +02:00
|
|
|
// in fact, we could use that one
|
|
|
|
#define ROCKSDB_PRIszt "zu"
|
|
|
|
|
2016-01-13 23:51:58 +01:00
|
|
|
#define __declspec(S)
|
|
|
|
|
2015-08-26 01:34:39 +02:00
|
|
|
#define ROCKSDB_NOEXCEPT noexcept
|
2015-08-26 00:17:14 +02:00
|
|
|
|
2012-08-27 08:45:35 +02:00
|
|
|
#undef PLATFORM_IS_LITTLE_ENDIAN
|
2012-03-05 19:35:46 +01:00
|
|
|
#if defined(OS_MACOSX)
|
2011-06-29 02:30:50 +02:00
|
|
|
#include <machine/endian.h>
|
2012-08-27 08:45:35 +02:00
|
|
|
#if defined(__DARWIN_LITTLE_ENDIAN) && defined(__DARWIN_BYTE_ORDER)
|
|
|
|
#define PLATFORM_IS_LITTLE_ENDIAN \
|
|
|
|
(__DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN)
|
|
|
|
#endif
|
2011-06-29 02:30:50 +02:00
|
|
|
#elif defined(OS_SOLARIS)
|
|
|
|
#include <sys/isa_defs.h>
|
|
|
|
#ifdef _LITTLE_ENDIAN
|
2012-08-27 08:45:35 +02:00
|
|
|
#define PLATFORM_IS_LITTLE_ENDIAN true
|
2011-06-29 02:30:50 +02:00
|
|
|
#else
|
2012-08-27 08:45:35 +02:00
|
|
|
#define PLATFORM_IS_LITTLE_ENDIAN false
|
2011-06-29 02:30:50 +02:00
|
|
|
#endif
|
2017-04-22 05:41:37 +02:00
|
|
|
#include <alloca.h>
|
|
|
|
#elif defined(OS_AIX)
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <arpa/nameser_compat.h>
|
|
|
|
#define PLATFORM_IS_LITTLE_ENDIAN (BYTE_ORDER == LITTLE_ENDIAN)
|
|
|
|
#include <alloca.h>
|
2015-11-16 21:56:21 +01:00
|
|
|
#elif defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || \
|
|
|
|
defined(OS_DRAGONFLYBSD) || defined(OS_ANDROID)
|
2015-02-27 00:19:17 +01:00
|
|
|
#include <sys/endian.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#define PLATFORM_IS_LITTLE_ENDIAN (_BYTE_ORDER == _LITTLE_ENDIAN)
|
2011-06-29 02:30:50 +02:00
|
|
|
#else
|
|
|
|
#include <endian.h>
|
|
|
|
#endif
|
2011-03-18 23:37:00 +01:00
|
|
|
#include <pthread.h>
|
2014-02-08 03:12:30 +01:00
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
#include <stdint.h>
|
2012-06-28 08:41:33 +02:00
|
|
|
#include <string.h>
|
support for concurrent adds to memtable
Summary:
This diff adds support for concurrent adds to the skiplist memtable
implementations. Memory allocation is made thread-safe by the addition of
a spinlock, with small per-core buffers to avoid contention. Concurrent
memtable writes are made via an additional method and don't impose a
performance overhead on the non-concurrent case, so parallelism can be
selected on a per-batch basis.
Write thread synchronization is an increasing bottleneck for higher levels
of concurrency, so this diff adds --enable_write_thread_adaptive_yield
(default off). This feature causes threads joining a write batch
group to spin for a short time (default 100 usec) using sched_yield,
rather than going to sleep on a mutex. If the timing of the yield calls
indicates that another thread has actually run during the yield then
spinning is avoided. This option improves performance for concurrent
situations even without parallel adds, although it has the potential to
increase CPU usage (and the heuristic adaptation is not yet mature).
Parallel writes are not currently compatible with
inplace updates, update callbacks, or delete filtering.
Enable it with --allow_concurrent_memtable_write (and
--enable_write_thread_adaptive_yield). Parallel memtable writes
are performance neutral when there is no actual parallelism, and in
my experiments (SSD server-class Linux and varying contention and key
sizes for fillrandom) they are always a performance win when there is
more than one thread.
Statistics are updated earlier in the write path, dropping the number
of DB mutex acquisitions from 2 to 1 for almost all cases.
This diff was motivated and inspired by Yahoo's cLSM work. It is more
conservative than cLSM: RocksDB's write batch group leader role is
preserved (along with all of the existing flush and write throttling
logic) and concurrent writers are blocked until all memtable insertions
have completed and the sequence number has been advanced, to preserve
linearizability.
My test config is "db_bench -benchmarks=fillrandom -threads=$T
-batch_size=1 -memtablerep=skip_list -value_size=100 --num=1000000/$T
-level0_slowdown_writes_trigger=9999 -level0_stop_writes_trigger=9999
-disable_auto_compactions --max_write_buffer_number=8
-max_background_flushes=8 --disable_wal --write_buffer_size=160000000
--block_size=16384 --allow_concurrent_memtable_write" on a two-socket
Xeon E5-2660 @ 2.2Ghz with lots of memory and an SSD hard drive. With 1
thread I get ~440Kops/sec. Peak performance for 1 socket (numactl
-N1) is slightly more than 1Mops/sec, at 16 threads. Peak performance
across both sockets happens at 30 threads, and is ~900Kops/sec, although
with fewer threads there is less performance loss when the system has
background work.
Test Plan:
1. concurrent stress tests for InlineSkipList and DynamicBloom
2. make clean; make check
3. make clean; DISABLE_JEMALLOC=1 make valgrind_check; valgrind db_bench
4. make clean; COMPILE_WITH_TSAN=1 make all check; db_bench
5. make clean; COMPILE_WITH_ASAN=1 make all check; db_bench
6. make clean; OPT=-DROCKSDB_LITE make check
7. verify no perf regressions when disabled
Reviewers: igor, sdong
Reviewed By: sdong
Subscribers: MarkCallaghan, IslamAbdelRahman, anthony, yhchiang, rven, sdong, guyg8, kradhakrishnan, dhruba
Differential Revision: https://reviews.facebook.net/D50589
2015-08-15 01:59:07 +02:00
|
|
|
#include <limits>
|
|
|
|
#include <string>
|
2011-06-29 02:30:50 +02:00
|
|
|
|
2012-08-27 08:45:35 +02:00
|
|
|
#ifndef PLATFORM_IS_LITTLE_ENDIAN
|
|
|
|
#define PLATFORM_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN)
|
2011-06-29 02:30:50 +02:00
|
|
|
#endif
|
|
|
|
|
2012-03-05 19:35:46 +01:00
|
|
|
#if defined(OS_MACOSX) || defined(OS_SOLARIS) || defined(OS_FREEBSD) ||\
|
2012-08-27 08:45:35 +02:00
|
|
|
defined(OS_NETBSD) || defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD) ||\
|
2017-04-22 05:41:37 +02:00
|
|
|
defined(OS_ANDROID) || defined(CYGWIN) || defined(OS_AIX)
|
2012-03-05 19:35:46 +01:00
|
|
|
// Use fread/fwrite/fflush on platforms without _unlocked variants
|
2011-06-29 02:30:50 +02:00
|
|
|
#define fread_unlocked fread
|
|
|
|
#define fwrite_unlocked fwrite
|
|
|
|
#define fflush_unlocked fflush
|
|
|
|
#endif
|
|
|
|
|
2012-03-05 19:35:46 +01:00
|
|
|
#if defined(OS_MACOSX) || defined(OS_FREEBSD) ||\
|
|
|
|
defined(OS_OPENBSD) || defined(OS_DRAGONFLYBSD)
|
|
|
|
// Use fsync() on platforms without fdatasync()
|
2011-06-29 02:30:50 +02:00
|
|
|
#define fdatasync fsync
|
|
|
|
#endif
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2012-08-27 08:45:35 +02:00
|
|
|
#if defined(OS_ANDROID) && __ANDROID_API__ < 9
|
|
|
|
// fdatasync() was only introduced in API level 9 on Android. Use fsync()
|
2015-12-10 17:54:48 +01:00
|
|
|
// when targeting older platforms.
|
2012-08-27 08:45:35 +02:00
|
|
|
#define fdatasync fsync
|
|
|
|
#endif
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2011-03-18 23:37:00 +01:00
|
|
|
namespace port {
|
|
|
|
|
2015-07-08 01:58:20 +02:00
|
|
|
// For use at db/file_indexer.h kLevelMaxIndex
|
2018-02-09 23:50:09 +01:00
|
|
|
const uint32_t kMaxUint32 = std::numeric_limits<uint32_t>::max();
|
2015-07-11 18:54:33 +02:00
|
|
|
const int kMaxInt32 = std::numeric_limits<int32_t>::max();
|
|
|
|
const uint64_t kMaxUint64 = std::numeric_limits<uint64_t>::max();
|
2016-05-28 01:14:24 +02:00
|
|
|
const int64_t kMaxInt64 = std::numeric_limits<int64_t>::max();
|
2015-09-22 19:34:21 +02:00
|
|
|
const size_t kMaxSizet = std::numeric_limits<size_t>::max();
|
2015-07-08 01:58:20 +02:00
|
|
|
|
2012-08-27 08:45:35 +02:00
|
|
|
static const bool kLittleEndian = PLATFORM_IS_LITTLE_ENDIAN;
|
|
|
|
#undef PLATFORM_IS_LITTLE_ENDIAN
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
class CondVar;
|
|
|
|
|
|
|
|
class Mutex {
|
|
|
|
public:
|
2016-12-28 00:47:43 +01:00
|
|
|
// We want to give users opportunity to default all the mutexes to adaptive if
|
|
|
|
// not specified otherwise. This enables a quick way to conduct various
|
|
|
|
// performance related experiements.
|
|
|
|
//
|
|
|
|
// NB! Support for adaptive mutexes is turned on by definining
|
|
|
|
// ROCKSDB_PTHREAD_ADAPTIVE_MUTEX during the compilation. If you use RocksDB
|
|
|
|
// build environment then this happens automatically; otherwise it's up to the
|
|
|
|
// consumer to define the identifier.
|
|
|
|
#ifdef ROCKSDB_DEFAULT_TO_ADAPTIVE_MUTEX
|
|
|
|
explicit Mutex(bool adaptive = true);
|
|
|
|
#else
|
|
|
|
explicit Mutex(bool adaptive = false);
|
|
|
|
#endif
|
2011-03-18 23:37:00 +01:00
|
|
|
~Mutex();
|
|
|
|
|
|
|
|
void Lock();
|
|
|
|
void Unlock();
|
2014-04-23 03:38:10 +02:00
|
|
|
// this will assert if the mutex is not locked
|
|
|
|
// it does NOT verify that mutex is held by a calling thread
|
2014-03-26 19:24:52 +01:00
|
|
|
void AssertHeld();
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class CondVar;
|
|
|
|
pthread_mutex_t mu_;
|
2014-03-26 19:24:52 +01:00
|
|
|
#ifndef NDEBUG
|
2014-04-23 03:38:10 +02:00
|
|
|
bool locked_;
|
2014-03-26 19:24:52 +01:00
|
|
|
#endif
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
// No copying
|
|
|
|
Mutex(const Mutex&);
|
|
|
|
void operator=(const Mutex&);
|
|
|
|
};
|
|
|
|
|
2012-10-02 06:58:36 +02:00
|
|
|
class RWMutex {
|
|
|
|
public:
|
|
|
|
RWMutex();
|
|
|
|
~RWMutex();
|
|
|
|
|
|
|
|
void ReadLock();
|
|
|
|
void WriteLock();
|
2014-06-17 00:41:46 +02:00
|
|
|
void ReadUnlock();
|
|
|
|
void WriteUnlock();
|
2012-10-02 06:58:36 +02:00
|
|
|
void AssertHeld() { }
|
|
|
|
|
|
|
|
private:
|
|
|
|
pthread_rwlock_t mu_; // the underlying platform mutex
|
|
|
|
|
|
|
|
// No copying allowed
|
|
|
|
RWMutex(const RWMutex&);
|
|
|
|
void operator=(const RWMutex&);
|
|
|
|
};
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
class CondVar {
|
|
|
|
public:
|
|
|
|
explicit CondVar(Mutex* mu);
|
|
|
|
~CondVar();
|
|
|
|
void Wait();
|
2014-07-03 19:22:08 +02:00
|
|
|
// Timed condition wait. Returns true if timeout occurred.
|
|
|
|
bool TimedWait(uint64_t abs_time_us);
|
2011-03-18 23:37:00 +01:00
|
|
|
void Signal();
|
|
|
|
void SignalAll();
|
|
|
|
private:
|
|
|
|
pthread_cond_t cv_;
|
|
|
|
Mutex* mu_;
|
|
|
|
};
|
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
using Thread = std::thread;
|
|
|
|
|
support for concurrent adds to memtable
Summary:
This diff adds support for concurrent adds to the skiplist memtable
implementations. Memory allocation is made thread-safe by the addition of
a spinlock, with small per-core buffers to avoid contention. Concurrent
memtable writes are made via an additional method and don't impose a
performance overhead on the non-concurrent case, so parallelism can be
selected on a per-batch basis.
Write thread synchronization is an increasing bottleneck for higher levels
of concurrency, so this diff adds --enable_write_thread_adaptive_yield
(default off). This feature causes threads joining a write batch
group to spin for a short time (default 100 usec) using sched_yield,
rather than going to sleep on a mutex. If the timing of the yield calls
indicates that another thread has actually run during the yield then
spinning is avoided. This option improves performance for concurrent
situations even without parallel adds, although it has the potential to
increase CPU usage (and the heuristic adaptation is not yet mature).
Parallel writes are not currently compatible with
inplace updates, update callbacks, or delete filtering.
Enable it with --allow_concurrent_memtable_write (and
--enable_write_thread_adaptive_yield). Parallel memtable writes
are performance neutral when there is no actual parallelism, and in
my experiments (SSD server-class Linux and varying contention and key
sizes for fillrandom) they are always a performance win when there is
more than one thread.
Statistics are updated earlier in the write path, dropping the number
of DB mutex acquisitions from 2 to 1 for almost all cases.
This diff was motivated and inspired by Yahoo's cLSM work. It is more
conservative than cLSM: RocksDB's write batch group leader role is
preserved (along with all of the existing flush and write throttling
logic) and concurrent writers are blocked until all memtable insertions
have completed and the sequence number has been advanced, to preserve
linearizability.
My test config is "db_bench -benchmarks=fillrandom -threads=$T
-batch_size=1 -memtablerep=skip_list -value_size=100 --num=1000000/$T
-level0_slowdown_writes_trigger=9999 -level0_stop_writes_trigger=9999
-disable_auto_compactions --max_write_buffer_number=8
-max_background_flushes=8 --disable_wal --write_buffer_size=160000000
--block_size=16384 --allow_concurrent_memtable_write" on a two-socket
Xeon E5-2660 @ 2.2Ghz with lots of memory and an SSD hard drive. With 1
thread I get ~440Kops/sec. Peak performance for 1 socket (numactl
-N1) is slightly more than 1Mops/sec, at 16 threads. Peak performance
across both sockets happens at 30 threads, and is ~900Kops/sec, although
with fewer threads there is less performance loss when the system has
background work.
Test Plan:
1. concurrent stress tests for InlineSkipList and DynamicBloom
2. make clean; make check
3. make clean; DISABLE_JEMALLOC=1 make valgrind_check; valgrind db_bench
4. make clean; COMPILE_WITH_TSAN=1 make all check; db_bench
5. make clean; COMPILE_WITH_ASAN=1 make all check; db_bench
6. make clean; OPT=-DROCKSDB_LITE make check
7. verify no perf regressions when disabled
Reviewers: igor, sdong
Reviewed By: sdong
Subscribers: MarkCallaghan, IslamAbdelRahman, anthony, yhchiang, rven, sdong, guyg8, kradhakrishnan, dhruba
Differential Revision: https://reviews.facebook.net/D50589
2015-08-15 01:59:07 +02:00
|
|
|
static inline void AsmVolatilePause() {
|
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
|
|
|
asm volatile("pause");
|
|
|
|
#elif defined(__aarch64__)
|
|
|
|
asm volatile("wfe");
|
|
|
|
#elif defined(__powerpc64__)
|
|
|
|
asm volatile("or 27,27,27");
|
|
|
|
#endif
|
|
|
|
// it's okay for other platforms to be no-ops
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns -1 if not available on this platform
|
|
|
|
extern int PhysicalCoreID();
|
|
|
|
|
2012-08-27 08:45:35 +02:00
|
|
|
typedef pthread_once_t OnceType;
|
|
|
|
#define LEVELDB_ONCE_INIT PTHREAD_ONCE_INIT
|
|
|
|
extern void InitOnce(OnceType* once, void (*initializer)());
|
|
|
|
|
2017-05-09 01:04:19 +02:00
|
|
|
#ifndef CACHE_LINE_SIZE
|
2017-07-15 00:12:47 +02:00
|
|
|
#if defined(__s390__)
|
|
|
|
#define CACHE_LINE_SIZE 256U
|
|
|
|
#elif defined(__powerpc__) || defined(__aarch64__)
|
|
|
|
#define CACHE_LINE_SIZE 128U
|
|
|
|
#else
|
|
|
|
#define CACHE_LINE_SIZE 64U
|
|
|
|
#endif
|
2017-05-09 01:04:19 +02:00
|
|
|
#endif
|
2014-03-28 17:21:20 +01:00
|
|
|
|
2017-07-24 19:46:21 +02:00
|
|
|
|
|
|
|
extern void *cacheline_aligned_alloc(size_t size);
|
|
|
|
|
|
|
|
extern void cacheline_aligned_free(void *memblock);
|
|
|
|
|
|
|
|
#define ALIGN_AS(n) alignas(n)
|
|
|
|
|
2014-06-12 19:06:18 +02:00
|
|
|
#define PREFETCH(addr, rw, locality) __builtin_prefetch(addr, rw, locality)
|
|
|
|
|
Move rate_limiter, write buffering, most perf context instrumentation and most random kill out of Env
Summary: We want to keep Env a think layer for better portability. Less platform dependent codes should be moved out of Env. In this patch, I create a wrapper of file readers and writers, and put rate limiting, write buffering, as well as most perf context instrumentation and random kill out of Env. It will make it easier to maintain multiple Env in the future.
Test Plan: Run all existing unit tests.
Reviewers: anthony, kradhakrishnan, IslamAbdelRahman, yhchiang, igor
Reviewed By: igor
Subscribers: leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D42321
2015-07-18 01:16:11 +02:00
|
|
|
extern void Crash(const std::string& srcfile, int srcline);
|
2015-09-10 19:49:28 +02:00
|
|
|
|
|
|
|
extern int GetMaxOpenFiles();
|
|
|
|
|
2011-06-29 02:30:50 +02:00
|
|
|
} // namespace port
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|