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
|
2022-10-26 19:08:20 +00:00
|
|
|
#include <unistd.h>
|
2016-05-13 01:34:04 +00:00
|
|
|
#endif
|
|
|
|
|
2015-10-27 12:03:43 +00:00
|
|
|
#ifdef OS_LINUX
|
2022-10-26 19:08:20 +00:00
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/syscall.h>
|
2015-10-27 12:03:43 +00:00
|
|
|
#endif
|
|
|
|
|
2017-02-06 22:43:55 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <atomic>
|
|
|
|
#include <condition_variable>
|
2024-03-04 18:08:32 +00:00
|
|
|
#include <cstdlib>
|
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 {
|
|
|
|
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
|
|
|
|
2022-10-26 19:08:20 +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,
|
2022-10-26 19:08:20 +00:00
|
|
|
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
|
|
|
|
2022-07-09 02:48:09 +00:00
|
|
|
int ReserveThreads(int threads_to_be_reserved) {
|
|
|
|
std::unique_lock<std::mutex> lock(mu_);
|
|
|
|
// We can reserve at most num_waiting_threads_ in total so the number of
|
|
|
|
// threads that can be reserved might be fewer than the desired one. In
|
|
|
|
// rare cases, num_waiting_threads_ could be less than reserved_threads
|
|
|
|
// due to SetBackgroundThreadInternal or last excessive threads. If that
|
|
|
|
// happens, we cannot reserve any other threads.
|
|
|
|
int reserved_threads_in_success =
|
|
|
|
std::min(std::max(num_waiting_threads_ - reserved_threads_, 0),
|
|
|
|
threads_to_be_reserved);
|
|
|
|
reserved_threads_ += reserved_threads_in_success;
|
|
|
|
return reserved_threads_in_success;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ReleaseThreads(int threads_to_be_released) {
|
|
|
|
std::unique_lock<std::mutex> lock(mu_);
|
|
|
|
// We cannot release more than reserved_threads_
|
|
|
|
int released_threads_in_success =
|
|
|
|
std::min(reserved_threads_, threads_to_be_released);
|
|
|
|
reserved_threads_ -= released_threads_in_success;
|
|
|
|
WakeUpAllThreads();
|
|
|
|
return released_threads_in_success;
|
|
|
|
}
|
|
|
|
|
2022-10-26 19:08:20 +00:00
|
|
|
private:
|
|
|
|
static void BGThreadWrapper(void* arg);
|
|
|
|
|
|
|
|
bool low_io_priority_;
|
|
|
|
CpuPriority cpu_priority_;
|
|
|
|
Env::Priority priority_;
|
|
|
|
Env* env_;
|
|
|
|
|
|
|
|
int total_threads_limit_;
|
|
|
|
std::atomic_uint queue_len_; // Queue length. Used for stats reporting
|
|
|
|
// Number of reserved threads, managed by ReserveThreads(..) and
|
|
|
|
// ReleaseThreads(..), if num_waiting_threads_ is no larger than
|
|
|
|
// reserved_threads_, its thread will be blocked to ensure the reservation
|
|
|
|
// mechanism
|
|
|
|
int reserved_threads_;
|
|
|
|
// Number of waiting threads (Maximum number of threads that can be
|
|
|
|
// reserved), in rare cases, num_waiting_threads_ could be less than
|
|
|
|
// reserved_threads due to SetBackgroundThreadInternal or last
|
|
|
|
// excessive threads.
|
|
|
|
int num_waiting_threads_;
|
|
|
|
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>;
|
2022-10-26 19:08:20 +00:00
|
|
|
BGQueue queue_;
|
2017-02-06 22:43:55 +00:00
|
|
|
|
2022-10-26 19:08:20 +00:00
|
|
|
std::mutex mu_;
|
|
|
|
std::condition_variable bgsignal_;
|
2017-02-06 22:43:55 +00:00
|
|
|
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_(),
|
2022-07-09 02:48:09 +00:00
|
|
|
reserved_threads_(0),
|
|
|
|
num_waiting_threads_(0),
|
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
|
|
|
|
2022-10-26 19:08:20 +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;
|
2022-07-09 02:48:09 +00:00
|
|
|
reserved_threads_ = 0;
|
|
|
|
num_waiting_threads_ = 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
|
|
|
}
|
|
|
|
|
2022-10-26 19:08:20 +00:00
|
|
|
inline void ThreadPoolImpl::Impl::LowerIOPriority() {
|
2017-02-06 22:43:55 +00:00
|
|
|
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.
|
2022-07-09 02:48:09 +00:00
|
|
|
// Increase num_waiting_threads_ once this task has started waiting
|
|
|
|
num_waiting_threads_++;
|
|
|
|
|
|
|
|
TEST_SYNC_POINT("ThreadPoolImpl::BGThread::WaitingThreadsInc");
|
|
|
|
TEST_IDX_SYNC_POINT("ThreadPoolImpl::BGThread::Start:th", thread_id);
|
|
|
|
// When not exist_all_threads and the current thread id is not the last
|
|
|
|
// excessive thread, it may be blocked due to 3 reasons: 1) queue is empty
|
|
|
|
// 2) it is the excessive thread (not the last one)
|
|
|
|
// 3) the number of waiting threads is not greater than reserved threads
|
|
|
|
// (i.e, no available threads due to full reservation")
|
2015-10-27 12:03:43 +00:00
|
|
|
while (!exit_all_threads_ && !IsLastExcessiveThread(thread_id) &&
|
2022-07-09 02:48:09 +00:00
|
|
|
(queue_.empty() || IsExcessiveThread(thread_id) ||
|
|
|
|
num_waiting_threads_ <= reserved_threads_)) {
|
2017-02-06 22:43:55 +00:00
|
|
|
bgsignal_.wait(lock);
|
2015-10-27 12:03:43 +00:00
|
|
|
}
|
2022-07-09 02:48:09 +00:00
|
|
|
// Decrease num_waiting_threads_ once the thread is not waiting
|
|
|
|
num_waiting_threads_--;
|
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
|
|
|
|
2022-10-26 19:08:20 +00:00
|
|
|
if (!wait_for_jobs_to_complete_ || queue_.empty()) {
|
2017-02-06 22:43:55 +00:00
|
|
|
break;
|
2022-10-26 19:08:20 +00:00
|
|
|
}
|
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();
|
|
|
|
if (HasExcessiveThread()) {
|
|
|
|
// There is still at least more excessive thread to terminate.
|
|
|
|
WakeUpAllThreads();
|
|
|
|
}
|
2022-07-09 02:48:09 +00:00
|
|
|
TEST_IDX_SYNC_POINT("ThreadPoolImpl::BGThread::Termination:th",
|
|
|
|
thread_id);
|
|
|
|
TEST_SYNC_POINT("ThreadPoolImpl::BGThread::Termination");
|
2015-10-27 12:03:43 +00:00
|
|
|
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) {
|
Prefer static_cast in place of most reinterpret_cast (#12308)
Summary:
The following are risks associated with pointer-to-pointer reinterpret_cast:
* Can produce the "wrong result" (crash or memory corruption). IIRC, in theory this can happen for any up-cast or down-cast for a non-standard-layout type, though in practice would only happen for multiple inheritance cases (where the base class pointer might be "inside" the derived object). We don't use multiple inheritance a lot, but we do.
* Can mask useful compiler errors upon code change, including converting between unrelated pointer types that you are expecting to be related, and converting between pointer and scalar types unintentionally.
I can only think of some obscure cases where static_cast could be troublesome when it compiles as a replacement:
* Going through `void*` could plausibly cause unnecessary or broken pointer arithmetic. Suppose we have
`struct Derived: public Base1, public Base2`. If we have `Derived*` -> `void*` -> `Base2*` -> `Derived*` through reinterpret casts, this could plausibly work (though technical UB) assuming the `Base2*` is not dereferenced. Changing to static cast could introduce breaking pointer arithmetic.
* Unnecessary (but safe) pointer arithmetic could arise in a case like `Derived*` -> `Base2*` -> `Derived*` where before the Base2 pointer might not have been dereferenced. This could potentially affect performance.
With some light scripting, I tried replacing pointer-to-pointer reinterpret_casts with static_cast and kept the cases that still compile. Most occurrences of reinterpret_cast have successfully been changed (except for java/ and third-party/). 294 changed, 257 remain.
A couple of related interventions included here:
* Previously Cache::Handle was not actually derived from in the implementations and just used as a `void*` stand-in with reinterpret_cast. Now there is a relationship to allow static_cast. In theory, this could introduce pointer arithmetic (as described above) but is unlikely without multiple inheritance AND non-empty Cache::Handle.
* Remove some unnecessary casts to void* as this is allowed to be implicit (for better or worse).
Most of the remaining reinterpret_casts are for converting to/from raw bytes of objects. We could consider better idioms for these patterns in follow-up work.
I wish there were a way to implement a template variant of static_cast that would only compile if no pointer arithmetic is generated, but best I can tell, this is not possible. AFAIK the best you could do is a dynamic check that the void* conversion after the static cast is unchanged.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/12308
Test Plan: existing tests, CI
Reviewed By: ltamasi
Differential Revision: D53204947
Pulled By: pdillinger
fbshipit-source-id: 9de23e618263b0d5b9820f4e15966876888a16e2
2024-02-07 18:44:11 +00:00
|
|
|
BGThreadMetadata* meta = static_cast<BGThreadMetadata*>(arg);
|
2015-10-27 12:03:43 +00:00
|
|
|
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,
|
2022-10-26 19:08:20 +00:00
|
|
|
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,
|
2022-10-26 19:08:20 +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,
|
2022-10-26 19:08:20 +00:00
|
|
|
std::function<void()>&& unschedule,
|
|
|
|
void* tag) {
|
2017-02-06 22:43:55 +00:00
|
|
|
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());
|
2022-07-09 02:48:09 +00:00
|
|
|
TEST_SYNC_POINT("ThreadPoolImpl::Submit::Enqueue");
|
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()),
|
2022-10-26 19:08:20 +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()),
|
2022-10-26 19:08:20 +00:00
|
|
|
std::memory_order_relaxed);
|
2015-10-27 12:03:43 +00:00
|
|
|
}
|
2017-02-06 22:43:55 +00:00
|
|
|
|
2022-10-26 19:08:20 +00:00
|
|
|
// Run unschedule functions outside the mutex
|
2017-02-06 22:43:55 +00:00
|
|
|
for (auto& f : candidates) {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
|
2015-10-27 12:03:43 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2022-10-26 19:08:20 +00:00
|
|
|
ThreadPoolImpl::ThreadPoolImpl() : impl_(new Impl()) {}
|
2017-02-06 22:43:55 +00:00
|
|
|
|
2024-03-04 18:08:32 +00:00
|
|
|
ThreadPoolImpl::~ThreadPoolImpl() = default;
|
2017-02-06 22:43:55 +00:00
|
|
|
|
2022-10-26 19:08:20 +00:00
|
|
|
void ThreadPoolImpl::JoinAllThreads() { impl_->JoinThreads(false); }
|
2017-02-06 22:43:55 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-10-26 19:08:20 +00:00
|
|
|
void ThreadPoolImpl::LowerIOPriority() { impl_->LowerIOPriority(); }
|
2017-02-06 22:43:55 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-10-26 19:08:20 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 19:08:20 +00:00
|
|
|
int ThreadPoolImpl::UnSchedule(void* arg) { return impl_->UnSchedule(arg); }
|
2017-02-06 22:43:55 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-07-09 02:48:09 +00:00
|
|
|
// Reserve a specific number of threads, prevent them from running other
|
|
|
|
// functions The number of reserved threads could be fewer than the desired one
|
|
|
|
int ThreadPoolImpl::ReserveThreads(int threads_to_be_reserved) {
|
|
|
|
return impl_->ReserveThreads(threads_to_be_reserved);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release a specific number of threads
|
|
|
|
int ThreadPoolImpl::ReleaseThreads(int threads_to_be_released) {
|
|
|
|
return impl_->ReleaseThreads(threads_to_be_released);
|
|
|
|
}
|
|
|
|
|
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
|