DDS  ver. 3.4
StatImpl.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 //
4 //
5 #ifndef __DDS__StatImpl__
6 #define __DDS__StatImpl__
7 
8 // STD
9 #include <atomic>
10 #include <deque>
11 #include <map>
12 #include <mutex>
13 // BOOST
14 #include <boost/accumulators/accumulators.hpp>
15 #include <boost/accumulators/statistics/count.hpp>
16 #include <boost/accumulators/statistics/max.hpp>
17 #include <boost/accumulators/statistics/mean.hpp>
18 #include <boost/accumulators/statistics/min.hpp>
19 #include <boost/accumulators/statistics/stats.hpp>
20 #include <boost/accumulators/statistics/sum.hpp>
21 #include <boost/asio.hpp>
22 // DDS
23 #include "ProtocolMessage.h"
24 
25 namespace dds
26 {
27  namespace protocol_api
28  {
29  typedef boost::accumulators::accumulator_set<double,
30  boost::accumulators::features<boost::accumulators::tag::mean,
31  boost::accumulators::tag::max,
32  boost::accumulators::tag::count,
33  boost::accumulators::tag::sum>>
35 
36  // Maps command to stats accumulator
37  typedef std::map<uint16_t, statsAccumulator_t> statsAccumulatorMap_t;
38 
39  struct SStatParams;
40  typedef std::map<uint16_t, SStatParams> statParamsMap_t;
41 
42  struct SStatParams
43  {
45  : m_mean(std::numeric_limits<double>::min())
46  , m_max(std::numeric_limits<double>::min())
47  , m_count(0)
48  , m_sum(0)
49  {
50  }
51 
52  void fillFromAccumulator(const statsAccumulator_t& _accumulator);
53 
56  void addFromStatParams(const SStatParams& _stat);
57 
60  std::string toString() const;
61 
62  double m_mean;
63  double m_max;
64  double m_count;
65  double m_sum;
66  };
67 
68  struct SWriteStat
69  {
71  : m_messageBytes()
72  , m_queueBytes()
73  , m_queueMessages()
74  {
75  }
76 
79  void addFromStat(const SWriteStat& _stat);
80 
83  std::string toString() const;
84 
89  };
90 
91  struct SReadStat
92  {
94  : m_messageBytes()
95  {
96  }
97 
100  void addFromStat(const SReadStat& _stat);
101 
104  std::string toString() const;
105 
108  };
109 
110  class CStatImpl
111  {
112  typedef std::deque<CProtocolMessage::protocolMessagePtr_t> protocolMessagePtrQueue_t;
113 
114  public:
115  CStatImpl(boost::asio::io_context& _context)
116  : m_statEnabled(false)
117  , m_logReadMutex()
118  , m_readMessageBytesAccumulator()
119  , m_readMessageBytesAccumulatorMap()
120  , m_logWriteMutex()
121  , m_writeMessageBytesAccumulator()
122  , m_writeQueueBytesAccumulator()
123  , m_writeQueueMessagesAccumulator()
124  , m_writeMessageBytesAccumulatorMap()
125  , m_ioContext(_context)
126  {
127  }
128 
130  SReadStat getReadStat() const;
131 
133  SWriteStat getWriteStat() const;
134 
136  void setStatEnabled(bool _statEnabled);
137 
138  // TODO: We have to think if it would be better to use weak_ptr here instead of shared_ptr
140  void logWriteMessages(const protocolMessagePtrQueue_t& _messageQueue);
141 
142  private:
143  // Enable/Disable statistics accumulation
144  std::atomic<bool> m_statEnabled;
145 
146  // Read statistics
147  mutable std::mutex m_logReadMutex;
148  statsAccumulator_t m_readMessageBytesAccumulator;
149  statsAccumulatorMap_t m_readMessageBytesAccumulatorMap;
150 
151  // Write statistics
152  mutable std::mutex m_logWriteMutex;
153  statsAccumulator_t m_writeMessageBytesAccumulator;
154  statsAccumulator_t m_writeQueueBytesAccumulator;
155  statsAccumulator_t m_writeQueueMessagesAccumulator;
156  statsAccumulatorMap_t m_writeMessageBytesAccumulatorMap;
157 
158  boost::asio::io_context& m_ioContext;
159  };
160  } // namespace protocol_api
161 }; // namespace dds
162 
163 #endif /* defined(__DDS__StatImpl__) */
SReadStat()
Definition: StatImpl.h:93
double m_sum
Definition: StatImpl.h:65
Definition: StatImpl.h:42
void logReadMessage(CProtocolMessage::protocolMessagePtr_t _message)
Definition: StatImpl.cpp:189
statParamsMap_t m_messageBytesMap
Definition: StatImpl.h:107
void addFromStatParams(const SStatParams &_stat)
Add statistics from another structure.
Definition: StatImpl.cpp:81
SStatParams m_queueBytes
Definition: StatImpl.h:86
double m_max
Definition: StatImpl.h:63
SStatParams m_messageBytes
Definition: StatImpl.h:85
SStatParams m_queueMessages
Definition: StatImpl.h:87
SReadStat getReadStat() const
Return read statistics.
Definition: StatImpl.cpp:154
SStatParams()
Definition: StatImpl.h:44
SWriteStat()
Definition: StatImpl.h:70
Definition: AgentConnectionManager.h:13
CStatImpl(boost::asio::io_context &_context)
Definition: StatImpl.h:115
std::map< uint16_t, statsAccumulator_t > statsAccumulatorMap_t
Definition: StatImpl.h:37
double m_mean
Definition: StatImpl.h:62
Definition: StatImpl.h:68
std::string toString() const
String representation of the object.
SWriteStat getWriteStat() const
Return write statistics.
Definition: StatImpl.cpp:168
double m_count
Definition: StatImpl.h:64
std::map< uint16_t, SStatParams > statParamsMap_t
Definition: StatImpl.h:39
void addFromStat(const SReadStat &_stat)
Add statistics from another structure.
Definition: StatImpl.cpp:133
void fillFromAccumulator(const statsAccumulator_t &_accumulator)
Definition: StatImpl.cpp:89
std::string toString() const
String representation of the object.
Definition: StatImpl.cpp:142
std::string toString() const
String representation of the object.
Definition: StatImpl.cpp:111
Definition: StatImpl.h:110
void setStatEnabled(bool _statEnabled)
Enable/disable statistics accumulation.
Definition: StatImpl.cpp:184
void addFromStat(const SWriteStat &_stat)
Add statistics from another structure.
Definition: StatImpl.cpp:100
boost::accumulators::accumulator_set< double, boost::accumulators::features< boost::accumulators::tag::mean, boost::accumulators::tag::max, boost::accumulators::tag::count, boost::accumulators::tag::sum > > statsAccumulator_t
Definition: StatImpl.h:34
statParamsMap_t m_messageBytesMap
Definition: StatImpl.h:88
Definition: StatImpl.h:91
SStatParams m_messageBytes
Definition: StatImpl.h:106
void logWriteMessages(const protocolMessagePtrQueue_t &_messageQueue)
Definition: StatImpl.cpp:205
std::shared_ptr< CProtocolMessage > protocolMessagePtr_t
Definition: ProtocolMessage.h:81