70
75
TORRENT_ASSERT(count >= 0);
72
77
m_counter += count;
78
TORRENT_ASSERT(m_counter >= 0);
73
79
m_total_counter += count;
80
TORRENT_ASSERT(m_total_counter >= 0);
76
83
// should be called once every second
77
void second_tick(float tick_interval);
78
float rate() const { return m_rate_sum / float(history); }
84
void second_tick(int tick_interval_ms);
85
int rate() const { return m_rate_sum / history; }
79
86
size_type rate_sum() const { return m_rate_sum; }
80
87
size_type total() const { return m_total_counter; }
82
void offset(size_type counter)
89
void offset(size_type c)
84
TORRENT_ASSERT(counter >= 0);
85
m_total_counter += counter;
91
TORRENT_ASSERT(c >= 0);
92
TORRENT_ASSERT(m_total_counter >= 0);
94
TORRENT_ASSERT(m_total_counter >= 0);
88
97
size_type counter() const { return m_counter; }
130
139
m_stat[i] += s.m_stat[i];
142
void sent_syn(bool ipv6)
144
m_stat[upload_ip_protocol].add(ipv6 ? 60 : 40);
147
void received_synack(bool ipv6)
149
// we received SYN-ACK and also sent ACK back
150
m_stat[download_ip_protocol].add(ipv6 ? 60 : 40);
151
m_stat[upload_ip_protocol].add(ipv6 ? 60 : 40);
154
void received_dht_bytes(int bytes)
156
TORRENT_ASSERT(bytes >= 0);
157
m_stat[download_dht_protocol].add(bytes);
160
void sent_dht_bytes(int bytes)
162
TORRENT_ASSERT(bytes >= 0);
163
m_stat[upload_dht_protocol].add(bytes);
166
void received_tracker_bytes(int bytes)
168
TORRENT_ASSERT(bytes >= 0);
169
m_stat[download_tracker_protocol].add(bytes);
172
void sent_tracker_bytes(int bytes)
174
TORRENT_ASSERT(bytes >= 0);
175
m_stat[upload_tracker_protocol].add(bytes);
133
178
void received_bytes(int bytes_payload, int bytes_protocol)
135
180
TORRENT_ASSERT(bytes_payload >= 0);
148
193
m_stat[upload_protocol].add(bytes_protocol);
151
// calculate ip protocol overhead
152
void calc_ip_overhead()
196
// and IP packet was received or sent
197
// account for the overhead caused by it
198
void trancieve_ip_packet(int bytes_transferred, bool ipv6)
154
int uploaded = m_stat[upload_protocol].counter()
155
+ m_stat[upload_payload].counter();
156
int downloaded = m_stat[download_protocol].counter()
157
+ m_stat[download_payload].counter();
159
// IP + TCP headers are 40 bytes per MTU (1460)
160
// bytes of payload, but at least 40 bytes
161
m_stat[upload_ip_protocol].add((std::max)(uploaded / 1460, uploaded>0?40:0));
162
m_stat[download_ip_protocol].add((std::max)(downloaded / 1460, downloaded>0?40:0));
164
// also account for ACK traffic. That adds to the transfers
165
// in the opposite direction. Even on connections with symmetric
166
// transfer rates, it seems to add a penalty.
167
m_stat[upload_ip_protocol].add((std::max)(downloaded * 40 / 1460, downloaded>0?40:0));
168
m_stat[download_ip_protocol].add((std::max)(uploaded * 40 / 1460, uploaded>0?40:0));
200
// one TCP/IP packet header for the packet
201
// sent or received, and one for the ACK
202
// The IPv4 header is 20 bytes
203
// and IPv6 header is 40 bytes
204
const int header = (ipv6 ? 40 : 20) + 20;
205
const int mtu = 1500;
206
const int packet_size = mtu - header;
207
const int overhead = (std::max)(1, (bytes_transferred + packet_size - 1) / packet_size) * header;
208
m_stat[download_ip_protocol].add(overhead);
209
m_stat[upload_ip_protocol].add(overhead);
171
212
int upload_ip_overhead() const { return m_stat[upload_ip_protocol].counter(); }
172
213
int download_ip_overhead() const { return m_stat[download_ip_protocol].counter(); }
214
int upload_dht() const { return m_stat[upload_dht_protocol].counter(); }
215
int download_dht() const { return m_stat[download_dht_protocol].counter(); }
216
int download_tracker() const { return m_stat[download_tracker_protocol].counter(); }
217
int upload_tracker() const { return m_stat[upload_tracker_protocol].counter(); }
174
219
// should be called once every second
175
void second_tick(float tick_interval)
220
void second_tick(int tick_interval_ms)
177
222
for (int i = 0; i < num_channels; ++i)
178
m_stat[i].second_tick(tick_interval);
223
m_stat[i].second_tick(tick_interval_ms);
181
float upload_rate() const
226
int upload_rate() const
183
228
return (m_stat[upload_payload].rate_sum()
184
229
+ m_stat[upload_protocol].rate_sum()
185
+ m_stat[upload_ip_protocol].rate_sum())
186
/ float(stat_channel::history);
230
+ m_stat[upload_ip_protocol].rate_sum()
231
+ m_stat[upload_dht_protocol].rate_sum())
232
/ stat_channel::history;
189
float download_rate() const
235
int download_rate() const
191
237
return (m_stat[download_payload].rate_sum()
192
238
+ m_stat[download_protocol].rate_sum()
193
+ m_stat[download_ip_protocol].rate_sum())
194
/ float(stat_channel::history);
197
float upload_payload_rate() const
239
+ m_stat[download_ip_protocol].rate_sum()
240
+ m_stat[download_dht_protocol].rate_sum())
241
/ stat_channel::history;
244
size_type total_upload() const
246
return m_stat[upload_payload].total()
247
+ m_stat[upload_protocol].total()
248
+ m_stat[upload_ip_protocol].total()
249
+ m_stat[upload_dht_protocol].total()
250
+ m_stat[upload_tracker_protocol].total();
253
size_type total_download() const
255
return m_stat[download_payload].total()
256
+ m_stat[download_protocol].total()
257
+ m_stat[download_ip_protocol].total()
258
+ m_stat[download_dht_protocol].total()
259
+ m_stat[download_tracker_protocol].total();
262
int upload_payload_rate() const
198
263
{ return m_stat[upload_payload].rate(); }
200
float download_payload_rate() const
264
int download_payload_rate() const
201
265
{ return m_stat[download_payload].rate(); }
203
267
size_type total_payload_upload() const
210
274
size_type total_protocol_download() const
211
275
{ return m_stat[download_protocol].total(); }
277
size_type total_transfer(int channel) const
278
{ return m_stat[channel].total(); }
279
int transfer_rate(int channel) const
280
{ return m_stat[channel].rate(); }
213
282
// this is used to offset the statistics when a
214
283
// peer_connection is opened and have some previous
215
284
// transfers from earlier connections.
216
285
void add_stat(size_type downloaded, size_type uploaded)
218
TORRENT_ASSERT(downloaded >= 0);
219
TORRENT_ASSERT(uploaded >= 0);
220
287
m_stat[download_payload].offset(downloaded);
221
288
m_stat[upload_payload].offset(uploaded);