source: trunk/libtransmission/ratecontrol.c @ 3842

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

progress on the "speed limits kill my transfer rate" bug.

  • Property svn:keywords set to Date Rev Author Id
File size: 4.5 KB
Line 
1/******************************************************************************
2 * $Id: ratecontrol.c 3842 2007-11-16 21:58:11Z 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> /* memset */
26
27#include "transmission.h"
28#include "platform.h"
29#include "ratecontrol.h"
30#include "utils.h"
31
32#define GRANULARITY_MSEC 250
33#define SHORT_INTERVAL_MSEC 2000
34#define LONG_INTERVAL_MSEC 10000
35#define HISTORY_SIZE (LONG_INTERVAL_MSEC / GRANULARITY_MSEC)
36
37struct tr_transfer
38{
39    uint64_t date;
40    uint64_t size;
41};
42
43struct tr_ratecontrol
44{
45    tr_lock * lock;
46    int limit;
47    int newest;
48    struct tr_transfer transfers[HISTORY_SIZE];
49};
50
51/* return the xfer rate over the last `interval' seconds in KiB/sec */
52static float
53rateForInterval( const tr_ratecontrol * r, int interval_msec )
54{
55    uint64_t bytes = 0;
56    const uint64_t cutoff = tr_date () - interval_msec;
57    int i = r->newest;
58    for( ;; )
59    {
60        if( r->transfers[i].date < cutoff )
61            break;
62
63        bytes += r->transfers[i].size;
64
65        if( --i == -1 ) i = HISTORY_SIZE - 1; /* circular history */
66        if( i == r->newest ) break; /* we've come all the way around */
67    }
68
69    return (bytes/1024.0) * (1000.0/interval_msec);
70}
71
72/***
73****
74***/
75
76tr_ratecontrol*
77tr_rcInit( void )
78{
79    tr_ratecontrol * r = tr_new0( tr_ratecontrol, 1 );
80    r->limit = 0;
81    r->lock = tr_lockNew( );
82    return r;
83}
84
85void
86tr_rcClose( tr_ratecontrol * r )
87{
88    tr_rcReset( r );
89    tr_lockFree( r->lock );
90    tr_free( r );
91}
92
93/***
94****
95***/
96
97int
98tr_rcCanTransfer( const tr_ratecontrol * r )
99{
100    int ret;
101
102    if( r == NULL )
103        ret = 0;
104    else {
105        tr_lockLock( (tr_lock*)r->lock );
106        ret = rateForInterval( r, SHORT_INTERVAL_MSEC ) < r->limit;
107        tr_lockUnlock( (tr_lock*)r->lock );
108    }
109
110    return ret;
111}
112
113size_t
114tr_rcBytesLeft( const tr_ratecontrol * r )
115{
116    size_t bytes = 0;
117
118    if( r != NULL )
119    {
120        float cur, max;
121        size_t kb;
122 
123        tr_lockLock( (tr_lock*)r->lock );
124
125        cur = rateForInterval( r, SHORT_INTERVAL_MSEC );
126        max = r->limit;
127        kb = max>cur ? max-cur : 0;
128        bytes = kb * 1024u;
129
130        tr_lockUnlock( (tr_lock*)r->lock );
131    }
132
133    return bytes;
134}
135
136float
137tr_rcRate( const tr_ratecontrol * r )
138{
139    float ret;
140
141    if( r == NULL )
142        ret = 0.0f;
143    else {
144        tr_lockLock( (tr_lock*)r->lock );
145        ret = rateForInterval( r, LONG_INTERVAL_MSEC );
146        tr_lockUnlock( (tr_lock*)r->lock );
147    }
148
149    return ret;
150}
151
152/***
153****
154***/
155
156void
157tr_rcTransferred( tr_ratecontrol * r, size_t size )
158{
159    uint64_t now;
160
161    if( size < 100 ) /* don't count small messages */
162        return;
163   
164    tr_lockLock( (tr_lock*)r->lock );
165
166    now = tr_date ();
167    if( r->transfers[r->newest].date + GRANULARITY_MSEC >= now )
168        r->transfers[r->newest].size += size;
169    else {
170        if( ++r->newest == HISTORY_SIZE ) r->newest = 0;
171        r->transfers[r->newest].date = now;
172        r->transfers[r->newest].size = size;
173    }
174
175    tr_lockUnlock( (tr_lock*)r->lock );
176}
177
178void
179tr_rcReset( tr_ratecontrol * r )
180{
181    tr_lockLock( (tr_lock*)r->lock );
182    r->newest = 0;
183    memset( r->transfers, 0, sizeof(struct tr_transfer) * HISTORY_SIZE );
184    tr_lockUnlock( (tr_lock*)r->lock );
185}
186
187void
188tr_rcSetLimit( tr_ratecontrol * r, int limit )
189{
190    tr_lockLock( (tr_lock*)r->lock );
191    r->limit = limit;
192    tr_lockUnlock( (tr_lock*)r->lock );
193}
194
195int
196tr_rcGetLimit( const tr_ratecontrol * r )
197{
198    return r->limit;
199}
Note: See TracBrowser for help on using the repository browser.