From: Sage Weil Date: Wed, 28 Feb 2018 15:47:39 +0000 (-0600) Subject: common/ceph_context: mark some singletones as drop-on-fork X-Git-Tag: v13.0.2~79^2~6 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=2e47633ffa499b41255aed1ba8ea12e14ef556ba;p=ceph.git common/ceph_context: mark some singletones as drop-on-fork Signed-off-by: Sage Weil --- diff --git a/src/common/TracepointProvider.h b/src/common/TracepointProvider.h index 82a0359484e..a2b5f89c40e 100644 --- a/src/common/TracepointProvider.h +++ b/src/common/TracepointProvider.h @@ -58,7 +58,7 @@ public: static void initialize(CephContext *cct) { #ifdef WITH_LTTNG cct->lookup_or_create_singleton_object>( - traits.library, cct); + traits.library, false, cct); #endif } diff --git a/src/common/ceph_context.cc b/src/common/ceph_context.cc index 58623767800..65a30167fc5 100644 --- a/src/common/ceph_context.cc +++ b/src/common/ceph_context.cc @@ -644,7 +644,7 @@ CephContext::CephContext(uint32_t module_type_, _crypto_aes = CryptoHandler::create(CEPH_CRYPTO_AES); _crypto_random.reset(new CryptoRandom()); - lookup_or_create_singleton_object("mempool_obs", this); + lookup_or_create_singleton_object("mempool_obs", false, this); } CephContext::~CephContext() @@ -851,3 +851,33 @@ CryptoHandler *CephContext::get_crypto_handler(int type) return NULL; } } + +void CephContext::notify_pre_fork() +{ + { + std::lock_guard lg(_fork_watchers_lock); + for (auto &&t : _fork_watchers) { + t->handle_pre_fork(); + } + } + { + // note: we don't hold a lock here, but we assume we are idle at + // fork time, which happens during process init and startup. + auto i = associated_objs.begin(); + while (i != associated_objs.end()) { + if (associated_objs_drop_on_fork.count(i->first.first)) { + i = associated_objs.erase(i); + } else { + ++i; + } + } + associated_objs_drop_on_fork.clear(); + } +} + +void CephContext::notify_post_fork() +{ + ceph::spin_unlock(&_fork_watchers_lock); + for (auto &&t : _fork_watchers) + t->handle_post_fork(); +} diff --git a/src/common/ceph_context.h b/src/common/ceph_context.h index b06f6e067e0..55c59eca22e 100644 --- a/src/common/ceph_context.h +++ b/src/common/ceph_context.h @@ -143,6 +143,7 @@ public: template T& lookup_or_create_singleton_object(std::string_view name, + bool drop_on_fork, Args&&... args) { static_assert(sizeof(T) <= largest_singleton, "Please increase largest singleton."); @@ -151,6 +152,9 @@ public: auto i = associated_objs.find(std::make_pair(name, type)); if (i == associated_objs.cend()) { + if (drop_on_fork) { + associated_objs_drop_on_fork.insert(std::string(name)); + } i = associated_objs.emplace_hint( i, std::piecewise_construct, @@ -211,17 +215,8 @@ public: _fork_watchers.push_back(w); } - void notify_pre_fork() { - std::lock_guard lg(_fork_watchers_lock); - for (auto &&t : _fork_watchers) - t->handle_pre_fork(); - } - - void notify_post_fork() { - ceph::spin_unlock(&_fork_watchers_lock); - for (auto &&t : _fork_watchers) - t->handle_post_fork(); - } + void notify_pre_fork(); + void notify_post_fork(); private: @@ -277,6 +272,7 @@ private: std::map, ceph::immobile_any, associated_objs_cmp> associated_objs; + std::set associated_objs_drop_on_fork; ceph::spinlock _fork_watchers_lock; std::vector _fork_watchers; diff --git a/src/librbd/ImageCtx.cc b/src/librbd/ImageCtx.cc index 9f8939b3972..05133636090 100644 --- a/src/librbd/ImageCtx.cc +++ b/src/librbd/ImageCtx.cc @@ -1178,7 +1178,7 @@ struct C_InvalidateCache : public Context { ContextWQ **op_work_queue) { auto thread_pool_singleton = &cct->lookup_or_create_singleton_object( - "librbd::thread_pool", cct); + "librbd::thread_pool", false, cct); *thread_pool = thread_pool_singleton; *op_work_queue = thread_pool_singleton->op_work_queue; } @@ -1187,7 +1187,7 @@ struct C_InvalidateCache : public Context { Mutex **timer_lock) { auto safe_timer_singleton = &cct->lookup_or_create_singleton_object( - "librbd::journal::safe_timer", cct); + "librbd::journal::safe_timer", false, cct); *timer = safe_timer_singleton; *timer_lock = &safe_timer_singleton->lock; } diff --git a/src/librbd/ImageState.cc b/src/librbd/ImageState.cc index 1fcb54a5342..9ac9f1570a2 100644 --- a/src/librbd/ImageState.cc +++ b/src/librbd/ImageState.cc @@ -213,7 +213,8 @@ private: return; } auto& thread_pool = m_cct->lookup_or_create_singleton_object< - ThreadPoolSingleton>("librbd::ImageUpdateWatchers::thread_pool", m_cct); + ThreadPoolSingleton>("librbd::ImageUpdateWatchers::thread_pool", + false, m_cct); m_work_queue = new ContextWQ("librbd::ImageUpdateWatchers::op_work_queue", m_cct->_conf->get_val("rbd_op_thread_timeout"), &thread_pool); diff --git a/src/librbd/Journal.cc b/src/librbd/Journal.cc index 21eea0ef6ea..4a13c2a0f1f 100644 --- a/src/librbd/Journal.cc +++ b/src/librbd/Journal.cc @@ -336,7 +336,7 @@ Journal::Journal(I &image_ctx) auto thread_pool_singleton = &cct->lookup_or_create_singleton_object( - "librbd::journal::thread_pool", cct); + "librbd::journal::thread_pool", false, cct); m_work_queue = new ContextWQ("librbd::journal::work_queue", cct->_conf->get_val("rbd_op_thread_timeout"), thread_pool_singleton); diff --git a/src/librbd/TaskFinisher.h b/src/librbd/TaskFinisher.h index 6ca0e19a6a0..8b9c94b56ad 100644 --- a/src/librbd/TaskFinisher.h +++ b/src/librbd/TaskFinisher.h @@ -46,7 +46,7 @@ public: TaskFinisher(CephContext &cct) : m_cct(cct) { auto& singleton = cct.lookup_or_create_singleton_object( - "librbd::TaskFinisher::m_safe_timer", &cct); + "librbd::TaskFinisher::m_safe_timer", false, &cct); m_lock = &singleton.m_lock; m_safe_timer = singleton.m_safe_timer; m_finisher = singleton.m_finisher; diff --git a/src/msg/async/AsyncMessenger.cc b/src/msg/async/AsyncMessenger.cc index d4222059573..d56ca7bd6be 100644 --- a/src/msg/async/AsyncMessenger.cc +++ b/src/msg/async/AsyncMessenger.cc @@ -260,7 +260,7 @@ AsyncMessenger::AsyncMessenger(CephContext *cct, entity_name_t name, transport_type = "dpdk"; auto single = &cct->lookup_or_create_singleton_object( - "AsyncMessenger::NetworkStack::" + transport_type, cct); + "AsyncMessenger::NetworkStack::" + transport_type, true, cct); single->ready(transport_type); stack = single->stack.get(); stack->start(); diff --git a/src/msg/async/Event.cc b/src/msg/async/Event.cc index eadfac4f755..9038df29c69 100644 --- a/src/msg/async/Event.cc +++ b/src/msg/async/Event.cc @@ -191,7 +191,7 @@ void EventCenter::set_owner() if (!global_centers) { global_centers = &cct->lookup_or_create_singleton_object< EventCenter::AssociatedCenters>( - "AsyncMessenger::EventCenter::global_center::" + type); + "AsyncMessenger::EventCenter::global_center::" + type, true); assert(global_centers); global_centers->centers[idx] = this; if (driver->need_wakeup()) { diff --git a/src/tools/rbd_mirror/Mirror.cc b/src/tools/rbd_mirror/Mirror.cc index 1989c78ad06..3483d662da1 100644 --- a/src/tools/rbd_mirror/Mirror.cc +++ b/src/tools/rbd_mirror/Mirror.cc @@ -206,7 +206,7 @@ Mirror::Mirror(CephContext *cct, const std::vector &args) : { m_threads = &(cct->lookup_or_create_singleton_object>( - "rbd_mirror::threads", cct)); + "rbd_mirror::threads", false, cct)); m_service_daemon.reset(new ServiceDaemon<>(m_cct, m_local, m_threads)); }