source: trunk/libtransmission/ratecontrol.c @ 2590

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

use a straight lock, rather than a rwlock, for rate control

  • Property svn:keywords set to Date Rev Author Id
File size: 4.0 KB
Line 
1/******************************************************************************
2 * $Id: ratecontrol.c 2590 2007-08-02 16:07:34Z 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    int      size;
42}
43tr_transfer_t;
44
45struct tr_ratecontrol_s
46{
47    tr_lock_t * 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_t * r, int interval_msec )
56{
57    uint64_t bytes = 0;
58    const uint64_t now = tr_date ();
59    int i = r->newest;
60    for( ;; )
61    {
62        if( r->transfers[i].date + interval_msec < now )
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_t*
79tr_rcInit( void )
80{
81    tr_ratecontrol_t * r = tr_new0( tr_ratecontrol_t, 1 );
82    r->limit = 0;
83    r->lock = tr_lockNew( );
84    return r;
85}
86
87void
88tr_rcClose( tr_ratecontrol_t * 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_t * r )
101{
102    int ret;
103    tr_lockLock( (tr_lock_t*)r->lock );
104
105    ret = rateForInterval( r, SHORT_INTERVAL_MSEC ) < r->limit;
106
107    tr_lockUnlock( (tr_lock_t*)r->lock );
108    return ret;
109}
110
111float
112tr_rcRate( const tr_ratecontrol_t * r )
113{
114    float ret;
115    tr_lockLock( (tr_lock_t*)r->lock );
116
117    ret = rateForInterval( r, LONG_INTERVAL_MSEC );
118
119    tr_lockUnlock( (tr_lock_t*)r->lock );
120    return ret;
121}
122
123/***
124****
125***/
126
127void
128tr_rcTransferred( tr_ratecontrol_t * r, int size )
129{
130    uint64_t now;
131
132    if( size < 100 ) /* don't count small messages */
133        return;
134   
135    tr_lockLock( (tr_lock_t*)r->lock );
136
137    now = tr_date ();
138    if( r->transfers[r->newest].date + GRANULARITY_MSEC >= now )
139        r->transfers[r->newest].size += size;
140    else {
141        if( ++r->newest == HISTORY_SIZE ) r->newest = 0;
142        r->transfers[r->newest].date = now;
143        r->transfers[r->newest].size = size;
144    }
145
146    tr_lockUnlock( (tr_lock_t*)r->lock );
147}
148
149void
150tr_rcReset( tr_ratecontrol_t * r )
151{
152    tr_lockLock( (tr_lock_t*)r->lock );
153    r->newest = 0;
154    memset( r->transfers, 0, sizeof(tr_transfer_t) * HISTORY_SIZE );
155    tr_lockUnlock( (tr_lock_t*)r->lock );
156}
157
158void
159tr_rcSetLimit( tr_ratecontrol_t * r, int limit )
160{
161    tr_lockLock( (tr_lock_t*)r->lock );
162    r->limit = limit;
163    tr_lockUnlock( (tr_lock_t*)r->lock );
164}
165
166int
167tr_rcGetLimit( const tr_ratecontrol_t * r )
168{
169    return r->limit;
170}
Note: See TracBrowser for help on using the repository browser.