transmission/libtransmission/ratecontrol.c

122 lines
3.1 KiB
C
Raw Normal View History

2006-07-16 19:39:23 +00:00
/******************************************************************************
* $Id$
*
2008-01-01 17:20:20 +00:00
* Copyright (c) 2006-2008 Transmission authors and contributors
2006-07-16 19:39:23 +00:00
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
2007-11-12 22:26:10 +00:00
#include <string.h> /* memset */
2006-07-16 19:39:23 +00:00
#include "transmission.h"
#include "platform.h"
#include "ratecontrol.h"
#include "utils.h"
2006-07-16 19:39:23 +00:00
#define INTERVAL_MSEC 1000
#define GRANULARITY_MSEC 200
#define HISTORY_SIZE ( INTERVAL_MSEC / GRANULARITY_MSEC )
2007-11-12 22:26:10 +00:00
struct tr_transfer
2006-07-16 19:39:23 +00:00
{
uint64_t date;
uint64_t size;
2007-11-12 22:26:10 +00:00
};
2006-07-16 19:39:23 +00:00
struct tr_ratecontrol
2006-07-16 19:39:23 +00:00
{
int newest;
struct tr_transfer transfers[HISTORY_SIZE];
};
2006-07-16 19:39:23 +00:00
2007-07-07 04:13:13 +00:00
/* return the xfer rate over the last `interval' seconds in KiB/sec */
static float
rateForInterval( const tr_ratecontrol * r,
int interval_msec )
2007-07-07 04:13:13 +00:00
{
uint64_t bytes = 0;
const uint64_t cutoff = tr_date ( ) - interval_msec;
int i = r->newest;
for( ; ; )
2007-07-07 04:13:13 +00:00
{
2008-06-27 02:42:44 +00:00
if( r->transfers[i].date <= cutoff )
2007-07-07 04:13:13 +00:00
break;
bytes += r->transfers[i].size;
2007-07-07 04:13:13 +00:00
if( --i == -1 ) i = HISTORY_SIZE - 1; /* circular history */
if( i == r->newest ) break; /* we've come all the way around */
}
return ( bytes / 1024.0 ) * ( 1000.0 / interval_msec );
2007-07-07 04:13:13 +00:00
}
2007-07-07 04:13:13 +00:00
/***
****
***/
2006-07-16 19:39:23 +00:00
tr_ratecontrol*
2007-07-07 04:13:13 +00:00
tr_rcInit( void )
2006-07-16 19:39:23 +00:00
{
2008-10-06 19:44:38 +00:00
return tr_new0( tr_ratecontrol, 1 );
2006-07-16 19:39:23 +00:00
}
2007-07-07 04:13:13 +00:00
void
tr_rcClose( tr_ratecontrol * r )
2006-07-16 19:39:23 +00:00
{
memset( r, 0, sizeof( tr_ratecontrol ) );
2007-07-07 04:13:13 +00:00
tr_free( r );
2006-07-16 19:39:23 +00:00
}
2007-07-07 04:13:13 +00:00
/***
****
***/
float
tr_rcRate( const tr_ratecontrol * r )
2006-07-16 19:39:23 +00:00
{
float ret = 0.0f;
2006-07-16 19:39:23 +00:00
if( r )
ret = rateForInterval( r, INTERVAL_MSEC );
2006-07-16 19:39:23 +00:00
return ret;
}
2007-07-07 04:13:13 +00:00
/***
****
***/
2006-07-16 19:39:23 +00:00
2007-07-07 04:13:13 +00:00
void
tr_rcTransferred( tr_ratecontrol * r,
size_t size )
2006-07-16 19:39:23 +00:00
{
const uint64_t now = tr_date ( );
2007-07-07 04:13:13 +00:00
if( r->transfers[r->newest].date + GRANULARITY_MSEC >= now )
r->transfers[r->newest].size += size;
else
{
2007-07-07 04:13:13 +00:00
if( ++r->newest == HISTORY_SIZE ) r->newest = 0;
r->transfers[r->newest].date = now;
r->transfers[r->newest].size = size;
}
}