source: trunk/libtransmission/ratecontrol.c @ 7069

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

more fucking around with the speed measurements.

  • Property svn:keywords set to Date Rev Author Id
File size: 3.1 KB
Line 
1/******************************************************************************
2 * $Id: ratecontrol.c 7069 2008-11-08 02:49:04Z 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
32enum
33{
34    INTERVAL_MSEC = TR_RATECONTROL_HISTORY_MSEC,
35
36    GRANULARITY_MSEC = 250,
37
38    HISTORY_SIZE = ( INTERVAL_MSEC / GRANULARITY_MSEC )
39};
40
41struct tr_transfer
42{
43    uint64_t    date;
44    uint64_t    size;
45};
46
47struct tr_ratecontrol
48{
49    int                   newest;
50    struct tr_transfer    transfers[HISTORY_SIZE];
51};
52
53/* return the xfer rate over the last `interval' seconds in KiB/sec */
54static float
55rateForInterval( const tr_ratecontrol * r,
56                 int                    interval_msec )
57{
58    uint64_t       bytes = 0;
59    const uint64_t cutoff = tr_date ( ) - interval_msec;
60    int            i = r->newest;
61
62    for( ; ; )
63    {
64        if( r->transfers[i].date <= cutoff )
65            break;
66
67        bytes += r->transfers[i].size;
68
69        if( --i == -1 ) i = HISTORY_SIZE - 1; /* circular history */
70        if( i == r->newest ) break; /* we've come all the way around */
71    }
72
73    return ( bytes / 1024.0 ) * ( 1000.0 / interval_msec );
74}
75
76/***
77****
78***/
79
80tr_ratecontrol*
81tr_rcInit( void )
82{
83    return tr_new0( tr_ratecontrol, 1 );
84}
85
86void
87tr_rcClose( tr_ratecontrol * r )
88{
89    memset( r, 0, sizeof( tr_ratecontrol ) );
90    tr_free( r );
91}
92
93/***
94****
95***/
96
97float
98tr_rcRate( const tr_ratecontrol * r )
99{
100    float ret = 0.0f;
101
102    if( r )
103        ret = rateForInterval( r, INTERVAL_MSEC );
104
105    return ret;
106}
107
108/***
109****
110***/
111
112void
113tr_rcTransferred( tr_ratecontrol * r,
114                  size_t           size )
115{
116    const uint64_t now = tr_date ( );
117
118    if( r->transfers[r->newest].date + GRANULARITY_MSEC >= now )
119        r->transfers[r->newest].size += size;
120    else
121    {
122        if( ++r->newest == HISTORY_SIZE ) r->newest = 0;
123        r->transfers[r->newest].date = now;
124        r->transfers[r->newest].size = size;
125    }
126}
Note: See TracBrowser for help on using the repository browser.