2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2015-10-27 13:03:43 +01:00
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
#pragma once
|
2016-05-13 03:34:04 +02:00
|
|
|
|
|
|
|
#ifdef OS_WIN
|
|
|
|
# define ROCKSDB_STD_THREADPOOL
|
|
|
|
#endif
|
|
|
|
|
2015-10-27 13:03:43 +01:00
|
|
|
#include "rocksdb/env.h"
|
2016-08-26 19:41:35 +02:00
|
|
|
#include "rocksdb/threadpool.h"
|
2015-10-27 13:03:43 +01:00
|
|
|
#include "util/thread_status_util.h"
|
|
|
|
|
2016-05-13 03:34:04 +02:00
|
|
|
#ifdef ROCKSDB_STD_THREADPOOL
|
|
|
|
# include <thread>
|
|
|
|
# include <mutex>
|
|
|
|
# include <condition_variable>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-10-27 13:03:43 +01:00
|
|
|
namespace rocksdb {
|
|
|
|
|
2016-08-26 19:41:35 +02:00
|
|
|
class ThreadPoolImpl : public ThreadPool {
|
2015-10-27 13:03:43 +01:00
|
|
|
public:
|
2016-08-26 19:41:35 +02:00
|
|
|
ThreadPoolImpl();
|
|
|
|
~ThreadPoolImpl();
|
2015-10-27 13:03:43 +01:00
|
|
|
|
2016-08-26 19:41:35 +02:00
|
|
|
void JoinAllThreads() override;
|
2015-10-27 13:03:43 +01:00
|
|
|
void LowerIOPriority();
|
|
|
|
void BGThread(size_t thread_id);
|
|
|
|
void WakeUpAllThreads();
|
|
|
|
void IncBackgroundThreadsIfNeeded(int num);
|
2016-08-26 19:41:35 +02:00
|
|
|
void SetBackgroundThreads(int num) override;
|
2015-10-27 13:03:43 +01:00
|
|
|
void StartBGThreads();
|
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
|
|
|
void Schedule(void (*function)(void* arg1), void* arg, void* tag,
|
|
|
|
void (*unschedFunction)(void* arg));
|
2015-10-27 13:03:43 +01:00
|
|
|
int UnSchedule(void* arg);
|
|
|
|
|
2016-08-26 19:41:35 +02:00
|
|
|
unsigned int GetQueueLen() const override {
|
2015-10-27 13:03:43 +01:00
|
|
|
return queue_len_.load(std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetHostEnv(Env* env) { env_ = env; }
|
2016-05-13 03:34:04 +02:00
|
|
|
Env* GetHostEnv() const { return env_; }
|
2015-10-27 13:03:43 +01:00
|
|
|
|
|
|
|
// Return true if there is at least one thread needs to terminate.
|
2016-05-13 03:34:04 +02:00
|
|
|
bool HasExcessiveThread() const {
|
2015-10-27 13:03:43 +01:00
|
|
|
return static_cast<int>(bgthreads_.size()) > total_threads_limit_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2016-05-13 03:34:04 +02:00
|
|
|
bool IsLastExcessiveThread(size_t thread_id) const {
|
2015-10-27 13:03:43 +01:00
|
|
|
return HasExcessiveThread() && thread_id == bgthreads_.size() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is one of the threads to terminate.
|
2016-05-13 03:34:04 +02:00
|
|
|
bool IsExcessiveThread(size_t thread_id) const {
|
2015-10-27 13:03:43 +01:00
|
|
|
return static_cast<int>(thread_id) >= total_threads_limit_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the thread priority.
|
|
|
|
// This would allow its member-thread to know its priority.
|
2016-05-13 03:34:04 +02:00
|
|
|
Env::Priority GetThreadPriority() const { return priority_; }
|
2015-10-27 13:03:43 +01:00
|
|
|
|
|
|
|
// Set the thread priority.
|
|
|
|
void SetThreadPriority(Env::Priority priority) { priority_ = priority; }
|
|
|
|
|
|
|
|
static void PthreadCall(const char* label, int result);
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Entry per Schedule() call
|
|
|
|
struct BGItem {
|
|
|
|
void* arg;
|
|
|
|
void (*function)(void*);
|
|
|
|
void* tag;
|
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
|
|
|
void (*unschedFunction)(void*);
|
2015-10-27 13:03:43 +01:00
|
|
|
};
|
2016-05-13 03:34:04 +02:00
|
|
|
|
2015-10-27 13:03:43 +01:00
|
|
|
typedef std::deque<BGItem> BGQueue;
|
|
|
|
|
2016-05-13 03:34:04 +02:00
|
|
|
int total_threads_limit_;
|
|
|
|
|
|
|
|
#ifdef ROCKSDB_STD_THREADPOOL
|
|
|
|
std::mutex mu_;
|
|
|
|
std::condition_variable bgsignal_;
|
|
|
|
std::vector<std::thread> bgthreads_;
|
|
|
|
#else
|
2015-10-27 13:03:43 +01:00
|
|
|
pthread_mutex_t mu_;
|
|
|
|
pthread_cond_t bgsignal_;
|
|
|
|
std::vector<pthread_t> bgthreads_;
|
2016-05-13 03:34:04 +02:00
|
|
|
#endif
|
2015-10-27 13:03:43 +01:00
|
|
|
BGQueue queue_;
|
|
|
|
std::atomic_uint queue_len_; // Queue length. Used for stats reporting
|
|
|
|
bool exit_all_threads_;
|
|
|
|
bool low_io_priority_;
|
|
|
|
Env::Priority priority_;
|
|
|
|
Env* env_;
|
|
|
|
|
|
|
|
void SetBackgroundThreadsInternal(int num, bool allow_reduce);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace rocksdb
|