source: trunk/libtransmission/ratecontrol.c @ 6425

Last change on this file since 6425 was 6425, checked in by charles, 13 years ago

minor text cleanup

  • Property svn:keywords set to Date Rev Author Id
File size: 3.6 KB
Line 
1/******************************************************************************
2 * $Id: ratecontrol.c 6425 2008-08-01 16:43:22Z charles $
3 *
4 * Copyright (c) 2006-2008 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 500
33#define SHORT_INTERVAL_MSEC 1000
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    int limit;
46    int newest;
47    struct tr_transfer transfers[HISTORY_SIZE];
48};
49
50/* return the xfer rate over the last `interval' seconds in KiB/sec */
51static float
52rateForInterval( const tr_ratecontrol * r, int interval_msec )
53{
54    uint64_t bytes = 0;
55    const uint64_t cutoff = tr_date () - interval_msec;
56    int i = r->newest;
57    for( ;; )
58    {
59        if( r->transfers[i].date <= cutoff )
60            break;
61
62        bytes += r->transfers[i].size;
63
64        if( --i == -1 ) i = HISTORY_SIZE - 1; /* circular history */
65        if( i == r->newest ) break; /* we've come all the way around */
66    }
67
68    return (bytes/1024.0) * (1000.0/interval_msec);
69}
70
71/***
72****
73***/
74
75tr_ratecontrol*
76tr_rcInit( void )
77{
78    tr_ratecontrol * r = tr_new0( tr_ratecontrol, 1 );
79    r->limit = 0;
80    return r;
81}
82
83void
84tr_rcClose( tr_ratecontrol * r )
85{
86    tr_rcReset( r );
87    tr_free( r );
88}
89
90/***
91****
92***/
93
94size_t
95tr_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
110float
111tr_rcRate( const tr_ratecontrol * r )
112{
113    float ret = 0.0f;
114
115    if( r )
116        ret = rateForInterval( r, LONG_INTERVAL_MSEC );
117
118    return ret;
119}
120
121/***
122****
123***/
124
125void
126tr_rcTransferred( tr_ratecontrol * r, size_t size )
127{
128    const uint64_t now = tr_date ();
129
130    if( r->transfers[r->newest].date + GRANULARITY_MSEC >= now )
131        r->transfers[r->newest].size += size;
132    else {
133        if( ++r->newest == HISTORY_SIZE ) r->newest = 0;
134        r->transfers[r->newest].date = now;
135        r->transfers[r->newest].size = size;
136    }
137}
138
139void
140tr_rcReset( tr_ratecontrol * r )
141{
142    r->newest = 0;
143    memset( r->transfers, 0, sizeof(struct tr_transfer) * HISTORY_SIZE );
144}
145
146void
147tr_rcSetLimit( tr_ratecontrol * r, int limit )
148{
149    r->limit = limit;
150}
151
152int
153tr_rcGetLimit( const tr_ratecontrol * r )
154{
155    return r->limit;
156}
Note: See TracBrowser for help on using the repository browser.