1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2011 New Dream Network
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #ifndef CEPH_CEPHCONTEXT_H
16 #define CEPH_CEPHCONTEXT_H
24 #include <string_view>
28 #include <boost/intrusive_ptr.hpp>
30 #include "include/any.h"
31 #include "include/common_fwd.h"
32 #include "include/compat.h"
34 #include "common/cmdparse.h"
35 #include "common/code_environment.h"
36 #include "msg/msg_types.h"
37 #if defined(WITH_SEASTAR) && !defined(WITH_ALIEN)
38 #include "crimson/common/config_proxy.h"
39 #include "crimson/common/perf_counters_collection.h"
41 #include "common/config_proxy.h"
42 #include "include/spinlock.h"
43 #include "common/perf_counters_collection.h"
47 #include "crush/CrushLocation.h"
54 namespace ceph::common
{
55 class CephContextServiceThread
;
57 class CephContextHook
;
68 #if defined(WITH_SEASTAR) && !defined(WITH_ALIEN)
69 namespace crimson::common
{
74 code_environment_t
=CODE_ENVIRONMENT_UTILITY
,
78 CephContext(CephContext
&&) = default;
81 uint32_t get_module_type() const;
82 bool check_experimental_feature_enabled(const std::string
& feature
) {
83 // everything crimson is experimental...
86 CryptoRandom
* random() const;
87 PerfCountersCollectionImpl
* get_perfcounters_collection();
88 crimson::common::ConfigProxy
& _conf
;
89 crimson::common::PerfCountersCollection
& _perf_counters_collection
;
93 std::unique_ptr
<CryptoRandom
> _crypto_random
;
99 namespace ceph::common
{
101 /* A CephContext represents the context held by a single library user.
102 * There can be multiple CephContexts in the same process.
104 * For daemons and utility programs, there will be only one CephContext. The
105 * CephContext contains the configuration, the dout object, and anything else
106 * that you might want to pass to libcommon with every function call.
110 CephContext(uint32_t module_type_
,
111 enum code_environment_t code_env
=CODE_ENVIRONMENT_UTILITY
,
112 int init_flags_
= 0);
114 CephContext(const CephContext
&) = delete;
115 CephContext
& operator =(const CephContext
&) = delete;
116 CephContext(CephContext
&&) = delete;
117 CephContext
& operator =(CephContext
&&) = delete;
119 bool _finished
= false;
124 std::atomic
<unsigned> nref
;
133 ceph::logging::Log
*_log
;
135 /* init ceph::crypto */
138 /// shutdown crypto (should match init_crypto calls)
139 void shutdown_crypto();
141 /* Start the Ceph Context's service thread */
142 void start_service_thread();
144 /* Reopen the log files */
147 /* Get the module type (client, mon, osd, mds, etc.) */
148 uint32_t get_module_type() const;
150 // this is here only for testing purposes!
151 void _set_module_type(uint32_t t
) {
155 void set_init_flags(int flags
);
156 int get_init_flags() const;
158 /* Get the PerfCountersCollection of this CephContext */
159 PerfCountersCollection
*get_perfcounters_collection();
161 ceph::HeartbeatMap
*get_heartbeat_map() {
162 return _heartbeat_map
;
166 * Get the admin socket associated with this CephContext.
168 * Currently there is always an admin socket object,
169 * so this will never return NULL.
171 * @return the admin socket
173 AdminSocket
*get_admin_socket();
176 * process an admin socket command
178 int do_command(std::string_view command
, const cmdmap_t
& cmdmap
,
181 ceph::bufferlist
*out
);
182 int _do_command(std::string_view command
, const cmdmap_t
& cmdmap
,
185 ceph::bufferlist
*out
);
187 static constexpr std::size_t largest_singleton
= 8 * 72;
189 template<typename T
, typename
... Args
>
190 T
& lookup_or_create_singleton_object(std::string_view name
,
193 static_assert(sizeof(T
) <= largest_singleton
,
194 "Please increase largest singleton.");
195 std::lock_guard
lg(associated_objs_lock
);
196 std::type_index type
= typeid(T
);
198 auto i
= associated_objs
.find(std::make_pair(name
, type
));
199 if (i
== associated_objs
.cend()) {
201 associated_objs_drop_on_fork
.insert(std::string(name
));
203 i
= associated_objs
.emplace_hint(
205 std::piecewise_construct
,
206 std::forward_as_tuple(name
, type
),
207 std::forward_as_tuple(std::in_place_type
<T
>,
208 std::forward
<Args
>(args
)...));
210 return ceph::any_cast
<T
&>(i
->second
);
214 * get a crypto handler
216 CryptoHandler
*get_crypto_handler(int type
);
218 CryptoRandom
* random() const { return _crypto_random
.get(); }
220 /// check if experimental feature is enable, and emit appropriate warnings
221 bool check_experimental_feature_enabled(const std::string
& feature
);
222 bool check_experimental_feature_enabled(const std::string
& feature
,
223 std::ostream
*message
);
225 ceph::PluginRegistry
*get_plugin_registry() {
226 return _plugin_registry
;
229 void set_uid_gid(uid_t u
, gid_t g
) {
233 uid_t
get_set_uid() const {
236 gid_t
get_set_gid() const {
240 void set_uid_gid_strings(const std::string
&u
, const std::string
&g
) {
244 std::string
get_set_uid_string() const {
245 return _set_uid_string
;
247 std::string
get_set_gid_string() const {
248 return _set_gid_string
;
253 virtual ~ForkWatcher() {}
254 virtual void handle_pre_fork() = 0;
255 virtual void handle_post_fork() = 0;
258 void register_fork_watcher(ForkWatcher
*w
) {
259 std::lock_guard
lg(_fork_watchers_lock
);
260 _fork_watchers
.push_back(w
);
263 void notify_pre_fork();
264 void notify_post_fork();
267 * update CephContext with a copy of the passed in MonMap mon addrs
269 * @param mm MonMap to extract and update mon addrs
271 void set_mon_addrs(const MonMap
& mm
);
272 void set_mon_addrs(const std::vector
<entity_addrvec_t
>& in
) {
273 auto ptr
= std::make_shared
<std::vector
<entity_addrvec_t
>>(in
);
274 atomic_store_explicit(&_mon_addrs
, std::move(ptr
), std::memory_order_relaxed
);
276 std::shared_ptr
<std::vector
<entity_addrvec_t
>> get_mon_addrs() const {
277 auto ptr
= atomic_load_explicit(&_mon_addrs
, std::memory_order_relaxed
);
284 /* Stop and join the Ceph Context's service thread */
285 void join_service_thread();
287 uint32_t _module_type
;
291 uid_t _set_uid
; ///< uid to drop privs to
292 gid_t _set_gid
; ///< gid to drop privs to
293 std::string _set_uid_string
;
294 std::string _set_gid_string
;
298 std::shared_ptr
<std::vector
<entity_addrvec_t
>> _mon_addrs
;
300 /* libcommon service thread.
301 * SIGHUP wakes this thread, which then reopens logfiles */
302 friend class CephContextServiceThread
;
303 CephContextServiceThread
*_service_thread
;
305 using md_config_obs_t
= ceph::md_config_obs_impl
<ConfigProxy
>;
307 md_config_obs_t
*_log_obs
;
309 /* The admin socket associated with this context */
310 AdminSocket
*_admin_socket
;
312 /* lock which protects service thread creation, destruction, etc. */
313 ceph::spinlock _service_thread_lock
;
315 /* The collection of profiling loggers associated with this context */
316 PerfCountersCollection
*_perf_counters_collection
;
318 md_config_obs_t
*_perf_counters_conf_obs
;
320 CephContextHook
*_admin_hook
;
322 ceph::HeartbeatMap
*_heartbeat_map
;
324 ceph::spinlock associated_objs_lock
;
326 struct associated_objs_cmp
{
327 using is_transparent
= std::true_type
;
328 template<typename T
, typename U
>
329 bool operator ()(const std::pair
<T
, std::type_index
>& l
,
330 const std::pair
<U
, std::type_index
>& r
) const noexcept
{
331 return ((l
.first
< r
.first
) ||
332 (l
.first
== r
.first
&& l
.second
< r
.second
));
336 std::map
<std::pair
<std::string
, std::type_index
>,
337 ceph::immobile_any
<largest_singleton
>,
338 associated_objs_cmp
> associated_objs
;
339 std::set
<std::string
> associated_objs_drop_on_fork
;
341 ceph::spinlock _fork_watchers_lock
;
342 std::vector
<ForkWatcher
*> _fork_watchers
;
345 CryptoHandler
*_crypto_none
;
346 CryptoHandler
*_crypto_aes
;
347 std::unique_ptr
<CryptoRandom
> _crypto_random
;
350 CephContextObs
*_cct_obs
;
351 ceph::spinlock _feature_lock
;
352 std::set
<std::string
> _experimental_features
;
354 ceph::PluginRegistry
* _plugin_registry
;
356 md_config_obs_t
*_lockdep_obs
;
359 TOPNSPC::crush::CrushLocation crush_location
;
365 l_cct_unhealthy_workers
,
369 l_mempool_first
= 873222,
374 PerfCounters
*_cct_perf
= nullptr;
375 PerfCounters
* _mempool_perf
= nullptr;
376 std::vector
<std::string
> _mempool_perf_names
, _mempool_perf_descriptions
;
379 * Enable the performance counters.
381 void _enable_perf_counter();
384 * Disable the performance counter.
386 void _disable_perf_counter();
389 * Refresh perf counter values.
391 void _refresh_perf_values();
393 friend class CephContextObs
;
398 #endif // WITH_SEASTAR
400 #if !(defined(WITH_SEASTAR) && !defined(WITH_ALIEN)) && defined(__cplusplus)
401 namespace ceph::common
{
402 inline void intrusive_ptr_add_ref(CephContext
* cct
)
407 inline void intrusive_ptr_release(CephContext
* cct
)
412 #endif // !(defined(WITH_SEASTAR) && !defined(WITH_ALIEN)) && defined(__cplusplus)