Electroneum
epee::net_utils::network_throttle Class Reference

#include <network_throttle-detail.hpp>

Inheritance diagram for epee::net_utils::network_throttle:
Collaboration diagram for epee::net_utils::network_throttle:

Public Member Functions

 network_throttle (const std::string &nameshort, const std::string &name, int window_size=-1)
 
virtual ~network_throttle ()
 
virtual void set_name (const std::string &name)
 
virtual void set_target_speed (network_speed_kbps target)
 
virtual network_speed_kbps get_target_speed ()
 
virtual void handle_trafic_exact (size_t packet_size)
 count the new traffic/packet; the size is exact considering all network costs More...
 
virtual void handle_trafic_tcp (size_t packet_size)
 count the new traffic/packet; the size is as TCP, we will consider MTU etc More...
 
virtual void tick ()
 poke and update timers/history (recalculates, moves the history if needed, checks the real clock etc) More...
 
virtual double get_time_seconds () const
 timer that we use, time in seconds, monotionic More...
 
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) More...
 
virtual network_time_seconds get_sleep_time_after_tick (size_t packet_size)
 increase the timer if needed, and get the package size More...
 
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 More...
 
virtual size_t get_recommended_size_of_planned_transport () const
 what should be the size (bytes) of next data block to be transported More...
 
virtual size_t get_recommended_size_of_planned_transport_window (double force_window) const
 ditto, but for given windows time frame More...
 
virtual double get_current_speed () const
 
virtual void get_stats (uint64_t &total_packets, uint64_t &total_bytes) const
 

Detailed Description

Definition at line 49 of file network_throttle-detail.hpp.

Constructor & Destructor Documentation

◆ network_throttle()

epee::net_utils::network_throttle::network_throttle ( const std::string &  nameshort,
const std::string &  name,
int  window_size = -1 
)

Definition at line 126 of file network_throttle-detail.cpp.

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 }
const char * name
virtual void set_name(const std::string &name)
Here is the call graph for this function:

◆ ~network_throttle()

epee::net_utils::network_throttle::~network_throttle ( )
virtual

Definition at line 119 of file network_throttle-detail.cpp.

119 { }

Member Function Documentation

◆ calculate_times()

void epee::net_utils::network_throttle::calculate_times ( size_t  packet_size,
calculate_times_struct cts,
bool  dbg,
double  force_window 
) const
virtual

MAIN LOGIC (see base class for info)

Implements epee::net_utils::i_network_throttle.

Definition at line 249 of file network_throttle-detail.cpp.

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 }
#define MTRACE(x)
Definition: misc_log_ex.h:77
::std::string string
Definition: gtest-port.h:1097
virtual double get_time_seconds() const
timer that we use, time in seconds, monotionic
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_current_speed()

double epee::net_utils::network_throttle::get_current_speed ( ) const
virtual

Definition at line 345 of file network_throttle-detail.cpp.

345  {
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 }

◆ get_recommended_size_of_planned_transport()

size_t epee::net_utils::network_throttle::get_recommended_size_of_planned_transport ( ) const
virtual

what should be the size (bytes) of next data block to be transported

Implements epee::net_utils::i_network_throttle.

Definition at line 334 of file network_throttle-detail.cpp.

334  {
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 }
virtual size_t get_recommended_size_of_planned_transport_window(double force_window) const
ditto, but for given windows time frame
Here is the call graph for this function:

◆ get_recommended_size_of_planned_transport_window()

size_t epee::net_utils::network_throttle::get_recommended_size_of_planned_transport_window ( double  force_window) const
virtual

ditto, but for given windows time frame

Definition at line 324 of file network_throttle-detail.cpp.

324  {
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 }
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)
calculate_times_struct calculate_times_struct
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_sleep_time()

network_time_seconds epee::net_utils::network_throttle::get_sleep_time ( size_t  packet_size) const
virtual

gets the Delay (recommended Delay time) from calc. (not safe: only if time didnt change?) TODO

Implements epee::net_utils::i_network_throttle.

Definition at line 240 of file network_throttle-detail.cpp.

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 }
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)
calculate_times_struct calculate_times_struct
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_sleep_time_after_tick()

network_time_seconds epee::net_utils::network_throttle::get_sleep_time_after_tick ( size_t  packet_size)
virtual

increase the timer if needed, and get the package size

Implements epee::net_utils::i_network_throttle.

Definition at line 219 of file network_throttle-detail.cpp.

219  {
220  tick();
221  return get_sleep_time(packet_size);
222 }
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(size_t packet_size) const
gets the Delay (recommended Delay time) from calc. (not safe: only if time didnt change?) TODO
Here is the call graph for this function:

◆ get_stats()

void epee::net_utils::network_throttle::get_stats ( uint64_t total_packets,
uint64_t total_bytes 
) const
virtual

Implements epee::net_utils::i_network_throttle.

Definition at line 360 of file network_throttle-detail.cpp.

360  {
361  total_packets = m_total_packets;
362  total_bytes = m_total_bytes;
363 }

◆ get_target_speed()

network_speed_kbps epee::net_utils::network_throttle::get_target_speed ( )
virtual

Implements epee::net_utils::i_network_throttle.

Definition at line 155 of file network_throttle-detail.cpp.

156 {
157  return m_target_speed / 1024;
158 }

◆ get_time_seconds()

double epee::net_utils::network_throttle::get_time_seconds ( ) const
virtual

timer that we use, time in seconds, monotionic

Implements epee::net_utils::i_network_throttle.

Definition at line 312 of file network_throttle-detail.cpp.

312  {
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 }
mdb_size_t count(MDB_cursor *cur)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_trafic_exact()

void epee::net_utils::network_throttle::handle_trafic_exact ( size_t  packet_size)
virtual

count the new traffic/packet; the size is exact considering all network costs

Implements epee::net_utils::i_network_throttle.

Definition at line 186 of file network_throttle-detail.cpp.

187 {
188  _handle_trafic_exact(packet_size, packet_size);
189 }

◆ handle_trafic_tcp()

void epee::net_utils::network_throttle::handle_trafic_tcp ( size_t  packet_size)
virtual

count the new traffic/packet; the size is as TCP, we will consider MTU etc

Implements epee::net_utils::i_network_throttle.

Definition at line 212 of file network_throttle-detail.cpp.

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 }

◆ set_name()

void epee::net_utils::network_throttle::set_name ( const std::string &  name)
virtual

Implements epee::net_utils::i_network_throttle.

Definition at line 144 of file network_throttle-detail.cpp.

145 {
146  m_name = name;
147 }
const char * name
Here is the caller graph for this function:

◆ set_target_speed()

void epee::net_utils::network_throttle::set_target_speed ( network_speed_kbps  target)
virtual

Implements epee::net_utils::i_network_throttle.

Definition at line 149 of file network_throttle-detail.cpp.

150 {
151  m_target_speed = target * 1024;
152  MINFO("Setting LIMIT: " << target << " kbps");
153 }
#define MINFO(x)
Definition: misc_log_ex.h:75

◆ tick()

void epee::net_utils::network_throttle::tick ( )
virtual

poke and update timers/history (recalculates, moves the history if needed, checks the real clock etc)

Implements epee::net_utils::i_network_throttle.

Definition at line 160 of file network_throttle-detail.cpp.

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 }
#define _dbg3(x)
Definition: misc_log_ex.h:105
virtual double get_time_seconds() const
timer that we use, time in seconds, monotionic
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: