source: trunk/libtransmission/ratecontrol.c @ 3997

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

fix upload speed limit bug tracked down by makira_

  • Property svn:keywords set to Date Rev Author Id
File size: 4.2 KB
Line 
1/******************************************************************************
2 * $Id: ratecontrol.c 3997 2007-11-27 04:27:03Z 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 4000
34#define LONG_INTERVAL_MSEC 8000
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
97size_t
98tr_rcBytesLeft( const tr_ratecontrol * r )
99{
100    size_t bytes = 0;
101
102    if( r != NULL )
103    {
104        float cur, max, kb;
105 
106        tr_lockLock( (tr_lock*)r->lock );
107
108        cur = rateForInterval( r, SHORT_INTERVAL_MSEC );
109        max = r->limit;
110        kb = max>cur ? max-cur : 0;
111        bytes = (size_t)(kb * 1024);
112
113        tr_lockUnlock( (tr_lock*)r->lock );
114    }
115
116    return bytes;
117}
118
119float
120tr_rcRate( const tr_ratecontrol * r )
121{
122    float ret;
123
124    if( r == NULL )
125        ret = 0.0f;
126    else {
127        tr_lockLock( (tr_lock*)r->lock );
128        ret = rateForInterval( r, LONG_INTERVAL_MSEC );
129        tr_lockUnlock( (tr_lock*)r->lock );
130    }
131
132    return ret;
133}
134
135/***
136****
137***/
138
139void
140tr_rcTransferred( tr_ratecontrol * r, size_t size )
141{
142    uint64_t now;
143
144    tr_lockLock( (tr_lock*)r->lock );
145
146    now = tr_date ();
147    if( r->transfers[r->newest].date + GRANULARITY_MSEC >= now )
148        r->transfers[r->newest].size += size;
149    else {
150        if( ++r->newest == HISTORY_SIZE ) r->newest = 0;
151        r->transfers[r->newest].date = now;
152        r->transfers[r->newest].size = size;
153    }
154
155    tr_lockUnlock( (tr_lock*)r->lock );
156}
157
158void
159tr_rcReset( tr_ratecontrol * r )
160{
161    tr_lockLock( (tr_lock*)r->lock );
162    r->newest = 0;
163    memset( r->transfers, 0, sizeof(struct tr_transfer) * HISTORY_SIZE );
164    tr_lockUnlock( (tr_lock*)r->lock );
165}
166
167void
168tr_rcSetLimit( tr_ratecontrol * r, int limit )
169{
170    tr_lockLock( (tr_lock*)r->lock );
171    r->limit = limit;
172    tr_lockUnlock( (tr_lock*)r->lock );
173}
174
175int
176tr_rcGetLimit( const tr_ratecontrol * r )
177{
178    return r->limit;
179}
Note: See TracBrowser for help on using the repository browser.