[go: up one dir, main page]

Menu

[r10]: / dtorrent / trunk / rate.cpp  Maximize  Restore  History

Download this file

83 lines (70 with data), 1.8 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "rate.h"
#include "bttime.h"
#define RATE_INTERVAL 20
void Rate::StartTimer()
{
if( !m_last_timestamp ) m_last_timestamp = now;
}
void Rate::StopTimer()
{
if( m_last_timestamp ){
m_total_timeused += (now - m_last_timestamp);
m_last_timestamp = 0;
}
}
void Rate::CountAdd(size_t nbytes)
{
m_count_bytes += nbytes;
// save bandwidth history data
for (int i=0; i <= n_samples; i++)
{
if (i < MAX_SAMPLES)
{
if (now == m_timestamp_sample[i]) {
m_bytes_sample[i] += nbytes;
break;
}
else if (now - RATE_INTERVAL > m_timestamp_sample[i]) {
m_timestamp_sample[i] = now;
m_bytes_sample[i] = nbytes;
if (n_samples < MAX_SAMPLES) n_samples++;
break;
}
}
}
}
void Rate::operator=(const Rate &ra)
{
m_last_timestamp = now;
m_count_bytes = ra.m_count_bytes;
}
size_t Rate::RateMeasure() const
{
// calculate rate based on bandwidth history data
time_t timestamp = now;
u_int64_t countbytes = 0;
time_t timeused = 0;
if( !m_last_timestamp ) return 0; // no current rate
timeused = (TimeUsed(&timestamp) < RATE_INTERVAL) ?
TimeUsed(&timestamp) : RATE_INTERVAL;
if( timeused < 1 ) timeused = 1;
for (int i=0; i<n_samples; i++)
{
if (timestamp - m_timestamp_sample[i] <= timeused)
countbytes += m_bytes_sample[i];
}
return (size_t)(countbytes / timeused);
}
size_t Rate::RateMeasure(const Rate &ra_to) const
{
int tmp;
time_t timeused = now - m_last_timestamp;
if( timeused < 1 ) timeused = 1;
tmp = (ra_to.m_count_bytes - ra_to.m_recent_base)
- (m_count_bytes - m_recent_base);
return (size_t)( (tmp>0) ? (tmp/timeused) : 0 );
}
time_t Rate::TimeUsed(const time_t *pnow) const
{
return (*pnow - m_last_timestamp);
}