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
23 #include "include/assert.h"
24 #include "include/buffer_fwd.h"
25 #include "include/atomic.h"
26 #include "common/cmdparse.h"
27 #include "include/Spinlock.h"
28 #include "crush/CrushLocation.h"
29 #include <boost/noncopyable.hpp>
32 class CephContextServiceThread
;
33 class PerfCountersCollection
;
35 class md_config_obs_t
;
37 class CephContextHook
;
49 using ceph::bufferlist
;
51 /* A CephContext represents the context held by a single library user.
52 * There can be multiple CephContexts in the same process.
54 * For daemons and utility programs, there will be only one CephContext. The
55 * CephContext contains the configuration, the dout object, and anything else
56 * that you might want to pass to libcommon with every function call.
60 CephContext(uint32_t module_type_
, int init_flags_
= 0);
74 ceph::logging::Log
*_log
;
76 /* init ceph::crypto */
79 /* Start the Ceph Context's service thread */
80 void start_service_thread();
82 /* Reopen the log files */
85 /* Get the module type (client, mon, osd, mds, etc.) */
86 uint32_t get_module_type() const;
88 void set_init_flags(int flags
);
89 int get_init_flags() const;
91 /* Get the PerfCountersCollection of this CephContext */
92 PerfCountersCollection
*get_perfcounters_collection();
94 ceph::HeartbeatMap
*get_heartbeat_map() {
95 return _heartbeat_map
;
99 * Enable the performance counter, currently we only have counter for the
100 * number of total/unhealthy workers.
102 void enable_perf_counter();
105 * Disable the performance counter.
107 void disable_perf_counter();
110 * Refresh perf counter values.
112 void refresh_perf_values();
115 * Get the admin socket associated with this CephContext.
117 * Currently there is always an admin socket object,
118 * so this will never return NULL.
120 * @return the admin socket
122 AdminSocket
*get_admin_socket();
125 * process an admin socket command
127 void do_command(std::string command
, cmdmap_t
& cmdmap
, std::string format
,
131 void lookup_or_create_singleton_object(T
*& p
, const std::string
&name
) {
132 ceph_spin_lock(&_associated_objs_lock
);
133 if (!_associated_objs
.count(name
)) {
135 _associated_objs
[name
] = new TypedSingletonWrapper
<T
>(p
);
137 TypedSingletonWrapper
<T
> *wrapper
=
138 dynamic_cast<TypedSingletonWrapper
<T
> *>(_associated_objs
[name
]);
139 assert(wrapper
!= NULL
);
140 p
= wrapper
->singleton
;
142 ceph_spin_unlock(&_associated_objs_lock
);
145 * get a crypto handler
147 CryptoHandler
*get_crypto_handler(int type
);
149 /// check if experimental feature is enable, and emit appropriate warnings
150 bool check_experimental_feature_enabled(const std::string
& feature
);
151 bool check_experimental_feature_enabled(const std::string
& feature
,
152 std::ostream
*message
);
154 PluginRegistry
*get_plugin_registry() {
155 return _plugin_registry
;
158 void set_uid_gid(uid_t u
, gid_t g
) {
162 uid_t
get_set_uid() const {
165 gid_t
get_set_gid() const {
169 void set_uid_gid_strings(std::string u
, std::string g
) {
173 std::string
get_set_uid_string() const {
174 return _set_uid_string
;
176 std::string
get_set_gid_string() const {
177 return _set_gid_string
;
182 virtual ~ForkWatcher() {}
183 virtual void handle_pre_fork() = 0;
184 virtual void handle_post_fork() = 0;
187 void register_fork_watcher(ForkWatcher
*w
) {
188 ceph_spin_lock(&_fork_watchers_lock
);
189 _fork_watchers
.push_back(w
);
190 ceph_spin_unlock(&_fork_watchers_lock
);
193 void notify_pre_fork() {
194 ceph_spin_lock(&_fork_watchers_lock
);
195 for (auto &&t
: _fork_watchers
)
196 t
->handle_pre_fork();
197 ceph_spin_unlock(&_fork_watchers_lock
);
200 void notify_post_fork() {
201 ceph_spin_lock(&_fork_watchers_lock
);
202 for (auto &&t
: _fork_watchers
)
203 t
->handle_post_fork();
204 ceph_spin_unlock(&_fork_watchers_lock
);
208 struct SingletonWrapper
: boost::noncopyable
{
209 virtual ~SingletonWrapper() {}
212 template <typename T
>
213 struct TypedSingletonWrapper
: public SingletonWrapper
{
214 TypedSingletonWrapper(T
*p
) : singleton(p
) {
216 ~TypedSingletonWrapper() override
{
223 CephContext(const CephContext
&rhs
);
224 CephContext
&operator=(const CephContext
&rhs
);
226 /* Stop and join the Ceph Context's service thread */
227 void join_service_thread();
229 uint32_t _module_type
;
233 uid_t _set_uid
; ///< uid to drop privs to
234 gid_t _set_gid
; ///< gid to drop privs to
235 std::string _set_uid_string
;
236 std::string _set_gid_string
;
240 /* libcommon service thread.
241 * SIGHUP wakes this thread, which then reopens logfiles */
242 friend class CephContextServiceThread
;
243 CephContextServiceThread
*_service_thread
;
245 md_config_obs_t
*_log_obs
;
247 /* The admin socket associated with this context */
248 AdminSocket
*_admin_socket
;
250 /* lock which protects service thread creation, destruction, etc. */
251 ceph_spinlock_t _service_thread_lock
;
253 /* The collection of profiling loggers associated with this context */
254 PerfCountersCollection
*_perf_counters_collection
;
256 md_config_obs_t
*_perf_counters_conf_obs
;
258 CephContextHook
*_admin_hook
;
260 ceph::HeartbeatMap
*_heartbeat_map
;
262 ceph_spinlock_t _associated_objs_lock
;
263 std::map
<std::string
, SingletonWrapper
*> _associated_objs
;
265 ceph_spinlock_t _fork_watchers_lock
;
266 std::vector
<ForkWatcher
*> _fork_watchers
;
269 CryptoHandler
*_crypto_none
;
270 CryptoHandler
*_crypto_aes
;
273 CephContextObs
*_cct_obs
;
274 ceph_spinlock_t _feature_lock
;
275 std::set
<std::string
> _experimental_features
;
277 PluginRegistry
*_plugin_registry
;
279 md_config_obs_t
*_lockdep_obs
;
282 CrushLocation crush_location
;
288 l_cct_unhealthy_workers
,
291 PerfCounters
*_cct_perf
;
292 ceph_spinlock_t _cct_perf_lock
;
294 friend class CephContextObs
;