MADARA  3.4.1
BandwidthMonitor.cpp
Go to the documentation of this file.
1 #include "BandwidthMonitor.h"
5 
7  : utilization_(0), window_(window_in_secs)
8 {
9 }
10 
12  const BandwidthMonitor& rhs)
13  : messages_(rhs.messages_),
14  utilization_(rhs.utilization_),
15  window_(rhs.window_)
16 {
17 }
18 
20 
22 {
23  MADARA_GUARD_TYPE guard(mutex_);
24  if (this != &rhs)
25  {
26  messages_ = rhs.messages_;
27  utilization_ = rhs.utilization_;
28  window_ = rhs.window_;
29  }
30 }
31 
33 {
34  MADARA_GUARD_TYPE guard(mutex_);
35 
36  window_ = window_in_secs;
37 }
38 
40 {
41  MADARA_GUARD_TYPE guard(mutex_);
42 
43  utilization_ += size;
44 
45  time_t cur_time = update_utilization();
46 
47  BandwidthRecord entry(cur_time, size);
48 
49  messages_.push_back(entry);
50 }
51 
52 void madara::transport::BandwidthMonitor::add(time_t timestamp, uint64_t size)
53 {
54  MADARA_GUARD_TYPE guard(mutex_);
55 
56  utilization_ += size;
57 
58  BandwidthRecord entry(timestamp, size);
59 
60  update_utilization();
61 
62  messages_.push_back(entry);
63 }
64 
66 {
67  bool result = false;
68 
69  if (limit >= 0 && uint64_t(limit) > get_utilization())
70  result = true;
71 
72  return result;
73 }
74 
76 {
77  MADARA_GUARD_TYPE guard(mutex_);
78 
79  update_utilization();
80 
81  return utilization_;
82 }
83 
85 {
86  MADARA_GUARD_TYPE guard(mutex_);
87 
88  update_utilization();
89 
90  return utilization_ / (uint64_t)window_;
91 }
92 
94 {
95  MADARA_GUARD_TYPE guard(mutex_);
96  messages_.clear();
97  utilization_ = 0;
98 }
99 
101 {
102  MADARA_GUARD_TYPE guard(mutex_);
103 
104  update_utilization();
105 
107  "Bandwidth: %d messages "
108  "for %" PRIu64 " bytes over %lld window (%" PRIu64 " B/s)\n",
109  messages_.size(), utilization_, (long long)window_,
110  get_bytes_per_second());
111 }
112 
114 {
115  MADARA_GUARD_TYPE guard(mutex_);
116 
117  update_utilization();
118 
119  return messages_.size();
120 }
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:41
Provides monitoring capability of a transport's bandwidth.
void operator=(const BandwidthMonitor &rhs)
Assignment operator.
void set_window(time_t window_in_secs)
Sets the window in seconds to measure bandwidth.
uint64_t get_bytes_per_second(void)
Queries the monitor for the current bandwidth utilization per second over the past window.
BandwidthMonitor(time_t window_in_secs=10)
Default constructor.
bool is_bandwidth_violated(int64_t limit)
Checks send and receive bandwidth against send and receive limits.
uint64_t get_utilization(void)
Queries the monitor for the current bandwidth utilization.
void add(uint64_t size)
Adds a message to the monitor.
time_t window_
Time window for useful messages to bandwidth calculations.
void clear(void)
Clears the bandwidth monitor.
size_t get_number_of_messages(void)
Returns the number of messages in the past window.
void print_utilization(void)
Prints the number of messages and utilization within the past window.
BandwidthMessages messages_
Map of timestamps to messages.
T get(const KnowledgeRecord &kr)
Get the value of a KnowlegeRecord.
Definition: GetRecord.h:121
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger
std::pair< time_t, uint64_t > BandwidthRecord