source: trunk/libtransmission/ratecontrol.c @ 3615

Last change on this file since 3615 was 3615, checked in by charles, 15 years ago

#412 - 0.90 "ignores" speed limits (deanr, berkut, SineOtter?)

  • Property svn:keywords set to Date Rev Author Id
File size: 4.5 KB
Line 
1/******************************************************************************
2 * $Id: ratecontrol.c 3615 2007-10-28 15:20:24Z charles $
3 *
4 * Copyright (c) 2006 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#include <string.h>
26
27#include "transmission.h"
28#include "platform.h"
29#include "ratecontrol.h"
30#include "shared.h"
31#include "utils.h"
32
33#define GRANULARITY_MSEC 250
34#define SHORT_INTERVAL_MSEC 3000
35#define LONG_INTERVAL_MSEC 20000
36#define HISTORY_SIZE (LONG_INTERVAL_MSEC / GRANULARITY_MSEC)
37
38typedef struct
39{
40    uint64_t date;
41    uint64_t size;
42}
43tr_transfer_t;
44
45struct tr_ratecontrol
46{
47    tr_lock * lock;
48    int limit;
49    int newest;
50    tr_transfer_t transfers[HISTORY_SIZE];
51};
52
53/* return the xfer rate over the last `interval' seconds in KiB/sec */
54static float
55rateForInterval( const tr_ratecontrol * r, int interval_msec )
56{
57    uint64_t bytes = 0;
58    const uint64_t cutoff = tr_date () - interval_msec;
59    int i = r->newest;
60    for( ;; )
61    {
62        if( r->transfers[i].date < cutoff )
63            break;
64
65        bytes += r->transfers[i].size;
66
67        if( --i == -1 ) i = HISTORY_SIZE - 1; /* circular history */
68        if( i == r->newest ) break; /* we've come all the way around */
69    }
70
71    return (bytes/1024.0) * (1000.0/interval_msec);
72}
73
74/***
75****
76***/
77
78tr_ratecontrol*
79tr_rcInit( void )
80{
81    tr_ratecontrol * r = tr_new0( tr_ratecontrol, 1 );
82    r->limit = 0;
83    r->lock = tr_lockNew( );
84    return r;
85}
86
87void
88tr_rcClose( tr_ratecontrol * r )
89{
90    tr_rcReset( r );
91    tr_lockFree( r->lock );
92    tr_free( r );
93}
94
95/***
96****
97***/
98
99int
100tr_rcCanTransfer( const tr_ratecontrol * r )
101{
102    int ret;
103
104    if( r == NULL )
105        ret = 0;
106    else {
107        tr_lockLock( (tr_lock*)r->lock );
108        ret = rateForInterval( r, SHORT_INTERVAL_MSEC ) < r->limit;
109        tr_lockUnlock( (tr_lock*)r->lock );
110    }
111
112    return ret;
113}
114
115size_t
116tr_rcBytesLeft( const tr_ratecontrol * r )
117{
118    size_t bytes = 0;
119
120    if( r != NULL )
121    {
122        float cur, max;
123        size_t kb;
124 
125        tr_lockLock( (tr_lock*)r->lock );
126
127        cur = rateForInterval( r, SHORT_INTERVAL_MSEC );
128        max = r->limit;
129        kb = max>cur ? max-cur : 0;
130        bytes = kb * 1024u;
131
132        tr_lockUnlock( (tr_lock*)r->lock );
133    }
134
135    return bytes;
136}
137
138float
139tr_rcRate( const tr_ratecontrol * r )
140{
141    float ret;
142
143    if( r == NULL )
144        ret = 0.0f;
145    else {
146        tr_lockLock( (tr_lock*)r->lock );
147        ret = rateForInterval( r, LONG_INTERVAL_MSEC );
148        tr_lockUnlock( (tr_lock*)r->lock );
149    }
150
151    return ret;
152}
153
154/***
155****
156***/
157
158void
159tr_rcTransferred( tr_ratecontrol * r, size_t size )
160{
161    uint64_t now;
162
163    if( size < 100 ) /* don't count small messages */
164        return;
165   
166    tr_lockLock( (tr_lock*)r->lock );
167
168    now = tr_date ();
169    if( r->transfers[r->newest].date + GRANULARITY_MSEC >= now )
170        r->transfers[r->newest].size += size;
171    else {
172        if( ++r->newest == HISTORY_SIZE ) r->newest = 0;
173        r->transfers[r->newest].date = now;
174        r->transfers[r->newest].size = size;
175    }
176
177    tr_lockUnlock( (tr_lock*)r->lock );
178}
179
180void
181tr_rcReset( tr_ratecontrol * r )
182{
183    tr_lockLock( (tr_lock*)r->lock );
184    r->newest = 0;
185    memset( r->transfers, 0, sizeof(tr_transfer_t) * HISTORY_SIZE );
186    tr_lockUnlock( (tr_lock*)r->lock );
187}
188
189void
190tr_rcSetLimit( tr_ratecontrol * r, int limit )
191{
192    tr_lockLock( (tr_lock*)r->lock );
193    r->limit = limit;
194    tr_lockUnlock( (tr_lock*)r->lock );
195}
196
197int
198tr_rcGetLimit( const tr_ratecontrol * r )
199{
200    return r->limit;
201}
Note: See TracBrowser for help on using the repository browser.