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
20 #include <boost/noncopyable.hpp>
22 #include "common/cmdparse.h"
23 #include "common/code_environment.h"
24 #include "crush/CrushLocation.h"
25 #include "include/Spinlock.h"
28 class CephContextServiceThread
;
29 class PerfCountersCollection
;
31 class md_config_obs_t
;
33 class CephContextHook
;
45 /* A CephContext represents the context held by a single library user.
46 * There can be multiple CephContexts in the same process.
48 * For daemons and utility programs, there will be only one CephContext. The
49 * CephContext contains the configuration, the dout object, and anything else
50 * that you might want to pass to libcommon with every function call.
54 CephContext(uint32_t module_type_
,
55 enum code_environment_t code_env
=CODE_ENVIRONMENT_UTILITY
,
61 std::atomic
<unsigned> nref
;
70 ceph::logging::Log
*_log
;
72 /* init ceph::crypto */
75 /* Start the Ceph Context's service thread */
76 void start_service_thread();
78 /* Reopen the log files */
81 /* Get the module type (client, mon, osd, mds, etc.) */
82 uint32_t get_module_type() const;
84 void set_init_flags(int flags
);
85 int get_init_flags() const;
87 /* Get the PerfCountersCollection of this CephContext */
88 PerfCountersCollection
*get_perfcounters_collection();
90 ceph::HeartbeatMap
*get_heartbeat_map() {
91 return _heartbeat_map
;
95 * Enable the performance counter, currently we only have counter for the
96 * number of total/unhealthy workers.
98 void enable_perf_counter();
101 * Disable the performance counter.
103 void disable_perf_counter();
106 * Refresh perf counter values.
108 void refresh_perf_values();
111 * Get the admin socket associated with this CephContext.
113 * Currently there is always an admin socket object,
114 * so this will never return NULL.
116 * @return the admin socket
118 AdminSocket
*get_admin_socket();
121 * process an admin socket command
123 void do_command(std::string command
, cmdmap_t
& cmdmap
, std::string format
,
124 ceph::bufferlist
*out
);
127 void lookup_or_create_singleton_object(T
*& p
, const std::string
&name
) {
128 ceph_spin_lock(&_associated_objs_lock
);
129 if (!_associated_objs
.count(name
)) {
131 _associated_objs
[name
] = new TypedSingletonWrapper
<T
>(p
);
133 TypedSingletonWrapper
<T
> *wrapper
=
134 dynamic_cast<TypedSingletonWrapper
<T
> *>(_associated_objs
[name
]);
135 assert(wrapper
!= NULL
);
136 p
= wrapper
->singleton
;
138 ceph_spin_unlock(&_associated_objs_lock
);
141 * get a crypto handler
143 CryptoHandler
*get_crypto_handler(int type
);
145 /// check if experimental feature is enable, and emit appropriate warnings
146 bool check_experimental_feature_enabled(const std::string
& feature
);
147 bool check_experimental_feature_enabled(const std::string
& feature
,
148 std::ostream
*message
);
150 PluginRegistry
*get_plugin_registry() {
151 return _plugin_registry
;
154 void set_uid_gid(uid_t u
, gid_t g
) {
158 uid_t
get_set_uid() const {
161 gid_t
get_set_gid() const {
165 void set_uid_gid_strings(std::string u
, std::string g
) {
169 std::string
get_set_uid_string() const {
170 return _set_uid_string
;
172 std::string
get_set_gid_string() const {
173 return _set_gid_string
;
178 virtual ~ForkWatcher() {}
179 virtual void handle_pre_fork() = 0;
180 virtual void handle_post_fork() = 0;
183 void register_fork_watcher(ForkWatcher
*w
) {
184 ceph_spin_lock(&_fork_watchers_lock
);
185 _fork_watchers
.push_back(w
);
186 ceph_spin_unlock(&_fork_watchers_lock
);
189 void notify_pre_fork() {
190 ceph_spin_lock(&_fork_watchers_lock
);
191 for (auto &&t
: _fork_watchers
)
192 t
->handle_pre_fork();
193 ceph_spin_unlock(&_fork_watchers_lock
);
196 void notify_post_fork() {
197 ceph_spin_lock(&_fork_watchers_lock
);
198 for (auto &&t
: _fork_watchers
)
199 t
->handle_post_fork();
200 ceph_spin_unlock(&_fork_watchers_lock
);
204 struct SingletonWrapper
: boost::noncopyable
{
205 virtual ~SingletonWrapper() {}
208 template <typename T
>
209 struct TypedSingletonWrapper
: public SingletonWrapper
{
210 TypedSingletonWrapper(T
*p
) : singleton(p
) {
212 ~TypedSingletonWrapper() override
{
219 CephContext(const CephContext
&rhs
);
220 CephContext
&operator=(const CephContext
&rhs
);
222 /* Stop and join the Ceph Context's service thread */
223 void join_service_thread();
225 uint32_t _module_type
;
229 uid_t _set_uid
; ///< uid to drop privs to
230 gid_t _set_gid
; ///< gid to drop privs to
231 std::string _set_uid_string
;
232 std::string _set_gid_string
;
236 /* libcommon service thread.
237 * SIGHUP wakes this thread, which then reopens logfiles */
238 friend class CephContextServiceThread
;
239 CephContextServiceThread
*_service_thread
;
241 md_config_obs_t
*_log_obs
;
243 /* The admin socket associated with this context */
244 AdminSocket
*_admin_socket
;
246 /* lock which protects service thread creation, destruction, etc. */
247 ceph_spinlock_t _service_thread_lock
;
249 /* The collection of profiling loggers associated with this context */
250 PerfCountersCollection
*_perf_counters_collection
;
252 md_config_obs_t
*_perf_counters_conf_obs
;
254 CephContextHook
*_admin_hook
;
256 ceph::HeartbeatMap
*_heartbeat_map
;
258 ceph_spinlock_t _associated_objs_lock
;
259 std::map
<std::string
, SingletonWrapper
*> _associated_objs
;
261 ceph_spinlock_t _fork_watchers_lock
;
262 std::vector
<ForkWatcher
*> _fork_watchers
;
265 CryptoHandler
*_crypto_none
;
266 CryptoHandler
*_crypto_aes
;
269 CephContextObs
*_cct_obs
;
270 ceph_spinlock_t _feature_lock
;
271 std::set
<std::string
> _experimental_features
;
273 PluginRegistry
*_plugin_registry
;
275 md_config_obs_t
*_lockdep_obs
;
278 CrushLocation crush_location
;
284 l_cct_unhealthy_workers
,
287 PerfCounters
*_cct_perf
;
288 ceph_spinlock_t _cct_perf_lock
;
290 friend class CephContextObs
;