DDS  ver. 1.6
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 #include <atomic>
9 #include <deque>
10 #include <map>
11 #include <mutex>
12 #pragma clang diagnostic push
13 #pragma clang diagnostic ignored "-Wunused-local-typedef"
14 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
15 #include <boost/accumulators/accumulators.hpp>
16 #include <boost/accumulators/statistics/count.hpp>
17 #include <boost/accumulators/statistics/max.hpp>
18 #include <boost/accumulators/statistics/mean.hpp>
19 #include <boost/accumulators/statistics/min.hpp>
20 #include <boost/accumulators/statistics/stats.hpp>
21 #include <boost/accumulators/statistics/sum.hpp>
22 #include <boost/asio.hpp>
23 #pragma clang diagnostic pop
24 
25 #include "ProtocolMessage.h"
26 
27 namespace dds
28 {
29  namespace protocol_api
30  {
31  typedef boost::accumulators::accumulator_set<double,
32  boost::accumulators::features<boost::accumulators::tag::mean,
33  boost::accumulators::tag::max,
34  boost::accumulators::tag::count,
35  boost::accumulators::tag::sum>>
37 
38  // Maps command to stats accumulator
39  typedef std::map<uint16_t, statsAccumulator_t> statsAccumulatorMap_t;
40 
41  struct SStatParams;
42  typedef std::map<uint16_t, SStatParams> statParamsMap_t;
43 
44  struct SStatParams
45  {
47  : m_mean(std::numeric_limits<double>::min())
48  , m_max(std::numeric_limits<double>::min())
49  , m_count(0)
50  , m_sum(0)
51  {
52  }
53 
54  void fillFromAccumulator(const statsAccumulator_t& _accumulator);
55 
58  void addFromStatParams(const SStatParams& _stat);
59 
62  std::string toString() const;
63 
64  double m_mean;
65  double m_max;
66  double m_count;
67  double m_sum;
68  };
69 
70  struct SWriteStat
71  {
73  : m_messageBytes()
74  , m_queueBytes()
75  , m_queueMessages()
76  {
77  }
78 
81  void addFromStat(const SWriteStat& _stat);
82 
85  std::string toString() const;
86 
90  statParamsMap_t m_messageBytesMap;
91  };
92 
93  struct SReadStat
94  {
96  : m_messageBytes()
97  {
98  }
99 
102  void addFromStat(const SReadStat& _stat);
103 
106  std::string toString() const;
107 
109  statParamsMap_t m_messageBytesMap;
110  };
111 
112  class CStatImpl
113  {
114  typedef std::deque<CProtocolMessage::protocolMessagePtr_t> protocolMessagePtrQueue_t;
115 
116  public:
117  CStatImpl(boost::asio::io_service& _service)
118  : m_statEnabled(false)
119  , m_logReadMutex()
120  , m_readMessageBytesAccumulator()
121  , m_readMessageBytesAccumulatorMap()
122  , m_logWriteMutex()
123  , m_writeMessageBytesAccumulator()
124  , m_writeQueueBytesAccumulator()
125  , m_writeQueueMessagesAccumulator()
126  , m_writeMessageBytesAccumulatorMap()
127  , m_io_service(_service)
128  {
129  }
130 
132  SReadStat getReadStat() const;
133 
135  SWriteStat getWriteStat() const;
136 
138  void setStatEnabled(bool _statEnabled);
139 
140  // TODO: We have to think if it would be better to use weak_ptr here instead of shared_ptr
141  void logReadMessage(CProtocolMessage::protocolMessagePtr_t _message);
142  void logWriteMessages(const protocolMessagePtrQueue_t& _messageQueue);
143 
144  private:
145  // Enable/Disable statistics accumulation
146  std::atomic<bool> m_statEnabled;
147 
148  // Read statistics
149  mutable std::mutex m_logReadMutex;
150  statsAccumulator_t m_readMessageBytesAccumulator;
151  statsAccumulatorMap_t m_readMessageBytesAccumulatorMap;
152 
153  // Write statistics
154  mutable std::mutex m_logWriteMutex;
155  statsAccumulator_t m_writeMessageBytesAccumulator;
156  statsAccumulator_t m_writeQueueBytesAccumulator;
157  statsAccumulator_t m_writeQueueMessagesAccumulator;
158  statsAccumulatorMap_t m_writeMessageBytesAccumulatorMap;
159 
160  boost::asio::io_service& m_io_service;
161  };
162  }
163 };
164 
165 #endif /* defined(__DDS__StatImpl__) */
SReadStat()
Definition: StatImpl.h:95
double m_sum
Definition: StatImpl.h:67
Definition: StatImpl.h:44
statParamsMap_t m_messageBytesMap
Definition: StatImpl.h:109
void addFromStatParams(const SStatParams &_stat)
Add statistics from another structure.
Definition: StatImpl.cpp:81
SStatParams m_queueBytes
Definition: StatImpl.h:88
double m_max
Definition: StatImpl.h:65
SStatParams m_messageBytes
Definition: StatImpl.h:87
SStatParams m_queueMessages
Definition: StatImpl.h:89
SStatParams()
Definition: StatImpl.h:46
SWriteStat()
Definition: StatImpl.h:72
Definition: dds-agent/src/AgentConnectionManager.h:16
std::map< uint16_t, statsAccumulator_t > statsAccumulatorMap_t
Definition: StatImpl.h:39
double m_mean
Definition: StatImpl.h:64
Definition: StatImpl.h:70
std::string toString() const
String representation of the object.
double m_count
Definition: StatImpl.h:66
std::map< uint16_t, SStatParams > statParamsMap_t
Definition: StatImpl.h:41
void fillFromAccumulator(const statsAccumulator_t &_accumulator)
Definition: StatImpl.cpp:89
Definition: StatImpl.h:112
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:36
statParamsMap_t m_messageBytesMap
Definition: StatImpl.h:90
Definition: StatImpl.h:93
SStatParams m_messageBytes
Definition: StatImpl.h:108
CStatImpl(boost::asio::io_service &_service)
Definition: StatImpl.h:117
std::shared_ptr< CProtocolMessage > protocolMessagePtr_t
Definition: ProtocolMessage.h:69