Monero
network_throttle-detail.hpp
Go to the documentation of this file.
1 
5 // Copyright (c) 2014-2022, The Monero Project
6 //
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without modification, are
10 // permitted provided that the following conditions are met:
11 //
12 // 1. Redistributions of source code must retain the above copyright notice, this list of
13 // conditions and the following disclaimer.
14 //
15 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
16 // of conditions and the following disclaimer in the documentation and/or other
17 // materials provided with the distribution.
18 //
19 // 3. Neither the name of the copyright holder nor the names of its contributors may be
20 // used to endorse or promote products derived from this software without specific
21 // prior written permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
24 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
26 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 /* rfree: throttle details, implementing rate limiting */
34 
35 
36 #ifndef INCLUDED_throttle_detail_hpp
37 #define INCLUDED_throttle_detail_hpp
38 
39 #include <boost/circular_buffer.hpp>
40 #include "network_throttle.hpp"
41 
42 namespace epee
43 {
44 namespace net_utils
45 {
46 
47 
49  public:
50  struct packet_info {
51  size_t m_size; // octets sent. Summary for given small-window (e.g. for all packaged in 1 second)
52  packet_info();
53  };
54 
55  private:
57  size_t m_network_add_cost; // estimated add cost of headers
58  size_t m_network_minimal_segment; // estimated minimal cost of sending 1 byte to round up to
59  size_t m_network_max_segment; // recommended max size of 1 TCP transmission
60 
61  const size_t m_window_size; // the number of samples to average over
62  network_time_seconds m_slot_size; // the size of one slot. TODO: now hardcoded for 1 second e.g. in time_to_slot()
63  // 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
64 
65  boost::circular_buffer< packet_info > m_history; // the history of bw usage
66  network_time_seconds m_last_sample_time; // time of last history[0] - so we know when to rotate the buffer
67  network_time_seconds m_start_time; // when we were created
68  bool m_any_packet_yet; // did we yet got any packet to count
71 
72  std::string m_name; // my name for debug and logs
73  std::string m_nameshort; // my name for debug and logs (used in log file name)
74 
75  // each sample is now 1 second
76  public:
77  network_throttle(const std::string &nameshort, const std::string &name, int window_size=-1);
78  virtual ~network_throttle();
79  virtual void set_name(const std::string &name);
80  virtual void set_target_speed( network_speed_kbps target );
82 
83  // add information about events:
84  virtual void handle_trafic_exact(size_t packet_size);
85  virtual void handle_trafic_tcp(size_t packet_size);
86 
87  virtual void tick();
88 
89  virtual double get_time_seconds() const ;
90 
91  // time calculations:
92  virtual void calculate_times(size_t packet_size, calculate_times_struct &cts, bool dbg, double force_window) const;
93 
94  virtual network_time_seconds get_sleep_time_after_tick(size_t packet_size);
95  virtual network_time_seconds get_sleep_time(size_t packet_size) const;
96 
97  virtual size_t get_recommended_size_of_planned_transport() const;
98  virtual size_t get_recommended_size_of_planned_transport_window(double force_window) const;
99  virtual double get_current_speed() const;
100  virtual void get_stats(uint64_t &total_packets, uint64_t &total_bytes) const;
101 
102  private:
103  virtual network_time_seconds time_to_slot(network_time_seconds t) const { return std::floor( t ); } // convert exact time eg 13.7 to rounded time for slot number in history 13
104  virtual void _handle_trafic_exact(size_t packet_size, size_t orginal_size);
105  virtual void logger_handle_net(const std::string &filename, double time, size_t size);
106 };
107 
108 /***
109  * The complete set of traffic throttle for one typical connection
110 */
112  public:
116 
117  public:
118  network_throttle_bw(const std::string &name1);
119 };
120 
121 
122 
123 } // namespace net_utils
124 } // namespace epee
125 
126 
127 #endif
128 
129 
size_t m_size
Definition: network_throttle-detail.hpp:51
virtual size_t get_recommended_size_of_planned_transport_window(double force_window) const
ditto, but for given windows time frame
Definition: network_throttle-detail.cpp:334
size_t m_network_add_cost
Definition: network_throttle-detail.hpp:57
::std::string string
Definition: gtest-port.h:1097
virtual ~network_throttle()
Definition: network_throttle-detail.cpp:117
network_time_seconds m_slot_size
Definition: network_throttle-detail.hpp:62
boost::circular_buffer< packet_info > m_history
Definition: network_throttle-detail.hpp:65
packet_info()
Definition: network_throttle-detail.cpp:119
t
Definition: console.py:33
network_throttle(const std::string &nameshort, const std::string &name, int window_size=-1)
Definition: network_throttle-detail.cpp:124
network_time_seconds m_last_sample_time
Definition: network_throttle-detail.hpp:66
virtual void _handle_trafic_exact(size_t packet_size, size_t orginal_size)
Definition: network_throttle-detail.cpp:206
virtual network_speed_kbps get_target_speed()
Definition: network_throttle-detail.cpp:153
virtual void logger_handle_net(const std::string &filename, double time, size_t size)
Definition: network_throttle-detail.cpp:236
network_speed_bps m_target_speed
Definition: network_throttle-detail.hpp:56
double network_time_seconds
Definition: network_throttle.hpp:78
virtual double get_current_speed() const
Definition: network_throttle-detail.cpp:355
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)
Definition: network_throttle-detail.cpp:261
uint64_t m_total_packets
Definition: network_throttle-detail.hpp:69
network_time_seconds m_start_time
Definition: network_throttle-detail.hpp:67
Definition: network_throttle-detail.hpp:50
virtual size_t get_recommended_size_of_planned_transport() const
what should be the size (bytes) of next data block to be transported
Definition: network_throttle-detail.cpp:344
network_throttle m_inreq
for requesting incomming traffic (this is exact usually)
Definition: network_throttle-detail.hpp:114
virtual network_time_seconds time_to_slot(network_time_seconds t) const
Definition: network_throttle-detail.hpp:103
virtual void set_target_speed(network_speed_kbps target)
Definition: network_throttle-detail.cpp:147
double network_speed_kbps
Definition: network_throttle.hpp:76
network_throttle_bw(const std::string &name1)
Definition: network_throttle.cpp:97
const size_t m_window_size
Definition: network_throttle-detail.hpp:61
interface for throttling of connection (count and rate-limit speed etc)
virtual void get_stats(uint64_t &total_packets, uint64_t &total_bytes) const
Definition: network_throttle-detail.cpp:370
virtual void handle_trafic_tcp(size_t packet_size)
count the new traffic/packet; the size is as TCP, we will consider MTU etc
Definition: network_throttle-detail.cpp:224
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
Definition: network_throttle-detail.cpp:184
network_throttle m_out
for outgoing traffic that we just sent (this is exact usually)
Definition: network_throttle-detail.hpp:115
uint64_t m_total_bytes
Definition: network_throttle-detail.hpp:70
virtual void tick()
poke and update timers/history (recalculates, moves the history if needed, checks the real clock etc)...
Definition: network_throttle-detail.cpp:158
bool m_any_packet_yet
Definition: network_throttle-detail.hpp:68
virtual network_time_seconds get_sleep_time_after_tick(size_t packet_size)
increase the timer if needed, and get the package size
Definition: network_throttle-detail.cpp:231
double network_speed_bps
Definition: network_throttle.hpp:77
Definition: network_throttle.hpp:125
Definition: network_throttle-detail.hpp:111
TODO: (mj-xmr) This will be reduced in an another PR.
Definition: byte_slice.h:39
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
Definition: network_throttle-detail.cpp:252
std::string m_nameshort
Definition: network_throttle-detail.hpp:73
Definition: network_throttle.hpp:86
Definition: network_throttle-detail.hpp:48
time
Definition: gen_wide_data.py:40
std::string m_name
Definition: network_throttle-detail.hpp:72
size_t m_network_minimal_segment
Definition: network_throttle-detail.hpp:58
size_t m_network_max_segment
Definition: network_throttle-detail.hpp:59
const char * name
Definition: options.c:30
virtual double get_time_seconds() const
timer that we use, time in seconds, monotionic
Definition: network_throttle-detail.cpp:322
network_throttle m_in
for incomming traffic (this we can not controll directly as it depends of what others send to us - us...
Definition: network_throttle-detail.hpp:113
virtual void set_name(const std::string &name)
Definition: network_throttle-detail.cpp:142