aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Sree Kuchibhotla <sreek@google.com>2018-07-23 23:38:13 -0700
committerGravatar Sree Kuchibhotla <sreek@google.com>2018-07-24 16:35:25 -0700
commitb95772eeb926f78b8ac14e03b36ed3e73b2e1a2c (patch)
treeff760e47e362bdfe86467a2f575cc0a9d8ca76c1 /src
parent7b8be4d6fd0a3b7374d5a28bea1eff319c49fefe (diff)
Add Tests in Core and C++ and fix a few related bugs in thread_manager.cc
Diffstat (limited to 'src')
-rw-r--r--src/core/lib/iomgr/resource_quota.cc7
-rw-r--r--src/cpp/server/server_cc.cc10
-rw-r--r--src/cpp/thread_manager/thread_manager.cc70
-rw-r--r--src/cpp/thread_manager/thread_manager.h42
4 files changed, 84 insertions, 45 deletions
diff --git a/src/core/lib/iomgr/resource_quota.cc b/src/core/lib/iomgr/resource_quota.cc
index a30688bd87..67d05aa202 100644
--- a/src/core/lib/iomgr/resource_quota.cc
+++ b/src/core/lib/iomgr/resource_quota.cc
@@ -547,6 +547,11 @@ static void ru_shutdown(void* ru, grpc_error* error) {
static void ru_destroy(void* ru, grpc_error* error) {
grpc_resource_user* resource_user = static_cast<grpc_resource_user*>(ru);
GPR_ASSERT(gpr_atm_no_barrier_load(&resource_user->refs) == 0);
+ // Free all the remaining thread quota
+ grpc_resource_user_free_threads(
+ resource_user,
+ static_cast<int>(gpr_atm_no_barrier_load(&resource_user->num_threads)));
+
for (int i = 0; i < GRPC_RULIST_COUNT; i++) {
rulist_remove(resource_user, static_cast<grpc_rulist>(i));
}
@@ -642,6 +647,7 @@ grpc_resource_quota* grpc_resource_quota_create(const char* name) {
void grpc_resource_quota_unref_internal(grpc_resource_quota* resource_quota) {
if (gpr_unref(&resource_quota->refs)) {
+ GPR_ASSERT(resource_quota->num_threads == 0); // No outstanding thd quota
GRPC_COMBINER_UNREF(resource_quota->combiner, "resource_quota");
gpr_free(resource_quota->name);
gpr_free(resource_quota);
@@ -846,6 +852,7 @@ void grpc_resource_user_free_threads(grpc_resource_user* resource_user,
"Releasing more threads (%d) that currently allocated (rq threads: "
"%d, ru threads: %d)",
thd_count, old_cnt, rq->num_threads + thd_count);
+ abort();
}
gpr_mu_unlock(&resource_user->resource_quota->thd_mu);
}
diff --git a/src/cpp/server/server_cc.cc b/src/cpp/server/server_cc.cc
index 6e6e0bfffe..786ef44e3e 100644
--- a/src/cpp/server/server_cc.cc
+++ b/src/cpp/server/server_cc.cc
@@ -47,6 +47,12 @@
namespace grpc {
namespace {
+// The default value for maximum number of threads that can be created in the
+// sync server. This value of 1500 is empirically chosen. To increase the max
+// number of threads in a sync server, pass a custom ResourceQuota object (with
+// the desired number of max-threads set) to the server builder
+#define DEFAULT_MAX_SYNC_SERVER_THREADS 1500
+
class DefaultGlobalCallbacks final : public Server::GlobalCallbacks {
public:
~DefaultGlobalCallbacks() override {}
@@ -395,7 +401,9 @@ Server::Server(
if (sync_server_cqs_ != nullptr) {
bool default_rq_created = false;
if (server_rq == nullptr) {
- server_rq = grpc_resource_quota_create("SyncServer-Default");
+ server_rq = grpc_resource_quota_create("SyncServer-default-rq");
+ grpc_resource_quota_set_max_threads(server_rq,
+ DEFAULT_MAX_SYNC_SERVER_THREADS);
default_rq_created = true;
}
diff --git a/src/cpp/thread_manager/thread_manager.cc b/src/cpp/thread_manager/thread_manager.cc
index c0fa98798a..5d367511e2 100644
--- a/src/cpp/thread_manager/thread_manager.cc
+++ b/src/cpp/thread_manager/thread_manager.cc
@@ -22,8 +22,8 @@
#include <mutex>
#include <grpc/support/log.h>
-
#include "src/core/lib/gprpp/thd.h"
+#include "src/core/lib/iomgr/exec_ctx.h"
namespace grpc {
@@ -55,7 +55,8 @@ ThreadManager::ThreadManager(const char* name,
num_pollers_(0),
min_pollers_(min_pollers),
max_pollers_(max_pollers == -1 ? INT_MAX : max_pollers),
- num_threads_(0) {
+ num_threads_(0),
+ max_active_threads_sofar_(0) {
resource_user_ = grpc_resource_user_create(resource_quota, name);
}
@@ -65,6 +66,7 @@ ThreadManager::~ThreadManager() {
GPR_ASSERT(num_threads_ == 0);
}
+ grpc_core::ExecCtx exec_ctx; // grpc_resource_user_unref needs an exec_ctx
grpc_resource_user_unref(resource_user_);
CleanupCompletedThreads();
}
@@ -86,17 +88,27 @@ bool ThreadManager::IsShutdown() {
return shutdown_;
}
+int ThreadManager::GetMaxActiveThreadsSoFar() {
+ std::lock_guard<std::mutex> list_lock(list_mu_);
+ return max_active_threads_sofar_;
+}
+
void ThreadManager::MarkAsCompleted(WorkerThread* thd) {
{
std::lock_guard<std::mutex> list_lock(list_mu_);
completed_threads_.push_back(thd);
}
- std::lock_guard<std::mutex> lock(mu_);
- num_threads_--;
- if (num_threads_ == 0) {
- shutdown_cv_.notify_one();
+ {
+ std::lock_guard<std::mutex> lock(mu_);
+ num_threads_--;
+ if (num_threads_ == 0) {
+ shutdown_cv_.notify_one();
+ }
}
+
+ // Give a thread back to the resource quota
+ grpc_resource_user_free_threads(resource_user_, 1);
}
void ThreadManager::CleanupCompletedThreads() {
@@ -111,34 +123,24 @@ void ThreadManager::CleanupCompletedThreads() {
}
void ThreadManager::Initialize() {
+ if (!grpc_resource_user_alloc_threads(resource_user_, min_pollers_)) {
+ gpr_log(GPR_ERROR,
+ "No thread quota available to even create the minimum required "
+ "polling threads (i.e %d). Unable to start the thread manager",
+ min_pollers_);
+ abort();
+ }
+
{
std::unique_lock<std::mutex> lock(mu_);
num_pollers_ = min_pollers_;
num_threads_ = min_pollers_;
+ max_active_threads_sofar_ = min_pollers_;
}
for (int i = 0; i < min_pollers_; i++) {
- if (!CreateNewThread(this)) {
- gpr_log(GPR_ERROR,
- "No quota available to create additional threads. Created %d (of "
- "%d) threads",
- i, min_pollers_);
- break;
- }
- }
-}
-
-bool ThreadManager::CreateNewThread(ThreadManager* thd_mgr) {
- if (!grpc_resource_user_alloc_threads(thd_mgr->resource_user_, 1)) {
- return false;
+ new WorkerThread(this);
}
- // Create a new thread (which ends up calling the MainWorkLoop() function
- new WorkerThread(thd_mgr);
- return true;
-}
-
-void ThreadManager::ReleaseThread(ThreadManager* thd_mgr) {
- grpc_resource_user_free_threads(thd_mgr->resource_user_, 1);
}
void ThreadManager::MainWorkLoop() {
@@ -162,14 +164,17 @@ void ThreadManager::MainWorkLoop() {
done = true;
break;
case WORK_FOUND:
- // If we got work and there are now insufficient pollers, start a new
- // one
- if (!shutdown_ && num_pollers_ < min_pollers_) {
+ // If we got work and there are now insufficient pollers and there is
+ // quota available to create a new thread,start a new poller thread
+ if (!shutdown_ && num_pollers_ < min_pollers_ &&
+ grpc_resource_user_alloc_threads(resource_user_, 1)) {
num_pollers_++;
num_threads_++;
+ max_active_threads_sofar_ =
+ std::max(max_active_threads_sofar_, num_threads_);
// Drop lock before spawning thread to avoid contention
lock.unlock();
- CreateNewThread(this);
+ new WorkerThread(this);
} else {
// Drop lock for consistency with above branch
lock.unlock();
@@ -219,10 +224,9 @@ void ThreadManager::MainWorkLoop() {
}
};
- // This thread is exiting. Do some cleanup work (i.e delete already completed
- // worker threads and also release 1 thread back to the resource quota)
+ // This thread is exiting. Do some cleanup work i.e delete already completed
+ // worker threads
CleanupCompletedThreads();
- ReleaseThread(this);
// If we are here, either ThreadManager is shutting down or it already has
// enough threads.
diff --git a/src/cpp/thread_manager/thread_manager.h b/src/cpp/thread_manager/thread_manager.h
index 23bd38ee4f..8332befed0 100644
--- a/src/cpp/thread_manager/thread_manager.h
+++ b/src/cpp/thread_manager/thread_manager.h
@@ -86,6 +86,11 @@ class ThreadManager {
// all the threads have drained all the outstanding work
virtual void Wait();
+ // Max number of concurrent threads that were ever active in this thread
+ // manager so far. This is useful for debugging purposes (and in unit tests)
+ // to check if resource_quota is properly being enforced.
+ int GetMaxActiveThreadsSoFar();
+
private:
// Helper wrapper class around grpc_core::Thread. Takes a ThreadManager object
// and starts a new grpc_core::Thread to calls the Run() function.
@@ -93,6 +98,23 @@ class ThreadManager {
// The Run() function calls ThreadManager::MainWorkLoop() function and once
// that completes, it marks the WorkerThread completed by calling
// ThreadManager::MarkAsCompleted()
+ //
+ // WHY IS THIS NEEDED?:
+ // When a thread terminates, some other tread *must* call Join() on that
+ // thread so that the resources are released. Having a WorkerThread wrapper
+ // will make this easier. Once Run() completes, each thread calls the
+ // following two functions:
+ // ThreadManager::CleanupCompletedThreads()
+ // ThreadManager::MarkAsCompleted()
+ //
+ // - MarkAsCompleted() puts the WorkerThread object in the ThreadManger's
+ // completed_threads_ list
+ // - CleanupCompletedThreads() calls "Join()" on the threads that are already
+ // in the completed_threads_ list (since a thread cannot call Join() on
+ // itself, it calls CleanupCompletedThreads() *before* calling
+ // MarkAsCompleted())
+ // TODO: sreek - consider creating the threads 'detached' so that Join() need
+ // not be called
class WorkerThread {
public:
WorkerThread(ThreadManager* thd_mgr);
@@ -113,15 +135,8 @@ class ThreadManager {
void MarkAsCompleted(WorkerThread* thd);
void CleanupCompletedThreads();
- // Checks the resource quota and if available, creates a thread and returns
- // true. If quota is not available, returns false (and thread is not created)
- static bool CreateNewThread(ThreadManager* thd_mgr);
-
- // Give back a thread to the resource quota
- static void ReleaseThread(ThreadManager* thd_mgr);
-
- // Protects shutdown_, num_pollers_ and num_threads_
- // TODO: sreek - Change num_pollers and num_threads_ to atomics
+ // Protects shutdown_, num_pollers_, num_threads_ and
+ // max_active_threads_sofar_
std::mutex mu_;
bool shutdown_;
@@ -142,10 +157,15 @@ class ThreadManager {
int min_pollers_;
int max_pollers_;
- // The total number of threads (includes threads includes the threads that are
- // currently polling i.e num_pollers_)
+ // The total number of threads currently active (includes threads includes the
+ // threads that are currently polling i.e num_pollers_)
int num_threads_;
+ // See GetMaxActiveThreadsSoFar()'s description.
+ // To be more specific, this variable tracks the max value num_threads_ was
+ // ever set so far
+ int max_active_threads_sofar_;
+
std::mutex list_mu_;
std::list<WorkerThread*> completed_threads_;
};