transmission/libtransmission/ratecontrol.c

171 lines
4.1 KiB
C
Raw Normal View History

2006-07-16 19:39:23 +00:00
/******************************************************************************
* $Id$
*
* Copyright (c) 2006 Transmission authors and contributors
*
* 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.
*****************************************************************************/
#include "transmission.h"
#include "shared.h"
2006-07-16 19:39:23 +00:00
#define GRANULARITY_MSEC 250
#define SHORT_INTERVAL_MSEC 3000
#define LONG_INTERVAL_MSEC 20000
2007-07-07 04:13:13 +00:00
#define HISTORY_SIZE (LONG_INTERVAL_MSEC / GRANULARITY_MSEC)
2007-07-07 04:13:13 +00:00
typedef struct
2006-07-16 19:39:23 +00:00
{
uint64_t date;
int size;
2006-07-16 19:39:23 +00:00
}
tr_transfer_t;
2006-07-16 19:39:23 +00:00
struct tr_ratecontrol_s
2006-07-16 19:39:23 +00:00
{
2007-07-07 04:13:13 +00:00
tr_rwlock_t lock;
int limit;
int newest;
tr_transfer_t 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_t * r, int interval_msec )
{
uint64_t bytes = 0;
const uint64_t now = tr_date ();
int i = r->newest;
for( ;; )
{
if( r->transfers[i].date + interval_msec < now )
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
2007-07-07 04:13:13 +00:00
tr_ratecontrol_t*
tr_rcInit( void )
2006-07-16 19:39:23 +00:00
{
2007-07-07 04:13:13 +00:00
tr_ratecontrol_t * r = tr_new0( tr_ratecontrol_t, 1 );
2006-07-16 19:39:23 +00:00
r->limit = -1;
2007-07-07 04:13:13 +00:00
tr_rwInit( &r->lock );
2006-07-16 19:39:23 +00:00
return r;
}
2007-07-07 04:13:13 +00:00
void
tr_rcClose( tr_ratecontrol_t * r )
2006-07-16 19:39:23 +00:00
{
2007-07-07 04:13:13 +00:00
tr_rcReset( r );
tr_rwClose( &r->lock );
tr_free( r );
2006-07-16 19:39:23 +00:00
}
2007-07-07 04:13:13 +00:00
/***
****
***/
int
tr_rcCanTransfer( const tr_ratecontrol_t * r )
2006-07-16 19:39:23 +00:00
{
int ret;
2007-07-07 04:13:13 +00:00
tr_rwReaderLock( (tr_rwlock_t*)&r->lock );
2006-07-16 19:39:23 +00:00
2007-07-07 04:13:13 +00:00
if( r->limit < 0 ) /* unbounded */
ret = TRUE;
else if( !r->limit ) /* off */
ret = FALSE;
else
ret = rateForInterval( r, SHORT_INTERVAL_MSEC ) < r->limit;
2006-07-16 19:39:23 +00:00
2007-07-07 04:13:13 +00:00
tr_rwReaderUnlock( (tr_rwlock_t*)&r->lock );
2006-07-16 19:39:23 +00:00
return ret;
}
2007-07-07 04:13:13 +00:00
float
tr_rcRate( const tr_ratecontrol_t * r )
2006-07-16 19:39:23 +00:00
{
float ret;
2007-07-07 04:13:13 +00:00
tr_rwReaderLock( (tr_rwlock_t*)&r->lock );
2006-07-16 19:39:23 +00:00
2007-07-07 04:13:13 +00:00
ret = rateForInterval( r, LONG_INTERVAL_MSEC );
2006-07-16 19:39:23 +00:00
2007-07-07 04:13:13 +00:00
tr_rwReaderUnlock( (tr_rwlock_t*)&r->lock );
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_t * r, int size )
2006-07-16 19:39:23 +00:00
{
2007-07-07 04:13:13 +00:00
uint64_t now;
2007-07-07 04:13:13 +00:00
if( size < 100 ) /* don't count small messages */
return;
tr_rwWriterLock( &r->lock );
now = tr_date ();
if( r->transfers[r->newest].date + GRANULARITY_MSEC >= now )
r->transfers[r->newest].size += size;
else {
if( ++r->newest == HISTORY_SIZE ) r->newest = 0;
r->transfers[r->newest].date = now;
r->transfers[r->newest].size = size;
}
2007-07-07 04:13:13 +00:00
tr_rwWriterUnlock( &r->lock );
}
2007-07-07 04:13:13 +00:00
void
tr_rcReset( tr_ratecontrol_t * r )
{
2007-07-07 04:13:13 +00:00
tr_rwWriterLock( &r->lock );
r->newest = 0;
memset( r->transfers, 0, sizeof(tr_transfer_t) * HISTORY_SIZE );
tr_rwWriterUnlock( &r->lock );
}
2007-07-07 04:13:13 +00:00
void
tr_rcSetLimit( tr_ratecontrol_t * r, int limit )
{
tr_rwWriterLock( &r->lock );
r->limit = limit;
tr_rwWriterUnlock( &r->lock );
}
int
tr_rcGetLimit( const tr_ratecontrol_t * r )
{
return r->limit;
}