~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to include/libtorrent/stat.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Sauthier
  • Date: 2010-08-10 12:59:37 UTC
  • mfrom: (1.3.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100810125937-jbcmmf17y8yo9hgz
Tags: 0.15.0-0ubuntu1
* New upstream version.
* debian/patches/100_fix_html_docs.patch: refreshed.
* debian/control: bump up standards-version to 3.9.1 (no changes).

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
 
62
62
                void operator+=(stat_channel const& s)
63
63
                {
 
64
                        TORRENT_ASSERT(m_counter >= 0);
 
65
                        TORRENT_ASSERT(m_total_counter >= 0);
 
66
                        TORRENT_ASSERT(s.m_counter >= 0);
64
67
                        m_counter += s.m_counter;
65
68
                        m_total_counter += s.m_counter;
 
69
                        TORRENT_ASSERT(m_counter >= 0);
 
70
                        TORRENT_ASSERT(m_total_counter >= 0);
66
71
                }
67
72
 
68
73
                void add(int count)
70
75
                        TORRENT_ASSERT(count >= 0);
71
76
 
72
77
                        m_counter += count;
 
78
                        TORRENT_ASSERT(m_counter >= 0);
73
79
                        m_total_counter += count;
 
80
                        TORRENT_ASSERT(m_total_counter >= 0);
74
81
                }
75
82
 
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; }
81
88
 
82
 
                void offset(size_type counter)
 
89
                void offset(size_type c)
83
90
                {
84
 
                        TORRENT_ASSERT(counter >= 0);
85
 
                        m_total_counter += counter;
 
91
                        TORRENT_ASSERT(c >= 0);
 
92
                        TORRENT_ASSERT(m_total_counter >= 0);
 
93
                        m_total_counter += c;
 
94
                        TORRENT_ASSERT(m_total_counter >= 0);
86
95
                }
87
96
 
88
97
                size_type counter() const { return m_counter; }
100
109
#ifdef TORRENT_DEBUG
101
110
                void check_invariant() const
102
111
                {
103
 
                        int sum = 0;
 
112
                        size_type sum = 0;
104
113
                        for (int i = 0; i < history; ++i) sum += m_rate_history[i];
105
114
                        TORRENT_ASSERT(m_rate_sum == sum);
106
115
                        TORRENT_ASSERT(m_total_counter >= 0);
130
139
                                m_stat[i] += s.m_stat[i];
131
140
                }
132
141
 
 
142
                void sent_syn(bool ipv6)
 
143
                {
 
144
                        m_stat[upload_ip_protocol].add(ipv6 ? 60 : 40);
 
145
                }
 
146
 
 
147
                void received_synack(bool ipv6)
 
148
                {
 
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);
 
152
                }
 
153
 
 
154
                void received_dht_bytes(int bytes)
 
155
                {
 
156
                        TORRENT_ASSERT(bytes >= 0);
 
157
                        m_stat[download_dht_protocol].add(bytes);
 
158
                }
 
159
 
 
160
                void sent_dht_bytes(int bytes)
 
161
                {
 
162
                        TORRENT_ASSERT(bytes >= 0);
 
163
                        m_stat[upload_dht_protocol].add(bytes);
 
164
                }
 
165
 
 
166
                void received_tracker_bytes(int bytes)
 
167
                {
 
168
                        TORRENT_ASSERT(bytes >= 0);
 
169
                        m_stat[download_tracker_protocol].add(bytes);
 
170
                }
 
171
 
 
172
                void sent_tracker_bytes(int bytes)
 
173
                {
 
174
                        TORRENT_ASSERT(bytes >= 0);
 
175
                        m_stat[upload_tracker_protocol].add(bytes);
 
176
                }
 
177
 
133
178
                void received_bytes(int bytes_payload, int bytes_protocol)
134
179
                {
135
180
                        TORRENT_ASSERT(bytes_payload >= 0);
148
193
                        m_stat[upload_protocol].add(bytes_protocol);
149
194
                }
150
195
 
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)
153
199
                {
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();
158
 
 
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));
163
 
 
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);
169
210
                }
170
211
 
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(); }
173
218
 
174
219
                // should be called once every second
175
 
                void second_tick(float tick_interval)
 
220
                void second_tick(int tick_interval_ms)
176
221
                {
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);
179
224
                }
180
225
 
181
 
                float upload_rate() const
 
226
                int upload_rate() const
182
227
                {
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;
187
233
                }
188
234
 
189
 
                float download_rate() const
 
235
                int download_rate() const
190
236
                {
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);
195
 
                }
196
 
 
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;
 
242
                }
 
243
 
 
244
                size_type total_upload() const
 
245
                {
 
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();
 
251
                }
 
252
 
 
253
                size_type total_download() const
 
254
                {
 
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();
 
260
                }
 
261
 
 
262
                int upload_payload_rate() const
198
263
                { return m_stat[upload_payload].rate(); }
199
 
 
200
 
                float download_payload_rate() const
 
264
                int download_payload_rate() const
201
265
                { return m_stat[download_payload].rate(); }
202
266
 
203
267
                size_type total_payload_upload() const
210
274
                size_type total_protocol_download() const
211
275
                { return m_stat[download_protocol].total(); }
212
276
 
 
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(); }
 
281
 
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)
217
286
                {
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);
222
289
                }
225
292
                { return m_stat[download_payload].counter(); }
226
293
                size_type last_payload_uploaded() const
227
294
                { return m_stat[upload_payload].counter(); }
228
 
 
229
 
                void clear()
230
 
                {
231
 
                        for (int i = 0; i < num_channels; ++i)
232
 
                                m_stat[i].clear();
233
 
                }
234
 
 
235
 
        private:
 
295
                size_type last_protocol_downloaded() const
 
296
                { return m_stat[download_protocol].counter(); }
 
297
                size_type last_protocol_uploaded() const
 
298
                { return m_stat[upload_protocol].counter(); }
236
299
 
237
300
                // these are the channels we keep stats for
238
301
                enum
240
303
                        upload_payload,
241
304
                        upload_protocol,
242
305
                        upload_ip_protocol,
 
306
                        upload_dht_protocol,
 
307
                        upload_tracker_protocol,
243
308
                        download_payload,
244
309
                        download_protocol,
245
310
                        download_ip_protocol,
 
311
                        download_dht_protocol,
 
312
                        download_tracker_protocol,
246
313
                        num_channels
247
314
                };
248
315
 
 
316
                void clear()
 
317
                {
 
318
                        for (int i = 0; i < num_channels; ++i)
 
319
                                m_stat[i].clear();
 
320
                }
 
321
 
 
322
                stat_channel const& operator[](int i) const
 
323
                {
 
324
                        TORRENT_ASSERT(i >= 0 && i < num_channels);
 
325
                        return m_stat[i];
 
326
                }
 
327
 
 
328
        private:
 
329
 
249
330
                stat_channel m_stat[num_channels];
250
331
        };
251
332