source: trunk/libtransmission/ratecontrol.c @ 6857

Last change on this file since 6857 was 6857, checked in by charles, 10 years ago

(libT) remove dead code

  • Property svn:keywords set to Date Rev Author Id
File size: 3.1 KB
Line 
1/******************************************************************************
2 * $Id: ratecontrol.c 6857 2008-10-06 19:44:38Z 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 INTERVAL_MSEC 1000
33#define GRANULARITY_MSEC 200
34#define HISTORY_SIZE ( INTERVAL_MSEC / GRANULARITY_MSEC )
35
36struct tr_transfer
37{
38    uint64_t    date;
39    uint64_t    size;
40};
41
42struct tr_ratecontrol
43{
44    int                   newest;
45    struct tr_transfer    transfers[HISTORY_SIZE];
46};
47
48/* return the xfer rate over the last `interval' seconds in KiB/sec */
49static float
50rateForInterval( const tr_ratecontrol * r,
51                 int                    interval_msec )
52{
53    uint64_t       bytes = 0;
54    const uint64_t cutoff = tr_date ( ) - interval_msec;
55    int            i = r->newest;
56
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    return tr_new0( tr_ratecontrol, 1 );
79}
80
81void
82tr_rcClose( tr_ratecontrol * r )
83{
84    memset( r, 0, sizeof( tr_ratecontrol ) );
85    tr_free( r );
86}
87
88/***
89****
90***/
91
92float
93tr_rcRate( const tr_ratecontrol * r )
94{
95    float ret = 0.0f;
96
97    if( r )
98        ret = rateForInterval( r, INTERVAL_MSEC );
99
100    return ret;
101}
102
103/***
104****
105***/
106
107void
108tr_rcTransferred( tr_ratecontrol * r,
109                  size_t           size )
110{
111    const uint64_t now = tr_date ( );
112
113    if( r->transfers[r->newest].date + GRANULARITY_MSEC >= now )
114        r->transfers[r->newest].size += size;
115    else
116    {
117        if( ++r->newest == HISTORY_SIZE ) r->newest = 0;
118        r->transfers[r->newest].date = now;
119        r->transfers[r->newest].size = size;
120    }
121}
Note: See TracBrowser for help on using the repository browser.