SObjectizer  5.8
Loading...
Searching...
No Matches
work_thread_activity.hpp
Go to the documentation of this file.
1/*
2 * SObjectizer-5
3 */
4
5/*!
6 * \file
7 * \brief Data types for work thread's activity stats.
8 *
9 * \since v.5.5.18
10 */
11
12#pragma once
13
14#include <cstdint>
15#include <chrono>
16#include <iostream>
17#include <optional>
18#include <type_traits>
19
20namespace so_5
21{
22
23namespace stats
24{
25
26/*!
27 * \brief An alias for clock type to be used for statistics.
28 *
29 * \since v.5.5.18
30 */
35 >::type;
36
37static_assert( clock_type_t::is_steady,
38 "clock_type_t must be steady clock!" );
39
40/*!
41 * \brief An alias for type for representing time duration.
42 *
43 * \since v.5.5.18
44 */
46
47/*!
48 * \brief Statistics of some activity.
49 *
50 * \since v.5.5.18
51 */
53 {
54 //! Count of events in that period of time.
56
57 //! Total time spent for events in that period of time.
59
60 //! Average time for one event.
62
63 //! Duration of the current activity.
64 //!
65 //! This value is defined only if the current activity is present.
66 //! Otherwise it will be nullopt.
67 //!
68 //! When it isn't nullopt it contains duration of the current
69 //! activity. For example, the total execution time of the current
70 //! event handler. If an instance of activity_stats_t describes
71 //! waiting, then this field shows duration of the current waiting
72 //! period.
73 //!
74 //! \since v.5.8.5
76 };
77
78/*!
79 * \brief Helper for printing value of activity_stats.
80 *
81 * \since v.5.5.18
82 */
83inline std::ostream &
85{
86 auto to_ms = []( const duration_t & d ) {
87 return double(std::chrono::duration_cast< std::chrono::nanoseconds >( d )
88 .count()) / 1000000.0;
89 };
90
91 to << "[count=" << what.m_count
92 << ";total=" << to_ms(what.m_total_time)
93 << "ms;avg=" << to_ms(what.m_avg_time) << "ms";
94 if( what.m_current_activity_time )
95 to << ";current=" << to_ms(*what.m_current_activity_time) << "ms";
96 to << ']';
97
98 return to;
99}
100
101/*!
102 * \brief Stats for a work thread activity.
103 *
104 * \since v.5.5.18
105 */
107 {
108 //! Stats for processed events.
110
111 //! Stats for waiting periods.
112 /*!
113 * \note
114 * Not all dispatchers can provide this information. In such a case
115 * there will be an object without actual information (all fields of
116 * m_waiting_stats will have default zero values).
117 */
119 };
120
121namespace details
122{
123
124/*!
125 * \brief A function for calculating average value.
126 *
127 * Uses modified moving average (also known as running moving average)
128 * which is calculated as: MMA(t) = (last + (N-1)*MMA(t-1))/N.
129 *
130 * \since v.5.5.18
131 */
132inline duration_t
134 //! Total count of samples.
136 //! Previous average value.
138 //! Duration of last activity (or waiting).
140{
141 const int N = count > 100u ? 100 : (count ? static_cast< int >(count) : 1);
142 return (last + (N-1) * previous) / N;
143}
144
145/*!
146 * \brief Helper function for simplification of current stats update.
147 *
148 * \since v.5.5.18
149 */
150inline void
152 activity_stats_t & value_to_update,
153 clock_type_t::duration last_duration )
154{
155 value_to_update.m_total_time += last_duration;
156 value_to_update.m_avg_time = calc_avg_time(
157 value_to_update.m_count,
158 value_to_update.m_avg_time, last_duration );
159}
160
161/*!
162 * \brief Helper function for simplification of current stats update.
163 *
164 * \since v.5.5.18
165 */
166inline void
168 activity_stats_t & value_to_update,
169 clock_type_t::time_point activity_started_at )
170{
171 const auto current_activity_time =
172 clock_type_t::now() - activity_started_at;
173 value_to_update.m_current_activity_time = current_activity_time;
174 update_stats_from_duration(
175 value_to_update,
176 current_activity_time );
177}
178
179} /* namespace details */
180
181} /* namespace stats */
182
183} /* namespace so_5 */
A base class for agents.
Definition agent.hpp:673
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.
Mixin that holds optional work thread factory.
An analog of unique_ptr for abstract_work_thread.
Interface for dispatcher binders.
SObjectizer Environment.
An interface of event queue for agent.
A class for the name which cannot be empty.
Helper class for indication of long-lived reference via its type.
Definition outliving.hpp:98
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.
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.
void update_stats_from_duration(activity_stats_t &value_to_update, clock_type_t::duration last_duration)
Helper function for simplification of current stats update.
duration_t calc_avg_time(std::uint_fast64_t count, duration_t previous, duration_t last)
A function for calculating average value.
All stuff related to run-time monitoring and statistics.
std::ostream & operator<<(std::ostream &to, const prefix_t &what)
Just a helper operator.
Definition prefix.hpp:139
std::ostream & operator<<(std::ostream &to, const suffix_t &what)
Just a helper operator.
Definition prefix.hpp:219
std::ostream & operator<<(std::ostream &to, const activity_stats_t &what)
Helper for printing value of activity_stats.
Private part of message limit implementation.
Definition agent.cpp:33
Statistics of some activity.
std::optional< duration_t > m_current_activity_time
duration_t m_avg_time
Average time for one event.
duration_t m_total_time
Total time spent for events in that period of time.
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.
activity_stats_t m_working_stats
Stats for processed events.
activity_stats_t m_waiting_stats
Stats for waiting periods.