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.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2007-11-12 22:26:10 +00:00
|
|
|
#include <string.h> /* memset */
|
2007-07-29 18:11:21 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2007-07-31 14:26:44 +00:00
|
|
|
#include "platform.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "ratecontrol.h"
|
|
|
|
#include "utils.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-11-18 01:00:49 +00:00
|
|
|
#define GRANULARITY_MSEC 250
|
2007-11-20 03:11:50 +00:00
|
|
|
#define SHORT_INTERVAL_MSEC 4000
|
|
|
|
#define LONG_INTERVAL_MSEC 8000
|
2007-07-07 04:13:13 +00:00
|
|
|
#define HISTORY_SIZE (LONG_INTERVAL_MSEC / GRANULARITY_MSEC)
|
2007-01-22 22:58:51 +00:00
|
|
|
|
2007-11-12 22:26:10 +00:00
|
|
|
struct tr_transfer
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-22 22:58:51 +00:00
|
|
|
uint64_t date;
|
2007-09-20 16:32:01 +00:00
|
|
|
uint64_t size;
|
2007-11-12 22:26:10 +00:00
|
|
|
};
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
struct tr_ratecontrol
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_lock * lock;
|
2007-07-07 04:13:13 +00:00
|
|
|
int limit;
|
|
|
|
int newest;
|
2007-11-12 22:26:10 +00:00
|
|
|
struct tr_transfer transfers[HISTORY_SIZE];
|
2007-01-22 22:58:51 +00:00
|
|
|
};
|
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
|
2007-09-20 16:32:01 +00:00
|
|
|
rateForInterval( const tr_ratecontrol * r, int interval_msec )
|
2007-07-07 04:13:13 +00:00
|
|
|
{
|
|
|
|
uint64_t bytes = 0;
|
2007-09-20 16:32:01 +00:00
|
|
|
const uint64_t cutoff = tr_date () - interval_msec;
|
2007-07-07 04:13:13 +00:00
|
|
|
int i = r->newest;
|
|
|
|
for( ;; )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
if( r->transfers[i].date < cutoff )
|
2007-07-07 04:13:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
bytes += r->transfers[i].size;
|
2007-01-22 22:58:51 +00:00
|
|
|
|
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 */
|
|
|
|
}
|
2007-01-22 22:58:51 +00:00
|
|
|
|
2007-07-09 04:37:16 +00:00
|
|
|
return (bytes/1024.0) * (1000.0/interval_msec);
|
2007-07-07 04:13:13 +00:00
|
|
|
}
|
2007-01-22 22:58:51 +00:00
|
|
|
|
2007-07-07 04:13:13 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ratecontrol*
|
2007-07-07 04:13:13 +00:00
|
|
|
tr_rcInit( void )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ratecontrol * r = tr_new0( tr_ratecontrol, 1 );
|
2007-07-20 06:08:34 +00:00
|
|
|
r->limit = 0;
|
2007-08-02 16:07:34 +00:00
|
|
|
r->lock = tr_lockNew( );
|
2006-07-16 19:39:23 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2007-07-07 04:13:13 +00:00
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_rcClose( tr_ratecontrol * r )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-07-07 04:13:13 +00:00
|
|
|
tr_rcReset( r );
|
2007-08-02 16:07:34 +00:00
|
|
|
tr_lockFree( r->lock );
|
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
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-10-28 15:20:24 +00:00
|
|
|
size_t
|
|
|
|
tr_rcBytesLeft( const tr_ratecontrol * r )
|
|
|
|
{
|
|
|
|
size_t bytes = 0;
|
|
|
|
|
|
|
|
if( r != NULL )
|
|
|
|
{
|
2007-11-17 00:16:26 +00:00
|
|
|
float cur, max, kb;
|
2007-10-28 15:20:24 +00:00
|
|
|
|
|
|
|
tr_lockLock( (tr_lock*)r->lock );
|
|
|
|
|
|
|
|
cur = rateForInterval( r, SHORT_INTERVAL_MSEC );
|
|
|
|
max = r->limit;
|
|
|
|
kb = max>cur ? max-cur : 0;
|
2007-11-18 01:00:49 +00:00
|
|
|
bytes = (size_t)(kb * 1024);
|
2007-10-28 15:20:24 +00:00
|
|
|
|
|
|
|
tr_lockUnlock( (tr_lock*)r->lock );
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
2007-07-07 04:13:13 +00:00
|
|
|
float
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_rcRate( const tr_ratecontrol * r )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
float ret;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
if( r == NULL )
|
|
|
|
ret = 0.0f;
|
|
|
|
else {
|
|
|
|
tr_lockLock( (tr_lock*)r->lock );
|
|
|
|
ret = rateForInterval( r, LONG_INTERVAL_MSEC );
|
|
|
|
tr_lockUnlock( (tr_lock*)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
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_rcTransferred( tr_ratecontrol * r, size_t size )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-07-07 04:13:13 +00:00
|
|
|
uint64_t now;
|
2007-01-22 22:58:51 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_lockLock( (tr_lock*)r->lock );
|
2007-07-07 04:13:13 +00:00
|
|
|
|
|
|
|
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-01-22 22:58:51 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_lockUnlock( (tr_lock*)r->lock );
|
2007-07-07 04:13:13 +00:00
|
|
|
}
|
2007-01-22 22:58:51 +00:00
|
|
|
|
2007-07-07 04:13:13 +00:00
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_rcReset( tr_ratecontrol * r )
|
2007-01-22 22:58:51 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_lockLock( (tr_lock*)r->lock );
|
2007-07-07 04:13:13 +00:00
|
|
|
r->newest = 0;
|
2007-11-12 22:26:10 +00:00
|
|
|
memset( r->transfers, 0, sizeof(struct tr_transfer) * HISTORY_SIZE );
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_lockUnlock( (tr_lock*)r->lock );
|
2007-01-22 22:58:51 +00:00
|
|
|
}
|
|
|
|
|
2007-07-07 04:13:13 +00:00
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_rcSetLimit( tr_ratecontrol * r, int limit )
|
2007-07-07 04:13:13 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_lockLock( (tr_lock*)r->lock );
|
2007-07-07 04:13:13 +00:00
|
|
|
r->limit = limit;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_lockUnlock( (tr_lock*)r->lock );
|
2007-07-07 04:13:13 +00:00
|
|
|
}
|
2007-07-18 05:27:45 +00:00
|
|
|
|
|
|
|
int
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_rcGetLimit( const tr_ratecontrol * r )
|
2007-07-18 05:27:45 +00:00
|
|
|
{
|
|
|
|
return r->limit;
|
|
|
|
}
|