SObjectizer 5.8
Loading...
Searching...
No Matches
activity_tracking.hpp
Go to the documentation of this file.
1/*
2 * SObjectizer-5
3 */
4
5/*!
6 * \file
7 * \brief Details for implementation of activity tracking.
8 *
9 * \since v.5.5.18
10 */
11
12#pragma once
13
14#include <so_5/stats/work_thread_activity.hpp>
15
16#include <so_5/types.hpp>
17#include <so_5/spinlocks.hpp>
18#include <so_5/outliving.hpp>
19
20#include <mutex>
21#include <string_view>
22
23namespace so_5
24{
25
26namespace stats
27{
28
30
31/*!
32 * \brief Various traits of activity tracking implementation.
33 *
34 * \since v.5.5.18
35 */
36struct traits
37 {
38 //! Type of lock object to be used for synchronization of
39 //! activity tracking data.
40 using lock_t = default_spinlock_t;
41 };
42
43/*!
44 * \brief An analog of std::lock_guard but without actual locking actions.
45 *
46 * This class is inteded to be used with different lock policies for
47 * stats_collector_t.
48 *
49 * \since v.5.5.18
50 */
51template< typename L >
53 {
54 no_actual_lock( L & ) { /* Do nothing. */ }
55 };
56
57/*!
58 * \brief Default locking policy for stats_collector_t.
59 *
60 * Performs actual locking on start/stop and take_activity operations.
61 *
62 * \since v.5.5.18
63 */
64template< typename Lock_Holder >
66 {
67 using start_stop_lock_t = std::lock_guard< Lock_Holder >;
68 using take_stats_lock_t = std::lock_guard< Lock_Holder >;
69 };
70
71/*!
72 * \brief A custom locking policy for stats_collector_t.
73 *
74 * Performs actual locking only on take_activity operation.
75 *
76 * \since v.5.5.18
77 */
78template< typename Lock_Holder >
80 {
81 using start_stop_lock_t = no_actual_lock< Lock_Holder >;
82 using take_stats_lock_t = std::lock_guard< Lock_Holder >;
83 };
84
85/*!
86 * \brief Base for the case of internal stats lock.
87 *
88 * \since v.5.5.18
89 */
91 {
92 traits::lock_t m_lock;
93 public :
94 using start_stop_lock_t = std::lock_guard< internal_lock >;
95 using take_stats_lock_t = std::lock_guard< internal_lock >;
96
98
99 void lock() { m_lock.lock(); }
101 };
102
103/*!
104 * \brief Base for the case of externals stats lock.
105 *
106 * \since v.5.5.18
107 */
108template<
109 typename Lock_Type = traits::lock_t,
110 template<class> class Lock_Policy = default_lock_policy >
112 {
113 Lock_Type & m_lock;
114 public :
115 using start_stop_lock_t =
116 typename Lock_Policy< external_lock >::start_stop_lock_t;
117 using take_stats_lock_t =
118 typename Lock_Policy< external_lock >::take_stats_lock_t;
119
120 external_lock( Lock_Type & lock ) : m_lock( lock ) {}
121
122 void lock() { m_lock.lock(); }
123 void unlock() { m_lock.unlock(); }
124 };
125
126/*!
127 * \brief A special class for cases where lock is not needed at all.
128 *
129 * Usage example:
130 * \code
131class real_activity_tracker_t final
132 {
133 so_5::stats::activity_tracking_stuff::stats_collector_t<
134 so_5::stats::activity_tracking_stuff::null_lock >
135 m_waiting{};
136
137 so_5::stats::activity_tracking_stuff::stats_collector_t<
138 so_5::stats::activity_tracking_stuff::null_lock >
139 m_working{};
140 ...
141 };
142 * \endcode
143 *
144 * \since v.5.5.19
145 */
147 {
148 public :
149 using start_stop_lock_t = no_actual_lock< null_lock >;
150 using take_stats_lock_t = no_actual_lock< null_lock >;
151
153 };
154
155/*!
156 * \brief Helper for collecting activity stats.
157 *
158 * \since v.5.5.18
159 */
160template< typename Lock_Holder >
161class stats_collector_t : protected Lock_Holder
162 {
163 Lock_Holder &
164 lock_holder() { return *this; }
165
166 public :
167 template< typename... Args >
168 stats_collector_t( Args && ...args )
169 : Lock_Holder( std::forward<Args>(args)... )
170 {}
171
172 void
174 {
175 typename Lock_Holder::start_stop_lock_t lock{ lock_holder() };
176
178 }
179
180 /*!
181 * \brief A helper method for safe start if start method hasn't been
182 * called yet.
183 *
184 * \since
185 * v.5.5.19
186 */
187 void
189 {
191
192 if( !m_is_in_working )
193 do_start();
194 }
195
196 void
198 {
199 typename Lock_Holder::start_stop_lock_t lock{ lock_holder() };
200
201 m_is_in_working = false;
205 }
206
209 {
210 so_5::stats::activity_stats_t result;
211 bool is_in_working{ false };
212 so_5::stats::clock_type_t::time_point work_started_at;
213
214 {
215 typename Lock_Holder::take_stats_lock_t lock{ lock_holder() };
216
217 result = m_work_activity;
218 if( true == (is_in_working = m_is_in_working) )
219 work_started_at = m_work_started_at;
220 }
221
222 if( is_in_working )
224 result,
225 work_started_at );
226
227 return result;
228 }
229
230 private :
231 //! A flag for indicating work activity.
232 bool m_is_in_working{ false };
233
234 //! A time point when current activity started.
235 so_5::stats::clock_type_t::time_point m_work_started_at;
236
237 //! A statistics for work activity.
239
240 void
242 {
243 m_is_in_working = true;
244 m_work_started_at = so_5::stats::clock_type_t::now();
246 }
247 };
248
249/*!
250 * \brief Helper function for creation of dispatcher with respect
251 * to activity tracking flag in dispatcher params and in Environment's
252 * params.
253 *
254 * \since v.5.5.18
255 */
256template<
257 typename Common_Disp_Iface_Type,
258 typename Disp_No_Tracking,
259 typename Disp_With_Tracking,
260 typename Env,
261 typename Disp_Params,
262 typename... Args >
263std::unique_ptr< Common_Disp_Iface_Type >
265 outliving_reference_t< Env > env,
266 const std::string_view name_base,
267 Disp_Params disp_params,
268 Args && ...args )
269 {
270 std::unique_ptr< Common_Disp_Iface_Type > disp;
271
272 auto tracking = disp_params.work_thread_activity_tracking();
274 tracking = env.get().work_thread_activity_tracking();
275
277 disp = std::make_unique< Disp_With_Tracking >(
278 env,
279 name_base,
280 std::move(disp_params),
281 std::forward<Args>(args)... );
282 else
283 disp = std::make_unique< Disp_No_Tracking >(
284 env,
285 name_base,
286 std::move(disp_params),
287 std::forward<Args>(args)... );
288
289 return disp;
290 }
291
292} /* namespace activity_tracking_stuff */
293
294} /* namespace stats */
295
296} /* namespace so_5 */
A base class for agents.
Definition agent.hpp:673
void so_bind_to_dispatcher(event_queue_t &queue) noexcept
Binding agent to the dispatcher.
Definition agent.cpp:872
Alias for namespace with traits of event queue.
const queue_traits::queue_params_t & queue_params() const
Getter for queue parameters.
disp_params_t & tune_queue_params(L tunner)
Tuner for queue parameters.
queue_traits::queue_params_t m_queue_params
Queue parameters.
friend void swap(disp_params_t &a, disp_params_t &b) noexcept
disp_params_t & set_queue_params(queue_traits::queue_params_t p)
Setter for queue parameters.
disp_params_t()=default
Default constructor.
A handle for active_group dispatcher.
dispatcher_handle_t(impl::basic_dispatcher_iface_shptr_t dispatcher) noexcept
impl::basic_dispatcher_iface_shptr_t m_dispatcher
A reference to actual implementation of a dispatcher.
bool empty() const noexcept
Is this handle empty?
void reset() noexcept
Drop the content of handle.
bool operator!() const noexcept
Does this handle contain a reference to dispatcher?
operator bool() const noexcept
Is this handle empty?
disp_binder_shptr_t binder(nonempty_name_t group_name) const
Get a binder for that dispatcher.
const std::string m_group_name
Name of group for new agents.
void preallocate_resources(agent_t &) override
Allocate resources in dispatcher for new agent.
void unbind(agent_t &) noexcept override
Unbind agent from dispatcher.
void bind(agent_t &agent) noexcept override
Bind agent to dispatcher.
actual_dispatcher_iface_shptr_t m_disp
Dispatcher to be used.
void undo_preallocation(agent_t &) noexcept override
Undo resources allocation.
actual_binder_t(actual_dispatcher_iface_shptr_t disp, nonempty_name_t group_name) noexcept
An actual interface of active group dispatcher.
virtual so_5::event_queue_t * query_thread_for_group(const std::string &group_name) noexcept=0
Get the event_queue for the specified active group.
virtual void release_thread_for_group(const std::string &group_name) noexcept=0
Release the thread for the specified active group.
virtual void allocate_thread_for_group(const std::string &group_name)=0
Create a new thread for a group if it necessary.
The very basic interface of active_group dispatcher.
virtual disp_binder_shptr_t binder(nonempty_name_t group_name)=0
static dispatcher_handle_t make(actual_dispatcher_iface_shptr_t disp) noexcept
outliving_reference_t< dispatcher_template_t > m_dispatcher
Dispatcher to work with.
void distribute_value_for_work_thread(const so_5::mbox_t &mbox, const std::string &group_name, const thread_with_refcounter_t &wt)
disp_data_source_t(const std::string_view name_base, outliving_reference_t< dispatcher_template_t > disp)
void distribute(const so_5::mbox_t &mbox) override
Send appropriate notification about the current value.
void release_thread_for_group(const std::string &group_name) noexcept override
Release the thread for the specified active group.
void allocate_thread_for_group(const std::string &group_name) override
Create a new thread for a group if it necessary.
outliving_reference_t< environment_t > m_env
SObjectizer Environment to work in.
so_5::event_queue_t * query_thread_for_group(const std::string &group_name) noexcept override
Get the event_queue for the specified active group.
active_group_map_t m_groups
A map of dispatchers for active groups.
const disp_params_t m_params
Parameters for the dispatcher.
dispatcher_template_t(outliving_reference_t< environment_t > env, const std::string_view name_base, disp_params_t params)
work_thread_shptr_t search_and_try_remove_group_from_map(const std::string &group_name) noexcept
Helper function for searching and erasing agent's thread from map of active threads.
stats::auto_registered_source_holder_t< disp_data_source_t > m_data_source
Data source for run-time monitoring.
disp_binder_shptr_t binder(nonempty_name_t group_name) override
Container for storing parameters for MPSC queue.
const lock_factory_t & lock_factory() const
Getter for lock factory.
queue_params_t & operator=(queue_params_t &&o) noexcept
Move operator.
friend void swap(queue_params_t &a, queue_params_t &b) noexcept
so_5::stats::work_thread_activity_stats_t take_activity_stats()
Get the activity stats.
so_5::current_thread_id_t thread_id() const
Get ID of work thread.
friend void swap(work_thread_activity_tracking_flag_mixin_t &a, work_thread_activity_tracking_flag_mixin_t &b) noexcept
Mixin that holds optional work thread factory.
friend void swap(work_thread_factory_mixin_t &a, work_thread_factory_mixin_t &b) noexcept
An analog of unique_ptr for abstract_work_thread.
Interface for dispatcher binders.
SObjectizer Environment.
stats::repository_t & stats_repository()
Access to repository of data sources for run-time monitoring.
so_5::disp::abstract_work_thread_factory_shptr_t work_thread_factory() const noexcept
Access to the global work thread factory.
An interface of event queue for agent.
A class for the name which cannot be empty.
std::string giveout_value() noexcept(noexcept(std::string{ std::move(m_nonempty_name) }))
Get the value away from the object.
Helper class for indication of long-lived reference via its type.
Definition outliving.hpp:98
T & get() const noexcept
outliving_reference_t(outliving_reference_t const &o) noexcept
void lock()
Lock object.
void unlock()
Unlock object.
Base for the case of externals stats lock.
Base for the case of internal stats lock.
so_5::stats::activity_stats_t m_work_activity
A statistics for work activity.
void start_if_not_started()
A helper method for safe start if start method hasn't been called yet.
so_5::stats::clock_type_t::time_point m_work_started_at
A time point when current activity started.
bool m_is_in_working
A flag for indicating work activity.
A holder for data-souce that should be automatically registered and deregistered in registry.
A type for storing prefix of data_source name.
Definition prefix.hpp:32
bool operator<(const prefix_t &o) const noexcept
Is less than?
Definition prefix.hpp:123
constexpr prefix_t() noexcept
Default constructor creates empty prefix.
Definition prefix.hpp:40
constexpr bool empty() const noexcept
Is prefix empty?
Definition prefix.hpp:99
static constexpr const std::size_t max_buffer_size
Max size of buffer for prefix value (including 0-symbol at the end).
Definition prefix.hpp:37
constexpr std::string_view as_string_view() const noexcept(noexcept(std::string_view{std::declval< const char * >()}))
Access to prefix value as string_view.
Definition prefix.hpp:91
constexpr const char * c_str() const noexcept
Access to prefix value.
Definition prefix.hpp:80
constexpr prefix_t(const char *value) noexcept
Initializing constructor.
Definition prefix.hpp:54
bool operator!=(const prefix_t &o) const noexcept
Is not equal?
Definition prefix.hpp:115
static constexpr const std::size_t max_length
Max length of prefix (not including 0-symbol at the end).
Definition prefix.hpp:35
char m_value[max_buffer_size]
Actual value.
Definition prefix.hpp:130
prefix_t(const std::string &value) noexcept(noexcept(value.c_str()))
Initializing constructor.
Definition prefix.hpp:73
bool operator==(const prefix_t &o) const noexcept
Is equal?
Definition prefix.hpp:107
An interface of data source.
A type for representing the suffix of data_source name.
Definition prefix.hpp:156
constexpr bool operator<(const suffix_t &o) const noexcept
Compares suffixes by pointer value.
Definition prefix.hpp:203
constexpr bool operator==(const suffix_t &o) const noexcept
Compares suffixes by pointer values.
Definition prefix.hpp:187
constexpr const char * c_str() const noexcept
Access to suffix value.
Definition prefix.hpp:168
const char * m_value
Actual value.
Definition prefix.hpp:210
constexpr std::string_view as_string_view() const noexcept(noexcept(std::string_view{std::declval< const char * >()}))
Access to prefix value as string_view.
Definition prefix.hpp:179
constexpr suffix_t(const char *value) noexcept
Initializing constructor.
Definition prefix.hpp:159
constexpr bool operator!=(const suffix_t &o) const noexcept
Compares suffixes by pointer value.
Definition prefix.hpp:195
#define SO_5_FUNC
Definition declspec.hpp:48
Helpers for manipulation with standard C++ I/O streams.
Some reusable and low-level classes/functions which can be used in public header files.
void shutdown_and_wait(T &w)
Just a helper function for consequetive call to shutdown and wait.
void send_thread_activity_stats(const so_5::mbox_t &, const stats::prefix_t &, work_thread::work_thread_no_activity_tracking_t &)
void send_thread_activity_stats(const so_5::mbox_t &mbox, const stats::prefix_t &prefix, work_thread::work_thread_with_activity_tracking_t &wt)
Active groups dispatcher implemetation details.
Active groups dispatcher.
dispatcher_handle_t make_dispatcher(so_5::environment_t &env, const std::string_view data_sources_name_base)
Create an instance of active_group dispatcher.
dispatcher_handle_t make_dispatcher(so_5::environment_t &env)
Create an instance of active_group dispatcher.
SO_5_FUNC dispatcher_handle_t make_dispatcher(environment_t &env, const std::string_view data_sources_name_base, disp_params_t params)
Create an instance of active_group dispatcher.
Various stuff related to MPSC event queue implementation and tuning.
Implemetation details of dispatcher's working thread.
Reusable components for dispatchers.
abstract_work_thread_factory_shptr_t actual_work_thread_factory_to_use(const work_thread_factory_mixin_t< Params > &params, const environment_t &env) noexcept
Helper to detect actual work thread factory to be used.
work_thread_holder_t acquire_work_thread(const work_thread_factory_mixin_t< Params > &params, environment_t &env)
Helper function for acquiring a new worker thread from an appropriate work thread factory.
so_5::stats::prefix_t make_disp_prefix(const std::string_view disp_type, const std::string_view data_sources_name_base, const void *disp_this_pointer)
Create basic prefix for dispatcher data source names.
void modify_disp_params(so_5::environment_t &env, Disp_Params_Type &params)
Helper functions to adjust some dispatcher parameters with respect to settings from environment.
so_5::stats::prefix_t make_disp_working_thread_prefix(const so_5::stats::prefix_t &disp_prefix, std::size_t thread_number)
Create prefix for dispatcher's working thread data source.
std::unique_ptr< Disp_Iface_Type > make_actual_dispatcher(outliving_reference_t< environment_t > env, const std::string_view name_base, Disp_Params_Type disp_params, Args &&...args)
Helper function for creation of dispatcher instance with respect to work thread activity tracking fla...
Event dispatchers.
Details of SObjectizer run-time implementations.
Definition agent.cpp:780
std::unique_ptr< Common_Disp_Iface_Type > create_appropriate_disp(outliving_reference_t< Env > env, const std::string_view name_base, Disp_Params disp_params, Args &&...args)
Helper function for creation of dispatcher with respect to activity tracking flag in dispatcher param...
void update_stats_from_current_time(activity_stats_t &value_to_update, clock_type_t::time_point activity_started_at)
Helper function for simplification of current stats update.
Declarations of messages used by run-time monitoring and statistics.
Definition messages.hpp:36
Predefined suffixes of data-sources.
Definition std_names.hpp:55
SO_5_FUNC suffix_t agent_count()
Suffix for data source with count of agents bound to some entity.
Definition std_names.cpp:66
SO_5_FUNC suffix_t work_thread_queue_size()
Suffix for data source with count of demands in a working thread event queue.
Definition std_names.cpp:78
SO_5_FUNC suffix_t disp_active_group_count()
Suffix for data source with count of active groups in an active_group dispatcher.
Definition std_names.cpp:72
SO_5_FUNC suffix_t work_thread_activity()
Suffix for data source with work thread activity statistics.
Definition std_names.cpp:84
All stuff related to run-time monitoring and statistics.
Private part of message limit implementation.
Definition agent.cpp:33
void send(Target &&to, Args &&... args)
A utility function for creating and delivering a message or a signal.
work_thread_activity_tracking_t
Values for dispatcher's work thread activity tracking.
Definition types.hpp:75
@ unspecified
Tracking mode is specified elsewhere.
outliving_reference_t< T > outliving_mutable(T &r)
Make outliving_reference wrapper for mutable reference.
Helper for showing only part of long string.
length_limited_string(const std::string_view what, std::size_t limit)
Helper for showing pointer value.
Statistics of some activity.
std::uint_fast64_t m_count
Count of events in that period of time.
Default locking policy for stats_collector_t.
An analog of std::lock_guard but without actual locking actions.
A special class for cases where lock is not needed at all.
Various traits of activity tracking implementation.
A message with value of some quantity.
Definition messages.hpp:60
Information about one work thread activity.
Definition messages.hpp:108