2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00: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).
|
2015-10-27 12:03:43 +00: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.
|
|
|
|
|
2016-08-26 17:41:35 +00:00
|
|
|
#include "util/threadpool_imp.h"
|
2017-02-06 22:43:55 +00:00
|
|
|
|
2016-05-13 01:34:04 +00:00
|
|
|
#ifndef OS_WIN
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2015-10-27 12:03:43 +00:00
|
|
|
#ifdef OS_LINUX
|
2016-05-13 01:34:04 +00:00
|
|
|
# include <sys/syscall.h>
|
2018-04-24 15:38:01 +00:00
|
|
|
# include <sys/resource.h>
|
2015-10-27 12:03:43 +00:00
|
|
|
#endif
|
|
|
|
|
2018-04-19 00:25:37 +00:00
|
|
|
#include <stdlib.h>
|
2021-03-25 06:06:31 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <atomic>
|
|
|
|
#include <condition_variable>
|
refactor SavePoints (#5192)
Summary:
Savepoints are assumed to be used in a stack-wise fashion (only
the top element should be used), so they were stored by `WriteBatch`
in a member variable `save_points` using an std::stack.
Conceptually this is fine, but the implementation had a few issues:
- the `save_points_` instance variable was a plain pointer to a heap-
allocated `SavePoints` struct. The destructor of `WriteBatch` simply
deletes this pointer. However, the copy constructor of WriteBatch
just copied that pointer, meaning that copying a WriteBatch with
active savepoints will very likely have crashed before. Now a proper
copy of the savepoints is made in the copy constructor, and not just
a copy of the pointer
- `save_points_` was an std::stack, which defaults to `std::deque` for
the underlying container. A deque is a bit over the top here, as we
only need access to the most recent savepoint (i.e. stack.top()) but
never any elements at the front. std::deque is rather expensive to
initialize in common environments. For example, the STL implementation
shipped with GNU g++ will perform a heap allocation of more than 500
bytes to create an empty deque object. Although the `save_points_`
container is created lazily by RocksDB, moving from a deque to a plain
`std::vector` is much more memory-efficient. So `save_points_` is now
a vector.
- `save_points_` was changed from a plain pointer to an `std::unique_ptr`,
making ownership more explicit.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5192
Differential Revision: D15024074
Pulled By: maysamyabandeh
fbshipit-source-id: 5b128786d3789cde94e46465c9e91badd07a25d7
2019-04-20 03:30:03 +00:00
|
|
|
#include <deque>
|
2017-02-06 22:43:55 +00:00
|
|
|
#include <mutex>
|
2018-04-19 00:25:37 +00:00
|
|
|
#include <sstream>
|
2017-02-06 22:43:55 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
2016-06-06 00:23:38 +00:00
|
|
|
|
2020-03-25 03:20:33 +00:00
|
|
|
#include "monitoring/thread_status_util.h"
|
|
|
|
#include "port/port.h"
|
|
|
|
#include "test_util/sync_point.h"
|
2021-03-25 06:06:31 +00:00
|
|
|
#include "util/string_util.h"
|
2020-03-25 03:20:33 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2015-10-27 12:03:43 +00:00
|
|
|
|
2016-08-26 17:41:35 +00:00
|
|
|
void ThreadPoolImpl::PthreadCall(const char* label, int result) {
|
2015-10-27 12:03:43 +00:00
|
|
|
if (result != 0) {
|
2021-03-25 06:06:31 +00:00
|
|
|
fprintf(stderr, "pthread %s: %s\n", label, errnoStr(result).c_str());
|
2015-10-27 12:03:43 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
struct ThreadPoolImpl::Impl {
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
Impl();
|
|
|
|
~Impl();
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
void JoinThreads(bool wait_for_jobs_to_complete);
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
void SetBackgroundThreadsInternal(int num, bool allow_reduce);
|
2017-05-23 18:04:25 +00:00
|
|
|
int GetBackgroundThreads();
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
unsigned int GetQueueLen() const {
|
|
|
|
return queue_len_.load(std::memory_order_relaxed);
|
|
|
|
}
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
void LowerIOPriority();
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2020-06-13 20:23:40 +00:00
|
|
|
void LowerCPUPriority(CpuPriority pri);
|
2018-04-24 15:38:01 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
void WakeUpAllThreads() {
|
|
|
|
bgsignal_.notify_all();
|
|
|
|
}
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
void BGThread(size_t thread_id);
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
void StartBGThreads();
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
void Submit(std::function<void()>&& schedule,
|
|
|
|
std::function<void()>&& unschedule, void* tag);
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
int UnSchedule(void* arg);
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
void SetHostEnv(Env* env) { env_ = env; }
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
Env* GetHostEnv() const { return env_; }
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
bool HasExcessiveThread() const {
|
|
|
|
return static_cast<int>(bgthreads_.size()) > total_threads_limit_;
|
|
|
|
}
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
// Return true iff the current thread is the excessive thread to terminate.
|
|
|
|
// Always terminate the running thread that is added last, even if there are
|
|
|
|
// more than one thread to terminate.
|
|
|
|
bool IsLastExcessiveThread(size_t thread_id) const {
|
|
|
|
return HasExcessiveThread() && thread_id == bgthreads_.size() - 1;
|
|
|
|
}
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
bool IsExcessiveThread(size_t thread_id) const {
|
|
|
|
return static_cast<int>(thread_id) >= total_threads_limit_;
|
|
|
|
}
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
// Return the thread priority.
|
|
|
|
// This would allow its member-thread to know its priority.
|
|
|
|
Env::Priority GetThreadPriority() const { return priority_; }
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
// Set the thread priority.
|
|
|
|
void SetThreadPriority(Env::Priority priority) { priority_ = priority; }
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
private:
|
2019-09-20 19:00:55 +00:00
|
|
|
static void BGThreadWrapper(void* arg);
|
|
|
|
|
|
|
|
bool low_io_priority_;
|
2020-06-13 20:23:40 +00:00
|
|
|
CpuPriority cpu_priority_;
|
2019-09-20 19:00:55 +00:00
|
|
|
Env::Priority priority_;
|
|
|
|
Env* env_;
|
|
|
|
|
|
|
|
int total_threads_limit_;
|
|
|
|
std::atomic_uint queue_len_; // Queue length. Used for stats reporting
|
|
|
|
bool exit_all_threads_;
|
|
|
|
bool wait_for_jobs_to_complete_;
|
|
|
|
|
|
|
|
// Entry per Schedule()/Submit() call
|
|
|
|
struct BGItem {
|
|
|
|
void* tag = nullptr;
|
|
|
|
std::function<void()> function;
|
|
|
|
std::function<void()> unschedFunction;
|
2017-02-06 22:43:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
using BGQueue = std::deque<BGItem>;
|
|
|
|
BGQueue queue_;
|
|
|
|
|
|
|
|
std::mutex mu_;
|
|
|
|
std::condition_variable bgsignal_;
|
|
|
|
std::vector<port::Thread> bgthreads_;
|
|
|
|
};
|
|
|
|
|
2020-06-13 20:23:40 +00:00
|
|
|
inline ThreadPoolImpl::Impl::Impl()
|
|
|
|
: low_io_priority_(false),
|
|
|
|
cpu_priority_(CpuPriority::kNormal),
|
2017-02-06 22:43:55 +00:00
|
|
|
priority_(Env::LOW),
|
|
|
|
env_(nullptr),
|
2017-08-03 22:36:28 +00:00
|
|
|
total_threads_limit_(0),
|
2016-05-13 01:34:04 +00:00
|
|
|
queue_len_(),
|
2015-10-27 12:03:43 +00:00
|
|
|
exit_all_threads_(false),
|
2017-02-06 22:43:55 +00:00
|
|
|
wait_for_jobs_to_complete_(false),
|
|
|
|
queue_(),
|
|
|
|
mu_(),
|
|
|
|
bgsignal_(),
|
2020-06-13 20:23:40 +00:00
|
|
|
bgthreads_() {}
|
2015-10-27 12:03:43 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
inline
|
|
|
|
ThreadPoolImpl::Impl::~Impl() { assert(bgthreads_.size() == 0U); }
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
void ThreadPoolImpl::Impl::JoinThreads(bool wait_for_jobs_to_complete) {
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(mu_);
|
2015-10-27 12:03:43 +00:00
|
|
|
assert(!exit_all_threads_);
|
2017-02-06 22:43:55 +00:00
|
|
|
|
|
|
|
wait_for_jobs_to_complete_ = wait_for_jobs_to_complete;
|
2015-10-27 12:03:43 +00:00
|
|
|
exit_all_threads_ = true;
|
2017-10-03 23:25:42 +00:00
|
|
|
// prevent threads from being recreated right after they're joined, in case
|
|
|
|
// the user is concurrently submitting jobs.
|
|
|
|
total_threads_limit_ = 0;
|
2017-02-06 22:43:55 +00:00
|
|
|
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
bgsignal_.notify_all();
|
2016-05-13 01:34:04 +00:00
|
|
|
|
|
|
|
for (auto& th : bgthreads_) {
|
2017-02-06 22:43:55 +00:00
|
|
|
th.join();
|
2015-10-27 12:03:43 +00:00
|
|
|
}
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2015-10-27 12:03:43 +00:00
|
|
|
bgthreads_.clear();
|
2017-02-06 22:43:55 +00:00
|
|
|
|
|
|
|
exit_all_threads_ = false;
|
|
|
|
wait_for_jobs_to_complete_ = false;
|
2015-10-27 12:03:43 +00:00
|
|
|
}
|
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
inline
|
|
|
|
void ThreadPoolImpl::Impl::LowerIOPriority() {
|
|
|
|
std::lock_guard<std::mutex> lock(mu_);
|
2015-10-27 12:03:43 +00:00
|
|
|
low_io_priority_ = true;
|
|
|
|
}
|
|
|
|
|
2020-06-13 20:23:40 +00:00
|
|
|
inline void ThreadPoolImpl::Impl::LowerCPUPriority(CpuPriority pri) {
|
2018-04-24 15:38:01 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mu_);
|
2020-06-13 20:23:40 +00:00
|
|
|
cpu_priority_ = pri;
|
2018-04-24 15:38:01 +00:00
|
|
|
}
|
2017-02-06 22:43:55 +00:00
|
|
|
|
|
|
|
void ThreadPoolImpl::Impl::BGThread(size_t thread_id) {
|
2015-10-27 12:03:43 +00:00
|
|
|
bool low_io_priority = false;
|
2020-06-13 20:23:40 +00:00
|
|
|
CpuPriority current_cpu_priority = CpuPriority::kNormal;
|
2018-04-24 15:38:01 +00:00
|
|
|
|
2015-10-27 12:03:43 +00:00
|
|
|
while (true) {
|
2018-10-23 17:33:55 +00:00
|
|
|
// Wait until there is an item that is ready to run
|
2017-02-06 22:43:55 +00:00
|
|
|
std::unique_lock<std::mutex> lock(mu_);
|
2015-10-27 12:03:43 +00:00
|
|
|
// Stop waiting if the thread needs to do work or needs to terminate.
|
|
|
|
while (!exit_all_threads_ && !IsLastExcessiveThread(thread_id) &&
|
|
|
|
(queue_.empty() || IsExcessiveThread(thread_id))) {
|
2017-02-06 22:43:55 +00:00
|
|
|
bgsignal_.wait(lock);
|
2015-10-27 12:03:43 +00:00
|
|
|
}
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2015-10-27 12:03:43 +00:00
|
|
|
if (exit_all_threads_) { // mechanism to let BG threads exit safely
|
2017-02-06 22:43:55 +00:00
|
|
|
|
2018-10-23 17:33:55 +00:00
|
|
|
if (!wait_for_jobs_to_complete_ ||
|
2017-02-06 22:43:55 +00:00
|
|
|
queue_.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
2021-08-07 02:05:01 +00:00
|
|
|
} else if (IsLastExcessiveThread(thread_id)) {
|
2015-10-27 12:03:43 +00:00
|
|
|
// Current thread is the last generated one and is excessive.
|
|
|
|
// We always terminate excessive thread in the reverse order of
|
2021-08-07 02:05:01 +00:00
|
|
|
// generation time. But not when `exit_all_threads_ == true`,
|
|
|
|
// otherwise `JoinThreads()` could try to `join()` a `detach()`ed
|
|
|
|
// thread.
|
2016-05-13 01:34:04 +00:00
|
|
|
auto& terminating_thread = bgthreads_.back();
|
2017-02-06 22:43:55 +00:00
|
|
|
terminating_thread.detach();
|
2015-10-27 12:03:43 +00:00
|
|
|
bgthreads_.pop_back();
|
2017-02-06 22:43:55 +00:00
|
|
|
|
2015-10-27 12:03:43 +00:00
|
|
|
if (HasExcessiveThread()) {
|
|
|
|
// There is still at least more excessive thread to terminate.
|
|
|
|
WakeUpAllThreads();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
auto func = std::move(queue_.front().function);
|
2015-10-27 12:03:43 +00:00
|
|
|
queue_.pop_front();
|
2017-02-06 22:43:55 +00:00
|
|
|
|
2015-10-27 12:03:43 +00:00
|
|
|
queue_len_.store(static_cast<unsigned int>(queue_.size()),
|
|
|
|
std::memory_order_relaxed);
|
|
|
|
|
|
|
|
bool decrease_io_priority = (low_io_priority != low_io_priority_);
|
2020-06-13 20:23:40 +00:00
|
|
|
CpuPriority cpu_priority = cpu_priority_;
|
2017-02-06 22:43:55 +00:00
|
|
|
lock.unlock();
|
2015-10-27 12:03:43 +00:00
|
|
|
|
2020-06-13 20:23:40 +00:00
|
|
|
if (cpu_priority < current_cpu_priority) {
|
|
|
|
TEST_SYNC_POINT_CALLBACK("ThreadPoolImpl::BGThread::BeforeSetCpuPriority",
|
|
|
|
¤t_cpu_priority);
|
2020-03-29 02:05:54 +00:00
|
|
|
// 0 means current thread.
|
2020-06-13 20:23:40 +00:00
|
|
|
port::SetCpuPriority(0, cpu_priority);
|
|
|
|
current_cpu_priority = cpu_priority;
|
|
|
|
TEST_SYNC_POINT_CALLBACK("ThreadPoolImpl::BGThread::AfterSetCpuPriority",
|
|
|
|
¤t_cpu_priority);
|
2018-04-24 15:38:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 20:23:40 +00:00
|
|
|
#ifdef OS_LINUX
|
2015-10-27 12:03:43 +00:00
|
|
|
if (decrease_io_priority) {
|
|
|
|
#define IOPRIO_CLASS_SHIFT (13)
|
|
|
|
#define IOPRIO_PRIO_VALUE(class, data) (((class) << IOPRIO_CLASS_SHIFT) | data)
|
|
|
|
// Put schedule into IOPRIO_CLASS_IDLE class (lowest)
|
|
|
|
// These system calls only have an effect when used in conjunction
|
|
|
|
// with an I/O scheduler that supports I/O priorities. As at
|
|
|
|
// kernel 2.6.17 the only such scheduler is the Completely
|
|
|
|
// Fair Queuing (CFQ) I/O scheduler.
|
|
|
|
// To change scheduler:
|
|
|
|
// echo cfq > /sys/block/<device_name>/queue/schedule
|
|
|
|
// Tunables to consider:
|
|
|
|
// /sys/block/<device_name>/queue/slice_idle
|
|
|
|
// /sys/block/<device_name>/queue/slice_sync
|
|
|
|
syscall(SYS_ioprio_set, 1, // IOPRIO_WHO_PROCESS
|
|
|
|
0, // current thread
|
|
|
|
IOPRIO_PRIO_VALUE(3, 0));
|
|
|
|
low_io_priority = true;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
(void)decrease_io_priority; // avoid 'unused variable' error
|
|
|
|
#endif
|
2020-03-25 03:20:33 +00:00
|
|
|
|
|
|
|
TEST_SYNC_POINT_CALLBACK("ThreadPoolImpl::Impl::BGThread:BeforeRun",
|
|
|
|
&priority_);
|
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
func();
|
2015-10-27 12:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper struct for passing arguments when creating threads.
|
|
|
|
struct BGThreadMetadata {
|
2017-02-06 22:43:55 +00:00
|
|
|
ThreadPoolImpl::Impl* thread_pool_;
|
2015-10-27 12:03:43 +00:00
|
|
|
size_t thread_id_; // Thread count in the thread.
|
2017-02-06 22:43:55 +00:00
|
|
|
BGThreadMetadata(ThreadPoolImpl::Impl* thread_pool, size_t thread_id)
|
2015-10-27 12:03:43 +00:00
|
|
|
: thread_pool_(thread_pool), thread_id_(thread_id) {}
|
|
|
|
};
|
|
|
|
|
2019-08-16 20:54:23 +00:00
|
|
|
void ThreadPoolImpl::Impl::BGThreadWrapper(void* arg) {
|
2015-10-27 12:03:43 +00:00
|
|
|
BGThreadMetadata* meta = reinterpret_cast<BGThreadMetadata*>(arg);
|
|
|
|
size_t thread_id = meta->thread_id_;
|
2017-02-06 22:43:55 +00:00
|
|
|
ThreadPoolImpl::Impl* tp = meta->thread_pool_;
|
2016-12-14 02:22:00 +00:00
|
|
|
#ifdef ROCKSDB_USING_THREAD_STATUS
|
2017-12-14 22:41:59 +00:00
|
|
|
// initialize it because compiler isn't good enough to see we don't use it
|
|
|
|
// uninitialized
|
|
|
|
ThreadStatus::ThreadType thread_type = ThreadStatus::NUM_THREAD_TYPES;
|
|
|
|
switch (tp->GetThreadPriority()) {
|
|
|
|
case Env::Priority::HIGH:
|
|
|
|
thread_type = ThreadStatus::HIGH_PRIORITY;
|
|
|
|
break;
|
|
|
|
case Env::Priority::LOW:
|
|
|
|
thread_type = ThreadStatus::LOW_PRIORITY;
|
|
|
|
break;
|
|
|
|
case Env::Priority::BOTTOM:
|
|
|
|
thread_type = ThreadStatus::BOTTOM_PRIORITY;
|
|
|
|
break;
|
2019-03-20 00:24:09 +00:00
|
|
|
case Env::Priority::USER:
|
|
|
|
thread_type = ThreadStatus::USER;
|
|
|
|
break;
|
2017-12-14 22:41:59 +00:00
|
|
|
case Env::Priority::TOTAL:
|
|
|
|
assert(false);
|
2019-08-16 20:54:23 +00:00
|
|
|
return;
|
2017-12-14 22:41:59 +00:00
|
|
|
}
|
|
|
|
assert(thread_type != ThreadStatus::NUM_THREAD_TYPES);
|
|
|
|
ThreadStatusUtil::RegisterThread(tp->GetHostEnv(), thread_type);
|
2015-10-27 12:03:43 +00:00
|
|
|
#endif
|
|
|
|
delete meta;
|
|
|
|
tp->BGThread(thread_id);
|
2016-12-14 02:22:00 +00:00
|
|
|
#ifdef ROCKSDB_USING_THREAD_STATUS
|
2015-10-27 12:03:43 +00:00
|
|
|
ThreadStatusUtil::UnregisterThread();
|
|
|
|
#endif
|
2019-08-16 20:54:23 +00:00
|
|
|
return;
|
2015-10-27 12:03:43 +00:00
|
|
|
}
|
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
void ThreadPoolImpl::Impl::SetBackgroundThreadsInternal(int num,
|
|
|
|
bool allow_reduce) {
|
2019-09-18 21:23:40 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mu_);
|
2015-10-27 12:03:43 +00:00
|
|
|
if (exit_all_threads_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (num > total_threads_limit_ ||
|
|
|
|
(num < total_threads_limit_ && allow_reduce)) {
|
2017-05-23 18:04:25 +00:00
|
|
|
total_threads_limit_ = std::max(0, num);
|
2015-10-27 12:03:43 +00:00
|
|
|
WakeUpAllThreads();
|
|
|
|
StartBGThreads();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:04:25 +00:00
|
|
|
int ThreadPoolImpl::Impl::GetBackgroundThreads() {
|
|
|
|
std::unique_lock<std::mutex> lock(mu_);
|
|
|
|
return total_threads_limit_;
|
|
|
|
}
|
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
void ThreadPoolImpl::Impl::StartBGThreads() {
|
2015-10-27 12:03:43 +00:00
|
|
|
// Start background thread if necessary
|
|
|
|
while ((int)bgthreads_.size() < total_threads_limit_) {
|
2017-02-06 22:43:55 +00:00
|
|
|
|
|
|
|
port::Thread p_t(&BGThreadWrapper,
|
2016-05-13 01:34:04 +00:00
|
|
|
new BGThreadMetadata(this, bgthreads_.size()));
|
2017-02-06 22:43:55 +00:00
|
|
|
|
2015-10-27 12:03:43 +00:00
|
|
|
// Set the thread name to aid debugging
|
|
|
|
#if defined(_GNU_SOURCE) && defined(__GLIBC_PREREQ)
|
|
|
|
#if __GLIBC_PREREQ(2, 12)
|
2017-02-06 22:43:55 +00:00
|
|
|
auto th_handle = p_t.native_handle();
|
2018-04-19 00:25:37 +00:00
|
|
|
std::string thread_priority = Env::PriorityToString(GetThreadPriority());
|
|
|
|
std::ostringstream thread_name_stream;
|
|
|
|
thread_name_stream << "rocksdb:";
|
|
|
|
for (char c : thread_priority) {
|
|
|
|
thread_name_stream << static_cast<char>(tolower(c));
|
|
|
|
}
|
|
|
|
pthread_setname_np(th_handle, thread_name_stream.str().c_str());
|
2015-10-27 12:03:43 +00:00
|
|
|
#endif
|
2016-05-13 01:34:04 +00:00
|
|
|
#endif
|
2017-02-06 22:43:55 +00:00
|
|
|
bgthreads_.push_back(std::move(p_t));
|
2015-10-27 12:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
void ThreadPoolImpl::Impl::Submit(std::function<void()>&& schedule,
|
|
|
|
std::function<void()>&& unschedule, void* tag) {
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(mu_);
|
2015-10-27 12:03:43 +00:00
|
|
|
|
|
|
|
if (exit_all_threads_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
StartBGThreads();
|
|
|
|
|
|
|
|
// Add to priority queue
|
|
|
|
queue_.push_back(BGItem());
|
2017-02-06 22:43:55 +00:00
|
|
|
|
|
|
|
auto& item = queue_.back();
|
|
|
|
item.tag = tag;
|
|
|
|
item.function = std::move(schedule);
|
|
|
|
item.unschedFunction = std::move(unschedule);
|
|
|
|
|
2015-10-27 12:03:43 +00:00
|
|
|
queue_len_.store(static_cast<unsigned int>(queue_.size()),
|
2017-02-06 22:43:55 +00:00
|
|
|
std::memory_order_relaxed);
|
2015-10-27 12:03:43 +00:00
|
|
|
|
|
|
|
if (!HasExcessiveThread()) {
|
|
|
|
// Wake up at least one waiting thread.
|
2017-02-06 22:43:55 +00:00
|
|
|
bgsignal_.notify_one();
|
2015-10-27 12:03:43 +00:00
|
|
|
} else {
|
|
|
|
// Need to wake up all threads to make sure the one woken
|
|
|
|
// up is not the one to terminate.
|
|
|
|
WakeUpAllThreads();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
int ThreadPoolImpl::Impl::UnSchedule(void* arg) {
|
2015-10-27 12:03:43 +00:00
|
|
|
int count = 0;
|
2016-05-13 01:34:04 +00:00
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
std::vector<std::function<void()>> candidates;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mu_);
|
|
|
|
|
|
|
|
// Remove from priority queue
|
|
|
|
BGQueue::iterator it = queue_.begin();
|
|
|
|
while (it != queue_.end()) {
|
|
|
|
if (arg == (*it).tag) {
|
|
|
|
if (it->unschedFunction) {
|
|
|
|
candidates.push_back(std::move(it->unschedFunction));
|
|
|
|
}
|
|
|
|
it = queue_.erase(it);
|
|
|
|
count++;
|
|
|
|
} else {
|
|
|
|
++it;
|
Running manual compactions in parallel with other automatic or manual compactions in restricted cases
Summary:
This diff provides a framework for doing manual
compactions in parallel with other compactions. We now have a deque of manual compactions. We also pass manual compactions as an argument from RunManualCompactions down to
BackgroundCompactions, so that RunManualCompactions can be reentrant.
Parallelism is controlled by the two routines
ConflictingManualCompaction to allow/disallow new parallel/manual
compactions based on already existing ManualCompactions. In this diff, by default manual compactions still have to run exclusive of other compactions. However, by setting the compaction option, exclusive_manual_compaction to false, it is possible to run other compactions in parallel with a manual compaction. However, we are still restricted to one manual compaction per column family at a time. All of these restrictions will be relaxed in future diffs.
I will be adding more tests later.
Test Plan: Rocksdb regression + new tests + valgrind
Reviewers: igor, anthony, IslamAbdelRahman, kradhakrishnan, yhchiang, sdong
Reviewed By: sdong
Subscribers: yoshinorim, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D47973
2015-12-14 19:20:34 +00:00
|
|
|
}
|
2015-10-27 12:03:43 +00:00
|
|
|
}
|
2017-02-06 22:43:55 +00:00
|
|
|
queue_len_.store(static_cast<unsigned int>(queue_.size()),
|
|
|
|
std::memory_order_relaxed);
|
2015-10-27 12:03:43 +00:00
|
|
|
}
|
2017-02-06 22:43:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Run unschedule functions outside the mutex
|
|
|
|
for (auto& f : candidates) {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
|
2015-10-27 12:03:43 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2017-08-03 22:36:28 +00:00
|
|
|
ThreadPoolImpl::ThreadPoolImpl() :
|
2017-02-06 22:43:55 +00:00
|
|
|
impl_(new Impl()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ThreadPoolImpl::~ThreadPoolImpl() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPoolImpl::JoinAllThreads() {
|
|
|
|
impl_->JoinThreads(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPoolImpl::SetBackgroundThreads(int num) {
|
|
|
|
impl_->SetBackgroundThreadsInternal(num, true);
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:04:25 +00:00
|
|
|
int ThreadPoolImpl::GetBackgroundThreads() {
|
|
|
|
return impl_->GetBackgroundThreads();
|
|
|
|
}
|
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
unsigned int ThreadPoolImpl::GetQueueLen() const {
|
|
|
|
return impl_->GetQueueLen();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPoolImpl::WaitForJobsAndJoinAllThreads() {
|
|
|
|
impl_->JoinThreads(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPoolImpl::LowerIOPriority() {
|
|
|
|
impl_->LowerIOPriority();
|
|
|
|
}
|
|
|
|
|
2020-06-13 20:23:40 +00:00
|
|
|
void ThreadPoolImpl::LowerCPUPriority(CpuPriority pri) {
|
|
|
|
impl_->LowerCPUPriority(pri);
|
2018-04-24 15:38:01 +00:00
|
|
|
}
|
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
void ThreadPoolImpl::IncBackgroundThreadsIfNeeded(int num) {
|
|
|
|
impl_->SetBackgroundThreadsInternal(num, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPoolImpl::SubmitJob(const std::function<void()>& job) {
|
|
|
|
auto copy(job);
|
|
|
|
impl_->Submit(std::move(copy), std::function<void()>(), nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ThreadPoolImpl::SubmitJob(std::function<void()>&& job) {
|
|
|
|
impl_->Submit(std::move(job), std::function<void()>(), nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPoolImpl::Schedule(void(*function)(void* arg1), void* arg,
|
|
|
|
void* tag, void(*unschedFunction)(void* arg)) {
|
2018-08-21 23:28:41 +00:00
|
|
|
if (unschedFunction == nullptr) {
|
|
|
|
impl_->Submit(std::bind(function, arg), std::function<void()>(), tag);
|
|
|
|
} else {
|
|
|
|
impl_->Submit(std::bind(function, arg), std::bind(unschedFunction, arg),
|
|
|
|
tag);
|
2017-02-06 22:43:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ThreadPoolImpl::UnSchedule(void* arg) {
|
|
|
|
return impl_->UnSchedule(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPoolImpl::SetHostEnv(Env* env) { impl_->SetHostEnv(env); }
|
|
|
|
|
|
|
|
Env* ThreadPoolImpl::GetHostEnv() const { return impl_->GetHostEnv(); }
|
|
|
|
|
|
|
|
// Return the thread priority.
|
|
|
|
// This would allow its member-thread to know its priority.
|
|
|
|
Env::Priority ThreadPoolImpl::GetThreadPriority() const {
|
|
|
|
return impl_->GetThreadPriority();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the thread priority.
|
|
|
|
void ThreadPoolImpl::SetThreadPriority(Env::Priority priority) {
|
|
|
|
impl_->SetThreadPriority(priority);
|
|
|
|
}
|
|
|
|
|
2016-08-26 17:41:35 +00:00
|
|
|
ThreadPool* NewThreadPool(int num_threads) {
|
|
|
|
ThreadPoolImpl* thread_pool = new ThreadPoolImpl();
|
|
|
|
thread_pool->SetBackgroundThreads(num_threads);
|
|
|
|
return thread_pool;
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|