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).
|
2015-10-27 13:03:43 +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.
|
|
|
|
|
2016-08-26 19:41:35 +02:00
|
|
|
#include "util/threadpool_imp.h"
|
2017-02-06 23:43:55 +01:00
|
|
|
|
2017-04-06 04:02:00 +02:00
|
|
|
#include "monitoring/thread_status_util.h"
|
2017-02-06 23:43:55 +01:00
|
|
|
#include "port/port.h"
|
2016-05-13 03:34:04 +02:00
|
|
|
|
|
|
|
#ifndef OS_WIN
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2015-10-27 13:03:43 +01:00
|
|
|
#ifdef OS_LINUX
|
2016-05-13 03:34:04 +02:00
|
|
|
# include <sys/syscall.h>
|
2018-04-24 17:38:01 +02:00
|
|
|
# include <sys/resource.h>
|
2015-10-27 13:03:43 +01:00
|
|
|
#endif
|
|
|
|
|
2018-04-19 02:25:37 +02:00
|
|
|
#include <stdlib.h>
|
2017-02-06 23:43:55 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <atomic>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
2018-04-19 02:25:37 +02:00
|
|
|
#include <sstream>
|
2017-02-06 23:43:55 +01:00
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
2016-06-06 02:23:38 +02:00
|
|
|
|
2015-10-27 13:03:43 +01:00
|
|
|
namespace rocksdb {
|
|
|
|
|
2016-08-26 19:41:35 +02:00
|
|
|
void ThreadPoolImpl::PthreadCall(const char* label, int result) {
|
2015-10-27 13:03:43 +01:00
|
|
|
if (result != 0) {
|
|
|
|
fprintf(stderr, "pthread %s: %s\n", label, strerror(result));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
struct ThreadPoolImpl::Impl {
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
Impl();
|
|
|
|
~Impl();
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
void JoinThreads(bool wait_for_jobs_to_complete);
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
void SetBackgroundThreadsInternal(int num, bool allow_reduce);
|
2017-05-23 20:04:25 +02:00
|
|
|
int GetBackgroundThreads();
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
unsigned int GetQueueLen() const {
|
|
|
|
return queue_len_.load(std::memory_order_relaxed);
|
|
|
|
}
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
void LowerIOPriority();
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2018-04-24 17:38:01 +02:00
|
|
|
void LowerCPUPriority();
|
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
void WakeUpAllThreads() {
|
|
|
|
bgsignal_.notify_all();
|
|
|
|
}
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
void BGThread(size_t thread_id);
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
void StartBGThreads();
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
void Submit(std::function<void()>&& schedule,
|
|
|
|
std::function<void()>&& unschedule, void* tag);
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
int UnSchedule(void* arg);
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
void SetHostEnv(Env* env) { env_ = env; }
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
Env* GetHostEnv() const { return env_; }
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
bool HasExcessiveThread() const {
|
|
|
|
return static_cast<int>(bgthreads_.size()) > total_threads_limit_;
|
|
|
|
}
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01: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 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
bool IsExcessiveThread(size_t thread_id) const {
|
|
|
|
return static_cast<int>(thread_id) >= total_threads_limit_;
|
|
|
|
}
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
// Return the thread priority.
|
|
|
|
// This would allow its member-thread to know its priority.
|
|
|
|
Env::Priority GetThreadPriority() const { return priority_; }
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
// Set the thread priority.
|
|
|
|
void SetThreadPriority(Env::Priority priority) { priority_ = priority; }
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
private:
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
static void* BGThreadWrapper(void* arg);
|
|
|
|
|
|
|
|
bool low_io_priority_;
|
2018-04-24 17:38:01 +02:00
|
|
|
bool low_cpu_priority_;
|
2017-02-06 23:43:55 +01: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;
|
|
|
|
};
|
|
|
|
|
|
|
|
using BGQueue = std::deque<BGItem>;
|
|
|
|
BGQueue queue_;
|
|
|
|
|
|
|
|
std::mutex mu_;
|
|
|
|
std::condition_variable bgsignal_;
|
|
|
|
std::vector<port::Thread> bgthreads_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
inline
|
|
|
|
ThreadPoolImpl::Impl::Impl()
|
2017-08-04 00:36:28 +02:00
|
|
|
:
|
2017-02-06 23:43:55 +01:00
|
|
|
low_io_priority_(false),
|
2018-04-24 17:38:01 +02:00
|
|
|
low_cpu_priority_(false),
|
2017-02-06 23:43:55 +01:00
|
|
|
priority_(Env::LOW),
|
|
|
|
env_(nullptr),
|
2017-08-04 00:36:28 +02:00
|
|
|
total_threads_limit_(0),
|
2016-05-13 03:34:04 +02:00
|
|
|
queue_len_(),
|
2015-10-27 13:03:43 +01:00
|
|
|
exit_all_threads_(false),
|
2017-02-06 23:43:55 +01:00
|
|
|
wait_for_jobs_to_complete_(false),
|
|
|
|
queue_(),
|
|
|
|
mu_(),
|
|
|
|
bgsignal_(),
|
|
|
|
bgthreads_() {
|
2015-10-27 13:03:43 +01:00
|
|
|
}
|
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
inline
|
|
|
|
ThreadPoolImpl::Impl::~Impl() { assert(bgthreads_.size() == 0U); }
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
void ThreadPoolImpl::Impl::JoinThreads(bool wait_for_jobs_to_complete) {
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(mu_);
|
2015-10-27 13:03:43 +01:00
|
|
|
assert(!exit_all_threads_);
|
2017-02-06 23:43:55 +01:00
|
|
|
|
|
|
|
wait_for_jobs_to_complete_ = wait_for_jobs_to_complete;
|
2015-10-27 13:03:43 +01:00
|
|
|
exit_all_threads_ = true;
|
2017-10-04 01:25:42 +02: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 23:43:55 +01:00
|
|
|
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
bgsignal_.notify_all();
|
2016-05-13 03:34:04 +02:00
|
|
|
|
|
|
|
for (auto& th : bgthreads_) {
|
2017-02-06 23:43:55 +01:00
|
|
|
th.join();
|
2015-10-27 13:03:43 +01:00
|
|
|
}
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2015-10-27 13:03:43 +01:00
|
|
|
bgthreads_.clear();
|
2017-02-06 23:43:55 +01:00
|
|
|
|
|
|
|
exit_all_threads_ = false;
|
|
|
|
wait_for_jobs_to_complete_ = false;
|
2015-10-27 13:03:43 +01:00
|
|
|
}
|
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
inline
|
|
|
|
void ThreadPoolImpl::Impl::LowerIOPriority() {
|
|
|
|
std::lock_guard<std::mutex> lock(mu_);
|
2015-10-27 13:03:43 +01:00
|
|
|
low_io_priority_ = true;
|
|
|
|
}
|
|
|
|
|
2018-04-24 17:38:01 +02:00
|
|
|
inline
|
|
|
|
void ThreadPoolImpl::Impl::LowerCPUPriority() {
|
|
|
|
std::lock_guard<std::mutex> lock(mu_);
|
|
|
|
low_cpu_priority_ = true;
|
|
|
|
}
|
2017-02-06 23:43:55 +01:00
|
|
|
|
|
|
|
void ThreadPoolImpl::Impl::BGThread(size_t thread_id) {
|
2015-10-27 13:03:43 +01:00
|
|
|
bool low_io_priority = false;
|
2018-04-24 17:38:01 +02:00
|
|
|
bool low_cpu_priority = false;
|
|
|
|
|
2015-10-27 13:03:43 +01:00
|
|
|
while (true) {
|
2016-05-13 03:34:04 +02:00
|
|
|
// Wait until there is an item that is ready to run
|
2017-02-06 23:43:55 +01:00
|
|
|
std::unique_lock<std::mutex> lock(mu_);
|
2015-10-27 13:03:43 +01: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 23:43:55 +01:00
|
|
|
bgsignal_.wait(lock);
|
2015-10-27 13:03:43 +01:00
|
|
|
}
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2015-10-27 13:03:43 +01:00
|
|
|
if (exit_all_threads_) { // mechanism to let BG threads exit safely
|
2017-02-06 23:43:55 +01:00
|
|
|
|
|
|
|
if(!wait_for_jobs_to_complete_ ||
|
|
|
|
queue_.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
2015-10-27 13:03:43 +01:00
|
|
|
}
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2015-10-27 13:03:43 +01:00
|
|
|
if (IsLastExcessiveThread(thread_id)) {
|
|
|
|
// Current thread is the last generated one and is excessive.
|
|
|
|
// We always terminate excessive thread in the reverse order of
|
|
|
|
// generation time.
|
2016-05-13 03:34:04 +02:00
|
|
|
auto& terminating_thread = bgthreads_.back();
|
2017-02-06 23:43:55 +01:00
|
|
|
terminating_thread.detach();
|
2015-10-27 13:03:43 +01:00
|
|
|
bgthreads_.pop_back();
|
2017-02-06 23:43:55 +01:00
|
|
|
|
2015-10-27 13:03:43 +01:00
|
|
|
if (HasExcessiveThread()) {
|
|
|
|
// There is still at least more excessive thread to terminate.
|
|
|
|
WakeUpAllThreads();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
auto func = std::move(queue_.front().function);
|
2015-10-27 13:03:43 +01:00
|
|
|
queue_.pop_front();
|
2017-02-06 23:43:55 +01:00
|
|
|
|
2015-10-27 13:03:43 +01:00
|
|
|
queue_len_.store(static_cast<unsigned int>(queue_.size()),
|
|
|
|
std::memory_order_relaxed);
|
|
|
|
|
|
|
|
bool decrease_io_priority = (low_io_priority != low_io_priority_);
|
2018-04-24 17:38:01 +02:00
|
|
|
bool decrease_cpu_priority = (low_cpu_priority != low_cpu_priority_);
|
2017-02-06 23:43:55 +01:00
|
|
|
lock.unlock();
|
2015-10-27 13:03:43 +01:00
|
|
|
|
|
|
|
#ifdef OS_LINUX
|
2018-04-24 17:38:01 +02:00
|
|
|
if (decrease_cpu_priority) {
|
|
|
|
setpriority(
|
|
|
|
PRIO_PROCESS,
|
|
|
|
// Current thread.
|
|
|
|
0,
|
|
|
|
// Lowest priority possible.
|
|
|
|
19);
|
|
|
|
low_cpu_priority = true;
|
|
|
|
}
|
|
|
|
|
2015-10-27 13:03:43 +01: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
|
2018-04-24 17:38:01 +02:00
|
|
|
(void)decrease_cpu_priority;
|
2015-10-27 13:03:43 +01:00
|
|
|
#endif
|
2017-02-06 23:43:55 +01:00
|
|
|
func();
|
2015-10-27 13:03:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper struct for passing arguments when creating threads.
|
|
|
|
struct BGThreadMetadata {
|
2017-02-06 23:43:55 +01:00
|
|
|
ThreadPoolImpl::Impl* thread_pool_;
|
2015-10-27 13:03:43 +01:00
|
|
|
size_t thread_id_; // Thread count in the thread.
|
2017-02-06 23:43:55 +01:00
|
|
|
BGThreadMetadata(ThreadPoolImpl::Impl* thread_pool, size_t thread_id)
|
2015-10-27 13:03:43 +01:00
|
|
|
: thread_pool_(thread_pool), thread_id_(thread_id) {}
|
|
|
|
};
|
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
void* ThreadPoolImpl::Impl::BGThreadWrapper(void* arg) {
|
2015-10-27 13:03:43 +01:00
|
|
|
BGThreadMetadata* meta = reinterpret_cast<BGThreadMetadata*>(arg);
|
|
|
|
size_t thread_id = meta->thread_id_;
|
2017-02-06 23:43:55 +01:00
|
|
|
ThreadPoolImpl::Impl* tp = meta->thread_pool_;
|
2016-12-14 03:22:00 +01:00
|
|
|
#ifdef ROCKSDB_USING_THREAD_STATUS
|
2017-12-14 23:41:59 +01: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;
|
|
|
|
case Env::Priority::TOTAL:
|
|
|
|
assert(false);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
assert(thread_type != ThreadStatus::NUM_THREAD_TYPES);
|
|
|
|
ThreadStatusUtil::RegisterThread(tp->GetHostEnv(), thread_type);
|
2015-10-27 13:03:43 +01:00
|
|
|
#endif
|
|
|
|
delete meta;
|
|
|
|
tp->BGThread(thread_id);
|
2016-12-14 03:22:00 +01:00
|
|
|
#ifdef ROCKSDB_USING_THREAD_STATUS
|
2015-10-27 13:03:43 +01:00
|
|
|
ThreadStatusUtil::UnregisterThread();
|
|
|
|
#endif
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
void ThreadPoolImpl::Impl::SetBackgroundThreadsInternal(int num,
|
|
|
|
bool allow_reduce) {
|
|
|
|
std::unique_lock<std::mutex> lock(mu_);
|
2015-10-27 13:03:43 +01:00
|
|
|
if (exit_all_threads_) {
|
2017-02-06 23:43:55 +01:00
|
|
|
lock.unlock();
|
2015-10-27 13:03:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (num > total_threads_limit_ ||
|
|
|
|
(num < total_threads_limit_ && allow_reduce)) {
|
2017-05-23 20:04:25 +02:00
|
|
|
total_threads_limit_ = std::max(0, num);
|
2015-10-27 13:03:43 +01:00
|
|
|
WakeUpAllThreads();
|
|
|
|
StartBGThreads();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 20:04:25 +02:00
|
|
|
int ThreadPoolImpl::Impl::GetBackgroundThreads() {
|
|
|
|
std::unique_lock<std::mutex> lock(mu_);
|
|
|
|
return total_threads_limit_;
|
|
|
|
}
|
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
void ThreadPoolImpl::Impl::StartBGThreads() {
|
2015-10-27 13:03:43 +01:00
|
|
|
// Start background thread if necessary
|
|
|
|
while ((int)bgthreads_.size() < total_threads_limit_) {
|
2017-02-06 23:43:55 +01:00
|
|
|
|
|
|
|
port::Thread p_t(&BGThreadWrapper,
|
2016-05-13 03:34:04 +02:00
|
|
|
new BGThreadMetadata(this, bgthreads_.size()));
|
2017-02-06 23:43:55 +01:00
|
|
|
|
2015-10-27 13:03:43 +01:00
|
|
|
// Set the thread name to aid debugging
|
|
|
|
#if defined(_GNU_SOURCE) && defined(__GLIBC_PREREQ)
|
|
|
|
#if __GLIBC_PREREQ(2, 12)
|
2017-02-06 23:43:55 +01:00
|
|
|
auto th_handle = p_t.native_handle();
|
2018-04-19 02:25:37 +02: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));
|
|
|
|
}
|
|
|
|
thread_name_stream << bgthreads_.size();
|
|
|
|
pthread_setname_np(th_handle, thread_name_stream.str().c_str());
|
2015-10-27 13:03:43 +01:00
|
|
|
#endif
|
2016-05-13 03:34:04 +02:00
|
|
|
#endif
|
2017-02-06 23:43:55 +01:00
|
|
|
bgthreads_.push_back(std::move(p_t));
|
2015-10-27 13:03:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 23:43:55 +01: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 13:03:43 +01:00
|
|
|
|
|
|
|
if (exit_all_threads_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
StartBGThreads();
|
|
|
|
|
|
|
|
// Add to priority queue
|
|
|
|
queue_.push_back(BGItem());
|
2017-02-06 23:43:55 +01:00
|
|
|
|
|
|
|
auto& item = queue_.back();
|
|
|
|
item.tag = tag;
|
|
|
|
item.function = std::move(schedule);
|
|
|
|
item.unschedFunction = std::move(unschedule);
|
|
|
|
|
2015-10-27 13:03:43 +01:00
|
|
|
queue_len_.store(static_cast<unsigned int>(queue_.size()),
|
2017-02-06 23:43:55 +01:00
|
|
|
std::memory_order_relaxed);
|
2015-10-27 13:03:43 +01:00
|
|
|
|
|
|
|
if (!HasExcessiveThread()) {
|
|
|
|
// Wake up at least one waiting thread.
|
2017-02-06 23:43:55 +01:00
|
|
|
bgsignal_.notify_one();
|
2015-10-27 13:03:43 +01: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 23:43:55 +01:00
|
|
|
int ThreadPoolImpl::Impl::UnSchedule(void* arg) {
|
2015-10-27 13:03:43 +01:00
|
|
|
int count = 0;
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2017-02-06 23:43:55 +01: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 20:20:34 +01:00
|
|
|
}
|
2015-10-27 13:03:43 +01:00
|
|
|
}
|
2017-02-06 23:43:55 +01:00
|
|
|
queue_len_.store(static_cast<unsigned int>(queue_.size()),
|
|
|
|
std::memory_order_relaxed);
|
2015-10-27 13:03:43 +01:00
|
|
|
}
|
2017-02-06 23:43:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
// Run unschedule functions outside the mutex
|
|
|
|
for (auto& f : candidates) {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
|
2015-10-27 13:03:43 +01:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2017-08-04 00:36:28 +02:00
|
|
|
ThreadPoolImpl::ThreadPoolImpl() :
|
2017-02-06 23:43:55 +01:00
|
|
|
impl_(new Impl()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ThreadPoolImpl::~ThreadPoolImpl() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPoolImpl::JoinAllThreads() {
|
|
|
|
impl_->JoinThreads(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPoolImpl::SetBackgroundThreads(int num) {
|
|
|
|
impl_->SetBackgroundThreadsInternal(num, true);
|
|
|
|
}
|
|
|
|
|
2017-05-23 20:04:25 +02:00
|
|
|
int ThreadPoolImpl::GetBackgroundThreads() {
|
|
|
|
return impl_->GetBackgroundThreads();
|
|
|
|
}
|
|
|
|
|
2017-02-06 23:43:55 +01:00
|
|
|
unsigned int ThreadPoolImpl::GetQueueLen() const {
|
|
|
|
return impl_->GetQueueLen();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPoolImpl::WaitForJobsAndJoinAllThreads() {
|
|
|
|
impl_->JoinThreads(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPoolImpl::LowerIOPriority() {
|
|
|
|
impl_->LowerIOPriority();
|
|
|
|
}
|
|
|
|
|
2018-04-24 17:38:01 +02:00
|
|
|
void ThreadPoolImpl::LowerCPUPriority() {
|
|
|
|
impl_->LowerCPUPriority();
|
|
|
|
}
|
|
|
|
|
2017-02-06 23:43:55 +01: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)) {
|
|
|
|
|
|
|
|
std::function<void()> fn = [arg, function] { function(arg); };
|
|
|
|
|
|
|
|
std::function<void()> unfn;
|
|
|
|
if (unschedFunction != nullptr) {
|
|
|
|
auto uf = [arg, unschedFunction] { unschedFunction(arg); };
|
|
|
|
unfn = std::move(uf);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_->Submit(std::move(fn), std::move(unfn), tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 19:41:35 +02:00
|
|
|
ThreadPool* NewThreadPool(int num_threads) {
|
|
|
|
ThreadPoolImpl* thread_pool = new ThreadPoolImpl();
|
|
|
|
thread_pool->SetBackgroundThreads(num_threads);
|
|
|
|
return thread_pool;
|
|
|
|
}
|
|
|
|
|
2015-10-27 13:03:43 +01:00
|
|
|
} // namespace rocksdb
|