MADARA  3.1.8
BandwidthMonitor.cpp
Go to the documentation of this file.
1 #include "BandwidthMonitor.h"
5 
6 
8  time_t window_in_secs)
9  : utilization_ (0), window_ (window_in_secs)
10 {
11 }
12 
14  const BandwidthMonitor & rhs)
16  window_ (rhs.window_)
17 {
18 }
19 
21 {
22 }
23 
24 void
26  const BandwidthMonitor & rhs)
27 {
28  MADARA_GUARD_TYPE guard (mutex_);
29  if (this != &rhs)
30  {
31  messages_ = rhs.messages_;
33  window_ = rhs.window_;
34  }
35 }
36 
37 void
39  time_t window_in_secs)
40 {
41  MADARA_GUARD_TYPE guard (mutex_);
42 
43  window_ = window_in_secs;
44 }
45 
46 void
48 {
49  MADARA_GUARD_TYPE guard (mutex_);
50 
51  utilization_ += size;
52 
53  time_t cur_time = update_utilization ();
54 
55  BandwidthRecord entry (cur_time, size);
56 
57  messages_.push_back (entry);
58 }
59 
60 void
62  time_t timestamp, uint64_t size)
63 {
64  MADARA_GUARD_TYPE guard (mutex_);
65 
66  utilization_ += size;
67 
68  BandwidthRecord entry (timestamp, size);
69 
71 
72  messages_.push_back (entry);
73 }
74 
75 bool
77  int64_t limit)
78 {
79  bool result = false;
80 
81  if (limit >= 0 && uint64_t (limit) > get_utilization ())
82  result = true;
83 
84  return result;
85 }
86 
87 uint64_t
89 {
90  MADARA_GUARD_TYPE guard (mutex_);
91 
93 
94  return utilization_;
95 }
96 
97 uint64_t
99 {
100  MADARA_GUARD_TYPE guard (mutex_);
101 
103 
104  return utilization_ / (uint64_t)window_;
105 }
106 
107 void
109 {
110  MADARA_GUARD_TYPE guard (mutex_);
111  messages_.clear ();
112  utilization_ = 0;
113 }
114 
115 void
117  void)
118 {
119  MADARA_GUARD_TYPE guard (mutex_);
120 
122 
123  madara_logger_ptr_log (logger::global_logger.get(), logger::LOG_ALWAYS, "Bandwidth: %d messages "
124  "for %" PRIu64 " bytes over %lld window (%" PRIu64 " B/s)\n", messages_.size (),
125  utilization_, (long long)window_, get_bytes_per_second ());
126 }
127 
128 
129 size_t
131  void)
132 {
133  MADARA_GUARD_TYPE guard (mutex_);
134 
136 
137  return messages_.size ();
138 }
void print_utilization(void)
Prints the number of messages and utilization within the past window.
uint64_t get_bytes_per_second(void)
Queries the monitor for the current bandwidth utilization per second over the past window...
MADARA_LOCK_TYPE mutex_
Mutex for supporting multithreaded monitor calls.
bool is_bandwidth_violated(int64_t limit)
Checks send and receive bandwidth against send and receive limits.
time_t update_utilization(void)
Updates utilization for most public functions.
void operator=(const BandwidthMonitor &rhs)
Assignment operator.
MADARA_Export utility::Refcounter< logger::Logger > global_logger
void clear(void)
Clears the bandwidth monitor.
BandwidthMonitor(time_t window_in_secs=10)
Default constructor.
uint64_t get_utilization(void)
Queries the monitor for the current bandwidth utilization.
BandwidthMessages messages_
Map of timestamps to messages.
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:32
size_t get_number_of_messages(void)
Returns the number of messages in the past window.
void add(uint64_t size)
Adds a message to the monitor.
std::pair< time_t, uint64_t > BandwidthRecord
Provides monitoring capability of a transport&#39;s bandwidth.
void set_window(time_t window_in_secs)
Sets the window in seconds to measure bandwidth.
time_t window_
Time window for useful messages to bandwidth calculations.