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 "crush/CrushLocation.h"
24 #include "include/Spinlock.h"
27 class CephContextServiceThread
;
28 class PerfCountersCollection
;
30 class md_config_obs_t
;
32 class CephContextHook
;
44 /* A CephContext represents the context held by a single library user.
45 * There can be multiple CephContexts in the same process.
47 * For daemons and utility programs, there will be only one CephContext. The
48 * CephContext contains the configuration, the dout object, and anything else
49 * that you might want to pass to libcommon with every function call.
53 CephContext(uint32_t module_type_
, int init_flags_
= 0);
58 std::atomic
<unsigned> nref
;
67 ceph::logging::Log
*_log
;
69 /* init ceph::crypto */
72 /* Start the Ceph Context's service thread */
73 void start_service_thread();
75 /* Reopen the log files */
78 /* Get the module type (client, mon, osd, mds, etc.) */
79 uint32_t get_module_type() const;
81 void set_init_flags(int flags
);
82 int get_init_flags() const;
84 /* Get the PerfCountersCollection of this CephContext */
85 PerfCountersCollection
*get_perfcounters_collection();
87 ceph::HeartbeatMap
*get_heartbeat_map() {
88 return _heartbeat_map
;
92 * Enable the performance counter, currently we only have counter for the
93 * number of total/unhealthy workers.
95 void enable_perf_counter();
98 * Disable the performance counter.
100 void disable_perf_counter();
103 * Refresh perf counter values.
105 void refresh_perf_values();
108 * Get the admin socket associated with this CephContext.
110 * Currently there is always an admin socket object,
111 * so this will never return NULL.
113 * @return the admin socket
115 AdminSocket
*get_admin_socket();
118 * process an admin socket command
120 void do_command(std::string command
, cmdmap_t
& cmdmap
, std::string format
,
121 ceph::bufferlist
*out
);
124 void lookup_or_create_singleton_object(T
*& p
, const std::string
&name
) {
125 ceph_spin_lock(&_associated_objs_lock
);
126 if (!_associated_objs
.count(name
)) {
128 _associated_objs
[name
] = new TypedSingletonWrapper
<T
>(p
);
130 TypedSingletonWrapper
<T
> *wrapper
=
131 dynamic_cast<TypedSingletonWrapper
<T
> *>(_associated_objs
[name
]);
132 assert(wrapper
!= NULL
);
133 p
= wrapper
->singleton
;
135 ceph_spin_unlock(&_associated_objs_lock
);
138 * get a crypto handler
140 CryptoHandler
*get_crypto_handler(int type
);
142 /// check if experimental feature is enable, and emit appropriate warnings
143 bool check_experimental_feature_enabled(const std::string
& feature
);
144 bool check_experimental_feature_enabled(const std::string
& feature
,
145 std::ostream
*message
);
147 PluginRegistry
*get_plugin_registry() {
148 return _plugin_registry
;
151 void set_uid_gid(uid_t u
, gid_t g
) {
155 uid_t
get_set_uid() const {
158 gid_t
get_set_gid() const {
162 void set_uid_gid_strings(std::string u
, std::string g
) {
166 std::string
get_set_uid_string() const {
167 return _set_uid_string
;
169 std::string
get_set_gid_string() const {
170 return _set_gid_string
;
175 virtual ~ForkWatcher() {}
176 virtual void handle_pre_fork() = 0;
177 virtual void handle_post_fork() = 0;
180 void register_fork_watcher(ForkWatcher
*w
) {
181 ceph_spin_lock(&_fork_watchers_lock
);
182 _fork_watchers
.push_back(w
);
183 ceph_spin_unlock(&_fork_watchers_lock
);
186 void notify_pre_fork() {
187 ceph_spin_lock(&_fork_watchers_lock
);
188 for (auto &&t
: _fork_watchers
)
189 t
->handle_pre_fork();
190 ceph_spin_unlock(&_fork_watchers_lock
);
193 void notify_post_fork() {
194 ceph_spin_lock(&_fork_watchers_lock
);
195 for (auto &&t
: _fork_watchers
)
196 t
->handle_post_fork();
197 ceph_spin_unlock(&_fork_watchers_lock
);
201 struct SingletonWrapper
: boost::noncopyable
{
202 virtual ~SingletonWrapper() {}
205 template <typename T
>
206 struct TypedSingletonWrapper
: public SingletonWrapper
{
207 TypedSingletonWrapper(T
*p
) : singleton(p
) {
209 ~TypedSingletonWrapper() override
{
216 CephContext(const CephContext
&rhs
);
217 CephContext
&operator=(const CephContext
&rhs
);
219 /* Stop and join the Ceph Context's service thread */
220 void join_service_thread();
222 uint32_t _module_type
;
226 uid_t _set_uid
; ///< uid to drop privs to
227 gid_t _set_gid
; ///< gid to drop privs to
228 std::string _set_uid_string
;
229 std::string _set_gid_string
;
233 /* libcommon service thread.
234 * SIGHUP wakes this thread, which then reopens logfiles */
235 friend class CephContextServiceThread
;
236 CephContextServiceThread
*_service_thread
;
238 md_config_obs_t
*_log_obs
;
240 /* The admin socket associated with this context */
241 AdminSocket
*_admin_socket
;
243 /* lock which protects service thread creation, destruction, etc. */
244 ceph_spinlock_t _service_thread_lock
;
246 /* The collection of profiling loggers associated with this context */
247 PerfCountersCollection
*_perf_counters_collection
;
249 md_config_obs_t
*_perf_counters_conf_obs
;
251 CephContextHook
*_admin_hook
;
253 ceph::HeartbeatMap
*_heartbeat_map
;
255 ceph_spinlock_t _associated_objs_lock
;
256 std::map
<std::string
, SingletonWrapper
*> _associated_objs
;
258 ceph_spinlock_t _fork_watchers_lock
;
259 std::vector
<ForkWatcher
*> _fork_watchers
;
262 CryptoHandler
*_crypto_none
;
263 CryptoHandler
*_crypto_aes
;
266 CephContextObs
*_cct_obs
;
267 ceph_spinlock_t _feature_lock
;
268 std::set
<std::string
> _experimental_features
;
270 PluginRegistry
*_plugin_registry
;
272 md_config_obs_t
*_lockdep_obs
;
275 CrushLocation crush_location
;
281 l_cct_unhealthy_workers
,
284 PerfCounters
*_cct_perf
;
285 ceph_spinlock_t _cct_perf_lock
;
287 friend class CephContextObs
;