Electroneum
network_throttle-detail.cpp
Go to the documentation of this file.
1 
5 // Copyrights(c) 2017-2021, The Electroneum Project
6 // Copyrights(c) 2014-2019, The Monero Project
7 //
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without modification, are
11 // permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright notice, this list of
14 // conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
17 // of conditions and the following disclaimer in the documentation and/or other
18 // materials provided with the distribution.
19 //
20 // 3. Neither the name of the copyright holder nor the names of its contributors may be
21 // used to endorse or promote products derived from this software without specific
22 // prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
32 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 /* rfree: implementation for throttle details */
35 
36 #include <string>
37 #include <vector>
38 #include <atomic>
39 
40 #include <boost/asio.hpp>
41 
42 #include <memory>
43 
44 #include "syncobj.h"
45 
46 #include "net/net_utils_base.h"
47 #include "misc_log_ex.h"
48 #include <boost/chrono.hpp>
49 #include "misc_language.h"
50 #include "pragma_comp_defs.h"
51 #include <sstream>
52 #include <iomanip>
53 #include <algorithm>
54 
55 
56 
57 #include <boost/asio/basic_socket.hpp>
58 #include <boost/asio/ip/unicast.hpp>
60 
61 // TODO:
63 
64 #undef ELECTRONEUM_DEFAULT_LOG_CATEGORY
65 #define ELECTRONEUM_DEFAULT_LOG_CATEGORY "net.throttle"
66 
67 // ################################################################################################
68 // ################################################################################################
69 // the "header part". Not separeted out for .hpp because point of this modification is
70 // to rebuild just 1 translation unit while working on this code.
71 // (But maybe common parts will be separated out later though - if needed)
72 // ################################################################################################
73 // ################################################################################################
74 
75 namespace epee
76 {
77 namespace net_utils
78 {
79 
80 
81 /* ============================================================================ */
82 
83 class connection_basic_pimpl {
84  public:
86 
87  static int m_default_tos;
88 
89  network_throttle_bw m_throttle; // per-perr
90  critical_section m_throttle_lock;
91 
92  void _packet(size_t packet_size, int phase, int q_len); // execute a sleep ; phase is not really used now(?) could be used for different kinds of sleep e.g. direct/queue write
93 };
94 
95 
96 } // namespace
97 } // namespace
98 
99 
100 
101 
102 
103 
104 // ################################################################################################
105 // ################################################################################################
106 // The implementation part
107 // ################################################################################################
108 // ################################################################################################
109 
110 namespace epee
111 {
112 namespace net_utils
113 {
114 
115 // ================================================================================================
116 // network_throttle
117 // ================================================================================================
118 
120 
121 network_throttle::packet_info::packet_info()
122  : m_size(0)
123 {
124 }
125 
126 network_throttle::network_throttle(const std::string &nameshort, const std::string &name, int window_size)
127  : m_window_size( (window_size==-1) ? 10 : window_size ),
128  m_history( m_window_size ), m_nameshort(nameshort)
129 {
130  set_name(name);
131  m_network_add_cost = 128;
132  m_network_minimal_segment = 256;
133  m_network_max_segment = 1024*1024;
134  m_start_time = 0;
135  m_any_packet_yet = false;
136  m_slot_size = 1.0; // hard coded in few places
137  m_target_speed = 16 * 1024; // other defaults are probably defined in the command-line parsing code when this class is used e.g. as main global throttle
138  m_last_sample_time = 0;
139  m_history.resize(m_window_size);
140  m_total_packets = 0;
141  m_total_bytes = 0;
142 }
143 
145 {
146  m_name = name;
147 }
148 
150 {
151  m_target_speed = target * 1024;
152  MINFO("Setting LIMIT: " << target << " kbps");
153 }
154 
156 {
157  return m_target_speed / 1024;
158 }
159 
161 {
162  double time_now = get_time_seconds();
163  if (!m_any_packet_yet) m_start_time = time_now; // starting now
164 
165  network_time_seconds current_sample_time_slot = time_to_slot( time_now ); // T=13.7 --> 13 (for 1-second smallwindow)
166  network_time_seconds last_sample_time_slot = time_to_slot( m_last_sample_time );
167 
168  // moving to next position, and filling gaps
169  // !! during this loop the m_last_sample_time and last_sample_time_slot mean the variable moved in +1
170  // TODO optimize when moving few slots at once
171  while ( (!m_any_packet_yet) || (last_sample_time_slot < current_sample_time_slot))
172  {
173  _dbg3("Moving counter buffer by 1 second " << last_sample_time_slot << " < " << current_sample_time_slot << " (last time " << m_last_sample_time<<")");
174  // rotate buffer
175  m_history.push_front(packet_info());
176  if (! m_any_packet_yet)
177  {
178  m_last_sample_time = time_now;
179  }
180  m_last_sample_time += 1; last_sample_time_slot = time_to_slot( m_last_sample_time ); // increase and recalculate time, time slot
181  m_any_packet_yet=true;
182  }
183  m_last_sample_time = time_now; // the real exact last time
184 }
185 
186 void network_throttle::handle_trafic_exact(size_t packet_size)
187 {
188  _handle_trafic_exact(packet_size, packet_size);
189 }
190 
191 void network_throttle::_handle_trafic_exact(size_t packet_size, size_t orginal_size)
192 {
193  tick();
194 
195  calculate_times_struct cts ; calculate_times(packet_size, cts , false, -1);
196  calculate_times_struct cts2; calculate_times(packet_size, cts2, false, 5);
197  m_history.front().m_size += packet_size;
198  m_total_packets++;
199  m_total_bytes += packet_size;
200 
201  std::ostringstream oss; oss << "["; for (auto sample: m_history) oss << sample.m_size << " "; oss << "]" << std::ends;
202  std::string history_str = oss.str();
203 
204  MTRACE("Throttle " << m_name << ": packet of ~"<<packet_size<<"b " << " (from "<<orginal_size<<" b)"
205  << " Speed AVG=" << std::setw(4) << ((long int)(cts .average/1024)) <<"[w="<<cts .window<<"]"
206  << " " << std::setw(4) << ((long int)(cts2.average/1024)) <<"[w="<<cts2.window<<"]"
207  <<" / " << " Limit="<< ((long int)(m_target_speed/1024)) <<" KiB/sec "
208  << " " << history_str
209  );
210 }
211 
212 void network_throttle::handle_trafic_tcp(size_t packet_size)
213 {
214  size_t all_size = packet_size + m_network_add_cost;
215  all_size = std::max( m_network_minimal_segment , all_size);
216  _handle_trafic_exact( all_size , packet_size );
217 }
218 
220  tick();
221  return get_sleep_time(packet_size);
222 }
223 
224 void network_throttle::logger_handle_net(const std::string &filename, double time, size_t size) {
225  static boost::mutex mutex;
226 
227  boost::lock_guard<boost::mutex> lock(mutex);
228  {
229  std::fstream file;
230  file.open(filename.c_str(), std::ios::app | std::ios::out );
231  file.precision(6);
232  if(!file.is_open())
233  _warn("Can't open file " << filename);
234  file << static_cast<int>(time) << " " << static_cast<double>(size/1024) << "\n";
235  file.close();
236  }
237 }
238 
239 // fine tune this to decide about sending speed:
241 {
242  double D2=0;
243  calculate_times_struct cts = { 0, 0, 0, 0};
244  calculate_times(packet_size, cts, true, m_window_size); D2=cts.delay;
245  return D2;
246 }
247 
248 // MAIN LOGIC:
249 void network_throttle::calculate_times(size_t packet_size, calculate_times_struct &cts, bool dbg, double force_window) const
250 {
251  const double the_window_size = std::max( (double)m_window_size ,
252  ((force_window>0) ? force_window : m_window_size)
253  );
254 
255  if (!m_any_packet_yet) {
256  cts.window=0; cts.average=0; cts.delay=0;
257  cts.recomendetDataSize = m_network_minimal_segment; // should be overrided by caller anyway
258  return ; // no packet yet, I can not decide about sleep time
259  }
260 
261  network_time_seconds window_len = (the_window_size-1) * m_slot_size ; // -1 since current slot is not finished
262  window_len += (m_last_sample_time - time_to_slot(m_last_sample_time)); // add the time for current slot e.g. 13.7-13 = 0.7
263 
264  auto time_passed = get_time_seconds() - m_start_time;
265  cts.window = std::max( std::min( window_len , time_passed ) , m_slot_size ) ; // window length resulting from size of history but limited by how long ago history was started,
266  // also at least slot size (e.g. 1 second) to not be ridiculous
267  // window_len e.g. 5.7 because takes into account current slot time
268 
269  size_t Epast = 0; // summ of traffic till now
270  for (auto sample : m_history) Epast += sample.m_size;
271 
272  const size_t E = Epast;
273  const size_t Enow = Epast + packet_size ; // including the data we're about to send now
274 
275  const double M = m_target_speed; // max
276  const double D1 = (Epast - M*cts.window) / M; // delay - how long to sleep to get back to target speed
277  const double D2 = (Enow - M*cts.window) / M; // delay - how long to sleep to get back to target speed (including current packet)
278 
279  cts.delay = (D1*0.80 + D2*0.20); // finall sleep depends on both with/without current packet
280  // update_overheat();
281  cts.average = Epast/cts.window; // current avg. speed (for info)
282 
283  if (Epast <= 0) {
284  if (cts.delay>=0) cts.delay = 0; // no traffic in history so we will not wait
285  }
286 
287  double Wgood=-1;
288  { // how much data we recommend now to download
289  Wgood = the_window_size + 1;
290  cts.recomendetDataSize = M*cts.window - E;
291  }
292 
293  if (dbg) {
294  std::ostringstream oss; oss << "["; for (auto sample: m_history) oss << sample.m_size << " "; oss << "]" << std::ends;
295  std::string history_str = oss.str();
296  MTRACE((cts.delay > 0 ? "SLEEP" : "")
297  << "dbg " << m_name << ": "
298  << "speed is A=" << std::setw(8) <<cts.average<<" vs "
299  << "Max=" << std::setw(8) <<M<<" "
300  << " so sleep: "
301  << "D=" << std::setw(8) <<cts.delay<<" sec "
302  << "E="<< std::setw(8) << E << " (Enow="<<std::setw(8)<<Enow<<") "
303  << "M=" << std::setw(8) << M <<" W="<< std::setw(8) << cts.window << " "
304  << "R=" << std::setw(8) << cts.recomendetDataSize << " Wgood" << std::setw(8) << Wgood << " "
305  << "History: " << std::setw(8) << history_str << " "
306  << "m_last_sample_time=" << std::setw(8) << m_last_sample_time
307  );
308 
309  }
310 }
311 
313  #if defined(__APPLE__)
314  auto point = std::chrono::system_clock::now();
315  #else
316  auto point = std::chrono::steady_clock::now();
317  #endif
318  auto time_from_epoh = point.time_since_epoch();
319  auto ms = std::chrono::duration_cast< std::chrono::milliseconds >( time_from_epoh ).count();
320  double ms_f = ms;
321  return ms_f / 1000.;
322 }
323 
325  calculate_times_struct cts = { 0, 0, 0, 0};
326  network_throttle::calculate_times(0, cts, true, force_window);
327  cts.recomendetDataSize += m_network_add_cost;
328  if (cts.recomendetDataSize<0) cts.recomendetDataSize=0;
329  if (cts.recomendetDataSize>m_network_max_segment) cts.recomendetDataSize=m_network_max_segment;
330  size_t RI = (long int)cts.recomendetDataSize;
331  return RI;
332 }
333 
335  size_t R1=0,R2=0,R3=0;
337  R2 = get_recommended_size_of_planned_transport_window(m_window_size / 2);
339  auto RM = std::min(R1, std::min(R2,R3));
340 
341  const double a1=20, a2=10, a3=10, am=10; // weight of the various windows in decisssion // TODO 70 => 20
342  return (R1*a1 + R2*a2 + R3*a3 + RM*am) / (a1+a2+a3+am);
343 }
344 
346  unsigned int bytes_transferred = 0;
347  if (m_history.size() == 0 || m_slot_size == 0)
348  return 0;
349 
350  auto it = m_history.begin();
351  while (it < m_history.end() - 1)
352  {
353  bytes_transferred += it->m_size;
354  it ++;
355  }
356 
357  return bytes_transferred / ((m_history.size() - 1) * m_slot_size);
358 }
359 
360 void network_throttle::get_stats(uint64_t &total_packets, uint64_t &total_bytes) const {
361  total_packets = m_total_packets;
362  total_bytes = m_total_bytes;
363 }
364 
365 
366 } // namespace
367 } // namespace
368 
virtual size_t get_recommended_size_of_planned_transport_window(double force_window) const
ditto, but for given windows time frame
#define MTRACE(x)
Definition: misc_log_ex.h:77
#define MINFO(x)
Definition: misc_log_ex.h:75
::std::string string
Definition: gtest-port.h:1097
network_throttle(const std::string &nameshort, const std::string &name, int window_size=-1)
virtual network_speed_kbps get_target_speed()
const char * name
virtual void calculate_times(size_t packet_size, calculate_times_struct &cts, bool dbg, double force_window) const
MAIN LOGIC (see base class for info)
mdb_size_t count(MDB_cursor *cur)
time_t time
Definition: blockchain.cpp:93
virtual size_t get_recommended_size_of_planned_transport() const
what should be the size (bytes) of next data block to be transported
virtual void set_target_speed(network_speed_kbps target)
the connection templated-class for one peer connection
#define _dbg3(x)
Definition: misc_log_ex.h:105
#define _warn(x)
Definition: misc_log_ex.h:112
virtual void get_stats(uint64_t &total_packets, uint64_t &total_bytes) const
virtual void handle_trafic_tcp(size_t packet_size)
count the new traffic/packet; the size is as TCP, we will consider MTU etc
unsigned __int64 uint64_t
Definition: stdint.h:136
virtual void handle_trafic_exact(size_t packet_size)
count the new traffic/packet; the size is exact considering all network costs
virtual void tick()
poke and update timers/history (recalculates, moves the history if needed, checks the real clock etc)...
virtual network_time_seconds get_sleep_time_after_tick(size_t packet_size)
increase the timer if needed, and get the package size
virtual network_time_seconds get_sleep_time(size_t packet_size) const
gets the Delay (recommended Delay time) from calc. (not safe: only if time didnt change?) TODO
void _packet(size_t packet_size, int phase, int q_len)
connection_basic_pimpl(const std::string &name)
implementaion for throttling of connection (count and rate-limit speed etc)
virtual double get_time_seconds() const
timer that we use, time in seconds, monotionic
virtual void set_name(const std::string &name)