40 #include <boost/asio.hpp> 48 #include <boost/chrono.hpp> 57 #include <boost/asio/basic_socket.hpp> 58 #include <boost/asio/ip/unicast.hpp> 64 #undef ELECTRONEUM_DEFAULT_LOG_CATEGORY 65 #define ELECTRONEUM_DEFAULT_LOG_CATEGORY "net.throttle" 83 class connection_basic_pimpl {
92 void _packet(
size_t packet_size,
int phase,
int q_len);
121 network_throttle::packet_info::packet_info()
127 : m_window_size( (window_size==-1) ? 10 : window_size ),
128 m_history( m_window_size ), m_nameshort(nameshort)
131 m_network_add_cost = 128;
132 m_network_minimal_segment = 256;
133 m_network_max_segment = 1024*1024;
135 m_any_packet_yet =
false;
137 m_target_speed = 16 * 1024;
138 m_last_sample_time = 0;
139 m_history.resize(m_window_size);
151 m_target_speed = target * 1024;
152 MINFO(
"Setting LIMIT: " << target <<
" kbps");
157 return m_target_speed / 1024;
163 if (!m_any_packet_yet) m_start_time = time_now;
171 while ( (!m_any_packet_yet) || (last_sample_time_slot < current_sample_time_slot))
173 _dbg3(
"Moving counter buffer by 1 second " << last_sample_time_slot <<
" < " << current_sample_time_slot <<
" (last time " << m_last_sample_time<<
")");
175 m_history.push_front(packet_info());
176 if (! m_any_packet_yet)
178 m_last_sample_time = time_now;
180 m_last_sample_time += 1; last_sample_time_slot = time_to_slot( m_last_sample_time );
181 m_any_packet_yet=
true;
183 m_last_sample_time = time_now;
188 _handle_trafic_exact(packet_size, packet_size);
191 void network_throttle::_handle_trafic_exact(
size_t packet_size,
size_t orginal_size)
197 m_history.front().m_size += packet_size;
199 m_total_bytes += packet_size;
201 std::ostringstream oss; oss <<
"[";
for (
auto sample: m_history) oss << sample.m_size <<
" "; oss <<
"]" << std::ends;
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
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 );
224 void network_throttle::logger_handle_net(
const std::string &filename,
double time,
size_t size) {
225 static boost::mutex mutex;
227 boost::lock_guard<boost::mutex> lock(mutex);
230 file.open(filename.c_str(), std::ios::app | std::ios::out );
233 _warn(
"Can't open file " << filename);
234 file << static_cast<int>(
time) <<
" " << static_cast<double>(size/1024) <<
"\n";
251 const double the_window_size = std::max( (
double)m_window_size ,
252 ((force_window>0) ? force_window : m_window_size)
255 if (!m_any_packet_yet) {
262 window_len += (m_last_sample_time - time_to_slot(m_last_sample_time));
265 cts.
window = std::max( std::min( window_len , time_passed ) , m_slot_size ) ;
270 for (
auto sample : m_history) Epast += sample.m_size;
272 const size_t E = Epast;
273 const size_t Enow = Epast + packet_size ;
275 const double M = m_target_speed;
276 const double D1 = (Epast - M*cts.
window) / M;
277 const double D2 = (Enow - M*cts.
window) / M;
279 cts.
delay = (D1*0.80 + D2*0.20);
289 Wgood = the_window_size + 1;
294 std::ostringstream oss; oss <<
"[";
for (
auto sample: m_history) oss << sample.m_size <<
" "; oss <<
"]" << std::ends;
297 <<
"dbg " << m_name <<
": " 298 <<
"speed is A=" << std::setw(8) <<cts.
average<<
" vs " 299 <<
"Max=" << std::setw(8) <<M<<
" " 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
313 #if defined(__APPLE__) 314 auto point = std::chrono::system_clock::now();
316 auto point = std::chrono::steady_clock::now();
318 auto time_from_epoh = point.time_since_epoch();
319 auto ms = std::chrono::duration_cast< std::chrono::milliseconds >( time_from_epoh ).
count();
335 size_t R1=0,R2=0,R3=0;
339 auto RM = std::min(R1, std::min(R2,R3));
341 const double a1=20, a2=10, a3=10, am=10;
342 return (R1*a1 + R2*a2 + R3*a3 + RM*am) / (a1+a2+a3+am);
346 unsigned int bytes_transferred = 0;
347 if (m_history.size() == 0 || m_slot_size == 0)
350 auto it = m_history.begin();
351 while (it < m_history.end() - 1)
353 bytes_transferred += it->m_size;
357 return bytes_transferred / ((m_history.size() - 1) * m_slot_size);
361 total_packets = m_total_packets;
362 total_bytes = m_total_bytes;
virtual size_t get_recommended_size_of_planned_transport_window(double force_window) const
ditto, but for given windows time frame
virtual ~network_throttle()
network_throttle(const std::string &nameshort, const std::string &name, int window_size=-1)
virtual network_speed_kbps get_target_speed()
double network_time_seconds
virtual double get_current_speed() const
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)
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)
double network_speed_kbps
the connection templated-class for one peer connection
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
virtual void handle_trafic_exact(size_t packet_size)
count the new traffic/packet; the size is exact considering all network costs
network_throttle_bw m_throttle
double recomendetDataSize
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)
critical_section m_throttle_lock
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)