network_throttle: use circular_buffer where appropriate

This commit is contained in:
moneromooo-monero 2019-01-25 20:51:59 +00:00
parent 4e72384318
commit 7c3ade4410
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3
2 changed files with 5 additions and 4 deletions

View file

@ -36,6 +36,7 @@
#ifndef INCLUDED_throttle_detail_hpp #ifndef INCLUDED_throttle_detail_hpp
#define INCLUDED_throttle_detail_hpp #define INCLUDED_throttle_detail_hpp
#include <boost/circular_buffer.hpp>
#include "network_throttle.hpp" #include "network_throttle.hpp"
namespace epee namespace epee
@ -61,7 +62,7 @@ class network_throttle : public i_network_throttle {
network_time_seconds m_slot_size; // the size of one slot. TODO: now hardcoded for 1 second e.g. in time_to_slot() network_time_seconds m_slot_size; // the size of one slot. TODO: now hardcoded for 1 second e.g. in time_to_slot()
// TODO for big window size, for performance better the substract on change of m_last_sample_time instead of recalculating average of eg >100 elements // TODO for big window size, for performance better the substract on change of m_last_sample_time instead of recalculating average of eg >100 elements
std::vector< packet_info > m_history; // the history of bw usage boost::circular_buffer< packet_info > m_history; // the history of bw usage
network_time_seconds m_last_sample_time; // time of last history[0] - so we know when to rotate the buffer network_time_seconds m_last_sample_time; // time of last history[0] - so we know when to rotate the buffer
network_time_seconds m_start_time; // when we were created network_time_seconds m_start_time; // when we were created
bool m_any_packet_yet; // did we yet got any packet to count bool m_any_packet_yet; // did we yet got any packet to count

View file

@ -135,6 +135,7 @@ network_throttle::network_throttle(const std::string &nameshort, const std::stri
m_slot_size = 1.0; // hard coded in few places m_slot_size = 1.0; // hard coded in few places
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 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
m_last_sample_time = 0; m_last_sample_time = 0;
m_history.resize(m_window_size);
} }
void network_throttle::set_name(const std::string &name) void network_throttle::set_name(const std::string &name)
@ -168,8 +169,7 @@ void network_throttle::tick()
{ {
_dbg3("Moving counter buffer by 1 second " << last_sample_time_slot << " < " << current_sample_time_slot << " (last time " << m_last_sample_time<<")"); _dbg3("Moving counter buffer by 1 second " << last_sample_time_slot << " < " << current_sample_time_slot << " (last time " << m_last_sample_time<<")");
// rotate buffer // rotate buffer
for (size_t i=m_history.size()-1; i>=1; --i) m_history[i] = m_history[i-1]; m_history.push_front(packet_info());
m_history[0] = packet_info();
if (! m_any_packet_yet) if (! m_any_packet_yet)
{ {
m_last_sample_time = time_now; m_last_sample_time = time_now;
@ -191,7 +191,7 @@ void network_throttle::_handle_trafic_exact(size_t packet_size, size_t orginal_s
calculate_times_struct cts ; calculate_times(packet_size, cts , false, -1); calculate_times_struct cts ; calculate_times(packet_size, cts , false, -1);
calculate_times_struct cts2; calculate_times(packet_size, cts2, false, 5); calculate_times_struct cts2; calculate_times(packet_size, cts2, false, 5);
m_history[0].m_size += packet_size; m_history.front().m_size += packet_size;
std::ostringstream oss; oss << "["; for (auto sample: m_history) oss << sample.m_size << " "; oss << "]" << std::ends; std::ostringstream oss; oss << "["; for (auto sample: m_history) oss << sample.m_size << " "; oss << "]" << std::ends;
std::string history_str = oss.str(); std::string history_str = oss.str();