~ubuntu-branches/ubuntu/jaunty/transmission/jaunty-security

« back to all changes in this revision

Viewing changes to libtransmission/ratecontrol.c

  • Committer: Bazaar Package Importer
  • Author(s): Chris Coulson
  • Date: 2008-11-28 15:33:48 UTC
  • mfrom: (1.1.19 upstream)
  • Revision ID: james.westby@ubuntu.com-20081128153348-it70trfnxiroblmc
Tags: 1.40-0ubuntu1
* New upstream release (LP: #302672)
  - Tracker communication uses fewer resources
  - More accurate bandwidth limits
  - Reduce disk fragmentation by preallocating files (LP: #287726)
  - Stability, security and performance improvements to the RPC /
    Web UI server (closes LP: #290423)
  - Support compression when serving Web UI and RPC responses
  - Simplify the RPC whitelist
  - Fix bug that prevented handshakes with encrypted BitComet peers
  - Fix 1.3x bug that could re-download some data unnecessarily
    (LP: #295040)
  - Option to automatically update the blocklist weekly
  - Added off-hour bandwidth scheduling
  - Simplify file/priority selection in the details dialog
  - Fix a couple of crashes
  - New / updated translations
  - Don't inhibit hibernation by default (LP: #292929)
  - Use "close" animation when sending to notification area (LP: #130811)
  - Fix resize problems (LP: #269872)
  - Support "--version" option when launching from command line
    (LP: #292011)
  - Correctly parse announce URLs that have leading or trailing
    spaces (LP: #262411)
  - Display an error when "Open Torrent" fails (LP: #281463)
* Dropped 10_fix_crasher_from_upstream.dpatch: Fix is in this
  upstream release.
* debian/control: Don't just build-depend on libcurl-dev, which is
  a virtual package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/******************************************************************************
2
 
 * $Id: ratecontrol.c 6425 2008-08-01 16:43:22Z charles $
 
2
 * $Id: ratecontrol.c 7069 2008-11-08 02:49:04Z charles $
3
3
 *
4
4
 * Copyright (c) 2006-2008 Transmission authors and contributors
5
5
 *
29
29
#include "ratecontrol.h"
30
30
#include "utils.h"
31
31
 
32
 
#define GRANULARITY_MSEC 500
33
 
#define SHORT_INTERVAL_MSEC 1000
34
 
#define LONG_INTERVAL_MSEC 8000
35
 
#define HISTORY_SIZE (LONG_INTERVAL_MSEC / GRANULARITY_MSEC)
 
32
enum
 
33
{
 
34
    INTERVAL_MSEC = TR_RATECONTROL_HISTORY_MSEC,
 
35
 
 
36
    GRANULARITY_MSEC = 250,
 
37
 
 
38
    HISTORY_SIZE = ( INTERVAL_MSEC / GRANULARITY_MSEC )
 
39
};
36
40
 
37
41
struct tr_transfer
38
42
{
39
 
    uint64_t date;
40
 
    uint64_t size;
 
43
    uint64_t    date;
 
44
    uint64_t    size;
41
45
};
42
46
 
43
47
struct tr_ratecontrol
44
48
{
45
 
    int limit;
46
 
    int newest;
47
 
    struct tr_transfer transfers[HISTORY_SIZE];
 
49
    int                   newest;
 
50
    struct tr_transfer    transfers[HISTORY_SIZE];
48
51
};
49
52
 
50
53
/* return the xfer rate over the last `interval' seconds in KiB/sec */
51
54
static float
52
 
rateForInterval( const tr_ratecontrol * r, int interval_msec )
 
55
rateForInterval( const tr_ratecontrol * r,
 
56
                 int                    interval_msec )
53
57
{
54
 
    uint64_t bytes = 0;
55
 
    const uint64_t cutoff = tr_date () - interval_msec;
56
 
    int i = r->newest;
57
 
    for( ;; )
 
58
    uint64_t       bytes = 0;
 
59
    const uint64_t cutoff = tr_date ( ) - interval_msec;
 
60
    int            i = r->newest;
 
61
 
 
62
    for( ; ; )
58
63
    {
59
64
        if( r->transfers[i].date <= cutoff )
60
65
            break;
65
70
        if( i == r->newest ) break; /* we've come all the way around */
66
71
    }
67
72
 
68
 
    return (bytes/1024.0) * (1000.0/interval_msec);
 
73
    return ( bytes / 1024.0 ) * ( 1000.0 / interval_msec );
69
74
}
70
75
 
71
76
/***
75
80
tr_ratecontrol*
76
81
tr_rcInit( void )
77
82
{
78
 
    tr_ratecontrol * r = tr_new0( tr_ratecontrol, 1 );
79
 
    r->limit = 0;
80
 
    return r;
 
83
    return tr_new0( tr_ratecontrol, 1 );
81
84
}
82
85
 
83
86
void
84
87
tr_rcClose( tr_ratecontrol * r )
85
88
{
86
 
    tr_rcReset( r );
 
89
    memset( r, 0, sizeof( tr_ratecontrol ) );
87
90
    tr_free( r );
88
91
}
89
92
 
91
94
****
92
95
***/
93
96
 
94
 
size_t
95
 
tr_rcBytesLeft( const tr_ratecontrol * r )
96
 
{
97
 
    size_t bytes = 0;
98
 
 
99
 
    if( r )
100
 
    {
101
 
        const float cur = rateForInterval( r, SHORT_INTERVAL_MSEC );
102
 
        const float max = r->limit;
103
 
        const float kb = max>cur ? max-cur : 0;
104
 
        bytes = (size_t)(kb * 1024);
105
 
    }
106
 
 
107
 
    return bytes;
108
 
}
109
 
 
110
97
float
111
98
tr_rcRate( const tr_ratecontrol * r )
112
99
{
113
100
    float ret = 0.0f;
114
101
 
115
102
    if( r )
116
 
        ret = rateForInterval( r, LONG_INTERVAL_MSEC );
 
103
        ret = rateForInterval( r, INTERVAL_MSEC );
117
104
 
118
105
    return ret;
119
106
}
123
110
***/
124
111
 
125
112
void
126
 
tr_rcTransferred( tr_ratecontrol * r, size_t size )
 
113
tr_rcTransferred( tr_ratecontrol * r,
 
114
                  size_t           size )
127
115
{
128
 
    const uint64_t now = tr_date ();
 
116
    const uint64_t now = tr_date ( );
129
117
 
130
118
    if( r->transfers[r->newest].date + GRANULARITY_MSEC >= now )
131
119
        r->transfers[r->newest].size += size;
132
 
    else {
 
120
    else
 
121
    {
133
122
        if( ++r->newest == HISTORY_SIZE ) r->newest = 0;
134
123
        r->transfers[r->newest].date = now;
135
124
        r->transfers[r->newest].size = size;
136
125
    }
137
126
}
138
 
 
139
 
void
140
 
tr_rcReset( tr_ratecontrol * r )
141
 
{
142
 
    r->newest = 0;
143
 
    memset( r->transfers, 0, sizeof(struct tr_transfer) * HISTORY_SIZE );
144
 
}
145
 
 
146
 
void
147
 
tr_rcSetLimit( tr_ratecontrol * r, int limit )
148
 
{
149
 
    r->limit = limit;
150
 
}
151
 
 
152
 
int
153
 
tr_rcGetLimit( const tr_ratecontrol * r )
154
 
{
155
 
    return r->limit;
156
 
}